1#![allow(unused_imports)]
2
3use crate::*;
4use pax_engine::api::*;
5use pax_engine::*;
6
7const MID_X: f64 = 50.0768;
8const TOP_BACK_LEFT: Point = Point::new(MID_X, 0.0);
9const TOP_BACK_RIGHT: Point = Point::new(100.0, 5.2404);
10const TOP_FRONT_LEFT: Point = Point::new(0.0, 5.622);
11const TOP_FRONT_RIGHT: Point = Point::new(MID_X, 10.4807);
12const BOTTOM_LEFT: Point = Point::new(0.0, 93.3227);
13const BOTTOM_RIGHT: Point = Point::new(MID_X, 100.0);
14const BACKING_BOTTOM_RIGHT: Point = Point::new(100.0, 94.7597);
15
16const CANVAS_Y_SCALE: f64 = 0.96;
18const COMPONENT_WIDTH_PX: f64 = 137.23;
19const COMPONENT_HEIGHT_PX: f64 = 463.95;
20const X_PX_PER_PERCENT: f64 = COMPONENT_WIDTH_PX / 100.0;
21const Y_PX_PER_PERCENT: f64 = COMPONENT_HEIGHT_PX * CANVAS_Y_SCALE / 100.0;
22
23const INITIAL_ROLL_RADIUS_PX: f64 = 13.0;
24
25#[pax]
27#[engine_import_path("pax_engine")]
28#[custom(Default)]
29#[file("drawing/pax_logo/animated_pax_logo_post.pax")]
30pub struct AnimatedPaxLogoPost {
31 pub fill: Property<Paint>,
32 pub backing_fill: Property<Paint>,
33 pub progress: Property<f64>,
34 pub rolling: Property<f64>,
35 pub extension: Property<f64>,
36 pub wave: Property<f64>,
37 pub swing: Property<f64>,
38 pub fall: Property<f64>,
39 pub roll_radius_px: Property<f64>,
40 pub top_fabric_elements: Property<Vec<PathElement>>,
41 pub fabric_elements: Property<Vec<PathElement>>,
42 pub backing_fabric_elements: Property<Vec<PathElement>>,
43 pub roll_start_cap_elements: Property<Vec<PathElement>>,
44 pub roll_body_elements: Property<Vec<PathElement>>,
45 pub roll_end_cap_elements: Property<Vec<PathElement>>,
46}
47
48impl Default for AnimatedPaxLogoPost {
49 fn default() -> Self {
50 let motion = motion_from_controls(MotionControls::default());
51 Self {
52 fill: Property::new(Paint::Solid(Color::BLACK)),
53 backing_fill: Property::new(Paint::Solid(Color::WHITE)),
54 progress: Property::new(0.0),
55 rolling: Property::new(0.0),
56 extension: Property::new(0.0),
57 wave: Property::new(0.0),
58 swing: Property::new(0.0),
59 fall: Property::new(0.0),
60 roll_radius_px: Property::new(INITIAL_ROLL_RADIUS_PX),
61 top_fabric_elements: Property::new(top_fabric_path(motion)),
62 fabric_elements: Property::new(fabric_path(motion)),
63 backing_fabric_elements: Property::new(backing_fabric_path(0.0)),
64 roll_start_cap_elements: Property::new(roll_start_cap_path(motion)),
65 roll_body_elements: Property::new(roll_body_path(motion)),
66 roll_end_cap_elements: Property::new(roll_end_cap_path(motion)),
67 }
68 }
69}
70
71impl AnimatedPaxLogoPost {
72 pub fn handle_mount(&mut self, _ctx: &NodeContext) {
73 let rolling = self.rolling.local();
74 let extension = self.extension.local();
75 let wave = self.wave.local();
76 let swing = self.swing.local();
77 let fall = self.fall.local();
78 let roll_radius_px = self.roll_radius_px.local();
79 let motion_dependencies = [
80 rolling.untyped(),
81 extension.untyped(),
82 wave.untyped(),
83 swing.untyped(),
84 fall.untyped(),
85 roll_radius_px.untyped(),
86 ];
87 let motion = LocalProperty::computed(
88 move || {
89 motion_from_controls(MotionControls {
90 rolling: rolling.get(),
91 extension: extension.get(),
92 wave: wave.get(),
93 swing: swing.get(),
94 fall: fall.get(),
95 roll_radius_px: roll_radius_px.get(),
96 })
97 },
98 &motion_dependencies,
99 );
100
101 self.top_fabric_elements
102 .replace_with(computed_path(&motion, top_fabric_path));
103 self.fabric_elements
104 .replace_with(computed_path(&motion, fabric_path));
105 let extension = self.extension.local();
106 self.backing_fabric_elements
107 .replace_with(LocalProperty::computed(
108 move || backing_fabric_path(extension.get()),
109 &[self.extension.untyped()],
110 ));
111 self.roll_start_cap_elements
112 .replace_with(computed_path(&motion, roll_start_cap_path));
113 self.roll_body_elements
114 .replace_with(computed_path(&motion, roll_body_path));
115 self.roll_end_cap_elements
116 .replace_with(computed_path(&motion, roll_end_cap_path));
117 }
118}
119
120fn computed_path(
121 motion: &LocalProperty<PostMotion>,
122 build: fn(PostMotion) -> Vec<PathElement>,
123) -> LocalProperty<Vec<PathElement>> {
124 let motion = motion.clone();
125 let dependencies = [motion.untyped()];
126 LocalProperty::computed(move || build(motion.get()), &dependencies)
127}
128
129pub(crate) fn settled_post_paths() -> [Vec<PathElement>; 6] {
130 let motion = motion_from_controls(MotionControls {
131 rolling: 1.0,
132 extension: 1.0,
133 wave: 0.0,
134 swing: 0.0,
135 fall: 1.0,
136 roll_radius_px: 0.7,
137 });
138 [
139 roll_start_cap_path(motion),
140 roll_body_path(motion),
141 roll_end_cap_path(motion),
142 top_fabric_path(motion),
143 fabric_path(motion),
144 backing_fabric_path(1.0),
145 ]
146}
147
148#[derive(Clone, Copy, Debug, Default, PartialEq)]
149struct Point {
150 x: f64,
151 y: f64,
152}
153
154impl Point {
155 const fn new(x: f64, y: f64) -> Self {
156 Self { x, y }
157 }
158
159 fn lerp(self, other: Self, t: f64) -> Self {
160 Self::new(lerp(self.x, other.x, t), lerp(self.y, other.y, t))
161 }
162
163 fn add(self, other: Self) -> Self {
164 Self::new(self.x + other.x, self.y + other.y)
165 }
166
167 fn scale(self, amount: f64) -> Self {
168 Self::new(self.x * amount, self.y * amount)
169 }
170}
171
172#[derive(Clone, Copy)]
173struct MotionControls {
174 rolling: f64,
175 extension: f64,
176 wave: f64,
177 swing: f64,
178 fall: f64,
179 roll_radius_px: f64,
180}
181
182impl Default for MotionControls {
183 fn default() -> Self {
184 Self {
185 rolling: 0.0,
186 extension: 0.0,
187 wave: 0.0,
188 swing: 0.0,
189 fall: 0.0,
190 roll_radius_px: INITIAL_ROLL_RADIUS_PX,
191 }
192 }
193}
194
195#[derive(Clone, Copy, Default)]
196struct PostMotion {
197 rolling: f64,
198 wave: f64,
199 swing: f64,
200 fall: f64,
201 fabric_bottom_left: Point,
202 fabric_bottom_right: Point,
203 roll_axis_start: Point,
204 roll_axis_end: Point,
205 roll_radius_px: f64,
206}
207
208impl Interpolatable for PostMotion {}
209
210fn motion_from_controls(controls: MotionControls) -> PostMotion {
211 let base_bottom_left = Point::new(
212 lerp(TOP_FRONT_LEFT.x, BOTTOM_LEFT.x, controls.extension)
213 + controls.swing
214 + controls.wave * 0.12,
215 lerp(TOP_FRONT_LEFT.y, BOTTOM_LEFT.y, controls.extension),
216 );
217 let base_bottom_right = Point::new(
218 lerp(TOP_FRONT_RIGHT.x, BOTTOM_RIGHT.x, controls.extension)
219 + controls.swing * 0.75
220 + controls.wave * 0.55,
221 lerp(TOP_FRONT_RIGHT.y, BOTTOM_RIGHT.y, controls.extension),
222 );
223 let (roll_axis_start, roll_axis_end) = if controls.fall <= f64::EPSILON {
224 (
225 TOP_BACK_LEFT.lerp(TOP_FRONT_LEFT, controls.rolling),
226 TOP_BACK_RIGHT.lerp(TOP_FRONT_RIGHT, controls.rolling),
227 )
228 } else {
229 (base_bottom_left, base_bottom_right)
230 };
231
232 PostMotion {
233 rolling: controls.rolling,
234 wave: controls.wave,
235 swing: controls.swing,
236 fall: controls.fall,
237 fabric_bottom_left: base_bottom_left,
238 fabric_bottom_right: base_bottom_right,
239 roll_axis_start,
240 roll_axis_end,
241 roll_radius_px: controls.roll_radius_px,
242 }
243}
244
245fn top_fabric_path(motion: PostMotion) -> Vec<PathElement> {
246 let reveal = motion.rolling.max(0.002);
247 let moving_left = TOP_BACK_LEFT.lerp(TOP_FRONT_LEFT, reveal);
248 let moving_right = TOP_BACK_RIGHT.lerp(TOP_FRONT_RIGHT, reveal);
249
250 vec![
251 point_element(TOP_BACK_LEFT),
252 PathElement::Line,
253 point_element(TOP_BACK_RIGHT),
254 PathElement::Line,
255 point_element(moving_right),
256 PathElement::Line,
257 point_element(moving_left),
258 PathElement::Close,
259 ]
260}
261
262fn fabric_path(motion: PostMotion) -> Vec<PathElement> {
263 let bottom_left = motion.fabric_bottom_left;
264 let bottom_right = motion.fabric_bottom_right;
265 let left_extent = bottom_left.y - TOP_FRONT_LEFT.y;
266 let right_extent = bottom_right.y - TOP_FRONT_RIGHT.y;
267 let roll_sag =
268 radius_y_percent(motion.roll_radius_px) * motion.fall * (1.0 - motion.fall) * 0.65;
269 let left_upper = Point::new(
270 motion.swing * 0.05 - motion.wave * 0.08,
271 TOP_FRONT_LEFT.y + left_extent / 3.0,
272 );
273 let left_middle = Point::new(
274 motion.swing * 0.52 + motion.wave * 0.32,
275 TOP_FRONT_LEFT.y + left_extent * 2.0 / 3.0,
276 );
277 let right_upper = Point::new(
278 TOP_FRONT_RIGHT.x - motion.wave * 0.22,
279 TOP_FRONT_RIGHT.y + right_extent / 3.0,
280 );
281 let right_middle = Point::new(
282 TOP_FRONT_RIGHT.x + motion.swing * 0.70 + motion.wave * 0.90,
283 TOP_FRONT_RIGHT.y + right_extent * 2.0 / 3.0,
284 );
285 let left_edge = [TOP_FRONT_LEFT, left_upper, left_middle, bottom_left];
286 let right_edge = [bottom_right, right_middle, right_upper, TOP_FRONT_RIGHT];
287
288 let mut path = vec![point_element(TOP_FRONT_LEFT)];
289 append_catmull_rom_edge(&mut path, &left_edge);
290 path.extend([
291 PathElement::Cubic(
292 percent_x(lerp(bottom_left.x, bottom_right.x, 1.0 / 3.0)),
293 percent_y(lerp(bottom_left.y, bottom_right.y, 1.0 / 3.0) + roll_sag),
294 percent_x(lerp(bottom_left.x, bottom_right.x, 2.0 / 3.0)),
295 percent_y(lerp(bottom_left.y, bottom_right.y, 2.0 / 3.0) + roll_sag),
296 ),
297 point_element(bottom_right),
298 ]);
299 append_catmull_rom_edge(&mut path, &right_edge);
300 path.push(PathElement::Close);
301 path
302}
303
304fn backing_fabric_path(extension: f64) -> Vec<PathElement> {
305 let extension = extension.clamp(0.0, 1.018).max(0.002);
306 let bottom_left = TOP_FRONT_RIGHT.lerp(BOTTOM_RIGHT, extension);
307 let bottom_right = TOP_BACK_RIGHT.lerp(BACKING_BOTTOM_RIGHT, extension);
308
309 vec![
310 point_element(TOP_FRONT_RIGHT),
311 PathElement::Line,
312 point_element(TOP_BACK_RIGHT),
313 PathElement::Line,
314 point_element(bottom_right),
315 PathElement::Line,
316 point_element(bottom_left),
317 PathElement::Close,
318 ]
319}
320
321fn append_catmull_rom_edge(path: &mut Vec<PathElement>, points: &[Point; 4]) {
322 for index in 0..3 {
323 let previous = if index == 0 {
324 points[index]
325 } else {
326 points[index - 1]
327 };
328 let start = points[index];
329 let end = points[index + 1];
330 let next = if index == 2 {
331 points[index + 1]
332 } else {
333 points[index + 2]
334 };
335 let control_1 = Point::new(
336 start.x + (end.x - previous.x) / 6.0,
337 start.y + (end.y - previous.y) / 6.0,
338 );
339 let control_2 = Point::new(
340 end.x - (next.x - start.x) / 6.0,
341 end.y - (next.y - start.y) / 6.0,
342 );
343 path.push(PathElement::Cubic(
344 percent_x(control_1.x),
345 percent_y(control_1.y),
346 percent_x(control_2.x),
347 percent_y(control_2.y),
348 ));
349 path.push(point_element(end));
350 }
351}
352
353fn roll_start_cap_path(motion: PostMotion) -> Vec<PathElement> {
354 circle_path(motion.roll_axis_start, motion.roll_radius_px)
355}
356
357fn roll_end_cap_path(motion: PostMotion) -> Vec<PathElement> {
358 circle_path(motion.roll_axis_end, motion.roll_radius_px)
359}
360
361fn roll_body_path(motion: PostMotion) -> Vec<PathElement> {
362 let start = motion.roll_axis_start;
363 let end = motion.roll_axis_end;
364 let normal = normal_offset(start, end, motion.roll_radius_px);
365
366 vec![
367 point_element(start.add(normal)),
368 PathElement::Line,
369 point_element(end.add(normal)),
370 PathElement::Line,
371 point_element(end.add(normal.scale(-1.0))),
372 PathElement::Line,
373 point_element(start.add(normal.scale(-1.0))),
374 PathElement::Close,
375 ]
376}
377
378fn circle_path(center: Point, radius_px: f64) -> Vec<PathElement> {
379 let rx = radius_x_percent(radius_px);
380 let ry = radius_y_percent(radius_px);
381 let kappa = 0.552_284_749_830_793_6;
382 vec![
383 point_element(Point::new(center.x + rx, center.y)),
384 PathElement::Cubic(
385 percent_x(center.x + rx),
386 percent_y(center.y + ry * kappa),
387 percent_x(center.x + rx * kappa),
388 percent_y(center.y + ry),
389 ),
390 point_element(Point::new(center.x, center.y + ry)),
391 PathElement::Cubic(
392 percent_x(center.x - rx * kappa),
393 percent_y(center.y + ry),
394 percent_x(center.x - rx),
395 percent_y(center.y + ry * kappa),
396 ),
397 point_element(Point::new(center.x - rx, center.y)),
398 PathElement::Cubic(
399 percent_x(center.x - rx),
400 percent_y(center.y - ry * kappa),
401 percent_x(center.x - rx * kappa),
402 percent_y(center.y - ry),
403 ),
404 point_element(Point::new(center.x, center.y - ry)),
405 PathElement::Cubic(
406 percent_x(center.x + rx * kappa),
407 percent_y(center.y - ry),
408 percent_x(center.x + rx),
409 percent_y(center.y - ry * kappa),
410 ),
411 point_element(Point::new(center.x + rx, center.y)),
412 PathElement::Close,
413 ]
414}
415
416fn normal_offset(start: Point, end: Point, radius_px: f64) -> Point {
417 let dx_px = (end.x - start.x) * X_PX_PER_PERCENT;
418 let dy_px = (end.y - start.y) * Y_PX_PER_PERCENT;
419 let length_px = (dx_px * dx_px + dy_px * dy_px).sqrt().max(f64::EPSILON);
420 Point::new(
421 (-dy_px / length_px * radius_px) / X_PX_PER_PERCENT,
422 (dx_px / length_px * radius_px) / Y_PX_PER_PERCENT,
423 )
424}
425
426#[cfg(test)]
427fn screen_distance(start: Point, end: Point) -> f64 {
428 let dx = (end.x - start.x) * X_PX_PER_PERCENT;
429 let dy = (end.y - start.y) * Y_PX_PER_PERCENT;
430 (dx * dx + dy * dy).sqrt()
431}
432
433fn radius_x_percent(radius_px: f64) -> f64 {
434 radius_px / X_PX_PER_PERCENT
435}
436
437fn radius_y_percent(radius_px: f64) -> f64 {
438 radius_px / Y_PX_PER_PERCENT
439}
440
441fn point_element(point: Point) -> PathElement {
442 PathElement::Point(percent_x(point.x), percent_y(point.y))
443}
444
445fn percent_x(value: f64) -> Size {
446 percent(value)
447}
448
449fn percent(value: f64) -> Size {
450 Size::Percent(Numeric::F64(value))
451}
452
453fn percent_y(value: f64) -> Size {
454 percent(value * CANVAS_Y_SCALE)
455}
456
457fn lerp(start: f64, end: f64, t: f64) -> f64 {
458 start + (end - start) * t
459}
460
461#[cfg(test)]
462mod tests {
463 use super::*;
464
465 fn top_controls(rolling: f64, radius: f64) -> MotionControls {
466 MotionControls {
467 rolling,
468 roll_radius_px: radius,
469 ..Default::default()
470 }
471 }
472
473 fn fall_controls(
474 extension: f64,
475 fall: f64,
476 wave: f64,
477 swing: f64,
478 radius: f64,
479 ) -> MotionControls {
480 MotionControls {
481 rolling: 1.0,
482 extension,
483 fall,
484 wave,
485 swing,
486 roll_radius_px: radius,
487 }
488 }
489
490 #[test]
491 fn post_paths_keep_stable_topologies_at_every_motion_phase() {
492 let samples = [
493 top_controls(0.0, INITIAL_ROLL_RADIUS_PX),
494 top_controls(0.5, 8.125),
495 top_controls(1.0, 6.5),
496 fall_controls(0.25, 0.5, -5.2503, -7.0, 3.6),
497 fall_controls(1.0, 1.0, 0.0, 0.0, 0.7),
498 fall_controls(1.018, 1.0, 0.0, 0.0, 0.7),
499 ];
500
501 for controls in samples {
502 let motion = motion_from_controls(controls);
503 let top = top_fabric_path(motion);
504 let fabric = fabric_path(motion);
505 let start_cap = roll_start_cap_path(motion);
506 let body = roll_body_path(motion);
507 let end_cap = roll_end_cap_path(motion);
508 assert_eq!(top.len(), 8);
509 assert_eq!(top.last(), Some(&PathElement::Close));
510 assert_eq!(fabric.len(), 16);
511 assert_eq!(fabric.last(), Some(&PathElement::Close));
512 assert_eq!(start_cap.len(), 10);
513 assert_eq!(start_cap.last(), Some(&PathElement::Close));
514 assert_eq!(body.len(), 8);
515 assert_eq!(body.last(), Some(&PathElement::Close));
516 assert_eq!(end_cap.len(), 10);
517 assert_eq!(end_cap.last(), Some(&PathElement::Close));
518 assert_eq!(backing_fabric_path(controls.extension).len(), 8);
519 }
520 }
521
522 #[test]
523 fn white_post_face_drops_as_a_rigid_parallelogram() {
524 let settled = backing_fabric_path(1.0);
525 assert_eq!(settled[0], point_element(TOP_FRONT_RIGHT));
526 assert_eq!(settled[2], point_element(TOP_BACK_RIGHT));
527 assert_eq!(settled[4], point_element(BACKING_BOTTOM_RIGHT));
528 assert_eq!(settled[6], point_element(BOTTOM_RIGHT));
529 }
530
531 #[test]
532 fn top_face_is_revealed_before_the_descent() {
533 let start = motion_from_controls(top_controls(0.0, INITIAL_ROLL_RADIUS_PX));
534 let rolled = motion_from_controls(top_controls(1.0, 6.5));
535 let falling = motion_from_controls(fall_controls(0.25, 0.5, 0.0, 0.0, 3.6));
536
537 assert_eq!(start.fabric_bottom_left, TOP_FRONT_LEFT);
538 assert_eq!(rolled.rolling, 1.0);
539 assert_eq!(rolled.fabric_bottom_left, TOP_FRONT_LEFT);
540 assert!(falling.fabric_bottom_left.y > TOP_FRONT_LEFT.y);
541 }
542
543 #[test]
544 fn collapsed_fabric_has_no_pre_fall_sag() {
545 let collapsed = fabric_path(motion_from_controls(top_controls(
546 1.0,
547 INITIAL_ROLL_RADIUS_PX,
548 )));
549 let straight_bottom_edge = PathElement::Cubic(
550 percent_x(lerp(TOP_FRONT_LEFT.x, TOP_FRONT_RIGHT.x, 1.0 / 3.0)),
551 percent_y(lerp(TOP_FRONT_LEFT.y, TOP_FRONT_RIGHT.y, 1.0 / 3.0)),
552 percent_x(lerp(TOP_FRONT_LEFT.x, TOP_FRONT_RIGHT.x, 2.0 / 3.0)),
553 percent_y(lerp(TOP_FRONT_LEFT.y, TOP_FRONT_RIGHT.y, 2.0 / 3.0)),
554 );
555
556 assert_eq!(collapsed[7], straight_bottom_edge);
557
558 let falling = fabric_path(motion_from_controls(fall_controls(0.5, 0.5, 0.0, 0.0, 3.6)));
559 assert_ne!(falling[7], straight_bottom_edge);
560 }
561
562 #[test]
563 fn capsule_keeps_its_axis_spanning_the_prism_edge() {
564 let expected = screen_distance(TOP_BACK_LEFT, TOP_BACK_RIGHT);
565 for rolling in [0.0, 0.25, 0.5, 0.75, 1.0] {
566 let motion = motion_from_controls(top_controls(rolling, 10.0));
567 let length = screen_distance(motion.roll_axis_start, motion.roll_axis_end);
568 assert!((length - expected).abs() < 0.5);
569 }
570 }
571
572 #[test]
573 fn settled_post_matches_the_original_rigid_extents() {
574 let motion = motion_from_controls(fall_controls(1.0, 1.0, 0.0, 0.0, 0.7));
575 let path = fabric_path(motion);
576 assert_eq!(path[6], point_element(BOTTOM_LEFT));
577 assert_eq!(path[8], point_element(BOTTOM_RIGHT));
578 }
579
580 #[test]
581 fn scalar_controls_drive_spool_shrink_and_full_extension() {
582 let start = motion_from_controls(top_controls(0.0, INITIAL_ROLL_RADIUS_PX));
583 let settled = motion_from_controls(fall_controls(1.0, 1.0, 0.0, 0.0, 0.7));
584
585 assert_eq!(start.roll_radius_px, INITIAL_ROLL_RADIUS_PX);
586 assert!(start.roll_radius_px > settled.roll_radius_px);
587 assert_eq!(settled.fabric_bottom_left, BOTTOM_LEFT);
588 assert_eq!(settled.fabric_bottom_right, BOTTOM_RIGHT);
589 }
590
591 #[test]
592 fn lower_edge_extends_past_the_wall_then_returns() {
593 let peak = motion_from_controls(fall_controls(1.018, 1.0, 0.0, 0.0, 0.7));
594 let settled = motion_from_controls(fall_controls(1.0, 1.0, 0.0, 0.0, 0.7));
595
596 assert!(peak.fabric_bottom_left.y > BOTTOM_LEFT.y);
597 assert!(peak.fabric_bottom_right.y > BOTTOM_RIGHT.y);
598 assert_eq!(settled.fabric_bottom_left, BOTTOM_LEFT);
599 assert_eq!(settled.fabric_bottom_right, BOTTOM_RIGHT);
600 }
601
602 #[test]
603 fn spool_geometry_intentionally_overflows_the_post_bounds() {
604 assert!(TOP_BACK_LEFT.y - radius_y_percent(INITIAL_ROLL_RADIUS_PX) < 0.0);
605 assert!(TOP_FRONT_LEFT.x - radius_x_percent(INITIAL_ROLL_RADIUS_PX) < 0.0);
606 }
607}