oxigdal-wasm 0.1.4

WebAssembly bindings for OxiGDAL - Browser-based geospatial processing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! Animation and easing utilities for smooth transitions
//!
//! This module provides animation support for the WASM viewer, including:

// Allow unused code in this module as it provides a comprehensive animation API
// that may not be fully utilized initially
#![allow(dead_code)]
//! - Easing functions (linear, quadratic, cubic, quartic, exponential, etc.)
//! - Animation state management
//! - Smooth pan and zoom transitions
//! - Spring physics for natural motion
//! - Animation sequencing and composition
//!
//! # Overview
//!
//! Animations are essential for creating smooth, professional user experiences
//! in interactive map viewers. This module provides tools for:
//!
//! ## Easing Functions
//!
//! Easing functions control the rate of change over time. Available functions:
//! - Linear: constant speed
//! - Quadratic: gradual acceleration/deceleration
//! - Cubic: more pronounced curves
//! - Quartic: even smoother motion
//! - Quintic: very smooth, natural feel
//! - Sine: smooth start and end
//! - Exponential: dramatic acceleration
//! - Circular: arc-based motion
//! - Elastic: spring-like bounce
//! - Back: slight overshoot
//! - Bounce: bouncing effect
//!
//! Each has "in", "out", and "in-out" variants for different effects.
//!
//! ## Animation State
//!
//! The `Animation` struct manages the state of an ongoing animation:
//! - Start and end values
//! - Duration and progress
//! - Easing function
//! - Current value calculation
//!
//! ## Pan and Zoom Animations
//!
//! Specialized animation types for common map operations:
//! - `PanAnimation`: smooth camera panning
//! - `ZoomAnimation`: smooth zoom in/out with focal point
//! - `FitBoundsAnimation`: animate to fit a bounding box
//!
//! ## Spring Physics
//!
//! The `SpringAnimation` provides physics-based motion that feels natural:
//! - Configurable stiffness and damping
//! - Automatic settling detection
//! - Velocity-based motion
//!
//! ## Animation Sequencing
//!
//! Combine multiple animations:
//! - Sequential: play animations one after another
//! - Parallel: play multiple animations simultaneously
//! - Delayed: start animations after a delay
//!
//! # Examples
//!
//! ## Basic Easing
//!
//! ```rust
//! use oxigdal_wasm::{Easing, EasingFunction};
//!
//! let easing = Easing::QuadraticInOut;
//! let t = 0.5; // Halfway through
//! let value = easing.apply(t);
//! ```
//!
//! ## Simple Animation
//!
//! ```rust
//! use oxigdal_wasm::{Animation, Easing};
//!
//! let mut anim = Animation::new(0.0, 100.0, 1000.0, Easing::QuadraticOut);
//!
//! // Update at each frame
//! anim.update(16.67); // 60 FPS
//! let current_value = anim.current_value();
//! ```
//!
//! ## Pan Animation
//!
//! ```rust
//! use oxigdal_wasm::{PanAnimation, Easing};
//!
//! let mut pan = PanAnimation::new(
//!     (0.0, 0.0),    // Start position
//!     (100.0, 50.0), // End position
//!     500.0,         // Duration (ms)
//!     Easing::CubicOut,
//! );
//!
//! while !pan.is_complete() {
//!     pan.update(16.67);
//!     let (x, y) = pan.current_position();
//!     // Update camera position
//! }
//! ```
//!
//! ## Spring Animation
//!
//! ```rust
//! use oxigdal_wasm::SpringAnimation;
//!
//! let mut spring = SpringAnimation::new(0.0, 100.0, 0.5, 0.8);
//! // stiffness: 0.5, damping: 0.8
//!
//! while !spring.is_settled() {
//!     spring.update(16.67);
//!     let value = spring.current_value();
//! }
//! ```
//!
//! # Performance Considerations
//!
//! - Animations use `f64` for precision
//! - Easing functions are pure and fast
//! - No allocations during updates
//! - Can run hundreds of animations simultaneously
//!
//! # Browser Compatibility
//!
//! Works in all modern browsers. For best performance, sync animations
//! with `requestAnimationFrame`.

use crate::WasmError;

/// Easing function types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Easing {
    /// Constant speed (no easing)
    Linear,

    /// Quadratic easing in (accelerating from zero)
    QuadraticIn,
    /// Quadratic easing out (decelerating to zero)
    QuadraticOut,
    /// Quadratic easing in-out (acceleration then deceleration)
    QuadraticInOut,

    /// Cubic easing in
    CubicIn,
    /// Cubic easing out
    CubicOut,
    /// Cubic easing in-out
    CubicInOut,

    /// Quartic easing in
    QuarticIn,
    /// Quartic easing out
    QuarticOut,
    /// Quartic easing in-out
    QuarticInOut,

    /// Quintic easing in
    QuinticIn,
    /// Quintic easing out
    QuinticOut,
    /// Quintic easing in-out
    QuinticInOut,

    /// Sine easing in
    SineIn,
    /// Sine easing out
    SineOut,
    /// Sine easing in-out
    SineInOut,

    /// Exponential easing in
    ExponentialIn,
    /// Exponential easing out
    ExponentialOut,
    /// Exponential easing in-out
    ExponentialInOut,

    /// Circular easing in
    CircularIn,
    /// Circular easing out
    CircularOut,
    /// Circular easing in-out
    CircularInOut,

    /// Elastic easing in (spring effect)
    ElasticIn,
    /// Elastic easing out (spring effect)
    ElasticOut,
    /// Elastic easing in-out (spring effect)
    ElasticInOut,

    /// Back easing in (slight overshoot)
    BackIn,
    /// Back easing out (slight overshoot)
    BackOut,
    /// Back easing in-out (slight overshoot)
    BackInOut,

    /// Bounce easing in (bouncing effect)
    BounceIn,
    /// Bounce easing out (bouncing effect)
    BounceOut,
    /// Bounce easing in-out (bouncing effect)
    BounceInOut,
}

/// Trait for applying easing functions
pub trait EasingFunction {
    /// Apply easing to a normalized time value (0.0 to 1.0)
    fn apply(&self, t: f64) -> f64;
}

impl EasingFunction for Easing {
    fn apply(&self, t: f64) -> f64 {
        let t = t.clamp(0.0, 1.0);

        match self {
            Easing::Linear => t,

            Easing::QuadraticIn => t * t,
            Easing::QuadraticOut => t * (2.0 - t),
            Easing::QuadraticInOut => {
                if t < 0.5 {
                    2.0 * t * t
                } else {
                    -1.0 + (4.0 - 2.0 * t) * t
                }
            }

            Easing::CubicIn => t * t * t,
            Easing::CubicOut => {
                let t = t - 1.0;
                t * t * t + 1.0
            }
            Easing::CubicInOut => {
                if t < 0.5 {
                    4.0 * t * t * t
                } else {
                    let t = 2.0 * t - 2.0;
                    1.0 + t * t * t / 2.0
                }
            }

            Easing::QuarticIn => t * t * t * t,
            Easing::QuarticOut => {
                let t = t - 1.0;
                1.0 - t * t * t * t
            }
            Easing::QuarticInOut => {
                if t < 0.5 {
                    8.0 * t * t * t * t
                } else {
                    let t = 2.0 * t - 2.0;
                    1.0 - t * t * t * t / 2.0
                }
            }

            Easing::QuinticIn => t * t * t * t * t,
            Easing::QuinticOut => {
                let t = t - 1.0;
                t * t * t * t * t + 1.0
            }
            Easing::QuinticInOut => {
                if t < 0.5 {
                    16.0 * t * t * t * t * t
                } else {
                    let t = 2.0 * t - 2.0;
                    1.0 + t * t * t * t * t / 2.0
                }
            }

            Easing::SineIn => 1.0 - (t * std::f64::consts::PI / 2.0).cos(),
            Easing::SineOut => (t * std::f64::consts::PI / 2.0).sin(),
            Easing::SineInOut => -0.5 * ((std::f64::consts::PI * t).cos() - 1.0),

            Easing::ExponentialIn => {
                if t == 0.0 {
                    0.0
                } else {
                    2.0_f64.powf(10.0 * (t - 1.0))
                }
            }
            Easing::ExponentialOut => {
                if t == 1.0 {
                    1.0
                } else {
                    1.0 - 2.0_f64.powf(-10.0 * t)
                }
            }
            Easing::ExponentialInOut => {
                if t == 0.0 || t == 1.0 {
                    t
                } else if t < 0.5 {
                    0.5 * 2.0_f64.powf(20.0 * t - 10.0)
                } else {
                    1.0 - 0.5 * 2.0_f64.powf(-20.0 * t + 10.0)
                }
            }

            Easing::CircularIn => 1.0 - (1.0 - t * t).sqrt(),
            Easing::CircularOut => (2.0 * t - t * t).sqrt(),
            Easing::CircularInOut => {
                if t < 0.5 {
                    0.5 * (1.0 - (1.0 - 4.0 * t * t).sqrt())
                } else {
                    0.5 * ((2.0 * t - 1.0) * (3.0 - 2.0 * t) * 4.0).sqrt() + 0.5
                }
            }

            Easing::ElasticIn => {
                if t == 0.0 || t == 1.0 {
                    t
                } else {
                    let p = 0.3;
                    let s = p / 4.0;
                    let t = t - 1.0;
                    -(2.0_f64.powf(10.0 * t)) * ((t - s) * (2.0 * std::f64::consts::PI) / p).sin()
                }
            }
            Easing::ElasticOut => {
                if t == 0.0 || t == 1.0 {
                    t
                } else {
                    let p = 0.3;
                    let s = p / 4.0;
                    2.0_f64.powf(-10.0 * t) * ((t - s) * (2.0 * std::f64::consts::PI) / p).sin()
                        + 1.0
                }
            }
            Easing::ElasticInOut => {
                if t == 0.0 || t == 1.0 {
                    t
                } else {
                    let p = 0.45;
                    let s = p / 4.0;

                    if t < 0.5 {
                        let t = 2.0 * t - 1.0;
                        -0.5 * 2.0_f64.powf(10.0 * t)
                            * ((t - s) * (2.0 * std::f64::consts::PI) / p).sin()
                    } else {
                        let t = 2.0 * t - 1.0;
                        0.5 * 2.0_f64.powf(-10.0 * t)
                            * ((t - s) * (2.0 * std::f64::consts::PI) / p).sin()
                            + 1.0
                    }
                }
            }

            Easing::BackIn => {
                let c1 = 1.70158;
                let c3 = c1 + 1.0;
                c3 * t * t * t - c1 * t * t
            }
            Easing::BackOut => {
                let c1 = 1.70158;
                let c3 = c1 + 1.0;
                let t = t - 1.0;
                1.0 + c3 * t * t * t + c1 * t * t
            }
            Easing::BackInOut => {
                let c1 = 1.70158;
                let c2 = c1 * 1.525;

                if t < 0.5 {
                    let t = 2.0 * t;
                    (t * t * ((c2 + 1.0) * t - c2)) / 2.0
                } else {
                    let t = 2.0 * t - 2.0;
                    (t * t * ((c2 + 1.0) * t + c2) + 2.0) / 2.0
                }
            }

            Easing::BounceIn => 1.0 - Easing::BounceOut.apply(1.0 - t),
            Easing::BounceOut => {
                let n1 = 7.5625;
                let d1 = 2.75;

                if t < 1.0 / d1 {
                    n1 * t * t
                } else if t < 2.0 / d1 {
                    let t = t - 1.5 / d1;
                    n1 * t * t + 0.75
                } else if t < 2.5 / d1 {
                    let t = t - 2.25 / d1;
                    n1 * t * t + 0.9375
                } else {
                    let t = t - 2.625 / d1;
                    n1 * t * t + 0.984375
                }
            }
            Easing::BounceInOut => {
                if t < 0.5 {
                    0.5 * Easing::BounceIn.apply(2.0 * t)
                } else {
                    0.5 * Easing::BounceOut.apply(2.0 * t - 1.0) + 0.5
                }
            }
        }
    }
}

/// Basic animation state
#[derive(Debug, Clone)]
pub struct Animation {
    start_value: f64,
    end_value: f64,
    duration_ms: f64,
    elapsed_ms: f64,
    easing: Easing,
}

impl Animation {
    /// Create a new animation
    pub fn new(start_value: f64, end_value: f64, duration_ms: f64, easing: Easing) -> Self {
        Self {
            start_value,
            end_value,
            duration_ms,
            elapsed_ms: 0.0,
            easing,
        }
    }

    /// Update animation with time delta
    pub fn update(&mut self, delta_ms: f64) {
        self.elapsed_ms += delta_ms;
        if self.elapsed_ms > self.duration_ms {
            self.elapsed_ms = self.duration_ms;
        }
    }

    /// Get current value
    pub fn current_value(&self) -> f64 {
        let t = if self.duration_ms > 0.0 {
            self.elapsed_ms / self.duration_ms
        } else {
            1.0
        };

        let eased_t = self.easing.apply(t);
        self.start_value + (self.end_value - self.start_value) * eased_t
    }

    /// Check if animation is complete
    pub fn is_complete(&self) -> bool {
        self.elapsed_ms >= self.duration_ms
    }

    /// Get progress (0.0 to 1.0)
    pub fn progress(&self) -> f64 {
        if self.duration_ms > 0.0 {
            (self.elapsed_ms / self.duration_ms).min(1.0)
        } else {
            1.0
        }
    }

    /// Reset animation to beginning
    pub fn reset(&mut self) {
        self.elapsed_ms = 0.0;
    }

    /// Reverse the animation
    pub fn reverse(&mut self) {
        std::mem::swap(&mut self.start_value, &mut self.end_value);
        self.elapsed_ms = 0.0;
    }
}

/// Animation for panning the viewport
#[derive(Debug, Clone)]
pub struct PanAnimation {
    start_pos: (f64, f64),
    end_pos: (f64, f64),
    duration_ms: f64,
    elapsed_ms: f64,
    easing: Easing,
}

impl PanAnimation {
    /// Create a new pan animation
    pub fn new(
        start_pos: (f64, f64),
        end_pos: (f64, f64),
        duration_ms: f64,
        easing: Easing,
    ) -> Self {
        Self {
            start_pos,
            end_pos,
            duration_ms,
            elapsed_ms: 0.0,
            easing,
        }
    }

    /// Update animation
    pub fn update(&mut self, delta_ms: f64) {
        self.elapsed_ms += delta_ms;
        if self.elapsed_ms > self.duration_ms {
            self.elapsed_ms = self.duration_ms;
        }
    }

    /// Get current position
    pub fn current_position(&self) -> (f64, f64) {
        let t = if self.duration_ms > 0.0 {
            self.elapsed_ms / self.duration_ms
        } else {
            1.0
        };

        let eased_t = self.easing.apply(t);
        let x = self.start_pos.0 + (self.end_pos.0 - self.start_pos.0) * eased_t;
        let y = self.start_pos.1 + (self.end_pos.1 - self.start_pos.1) * eased_t;

        (x, y)
    }

    /// Check if complete
    pub fn is_complete(&self) -> bool {
        self.elapsed_ms >= self.duration_ms
    }

    /// Get progress
    pub fn progress(&self) -> f64 {
        if self.duration_ms > 0.0 {
            (self.elapsed_ms / self.duration_ms).min(1.0)
        } else {
            1.0
        }
    }
}

/// Animation for zooming the viewport
#[derive(Debug, Clone)]
pub struct ZoomAnimation {
    start_zoom: f64,
    end_zoom: f64,
    focal_point: (f64, f64),
    duration_ms: f64,
    elapsed_ms: f64,
    easing: Easing,
}

impl ZoomAnimation {
    /// Create a new zoom animation
    pub fn new(
        start_zoom: f64,
        end_zoom: f64,
        focal_point: (f64, f64),
        duration_ms: f64,
        easing: Easing,
    ) -> Self {
        Self {
            start_zoom,
            end_zoom,
            focal_point,
            duration_ms,
            elapsed_ms: 0.0,
            easing,
        }
    }

    /// Update animation
    pub fn update(&mut self, delta_ms: f64) {
        self.elapsed_ms += delta_ms;
        if self.elapsed_ms > self.duration_ms {
            self.elapsed_ms = self.duration_ms;
        }
    }

    /// Get current zoom level
    pub fn current_zoom(&self) -> f64 {
        let t = if self.duration_ms > 0.0 {
            self.elapsed_ms / self.duration_ms
        } else {
            1.0
        };

        let eased_t = self.easing.apply(t);
        self.start_zoom + (self.end_zoom - self.start_zoom) * eased_t
    }

    /// Get focal point
    pub fn focal_point(&self) -> (f64, f64) {
        self.focal_point
    }

    /// Check if complete
    pub fn is_complete(&self) -> bool {
        self.elapsed_ms >= self.duration_ms
    }

    /// Get progress
    pub fn progress(&self) -> f64 {
        if self.duration_ms > 0.0 {
            (self.elapsed_ms / self.duration_ms).min(1.0)
        } else {
            1.0
        }
    }
}

/// Spring animation using physics simulation
#[derive(Debug, Clone)]
pub struct SpringAnimation {
    current: f64,
    target: f64,
    velocity: f64,
    stiffness: f64,
    damping: f64,
    threshold: f64,
}

impl SpringAnimation {
    /// Create a new spring animation
    ///
    /// - `current`: starting value
    /// - `target`: target value
    /// - `stiffness`: spring stiffness (0.0 to 1.0, higher = faster)
    /// - `damping`: damping factor (0.0 to 1.0, higher = less bounce)
    pub fn new(current: f64, target: f64, stiffness: f64, damping: f64) -> Self {
        Self {
            current,
            target,
            velocity: 0.0,
            stiffness: stiffness.clamp(0.0, 1.0),
            damping: damping.clamp(0.0, 1.0),
            threshold: 0.01,
        }
    }

    /// Update spring animation
    pub fn update(&mut self, delta_ms: f64) {
        let delta_s = delta_ms / 1000.0; // Convert to seconds

        // Spring force
        let spring_force = (self.target - self.current) * self.stiffness;

        // Damping force
        let damping_force = -self.velocity * self.damping;

        // Update velocity and position
        self.velocity += (spring_force + damping_force) * delta_s;
        self.current += self.velocity * delta_s;
    }

    /// Get current value
    pub fn current_value(&self) -> f64 {
        self.current
    }

    /// Check if spring has settled
    pub fn is_settled(&self) -> bool {
        (self.current - self.target).abs() < self.threshold && self.velocity.abs() < self.threshold
    }

    /// Set new target
    pub fn set_target(&mut self, target: f64) {
        self.target = target;
    }

    /// Set threshold for settlement detection
    pub fn set_threshold(&mut self, threshold: f64) {
        self.threshold = threshold;
    }
}

/// Animation sequence manager
pub struct AnimationSequence {
    animations: Vec<Box<dyn AnimationTrait>>,
    current_index: usize,
}

/// Trait for animation types
pub trait AnimationTrait {
    /// Update the animation
    fn update(&mut self, delta_ms: f64);

    /// Check if animation is complete
    fn is_complete(&self) -> bool;

    /// Get progress (0.0 to 1.0)
    fn progress(&self) -> f64;
}

impl AnimationTrait for Animation {
    fn update(&mut self, delta_ms: f64) {
        Animation::update(self, delta_ms);
    }

    fn is_complete(&self) -> bool {
        Animation::is_complete(self)
    }

    fn progress(&self) -> f64 {
        Animation::progress(self)
    }
}

impl AnimationTrait for PanAnimation {
    fn update(&mut self, delta_ms: f64) {
        PanAnimation::update(self, delta_ms);
    }

    fn is_complete(&self) -> bool {
        PanAnimation::is_complete(self)
    }

    fn progress(&self) -> f64 {
        PanAnimation::progress(self)
    }
}

impl AnimationTrait for ZoomAnimation {
    fn update(&mut self, delta_ms: f64) {
        ZoomAnimation::update(self, delta_ms);
    }

    fn is_complete(&self) -> bool {
        ZoomAnimation::is_complete(self)
    }

    fn progress(&self) -> f64 {
        ZoomAnimation::progress(self)
    }
}

impl AnimationSequence {
    /// Create a new animation sequence
    pub fn new() -> Self {
        Self {
            animations: Vec::new(),
            current_index: 0,
        }
    }

    /// Add an animation to the sequence
    pub fn add<A: AnimationTrait + 'static>(&mut self, animation: A) {
        self.animations.push(Box::new(animation));
    }

    /// Update the current animation in the sequence
    pub fn update(&mut self, delta_ms: f64) -> Result<(), WasmError> {
        if self.current_index >= self.animations.len() {
            return Ok(());
        }

        let current = &mut self.animations[self.current_index];
        current.update(delta_ms);

        if current.is_complete() {
            self.current_index += 1;
        }

        Ok(())
    }

    /// Check if the entire sequence is complete
    pub fn is_complete(&self) -> bool {
        self.current_index >= self.animations.len()
    }

    /// Get overall progress
    pub fn progress(&self) -> f64 {
        if self.animations.is_empty() {
            return 1.0;
        }

        let completed = self.current_index as f64;
        let current_progress = if self.current_index < self.animations.len() {
            self.animations[self.current_index].progress()
        } else {
            0.0
        };

        (completed + current_progress) / self.animations.len() as f64
    }

    /// Reset the sequence to the beginning
    pub fn reset(&mut self) {
        self.current_index = 0;
    }
}

impl Default for AnimationSequence {
    fn default() -> Self {
        Self::new()
    }
}

/// Parallel animation manager (runs multiple animations simultaneously)
pub struct ParallelAnimation {
    animations: Vec<Box<dyn AnimationTrait>>,
}

impl ParallelAnimation {
    /// Create a new parallel animation manager
    pub fn new() -> Self {
        Self {
            animations: Vec::new(),
        }
    }

    /// Add an animation
    pub fn add<A: AnimationTrait + 'static>(&mut self, animation: A) {
        self.animations.push(Box::new(animation));
    }

    /// Update all animations
    pub fn update(&mut self, delta_ms: f64) {
        for anim in &mut self.animations {
            if !anim.is_complete() {
                anim.update(delta_ms);
            }
        }
    }

    /// Check if all animations are complete
    pub fn is_complete(&self) -> bool {
        self.animations.iter().all(|a| a.is_complete())
    }

    /// Get average progress
    pub fn progress(&self) -> f64 {
        if self.animations.is_empty() {
            return 1.0;
        }

        let total: f64 = self.animations.iter().map(|a| a.progress()).sum();
        total / self.animations.len() as f64
    }
}

impl Default for ParallelAnimation {
    fn default() -> Self {
        Self::new()
    }
}

/// Delayed animation (starts after a delay)
#[derive(Debug)]
pub struct DelayedAnimation<A: AnimationTrait> {
    animation: A,
    delay_ms: f64,
    elapsed_delay_ms: f64,
    started: bool,
}

impl<A: AnimationTrait> DelayedAnimation<A> {
    /// Create a new delayed animation
    pub fn new(animation: A, delay_ms: f64) -> Self {
        Self {
            animation,
            delay_ms,
            elapsed_delay_ms: 0.0,
            started: false,
        }
    }

    /// Update animation (including delay)
    pub fn update(&mut self, delta_ms: f64) {
        if !self.started {
            self.elapsed_delay_ms += delta_ms;
            if self.elapsed_delay_ms >= self.delay_ms {
                self.started = true;
                let overflow = self.elapsed_delay_ms - self.delay_ms;
                if overflow > 0.0 {
                    self.animation.update(overflow);
                }
            }
        } else {
            self.animation.update(delta_ms);
        }
    }

    /// Check if complete
    pub fn is_complete(&self) -> bool {
        self.started && self.animation.is_complete()
    }

    /// Get progress (including delay)
    pub fn progress(&self) -> f64 {
        if !self.started {
            (self.elapsed_delay_ms / self.delay_ms).min(1.0) * 0.5
        } else {
            0.5 + self.animation.progress() * 0.5
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_linear_easing() {
        let easing = Easing::Linear;
        assert_eq!(easing.apply(0.0), 0.0);
        assert_eq!(easing.apply(0.5), 0.5);
        assert_eq!(easing.apply(1.0), 1.0);
    }

    #[test]
    fn test_quadratic_easing() {
        let easing = Easing::QuadraticIn;
        assert_eq!(easing.apply(0.0), 0.0);
        assert!(easing.apply(0.5) < 0.5);
        assert_eq!(easing.apply(1.0), 1.0);
    }

    #[test]
    fn test_animation_basic() {
        let mut anim = Animation::new(0.0, 100.0, 1000.0, Easing::Linear);

        assert_eq!(anim.current_value(), 0.0);
        assert!(!anim.is_complete());

        anim.update(500.0);
        assert!((anim.current_value() - 50.0).abs() < 0.01);

        anim.update(500.0);
        assert_eq!(anim.current_value(), 100.0);
        assert!(anim.is_complete());
    }

    #[test]
    fn test_pan_animation() {
        let mut pan = PanAnimation::new((0.0, 0.0), (100.0, 50.0), 1000.0, Easing::Linear);

        let (x, y) = pan.current_position();
        assert_eq!(x, 0.0);
        assert_eq!(y, 0.0);

        pan.update(500.0);
        let (x, y) = pan.current_position();
        assert!((x - 50.0).abs() < 0.01);
        assert!((y - 25.0).abs() < 0.01);
    }

    #[test]
    fn test_spring_animation() {
        // Use higher stiffness and damping for predictable settling
        let mut spring = SpringAnimation::new(0.0, 100.0, 0.8, 0.98);
        spring.set_threshold(1.0); // Larger threshold for easier settling

        // Run spring for enough iterations to settle
        for _ in 0..1000 {
            spring.update(16.67);
            if spring.is_settled() {
                break;
            }
        }

        // Spring should settle close to target (within 5% tolerance)
        assert!((spring.current_value() - 100.0).abs() < 5.0);
    }

    #[test]
    fn test_animation_reverse() {
        let mut anim = Animation::new(0.0, 100.0, 1000.0, Easing::Linear);
        anim.update(500.0);

        anim.reverse();
        assert_eq!(anim.current_value(), 100.0);

        anim.update(500.0);
        assert_eq!(anim.current_value(), 50.0);
    }
}