pax-std 0.39.1

Standard library for Pax, including layouts, drawing primitives, and form controls
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
#![allow(unused_imports)]

use crate::*;
use pax_engine::api::*;
use pax_engine::*;

const CANVAS_WIDTH_PX: f64 = 920.72;
const CANVAS_HEIGHT_PX: f64 = 463.95;

const BANNER_LEFT_X: f64 = 137.23;
const BANNER_TOP_Y: f64 = 23.34;
const BANNER_SHOULDER_X: f64 = 789.81;
const BANNER_TIP_X: f64 = 892.72;
const BANNER_TIP_Y: f64 = 168.57;
const BANNER_BOTTOM_Y: f64 = 313.20;
const CURTAIN_ROPE_XS: [f64; 5] = [
    BANNER_LEFT_X,
    BANNER_LEFT_X + (BANNER_SHOULDER_X - BANNER_LEFT_X) / 3.0,
    BANNER_LEFT_X + (BANNER_SHOULDER_X - BANNER_LEFT_X) * 2.0 / 3.0,
    BANNER_SHOULDER_X,
    BANNER_TIP_X,
];

const A_CENTER: Point = Point::new(413.60, 168.77);
const A_BRAKE_PIVOT: Point = Point::new(530.43, 283.94);
const P_COUNTER_CENTER: Point = Point::new(189.01, 169.47);
const P_RING_CENTER: Point = Point::new(189.01, 168.305);
const P_RING_RADIUS_X_PX: f64 = 109.5;
const P_RING_RADIUS_Y_PX: f64 = 112.925;
const A_COUNTER_CENTER: Point = Point::new(412.46, 168.76);
const X_CENTER: Point = Point::new(648.34, 168.755);
const X_FINAL_Y_OFFSET_PX: f64 = 0.73;
const X_BRAKE_PIVOT: Point = Point::new(766.09, 282.99 + X_FINAL_Y_OFFSET_PX);
const P_COUNTER_RADIUS_PX: f64 = 53.51;
const P_INITIAL_SCALE: f64 = 0.28;
const A_COUNTER_RADIUS_PX: f64 = 54.61;
const A_INITIAL_OUTER_RADIUS_PX: f64 = 112.925;
// The a begins at 380ms, when the p's 190–560ms InQuad roll has reached these
// values. Keeping their centers aligned guarantees the scaled a starts fully
// inside the moving p counter before it naturally peeks out.
const P_DISPATCH_TRANSLATION_X: f64 = -80.993_425_858_290_72;
#[cfg(test)]
const P_DISPATCH_SCALE: f64 = 0.469_861_212_563_915_3;
const A_INITIAL_SCALE: f64 = 0.16;
const A_INITIAL_TRANSLATION_X: f64 = P_COUNTER_CENTER.x + P_DISPATCH_TRANSLATION_X - A_CENTER.x;
const A_SIDEBAR_LEFT_X: f64 = 465.49;
const A_SIDEBAR_RIGHT_X: f64 = 530.43;
const X_INITIAL_SCALE: f64 = 0.28;
const X_INITIAL_TRANSLATION_X: f64 = (A_SIDEBAR_LEFT_X + A_SIDEBAR_RIGHT_X) * 0.5 - X_CENTER.x;
const X_INITIAL_TRANSLATION_Y: f64 = X_FINAL_Y_OFFSET_PX;
const X_INITIAL_ROTATION_DEG: f64 = -90.0;

/// The animated signboard and lettering used by [`AnimatedPaxLogo`].
#[pax]
#[engine_import_path("pax_engine")]
#[custom(Default)]
#[file("drawing/pax_logo/animated_pax_logo_banner.pax")]
pub struct AnimatedPaxLogoBanner {
    pub fill: Property<Paint>,
    pub letter_fill: Property<Paint>,
    pub progress: Property<f64>,
    pub curtain_drop_1: Property<f64>,
    pub curtain_drop_2: Property<f64>,
    pub curtain_drop_3: Property<f64>,
    pub curtain_drop_4: Property<f64>,
    pub curtain_drop_5: Property<f64>,
    pub banner_opacity: Property<f64>,
    pub p_counter_reveal_progress: Property<f64>,
    pub p_counter_x_px: Property<f64>,
    pub p_rotation_deg: Property<f64>,
    pub p_scale: Property<f64>,
    pub a_x_px: Property<f64>,
    pub a_rotation_deg: Property<f64>,
    pub a_scale: Property<f64>,
    pub a_brake_rotation_deg: Property<f64>,
    pub a_morph_progress: Property<f64>,
    pub a_opacity: Property<f64>,
    pub x_offset_px: Property<f64>,
    pub x_offset_y_px: Property<f64>,
    pub x_rotation_deg: Property<f64>,
    pub x_scale: Property<f64>,
    pub x_brake_rotation_deg: Property<f64>,
    pub x_opacity: Property<f64>,
    pub banner_elements: Property<Vec<PathElement>>,
    pub p_ring_elements: Property<Vec<PathElement>>,
    pub p_counter_elements: Property<Vec<PathElement>>,
    pub p_reveal_mask_elements: Property<Vec<PathElement>>,
    pub p_counter_reveal_mask_elements: Property<Vec<PathElement>>,
    pub a_elements: Property<Vec<PathElement>>,
    pub a_counter_elements: Property<Vec<PathElement>>,
    pub a_counter_reveal_mask_elements: Property<Vec<PathElement>>,
    pub x_elements: Property<Vec<PathElement>>,
}

impl Default for AnimatedPaxLogoBanner {
    fn default() -> Self {
        Self {
            fill: Property::new(Paint::Solid(Color::BLACK)),
            letter_fill: Property::new(Paint::Solid(Color::WHITE)),
            progress: Property::new(0.0),
            curtain_drop_1: Property::new(0.0),
            curtain_drop_2: Property::new(0.0),
            curtain_drop_3: Property::new(0.0),
            curtain_drop_4: Property::new(0.0),
            curtain_drop_5: Property::new(0.0),
            banner_opacity: Property::new(0.0),
            p_counter_reveal_progress: Property::new(0.0),
            p_counter_x_px: Property::new(-110.0),
            p_rotation_deg: Property::new(-90.0),
            p_scale: Property::new(P_INITIAL_SCALE),
            a_x_px: Property::new(A_INITIAL_TRANSLATION_X),
            a_rotation_deg: Property::new(-90.0),
            a_scale: Property::new(A_INITIAL_SCALE),
            a_brake_rotation_deg: Property::new(0.0),
            a_morph_progress: Property::new(0.0),
            a_opacity: Property::new(0.0),
            x_offset_px: Property::new(X_INITIAL_TRANSLATION_X),
            x_offset_y_px: Property::new(X_INITIAL_TRANSLATION_Y),
            x_rotation_deg: Property::new(X_INITIAL_ROTATION_DEG),
            x_scale: Property::new(X_INITIAL_SCALE),
            x_brake_rotation_deg: Property::new(0.0),
            x_opacity: Property::new(0.0),
            banner_elements: Property::new(banner_path([0.0; 5])),
            p_ring_elements: Property::new(p_ring_path(Transform {
                translation_x: -110.0,
                rotation_deg: -90.0,
                scale: P_INITIAL_SCALE,
                brake_rotation_deg: 0.0,
            })),
            p_counter_elements: Property::new(p_counter_path(Transform {
                translation_x: -110.0,
                rotation_deg: -90.0,
                scale: P_INITIAL_SCALE,
                brake_rotation_deg: 0.0,
            })),
            p_reveal_mask_elements: Property::new(p_reveal_mask_path()),
            p_counter_reveal_mask_elements: Property::new(p_counter_reveal_mask_path(0.0)),
            a_elements: Property::new(a_path(
                Transform {
                    translation_x: A_INITIAL_TRANSLATION_X,
                    rotation_deg: -90.0,
                    scale: A_INITIAL_SCALE,
                    brake_rotation_deg: 0.0,
                },
                0.0,
            )),
            a_counter_elements: Property::new(a_counter_path(
                Transform {
                    translation_x: A_INITIAL_TRANSLATION_X,
                    rotation_deg: -90.0,
                    scale: A_INITIAL_SCALE,
                    brake_rotation_deg: 0.0,
                },
                0.0,
            )),
            a_counter_reveal_mask_elements: Property::new(a_counter_reveal_mask_path()),
            x_elements: Property::new(x_path(XTransform::initial())),
        }
    }
}

impl AnimatedPaxLogoBanner {
    pub fn handle_mount(&mut self, _ctx: &NodeContext) {
        let curtain_drop_1 = self.curtain_drop_1.local();
        let curtain_drop_2 = self.curtain_drop_2.local();
        let curtain_drop_3 = self.curtain_drop_3.local();
        let curtain_drop_4 = self.curtain_drop_4.local();
        let curtain_drop_5 = self.curtain_drop_5.local();
        let banner_dependencies = [
            curtain_drop_1.untyped(),
            curtain_drop_2.untyped(),
            curtain_drop_3.untyped(),
            curtain_drop_4.untyped(),
            curtain_drop_5.untyped(),
        ];
        self.banner_elements.replace_with(LocalProperty::computed(
            move || {
                banner_path([
                    curtain_drop_1.get(),
                    curtain_drop_2.get(),
                    curtain_drop_3.get(),
                    curtain_drop_4.get(),
                    curtain_drop_5.get(),
                ])
            },
            &banner_dependencies,
        ));

        let p_counter_x_px = self.p_counter_x_px.local();
        let p_rotation_deg = self.p_rotation_deg.local();
        let p_scale = self.p_scale.local();
        let p_dependencies = [
            p_counter_x_px.untyped(),
            p_rotation_deg.untyped(),
            p_scale.untyped(),
        ];
        let p_transform = LocalProperty::computed(
            move || Transform {
                translation_x: p_counter_x_px.get(),
                rotation_deg: p_rotation_deg.get(),
                scale: p_scale.get(),
                brake_rotation_deg: 0.0,
            },
            &p_dependencies,
        );

        let p_transform_for_ring = p_transform.clone();
        self.p_ring_elements.replace_with(LocalProperty::computed(
            move || p_ring_path(p_transform_for_ring.get()),
            &[p_transform.untyped()],
        ));

        let p_transform_for_counter = p_transform.clone();
        self.p_counter_elements
            .replace_with(LocalProperty::computed(
                move || p_counter_path(p_transform_for_counter.get()),
                &[p_transform.untyped()],
            ));

        let p_counter_reveal_progress = self.p_counter_reveal_progress.local();
        let p_counter_reveal_dependency = p_counter_reveal_progress.clone();
        self.p_counter_reveal_mask_elements
            .replace_with(LocalProperty::computed(
                move || p_counter_reveal_mask_path(p_counter_reveal_progress.get()),
                &[p_counter_reveal_dependency.untyped()],
            ));

        let a_x_px = self.a_x_px.local();
        let a_rotation_deg = self.a_rotation_deg.local();
        let a_scale = self.a_scale.local();
        let a_brake_rotation_deg = self.a_brake_rotation_deg.local();
        let a_morph_progress = self.a_morph_progress.local();
        let transform_dependencies = [
            a_x_px.untyped(),
            a_rotation_deg.untyped(),
            a_scale.untyped(),
            a_brake_rotation_deg.untyped(),
        ];
        let a_transform = LocalProperty::computed(
            move || Transform {
                translation_x: a_x_px.get(),
                rotation_deg: a_rotation_deg.get(),
                scale: a_scale.get(),
                brake_rotation_deg: a_brake_rotation_deg.get(),
            },
            &transform_dependencies,
        );

        let a_transform_for_outer = a_transform.clone();
        let a_morph_for_outer = a_morph_progress.clone();
        self.a_elements.replace_with(LocalProperty::computed(
            move || a_path(a_transform_for_outer.get(), a_morph_for_outer.get()),
            &[a_transform.untyped(), a_morph_progress.untyped()],
        ));

        let a_transform_for_counter = a_transform.clone();
        let a_morph_for_counter = a_morph_progress.clone();
        self.a_counter_elements
            .replace_with(LocalProperty::computed(
                move || a_counter_path(a_transform_for_counter.get(), a_morph_for_counter.get()),
                &[a_transform.untyped(), a_morph_progress.untyped()],
            ));

        let x_offset_px = self.x_offset_px.local();
        let x_offset_y_px = self.x_offset_y_px.local();
        let x_rotation_deg = self.x_rotation_deg.local();
        let x_scale = self.x_scale.local();
        let x_brake_rotation_deg = self.x_brake_rotation_deg.local();
        let x_dependencies = [
            x_offset_px.untyped(),
            x_offset_y_px.untyped(),
            x_rotation_deg.untyped(),
            x_scale.untyped(),
            x_brake_rotation_deg.untyped(),
        ];
        self.x_elements.replace_with(LocalProperty::computed(
            move || {
                x_path(XTransform {
                    translation_x: x_offset_px.get(),
                    translation_y: x_offset_y_px.get(),
                    rotation_deg: x_rotation_deg.get(),
                    scale: x_scale.get(),
                    brake_rotation_deg: x_brake_rotation_deg.get(),
                })
            },
            &x_dependencies,
        ));
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Point {
    x: f64,
    y: f64,
}

impl Point {
    const fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

#[derive(Clone, Copy, Debug)]
struct Transform {
    translation_x: f64,
    rotation_deg: f64,
    scale: f64,
    brake_rotation_deg: f64,
}

impl Default for Transform {
    fn default() -> Self {
        Self {
            translation_x: 0.0,
            rotation_deg: 0.0,
            scale: 1.0,
            brake_rotation_deg: 0.0,
        }
    }
}

impl Interpolatable for Transform {}

#[derive(Clone, Copy, Debug, PartialEq)]
struct XTransform {
    translation_x: f64,
    translation_y: f64,
    rotation_deg: f64,
    scale: f64,
    brake_rotation_deg: f64,
}

impl XTransform {
    const fn initial() -> Self {
        Self {
            translation_x: X_INITIAL_TRANSLATION_X,
            translation_y: X_INITIAL_TRANSLATION_Y,
            rotation_deg: X_INITIAL_ROTATION_DEG,
            scale: X_INITIAL_SCALE,
            brake_rotation_deg: 0.0,
        }
    }
}

impl Default for XTransform {
    fn default() -> Self {
        Self {
            translation_x: 0.0,
            translation_y: X_FINAL_Y_OFFSET_PX,
            rotation_deg: 0.0,
            scale: 1.0,
            brake_rotation_deg: 0.0,
        }
    }
}

impl Interpolatable for XTransform {}

pub(crate) fn settled_banner_paths() -> [Vec<PathElement>; 6] {
    [
        banner_path([1.0; 5]),
        p_ring_path(Transform::default()),
        p_counter_path(Transform::default()),
        a_path(Transform::default(), 1.0),
        a_counter_path(Transform::default(), 1.0),
        x_path(XTransform::default()),
    ]
}

fn banner_path(drops: [f64; 5]) -> Vec<PathElement> {
    let drops = drops.map(|drop| drop.clamp(0.0, 1.08));
    let top = [
        Point::new(CURTAIN_ROPE_XS[0], BANNER_TOP_Y),
        Point::new(CURTAIN_ROPE_XS[1], BANNER_TOP_Y),
        Point::new(CURTAIN_ROPE_XS[2], BANNER_TOP_Y),
        Point::new(CURTAIN_ROPE_XS[3], BANNER_TOP_Y),
        Point::new(CURTAIN_ROPE_XS[4], BANNER_TIP_Y),
    ];
    let target_bottom = [
        Point::new(CURTAIN_ROPE_XS[0], BANNER_BOTTOM_Y),
        Point::new(CURTAIN_ROPE_XS[1], BANNER_BOTTOM_Y),
        Point::new(CURTAIN_ROPE_XS[2], BANNER_BOTTOM_Y),
        Point::new(CURTAIN_ROPE_XS[3], BANNER_BOTTOM_Y),
        Point::new(CURTAIN_ROPE_XS[4], BANNER_TIP_Y),
    ];
    let bottom: [Point; 5] = std::array::from_fn(|index| {
        let visible_drop = drops[index].max(0.000_5);
        Point::new(
            top[index].x,
            lerp(top[index].y, target_bottom[index].y, visible_drop),
        )
    });

    let mut path = vec![
        point_element(top[0]),
        PathElement::Line,
        point_element(top[3]),
        PathElement::Line,
        point_element(top[4]),
    ];

    // Trace the hem right-to-left. During each handoff the descended left
    // neighbor supplies more slack than the still-tight right neighbor, so
    // the cubic bows more deeply on its left side. Once both releases reach
    // one, the controls return to the straight final silhouette.
    for index in (0..4).rev() {
        let left = bottom[index];
        let right = bottom[index + 1];
        let handoff = (drops[index] - drops[index + 1]).max(0.0);
        let width = right.x - left.x;
        let near_left = morph_point(left, right, 1.0 / 3.0);
        let near_right = morph_point(left, right, 2.0 / 3.0);
        let left_control = Point::new(near_left.x, near_left.y + width * 0.32 * handoff);
        let right_control = Point::new(near_right.x, near_right.y + width * 0.12 * handoff);

        path.push(cubic_element(right_control, left_control));
        path.push(point_element(left));
    }
    path.push(PathElement::Close);
    path
}

fn p_ring_path(transform: Transform) -> Vec<PathElement> {
    ellipse_path_about(
        P_RING_CENTER,
        P_RING_RADIUS_X_PX,
        P_RING_RADIUS_Y_PX,
        transform,
        P_COUNTER_CENTER,
    )
}

fn p_counter_path(transform: Transform) -> Vec<PathElement> {
    circle_path_about(
        P_COUNTER_CENTER,
        P_COUNTER_RADIUS_PX,
        transform,
        P_COUNTER_CENTER,
    )
}

fn p_reveal_mask_path() -> Vec<PathElement> {
    reveal_half_plane_path(BANNER_LEFT_X)
}

fn p_counter_reveal_mask_path(progress: f64) -> Vec<PathElement> {
    reveal_half_plane_path(lerp(BANNER_LEFT_X, 0.0, progress.clamp(0.0, 1.0)))
}

fn reveal_half_plane_path(boundary_x: f64) -> Vec<PathElement> {
    vec![
        point_element(Point::new(boundary_x, 0.0)),
        PathElement::Line,
        point_element(Point::new(CANVAS_WIDTH_PX, 0.0)),
        PathElement::Line,
        point_element(Point::new(CANVAS_WIDTH_PX, CANVAS_HEIGHT_PX)),
        PathElement::Line,
        point_element(Point::new(boundary_x, CANVAS_HEIGHT_PX)),
        PathElement::Close,
    ]
}

fn a_path(transform: Transform, morph_progress: f64) -> Vec<PathElement> {
    let morph_progress = morph_progress.clamp(0.0, 1.0);
    let kappa = 0.552_284_749_830_793_6;
    let center = A_CENTER;
    let radius = A_INITIAL_OUTER_RADIUS_PX;
    let left = Point::new(center.x - radius, center.y);
    let top = Point::new(center.x, center.y - radius);
    let right = Point::new(center.x + radius, center.y);
    let bottom = Point::new(center.x, center.y + radius);
    let left_top_control = Point::new(center.x - radius, center.y - radius * kappa);
    let top_left_control = Point::new(center.x - radius * kappa, center.y - radius);
    let top_right_control = Point::new(center.x + radius * kappa, center.y - radius);
    let right_top_control = Point::new(center.x + radius, center.y - radius * kappa);
    let right_bottom_control = Point::new(center.x + radius, center.y + radius * kappa);
    let bottom_right_control = Point::new(center.x + radius * kappa, center.y + radius);
    let bottom_left_control = Point::new(center.x - radius * kappa, center.y + radius);
    let left_bottom_control = Point::new(center.x - radius, center.y + radius * kappa);

    vec![
        transformed_point_element(
            morph_point(left, Point::new(297.93, 152.39), morph_progress),
            transform,
        ),
        transformed_cubic_element(
            morph_point(left_top_control, Point::new(306.34, 97.33), morph_progress),
            morph_point(top_left_control, Point::new(356.34, 55.03), morph_progress),
            transform,
        ),
        transformed_point_element(
            morph_point(top, Point::new(413.60, 55.03), morph_progress),
            transform,
        ),
        transformed_cubic_element(
            morph_point(top_right_control, Point::new(432.29, 55.03), morph_progress),
            morph_point(right_top_control, Point::new(450.09, 62.38), morph_progress),
            transform,
        ),
        transformed_point_element(
            morph_point(right, Point::new(465.49, 74.32), morph_progress),
            transform,
        ),
        PathElement::Line,
        transformed_point_element(
            morph_point(right, Point::new(465.49, 55.03), morph_progress),
            transform,
        ),
        PathElement::Line,
        transformed_point_element(
            morph_point(right, Point::new(530.43, 55.03), morph_progress),
            transform,
        ),
        PathElement::Line,
        transformed_point_element(
            morph_point(right, Point::new(530.43, 283.94), morph_progress),
            transform,
        ),
        PathElement::Line,
        transformed_point_element(
            morph_point(right, Point::new(465.49, 283.94), morph_progress),
            transform,
        ),
        PathElement::Line,
        transformed_point_element(
            morph_point(right, Point::new(465.49, 263.22), morph_progress),
            transform,
        ),
        transformed_cubic_element(
            morph_point(
                right_bottom_control,
                Point::new(452.01, 275.65),
                morph_progress,
            ),
            morph_point(
                bottom_right_control,
                Point::new(432.29, 282.51),
                morph_progress,
            ),
            transform,
        ),
        transformed_point_element(
            morph_point(bottom, Point::new(413.60, 282.51), morph_progress),
            transform,
        ),
        transformed_cubic_element(
            morph_point(
                bottom_left_control,
                Point::new(356.69, 282.51),
                morph_progress,
            ),
            morph_point(
                left_bottom_control,
                Point::new(306.95, 240.72),
                morph_progress,
            ),
            transform,
        ),
        transformed_point_element(
            morph_point(left, Point::new(298.08, 186.16), morph_progress),
            transform,
        ),
        transformed_cubic_element(
            morph_point(left, Point::new(295.40, 180.20), morph_progress),
            morph_point(left, Point::new(295.25, 158.20), morph_progress),
            transform,
        ),
        transformed_point_element(
            morph_point(left, Point::new(297.93, 152.39), morph_progress),
            transform,
        ),
        PathElement::Close,
    ]
}

fn a_counter_path(transform: Transform, morph_progress: f64) -> Vec<PathElement> {
    let morph_progress = morph_progress.clamp(0.0, 1.0);
    circle_path(
        morph_point(A_CENTER, A_COUNTER_CENTER, morph_progress),
        lerp(P_COUNTER_RADIUS_PX, A_COUNTER_RADIUS_PX, morph_progress),
        transform,
    )
}

fn a_counter_reveal_mask_path() -> Vec<PathElement> {
    let boundary_x = P_RING_CENTER.x + P_RING_RADIUS_X_PX;
    vec![
        point_element(Point::new(boundary_x, 0.0)),
        PathElement::Line,
        point_element(Point::new(CANVAS_WIDTH_PX, 0.0)),
        PathElement::Line,
        point_element(Point::new(CANVAS_WIDTH_PX, CANVAS_HEIGHT_PX)),
        PathElement::Line,
        point_element(Point::new(boundary_x, CANVAS_HEIGHT_PX)),
        PathElement::Close,
    ]
}

fn x_source_points() -> [Point; 13] {
    [
        Point::new(766.09, 282.99),
        Point::new(691.83, 282.99),
        Point::new(649.52, 220.99),
        Point::new(603.67, 282.99),
        Point::new(530.59, 282.99),
        Point::new(613.08, 169.71),
        Point::new(533.30, 54.52),
        Point::new(606.94, 54.52),
        Point::new(650.44, 117.45),
        Point::new(692.06, 54.52),
        Point::new(766.08, 54.52),
        Point::new(686.44, 169.71),
        Point::new(766.09, 282.99),
    ]
}

fn x_path(transform: XTransform) -> Vec<PathElement> {
    x_source_points()
        .into_iter()
        .enumerate()
        .flat_map(|(index, point)| {
            let point = transform_x_point(point, transform);
            if index == 0 {
                vec![point_element(point)]
            } else {
                vec![PathElement::Line, point_element(point)]
            }
        })
        .chain([PathElement::Close])
        .collect()
}

fn circle_path(center: Point, radius_px: f64, transform: Transform) -> Vec<PathElement> {
    circle_path_about(center, radius_px, transform, A_CENTER)
}

fn circle_path_about(
    center: Point,
    radius_px: f64,
    transform: Transform,
    pivot: Point,
) -> Vec<PathElement> {
    let kappa = 0.552_284_749_830_793_6;
    vec![
        transformed_point_element_about(
            Point::new(center.x + radius_px, center.y),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x + radius_px, center.y + radius_px * kappa),
            Point::new(center.x + radius_px * kappa, center.y + radius_px),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x, center.y + radius_px),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x - radius_px * kappa, center.y + radius_px),
            Point::new(center.x - radius_px, center.y + radius_px * kappa),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x - radius_px, center.y),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x - radius_px, center.y - radius_px * kappa),
            Point::new(center.x - radius_px * kappa, center.y - radius_px),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x, center.y - radius_px),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x + radius_px * kappa, center.y - radius_px),
            Point::new(center.x + radius_px, center.y - radius_px * kappa),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x + radius_px, center.y),
            transform,
            pivot,
        ),
        PathElement::Close,
    ]
}

fn ellipse_path_about(
    center: Point,
    radius_x_px: f64,
    radius_y_px: f64,
    transform: Transform,
    pivot: Point,
) -> Vec<PathElement> {
    let kappa = 0.552_284_749_830_793_6;
    vec![
        transformed_point_element_about(
            Point::new(center.x + radius_x_px, center.y),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x + radius_x_px, center.y + radius_y_px * kappa),
            Point::new(center.x + radius_x_px * kappa, center.y + radius_y_px),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x, center.y + radius_y_px),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x - radius_x_px * kappa, center.y + radius_y_px),
            Point::new(center.x - radius_x_px, center.y + radius_y_px * kappa),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x - radius_x_px, center.y),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x - radius_x_px, center.y - radius_y_px * kappa),
            Point::new(center.x - radius_x_px * kappa, center.y - radius_y_px),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x, center.y - radius_y_px),
            transform,
            pivot,
        ),
        transformed_cubic_element_about(
            Point::new(center.x + radius_x_px * kappa, center.y - radius_y_px),
            Point::new(center.x + radius_x_px, center.y - radius_y_px * kappa),
            transform,
            pivot,
        ),
        transformed_point_element_about(
            Point::new(center.x + radius_x_px, center.y),
            transform,
            pivot,
        ),
        PathElement::Close,
    ]
}

fn transformed_point_element(point: Point, transform: Transform) -> PathElement {
    transformed_point_element_about(point, transform, A_CENTER)
}

fn transformed_cubic_element(
    control_1: Point,
    control_2: Point,
    transform: Transform,
) -> PathElement {
    transformed_cubic_element_about(control_1, control_2, transform, A_CENTER)
}

fn transformed_point_element_about(
    point: Point,
    transform: Transform,
    pivot: Point,
) -> PathElement {
    point_element(transform_point_about(point, transform, pivot))
}

fn transformed_cubic_element_about(
    control_1: Point,
    control_2: Point,
    transform: Transform,
    pivot: Point,
) -> PathElement {
    cubic_element(
        transform_point_about(control_1, transform, pivot),
        transform_point_about(control_2, transform, pivot),
    )
}

fn transform_point_about(point: Point, transform: Transform, pivot: Point) -> Point {
    let radians = transform.rotation_deg.to_radians();
    let cosine = radians.cos();
    let sine = radians.sin();
    let local_x = (point.x - pivot.x) * transform.scale;
    let local_y = (point.y - pivot.y) * transform.scale;
    let primary = Point::new(
        pivot.x + local_x * cosine - local_y * sine + transform.translation_x,
        pivot.y + local_x * sine + local_y * cosine,
    );
    rotate_point_about(primary, A_BRAKE_PIVOT, transform.brake_rotation_deg)
}

fn transform_x_point(point: Point, transform: XTransform) -> Point {
    let radians = transform.rotation_deg.to_radians();
    let cosine = radians.cos();
    let sine = radians.sin();
    let local_x = (point.x - X_CENTER.x) * transform.scale;
    let local_y = (point.y - X_CENTER.y) * transform.scale;
    let primary = Point::new(
        X_CENTER.x + local_x * cosine - local_y * sine + transform.translation_x,
        X_CENTER.y + local_x * sine + local_y * cosine + transform.translation_y,
    );
    rotate_point_about(primary, X_BRAKE_PIVOT, transform.brake_rotation_deg)
}

fn rotate_point_about(point: Point, pivot: Point, rotation_deg: f64) -> Point {
    let radians = rotation_deg.to_radians();
    let cosine = radians.cos();
    let sine = radians.sin();
    let local_x = point.x - pivot.x;
    let local_y = point.y - pivot.y;
    Point::new(
        pivot.x + local_x * cosine - local_y * sine,
        pivot.y + local_x * sine + local_y * cosine,
    )
}

fn point_element(point: Point) -> PathElement {
    PathElement::Point(percent_x(point.x), percent_y(point.y))
}

fn cubic_element(control_1: Point, control_2: Point) -> PathElement {
    PathElement::Cubic(
        percent_x(control_1.x),
        percent_y(control_1.y),
        percent_x(control_2.x),
        percent_y(control_2.y),
    )
}

fn percent_x(value: f64) -> Size {
    percent(value / CANVAS_WIDTH_PX * 100.0)
}

fn percent_y(value: f64) -> Size {
    percent(value / CANVAS_HEIGHT_PX * 100.0)
}

fn percent(value: f64) -> Size {
    Size::Percent(Numeric::F64(value))
}

fn lerp(start: f64, end: f64, t: f64) -> f64 {
    start + (end - start) * t
}

fn morph_point(start: Point, end: Point, t: f64) -> Point {
    Point::new(lerp(start.x, end.x, t), lerp(start.y, end.y, t))
}

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

    #[test]
    fn banner_keeps_stable_topology_through_the_curtain_relay() {
        for drops in [
            [0.0; 5],
            [0.5, 0.0, 0.0, 0.0, 0.0],
            [1.06, 0.7, 0.2, 0.0, 0.0],
            [1.0; 5],
        ] {
            assert_eq!(banner_path(drops).len(), 14);
        }
    }

    #[test]
    fn a_paths_keep_stable_topology_while_rolling() {
        for transform in [
            Transform {
                translation_x: A_INITIAL_TRANSLATION_X,
                rotation_deg: -90.0,
                scale: A_INITIAL_SCALE,
                brake_rotation_deg: 0.0,
            },
            Transform {
                translation_x: -110.0,
                rotation_deg: -45.0,
                scale: 0.6,
                brake_rotation_deg: 0.0,
            },
            Transform {
                translation_x: -5.0,
                rotation_deg: -2.0,
                scale: 0.98,
                brake_rotation_deg: 0.0,
            },
            Transform::default(),
        ] {
            for morph_progress in [0.0, 0.2, 0.75, 1.0] {
                assert_eq!(a_path(transform, morph_progress).len(), 22);
                assert_eq!(a_counter_path(transform, morph_progress).len(), 10);
            }
        }
    }

    #[test]
    fn settled_banner_and_letters_match_source_extents() {
        let banner = banner_path([1.0; 5]);
        assert_eq!(banner[0], point_element(Point::new(137.23, 23.34)));
        assert_eq!(banner[4], point_element(Point::new(892.72, 168.57)));
        assert_eq!(banner[6], point_element(Point::new(789.81, 313.20)));
        assert_eq!(banner[12], point_element(Point::new(137.23, 313.20)));

        let settled_a = a_path(Transform::default(), 1.0);
        assert_eq!(settled_a[0], point_element(Point::new(297.93, 152.39)));

        let settled_x = x_path(XTransform::default());
        assert_eq!(
            settled_x[0],
            point_element(Point::new(766.09, 282.99 + X_FINAL_Y_OFFSET_PX))
        );
    }

    #[test]
    fn p_counter_preserves_the_source_overlap_without_a_cutout_seam() {
        assert!(P_COUNTER_CENTER.x - P_COUNTER_RADIUS_PX < BANNER_LEFT_X);
    }

    #[test]
    fn p_counter_starts_fully_behind_the_pillar_edge() {
        let initial_right_edge = P_COUNTER_CENTER.x - 110.0 + P_COUNTER_RADIUS_PX * P_INITIAL_SCALE;
        assert!(initial_right_edge < BANNER_LEFT_X);
    }

    #[test]
    fn p_counter_reveal_mask_opens_after_emergence() {
        let hidden = p_counter_reveal_mask_path(0.0);
        let emerged = p_counter_reveal_mask_path(1.0);
        assert_eq!(hidden[0], point_element(Point::new(BANNER_LEFT_X, 0.0)));
        assert_eq!(emerged[0], point_element(Point::new(0.0, 0.0)));
    }

    #[test]
    fn p_ring_and_counter_keep_stable_topology_through_the_roll() {
        for transform in [
            Transform {
                translation_x: -110.0,
                rotation_deg: -90.0,
                scale: P_INITIAL_SCALE,
                brake_rotation_deg: 0.0,
            },
            Transform {
                translation_x: -45.0,
                rotation_deg: -35.0,
                scale: 0.72,
                brake_rotation_deg: 0.0,
            },
            Transform::default(),
        ] {
            assert_eq!(p_ring_path(transform).len(), 10);
            assert_eq!(p_counter_path(transform).len(), 10);
        }
    }

    #[test]
    fn a_morph_starts_as_a_circle_with_collapsed_sidebar_points() {
        let initial = a_path(
            Transform {
                translation_x: A_INITIAL_TRANSLATION_X,
                rotation_deg: -90.0,
                scale: A_INITIAL_SCALE,
                brake_rotation_deg: 0.0,
            },
            0.0,
        );
        assert_eq!(initial[4], initial[6]);
        assert_eq!(initial[6], initial[8]);
        assert_eq!(initial[8], initial[10]);
        assert_eq!(initial[10], initial[12]);
        assert_eq!(initial[12], initial[14]);
    }

    #[test]
    fn initial_a_stays_inside_the_p_counter_until_its_roll_begins() {
        let initial_center = Point::new(A_CENTER.x + A_INITIAL_TRANSLATION_X, A_CENTER.y);
        let dispatch_center = Point::new(
            P_COUNTER_CENTER.x + P_DISPATCH_TRANSLATION_X,
            P_COUNTER_CENTER.y,
        );
        let center_offset = ((initial_center.x - dispatch_center.x).powi(2)
            + (initial_center.y - P_COUNTER_CENTER.y).powi(2))
        .sqrt();
        assert!(
            center_offset + A_INITIAL_OUTER_RADIUS_PX * A_INITIAL_SCALE
                < P_COUNTER_RADIUS_PX * P_DISPATCH_SCALE
        );
    }

    #[test]
    fn x_starts_nested_inside_the_settled_a_sidebar() {
        let initial = XTransform::initial();
        let nested = x_source_points().map(|point| transform_x_point(point, initial));
        let min_x = nested
            .iter()
            .map(|point| point.x)
            .fold(f64::INFINITY, f64::min);
        let max_x = nested
            .iter()
            .map(|point| point.x)
            .fold(f64::NEG_INFINITY, f64::max);
        assert!(min_x >= A_SIDEBAR_LEFT_X);
        assert!(max_x <= A_SIDEBAR_RIGHT_X);
    }

    #[test]
    fn braking_rotations_keep_the_lower_right_contacts_planted() {
        let a_contact = transform_point_about(
            A_BRAKE_PIVOT,
            Transform {
                brake_rotation_deg: 7.5,
                ..Transform::default()
            },
            A_CENTER,
        );
        assert!((a_contact.x - A_BRAKE_PIVOT.x).abs() < 0.000_001);
        assert!((a_contact.y - A_BRAKE_PIVOT.y).abs() < 0.000_001);

        let x_contact = transform_x_point(
            Point::new(766.09, 282.99),
            XTransform {
                brake_rotation_deg: 7.0,
                ..XTransform::default()
            },
        );
        assert!((x_contact.x - X_BRAKE_PIVOT.x).abs() < 0.000_001);
        assert!((x_contact.y - X_BRAKE_PIVOT.y).abs() < 0.000_001);
    }

    #[test]
    fn a_counter_stays_clipped_until_it_clears_the_p_ring() {
        let reveal_mask = a_counter_reveal_mask_path();
        let boundary_x = P_RING_CENTER.x + P_RING_RADIUS_X_PX;
        assert_eq!(reveal_mask[0], point_element(Point::new(boundary_x, 0.0)));
        assert_eq!(
            reveal_mask[6],
            point_element(Point::new(boundary_x, CANVAS_HEIGHT_PX))
        );
    }
}