rustmotion 0.5.0

A CLI tool that renders motion design videos from JSON scenarios. No browser, no Node.js — just a single Rust binary.
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
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
use anyhow::Result;
use skia_safe::{surfaces, Canvas, ColorType, ImageInfo, Paint, M44, V3};

use super::animator::{apply_orbits, apply_wiggles, extract_effects, resolve_animations, AnimatedProperties};
use super::renderer::color4f_from_hex;
use crate::components::{ChildComponent, Overlay, OverlayAnchor};
use crate::error::RustmotionError;
use crate::layout::{Constraints, LayoutNode};
use crate::schema::{AnimatedBackground, Camera, GradientType, InnerShadow, LayerStyle, Scene, SceneLayout, VideoConfig};
use crate::traits::{Container, RenderContext, Styled};

/// Render a single v2 ChildComponent with full animation/transform support.
///
/// This is the core of the v2 render pipeline. It:
/// 1. Checks timing (start_at/end_at)
/// 2. Resolves animations and wiggles
/// 3. Applies canvas transforms (translate, scale, rotate)
/// 4. Applies opacity/blur via Skia save_layer
/// 5. Calls widget.render()
pub fn render_component(
    canvas: &Canvas,
    child: &ChildComponent,
    layout: &LayoutNode,
    ctx: &RenderContext,
) -> Result<()> {
    let component = &child.component;

    // 1. Check timing
    if let Some(timed) = component.as_timed() {
        let (start_at, end_at) = timed.timing();
        if let Some(start) = start_at {
            if ctx.time < start {
                return Ok(());
            }
        }
        if let Some(end) = end_at {
            if ctx.time > end {
                return Ok(());
            }
        }
    }

    // 2. Resolve animations
    // Compute base animation time adjusted for start_at and stagger
    let anim_time = {
        let base_time = if let Some(timed) = component.as_timed() {
            let (start_at, _) = timed.timing();
            if let Some(start) = start_at {
                ctx.time - start
            } else {
                ctx.time
            }
        } else {
            ctx.time
        };
        (base_time - ctx.stagger_offset).max(0.0)
    };

    let props = {
        let styled = component.as_styled();
        let timeline = &styled.style_config().timeline;

        // Collect all animation effects: base animation + active timeline steps
        let base_effects = component.as_animatable()
            .map(|a| a.animation_effects())
            .unwrap_or(&[]);

        // Timeline: merge active steps' effects with base effects
        let timeline_effects: Vec<_> = if !timeline.is_empty() {
            timeline.iter()
                .filter(|step| anim_time >= step.at)
                .flat_map(|step| {
                    // Adjust each step's animation times relative to step.at
                    step.animation.iter().map(move |effect| {
                        (effect, step.at)
                    })
                })
                .collect()
        } else {
            Vec::new()
        };

        let has_effects = !base_effects.is_empty() || !timeline_effects.is_empty();

        if has_effects {
            // Extract base effects
            let extracted_base = extract_effects(base_effects);

            // Resolve base presets + keyframes
            let mut props = AnimatedProperties::default();
            for (preset, preset_config) in &extracted_base.presets {
                let p = resolve_animations(
                    &[],
                    Some(preset),
                    Some(preset_config),
                    anim_time,
                    ctx.scene_duration,
                );
                props.merge(&p);
            }
            if !extracted_base.keyframes.is_empty() {
                let kf_animations: Vec<_> = extracted_base.keyframes.into_iter().cloned().collect();
                let kf_props = resolve_animations(
                    &kf_animations,
                    None,
                    None,
                    anim_time,
                    ctx.scene_duration,
                );
                props.merge(&kf_props);
            }

            // Resolve timeline step effects (time relative to each step's `at`)
            for (effect, step_at) in &timeline_effects {
                let step_time = anim_time - step_at;
                let step_effects = std::slice::from_ref(*effect);
                let extracted_step = extract_effects(step_effects);

                for (preset, preset_config) in &extracted_step.presets {
                    let p = resolve_animations(
                        &[],
                        Some(preset),
                        Some(preset_config),
                        step_time,
                        ctx.scene_duration,
                    );
                    props.merge(&p);
                }
                if !extracted_step.keyframes.is_empty() {
                    let kf_animations: Vec<_> = extracted_step.keyframes.into_iter().cloned().collect();
                    let kf_props = resolve_animations(
                        &kf_animations,
                        None,
                        None,
                        step_time,
                        ctx.scene_duration,
                    );
                    props.merge(&kf_props);
                }
            }

            // Propagate char animation
            if extracted_base.char_animation.is_some() {
                props.char_animation = extracted_base.char_animation;
            }

            // Apply wiggles additively (base only)
            if !extracted_base.wiggles.is_empty() {
                let wiggles: Vec<_> = extracted_base.wiggles.into_iter().cloned().collect();
                apply_wiggles(&mut props, &wiggles, ctx.time);
            }

            // Apply orbit effects (base only)
            if !extracted_base.orbits.is_empty() {
                let orbits: Vec<_> = extracted_base.orbits.into_iter().cloned().collect();
                apply_orbits(&mut props, &orbits, ctx.time);
            }

            // Handle motion blur
            if let Some(blur_intensity) = extracted_base.motion_blur {
                if blur_intensity > 0.01 {
                    return render_component_with_motion_blur(
                        canvas,
                        child,
                        layout,
                        ctx,
                        blur_intensity,
                    );
                }
            }

            props
        } else {
            AnimatedProperties::default()
        }
    };

    // Skip if fully transparent
    if props.opacity <= 0.0 {
        return Ok(());
    }

    render_component_inner(canvas, child, layout, ctx, &props)
}

fn render_component_inner(
    canvas: &Canvas,
    child: &ChildComponent,
    layout: &LayoutNode,
    ctx: &RenderContext,
    props: &AnimatedProperties,
) -> Result<()> {
    let component = &child.component;
    let styled = component.as_styled();

    canvas.save();

    // Component center for scale/rotation (relative to layout origin)
    let cx = layout.width / 2.0;
    let cy = layout.height / 2.0;

    // Apply position offset from animation
    canvas.translate((props.translate_x, props.translate_y));

    // Motion path: move element along an SVG path based on motion_progress
    if let Some(ref motion_path_svg) = styled.style_config().motion_path {
        if props.motion_progress >= 0.0 {
            if let Some(path) = skia_safe::Path::from_svg(motion_path_svg) {
                let mut measure = skia_safe::PathMeasure::new(&path, false, None);
                let length = measure.length();
                let distance = length * props.motion_progress.clamp(0.0, 1.0);
                if let Some((pos, _tangent)) = measure.pos_tan(distance) {
                    canvas.translate((pos.x, pos.y));
                }
            }
        }
    }

    // Check if we need 3D perspective transforms
    let needs_3d = props.rotate_x.abs() > 0.01
        || props.rotate_y.abs() > 0.01
        || props.perspective >= 0.0;

    // 3D adaptive shadow: draw ground-plane shadow BEFORE applying 3D transform
    if needs_3d {
        if let Some(ref shadow) = styled.style_config().box_shadow {
            draw_3d_shadow(canvas, layout.width, layout.height, styled.style_config().border_radius_or(0.0), shadow, props);
        }
    }

    if needs_3d {
        // 3D transform via M44: translate to center, apply perspective + rotations, translate back
        let perspective_dist = if props.perspective >= 0.0 { props.perspective } else { 800.0 };

        // Build the 4x4 transform matrix
        let mut m = M44::translate(cx, cy, 0.0);

        // Apply perspective: shrink Z contribution to simulate depth
        if perspective_dist > 0.0 {
            // Perspective matrix: row 3 col 2 = -1/d
            // This makes objects further in Z appear smaller
            let persp = M44::col_major(&[
                1.0, 0.0, 0.0, 0.0,
                0.0, 1.0, 0.0, 0.0,
                0.0, 0.0, 1.0, 0.0,
                0.0, 0.0, -1.0 / perspective_dist, 1.0,
            ]);
            m.pre_concat(&persp);
        }

        // Apply rotations (in degrees → radians)
        if props.rotate_x.abs() > 0.01 {
            let rad = props.rotate_x * std::f32::consts::PI / 180.0;
            m.pre_concat(&M44::rotate(V3::new(1.0, 0.0, 0.0), rad));
        }
        if props.rotate_y.abs() > 0.01 {
            let rad = props.rotate_y * std::f32::consts::PI / 180.0;
            m.pre_concat(&M44::rotate(V3::new(0.0, 1.0, 0.0), rad));
        }

        // Apply 2D rotation (Z axis)
        if props.rotation.abs() > 0.01 {
            let rad = props.rotation * std::f32::consts::PI / 180.0;
            m.pre_concat(&M44::rotate(V3::new(0.0, 0.0, 1.0), rad));
        }

        // Apply scale
        if (props.scale_x - 1.0).abs() > 0.001 || (props.scale_y - 1.0).abs() > 0.001 {
            let scale_m = M44::col_major(&[
                props.scale_x, 0.0, 0.0, 0.0,
                0.0, props.scale_y, 0.0, 0.0,
                0.0, 0.0, 1.0, 0.0,
                0.0, 0.0, 0.0, 1.0,
            ]);
            m.pre_concat(&scale_m);
        }

        // Translate back from center
        m.pre_concat(&M44::translate(-cx, -cy, 0.0));

        canvas.concat_44(&m);
    } else {
        // Standard 2D transforms (faster path, no M44 overhead)
        if (props.scale_x - 1.0).abs() > 0.001
            || (props.scale_y - 1.0).abs() > 0.001
            || props.rotation.abs() > 0.01
        {
            canvas.translate((cx, cy));
            if props.rotation.abs() > 0.01 {
                canvas.rotate(props.rotation, None);
            }
            if (props.scale_x - 1.0).abs() > 0.001 || (props.scale_y - 1.0).abs() > 0.001 {
                canvas.scale((props.scale_x, props.scale_y));
            }
            canvas.translate((-cx, -cy));
        }
    }

    // Margin offset
    let (mt, _mr, _mb, ml) = styled.margin();
    if mt.abs() > 0.001 || ml.abs() > 0.001 {
        canvas.translate((ml, mt));
    }

    // Padding inset (only for leaf widgets; containers handle padding internally)
    if !component.is_container() {
        let (pad_t, _pad_r, _pad_b, pad_l) = styled.padding();
        if pad_t.abs() > 0.001 || pad_l.abs() > 0.001 {
            canvas.translate((pad_l, pad_t));
        }
    }

    // Backdrop blur (glassmorphism): apply blur to content behind this element
    let backdrop_blur = styled.backdrop_blur();
    if backdrop_blur > 0.01 {
        if let Some(blur_filter) = skia_safe::image_filters::blur(
            (backdrop_blur, backdrop_blur),
            skia_safe::TileMode::Clamp,
            None,
            None,
        ) {
            // Clip to element bounds, then save_layer with backdrop filter
            let bounds = skia_safe::Rect::from_xywh(0.0, 0.0, layout.width, layout.height);
            canvas.save();
            canvas.clip_rect(bounds, skia_safe::ClipOp::Intersect, true);
            canvas.save_layer(
                &skia_safe::canvas::SaveLayerRec::default().backdrop(&blur_filter),
            );
            canvas.restore(); // restore save_layer (backdrop is now applied)
            canvas.restore(); // restore clip
        }
    }

    // Build image filter chain (blur only)
    let mut image_filter: Option<skia_safe::ImageFilter> = None;

    if props.blur > 0.01 {
        image_filter = skia_safe::image_filters::blur(
            (props.blur, props.blur),
            skia_safe::TileMode::Clamp,
            None,
            None,
        );
    }

    // Apply opacity/blur via save_layer
    let needs_layer = props.opacity < 1.0 || image_filter.is_some();
    if needs_layer {
        let mut layer_paint = Paint::default();
        layer_paint.set_alpha_f(props.opacity);
        if let Some(filter) = image_filter {
            layer_paint.set_image_filter(filter);
        }
        canvas.save_layer(&skia_safe::canvas::SaveLayerRec::default().paint(&layer_paint));
    }

    // Glow pre-pass: render only the blurred halo, content renders on top
    // Extract glow from animation effects
    let glow_config = if let Some(animatable) = component.as_animatable() {
        extract_effects(animatable.animation_effects()).glow.cloned()
    } else {
        None
    };
    if let Some(ref glow) = glow_config {
        let radius = if props.glow_radius >= 0.0 { props.glow_radius } else { glow.radius };
        let intensity = if props.glow_intensity >= 0.0 { props.glow_intensity } else { glow.intensity };
        let mut glow_color = color4f_from_hex(&glow.color);
        glow_color.a = (glow_color.a * intensity).min(1.0);
        let glow_filter = skia_safe::image_filters::drop_shadow_only(
            (0.0, 0.0),
            (radius, radius),
            glow_color,
            None,
            None,
            None,
        );
        if let Some(glow_f) = glow_filter {
            let mut glow_paint = Paint::default();
            glow_paint.set_image_filter(glow_f);
            canvas.save_layer(&skia_safe::canvas::SaveLayerRec::default().paint(&glow_paint));
            component.as_widget().render(canvas, layout, ctx, props)?;
            canvas.restore();
        }
    }

    // Render the component on top
    component.as_widget().render(canvas, layout, ctx, props)?;

    // Inner shadow (inset shadow effect)
    if let Some(ref inner_shadow) = styled.style_config().inner_shadow {
        draw_inner_shadow(canvas, layout.width, layout.height, styled.style_config().border_radius.unwrap_or(0.0), inner_shadow);
    }

    if needs_layer {
        canvas.restore();
    }
    canvas.restore();

    Ok(())
}

fn render_component_with_motion_blur(
    canvas: &Canvas,
    child: &ChildComponent,
    layout: &LayoutNode,
    ctx: &RenderContext,
    intensity: f32,
) -> Result<()> {
    let num_samples = if intensity < 0.3 { 3 } else { 5 };
    let frame_duration = 1.0 / ctx.fps as f64;
    let spread = frame_duration * intensity as f64;

    let width = ctx.video_width as i32;
    let height = ctx.video_height as i32;
    let info = ImageInfo::new(
        (width, height),
        ColorType::RGBA8888,
        skia_safe::AlphaType::Premul,
        None,
    );
    let mut temp_surface = surfaces::raster(&info, None, None)
        .ok_or(RustmotionError::MotionBlurSurface)?;

    temp_surface
        .canvas()
        .clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));

    let component = &child.component;

    for i in 0..num_samples {
        let t = if num_samples > 1 {
            (i as f64 / (num_samples - 1) as f64 - 0.5) * spread
        } else {
            0.0
        };
        let sample_time = (ctx.time + t).max(0.0);

        let anim_time = if let Some(timed) = component.as_timed() {
            let (start_at, _) = timed.timing();
            if let Some(start) = start_at {
                sample_time - start
            } else {
                sample_time
            }
        } else {
            sample_time
        };

        let mut props = if let Some(animatable) = component.as_animatable() {
            let effects = animatable.animation_effects();
            if !effects.is_empty() {
                let extracted = extract_effects(effects);
                let mut p = AnimatedProperties::default();
                for (preset, preset_config) in &extracted.presets {
                    let pp = resolve_animations(&[], Some(preset), Some(preset_config), anim_time, ctx.scene_duration);
                    p.merge(&pp);
                }
                if !extracted.keyframes.is_empty() {
                    let kf: Vec<_> = extracted.keyframes.into_iter().cloned().collect();
                    let kp = resolve_animations(&kf, None, None, anim_time, ctx.scene_duration);
                    p.merge(&kp);
                }
                if !extracted.wiggles.is_empty() {
                    let wiggles: Vec<_> = extracted.wiggles.into_iter().cloned().collect();
                    apply_wiggles(&mut p, &wiggles, sample_time);
                }
                if !extracted.orbits.is_empty() {
                    let orbits: Vec<_> = extracted.orbits.into_iter().cloned().collect();
                    apply_orbits(&mut p, &orbits, sample_time);
                }
                if extracted.char_animation.is_some() {
                    p.char_animation = extracted.char_animation;
                }
                p
            } else {
                AnimatedProperties::default()
            }
        } else {
            AnimatedProperties::default()
        };

        props.opacity /= num_samples as f32;
        render_component_inner(temp_surface.canvas(), child, layout, ctx, &props)?;
    }

    let image = temp_surface.image_snapshot();
    canvas.draw_image(&image, (0.0, 0.0), None);

    Ok(())
}

/// Render a list of ChildComponents at their layout positions.
/// This is used by containers to render their children with animation support.
pub fn render_children(
    canvas: &Canvas,
    children: &[ChildComponent],
    layout: &LayoutNode,
    ctx: &RenderContext,
) -> Result<()> {
    render_children_with_stagger(canvas, children, layout, ctx, None)
}

pub fn render_children_with_stagger(
    canvas: &Canvas,
    children: &[ChildComponent],
    layout: &LayoutNode,
    ctx: &RenderContext,
    stagger: Option<f32>,
) -> Result<()> {
    for (i, child) in children.iter().enumerate() {
        if i >= layout.children.len() {
            break;
        }
        let child_layout = &layout.children[i];

        let child_ctx = if let Some(stagger_val) = stagger {
            let mut c = ctx.clone();
            c.stagger_offset = ctx.stagger_offset + i as f64 * stagger_val as f64;
            c
        } else {
            ctx.clone()
        };

        canvas.save();
        canvas.translate((child_layout.x, child_layout.y));
        render_component(canvas, child, child_layout, &child_ctx)?;

        // Render overlays positioned relative to this component's bounds
        if !child.overlays.is_empty() {
            render_overlays(canvas, &child.overlays, child_layout, &child_ctx)?;
        }

        canvas.restore();
    }
    Ok(())
}

/// Render overlay components positioned relative to a parent's bounds.
fn render_overlays(
    canvas: &Canvas,
    overlays: &[Overlay],
    parent_layout: &LayoutNode,
    ctx: &RenderContext,
) -> Result<()> {
    let pw = parent_layout.width;
    let ph = parent_layout.height;

    for overlay in overlays {
        // Measure the overlay component
        let widget = overlay.component.as_widget();
        let constraints = Constraints { min_width: 0.0, max_width: pw, min_height: 0.0, max_height: ph };
        let (ow, oh) = widget.measure(&constraints);

        // Compute position based on anchor
        let (ax, ay) = match overlay.anchor {
            OverlayAnchor::TopRight => (pw - ow / 2.0, -oh / 2.0),
            OverlayAnchor::TopLeft => (-ow / 2.0, -oh / 2.0),
            OverlayAnchor::BottomRight => (pw - ow / 2.0, ph - oh / 2.0),
            OverlayAnchor::BottomLeft => (-ow / 2.0, ph - oh / 2.0),
            OverlayAnchor::Center => ((pw - ow) / 2.0, (ph - oh) / 2.0),
        };

        let x = ax + overlay.offset_x;
        let y = ay + overlay.offset_y;

        let overlay_layout = LayoutNode::new(0.0, 0.0, ow, oh);

        // Resolve animations for the overlay component
        let props = if let Some(animatable) = overlay.component.as_animatable() {
            let effects = animatable.animation_effects();
            if !effects.is_empty() {
                let extracted = extract_effects(effects);
                let mut props = AnimatedProperties::default();
                for (preset, preset_config) in &extracted.presets {
                    let p = resolve_animations(&[], Some(preset), Some(preset_config), ctx.time, ctx.scene_duration);
                    props.merge(&p);
                }
                if !extracted.keyframes.is_empty() {
                    let kf_animations: Vec<_> = extracted.keyframes.into_iter().cloned().collect();
                    let kf_props = resolve_animations(&kf_animations, None, None, ctx.time, ctx.scene_duration);
                    props.merge(&kf_props);
                }
                if extracted.char_animation.is_some() {
                    props.char_animation = extracted.char_animation;
                }
                props
            } else {
                AnimatedProperties::default()
            }
        } else {
            AnimatedProperties::default()
        };

        if props.opacity <= 0.0 { continue; }

        canvas.save();
        canvas.translate((x, y));

        // Apply opacity
        let needs_layer = props.opacity < 1.0;
        if needs_layer {
            let mut layer_paint = Paint::default();
            layer_paint.set_alpha_f(props.opacity);
            canvas.save_layer(&skia_safe::canvas::SaveLayerRec::default().paint(&layer_paint));
        }

        widget.render(canvas, &overlay_layout, ctx, &props)?;

        if needs_layer { canvas.restore(); }
        canvas.restore();
    }

    Ok(())
}

/// Render a complete frame using the v2 component pipeline.
///
/// This function has the same signature as `render_frame` for easy integration
/// with the existing encoding pipeline.
pub fn render_frame_v2(
    config: &VideoConfig,
    scene: &Scene,
    frame_index: u32,
    _total_frames: u32,
    root_children: &[ChildComponent],
    root_layout: &LayoutNode,
) -> Result<Vec<u8>> {
    render_frame_v2_scaled(config, scene, frame_index, _total_frames, root_children, root_layout, 1.0)
}

/// Render a frame with an optional scale factor for higher-resolution output.
/// The layout is computed at video dimensions; the surface and rendering are
/// scaled up so text and vector graphics remain sharp.
pub fn render_frame_v2_scaled(
    config: &VideoConfig,
    scene: &Scene,
    frame_index: u32,
    _total_frames: u32,
    root_children: &[ChildComponent],
    root_layout: &LayoutNode,
    scale_factor: f32,
) -> Result<Vec<u8>> {
    let scaled_w = (config.width as f32 * scale_factor) as i32;
    let scaled_h = (config.height as f32 * scale_factor) as i32;
    let mut time = frame_index as f64 / config.fps as f64;

    // Apply freeze_at
    if let Some(freeze_at) = scene.freeze_at {
        if time > freeze_at {
            time = freeze_at;
        }
    }

    let info = ImageInfo::new(
        (scaled_w, scaled_h),
        ColorType::RGBA8888,
        skia_safe::AlphaType::Premul,
        None,
    );

    let mut surface = surfaces::raster(&info, None, None)
        .ok_or(RustmotionError::SurfaceCreation)?;

    let canvas = surface.canvas();

    // Apply scale factor before rendering
    if scale_factor != 1.0 {
        canvas.scale((scale_factor, scale_factor));
    }

    // Fill background
    let bg = scene.background.as_deref().unwrap_or(&config.background);
    canvas.clear(color4f_from_hex(bg));

    // Animated background gradient(s)
    for anim_bg in &scene.animated_background {
        draw_animated_background(canvas, anim_bg, time as f32, config.width as f32, config.height as f32);
    }

    // Build render context
    let ctx = RenderContext {
        time,
        scene_duration: scene.duration,
        frame_index,
        fps: config.fps,
        video_width: config.width,
        video_height: config.height,
        stagger_offset: 0.0,
    };

    // Apply virtual camera transform
    let has_camera = scene.camera.is_some();
    if let Some(ref camera) = scene.camera {
        apply_camera_transform(canvas, camera, time as f32, config.width as f32, config.height as f32);
    }

    // Render component tree
    render_children(canvas, root_children, root_layout, &ctx)?;

    // Restore camera transform
    if has_camera {
        canvas.restore();
    }

    // Read pixels at scaled dimensions
    let row_bytes = scaled_w as usize * 4;
    let mut pixels = vec![0u8; row_bytes * scaled_h as usize];
    let dst_info = ImageInfo::new(
        (scaled_w, scaled_h),
        ColorType::RGBA8888,
        skia_safe::AlphaType::Premul,
        None,
    );
    surface
        .read_pixels(&dst_info, &mut pixels, row_bytes, (0, 0))
        .then_some(())
        .ok_or(RustmotionError::PixelRead)?;

    Ok(pixels)
}

/// Adapter that wraps root-level children as a flex container.
/// This lets us reuse `layout_flex` for scene-level layout.
struct RootContainer<'a> {
    children: &'a [ChildComponent],
    style: LayerStyle,
}

impl<'a> Container for RootContainer<'a> {
    fn children(&self) -> &[ChildComponent] {
        self.children
    }
}

impl<'a> Styled for RootContainer<'a> {
    fn style_config(&self) -> &LayerStyle {
        &self.style
    }
}

impl<'a> RootContainer<'a> {
    fn new(children: &'a [ChildComponent], scene_layout: Option<&SceneLayout>) -> Self {
        let mut style = LayerStyle::default();
        if let Some(layout) = scene_layout {
            style.flex_direction = layout.direction.clone();
            style.gap = layout.gap;
            style.align_items = layout.align_items.clone();
            style.justify_content = layout.justify_content.clone();
            if let Some(p) = layout.padding {
                style.padding = Some(crate::schema::Spacing::Uniform(p));
            }
        }
        // Default direction is column (like a web page)
        if style.flex_direction.is_none() {
            style.flex_direction = Some(crate::schema::CardDirection::Column);
        }
        Self { children, style }
    }
}

/// Compute the layout tree for a set of root-level ChildComponents.
/// Uses an implicit flex container at video dimensions (like a full-screen web page).
/// All root children participate in flex flow (position is stripped before layout).
/// Only children inside `Positioned` containers use absolute coordinates.
pub fn compute_root_layout(
    children: &[ChildComponent],
    config: &VideoConfig,
    scene_layout: Option<&SceneLayout>,
) -> LayoutNode {
    let constraints = Constraints::tight(config.width as f32, config.height as f32);
    let root = RootContainer::new(children, scene_layout);
    crate::layout::flex::layout_flex(&root, &constraints)
}

/// Like `compute_root_layout`, but forces all children into flex flow
/// (ignores absolute positions). Used for world scenes where content
/// should be centered regardless of position attributes in the JSON.
pub fn compute_root_layout_all_flow(
    children: &[ChildComponent],
    config: &VideoConfig,
    scene_layout: Option<&SceneLayout>,
) -> LayoutNode {
    let constraints = Constraints::tight(config.width as f32, config.height as f32);
    let root = RootContainer::new(children, scene_layout);
    crate::layout::flex::layout_flex_all_flow(&root, &constraints)
}

/// Compute layout for a scene's children — ready for render_frame_v2.
pub fn prepare_scene<'a>(
    scene: &'a Scene,
    config: &VideoConfig,
) -> (&'a [ChildComponent], LayoutNode) {
    let layout = compute_root_layout(&scene.children, config, scene.layout.as_ref());
    (&scene.children, layout)
}

/// Render a single frame using the v2 pipeline.
/// This is the unified entry point for both single-frame and video encoding.
pub fn render_scene_frame(
    config: &VideoConfig,
    scene: &Scene,
    frame_in_scene: u32,
    scene_total_frames: u32,
) -> Result<Vec<u8>> {
    let (children, layout) = prepare_scene(scene, config);
    render_frame_v2(config, scene, frame_in_scene, scene_total_frames, children, &layout)
}

pub fn render_scene_frame_scaled(
    config: &VideoConfig,
    scene: &Scene,
    frame_in_scene: u32,
    scene_total_frames: u32,
    scale_factor: f32,
) -> Result<Vec<u8>> {
    let (children, layout) = prepare_scene(scene, config);
    render_frame_v2_scaled(config, scene, frame_in_scene, scene_total_frames, children, &layout, scale_factor)
}

/// Render a single frame of a world view: shared background, camera translation, visible scenes.
pub fn render_world_frame_scaled(
    config: &VideoConfig,
    view: &crate::schema::ResolvedView,
    timeline: &crate::engine::world::WorldTimeline,
    frame_in_view: u32,
    scale_factor: f32,
) -> Result<Vec<u8>> {
    let scaled_w = (config.width as f32 * scale_factor) as i32;
    let scaled_h = (config.height as f32 * scale_factor) as i32;
    let fps = config.fps;
    let time = frame_in_view as f64 / fps as f64;

    let info = ImageInfo::new(
        (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
    );
    let mut surface = surfaces::raster(&info, None, None)
        .ok_or(RustmotionError::SurfaceCreation)?;
    let canvas = surface.canvas();

    if scale_factor != 1.0 {
        canvas.scale((scale_factor, scale_factor));
    }

    let vw = config.width as f32;
    let vh = config.height as f32;

    // 1. Draw base background (view-level or video-level)
    let bg_color = view.background.as_deref().unwrap_or(&config.background);
    canvas.clear(color4f_from_hex(bg_color));

    // Pre-compute camera position for background parallax
    let (cam_x, cam_y) = timeline.camera_at(time, &view.camera_easing);
    let viewport_cx = vw / 2.0;
    let viewport_cy = vh / 2.0;

    // 2. Draw animated backgrounds
    // Determine which scene's backgrounds to use (crossfade during pan)
    let visible = timeline.visible_scenes_at(time, &view.scenes, fps);

    // Find the "active" scene (the one currently being displayed, latest in sequence)
    // and optionally a "previous" scene during camera pan for crossfade
    let active_scene_idx = visible.iter()
        .filter(|v| !v.is_persisted)
        .map(|v| v.scene_idx)
        .max();

    if let Some(active_idx) = active_scene_idx {
        let active_scene = &view.scenes[active_idx];
        let active_bgs = if active_scene.animated_background.is_empty() {
            &view.animated_background
        } else {
            &active_scene.animated_background
        };

        // Check if we're during a camera pan (two non-persisted scenes visible)
        let non_persisted: Vec<_> = visible.iter()
            .filter(|v| !v.is_persisted)
            .collect();

        if non_persisted.len() >= 2 {
            // Crossfade between outgoing and incoming scene backgrounds
            let scene_a_idx = non_persisted[0].scene_idx;
            let scene_b_idx = non_persisted[1].scene_idx;
            let scene_a = &view.scenes[scene_a_idx];
            let scene_b = &view.scenes[scene_b_idx];

            let bgs_a = if scene_a.animated_background.is_empty() {
                &view.animated_background
            } else {
                &scene_a.animated_background
            };
            let bgs_b = if scene_b.animated_background.is_empty() {
                &view.animated_background
            } else {
                &scene_b.animated_background
            };

            // Calculate crossfade progress based on camera pan position
            let pan_half = timeline.camera_pan_duration / 2.0;
            let (_, _scene_b_start) = timeline.scene_windows[scene_b_idx];
            let pan_start = timeline.scene_windows[scene_b_idx].0 - pan_half;
            let pan_end = timeline.scene_windows[scene_b_idx].0 + pan_half;
            let crossfade = if pan_end > pan_start {
                ((time - pan_start) / (pan_end - pan_start)).clamp(0.0, 1.0) as f32
            } else {
                1.0
            };

            // Draw scene A backgrounds with fading alpha
            if crossfade < 1.0 {
                for bg in bgs_a {
                    draw_world_bg_with_parallax(canvas, bg, time as f32, vw, vh, cam_x, cam_y);
                }
            }
            // Draw scene B backgrounds with growing alpha
            if crossfade > 0.0 && bgs_a as *const _ != bgs_b as *const _ {
                // If backgrounds are different, create a temporary surface for B and blend
                let bg_info = ImageInfo::new(
                    (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
                );
                if let Some(mut bg_surface) = surfaces::raster(&bg_info, None, None) {
                    let bg_canvas = bg_surface.canvas();
                    if scale_factor != 1.0 { bg_canvas.scale((scale_factor, scale_factor)); }
                    bg_canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));
                    for bg in bgs_b {
                        draw_world_bg_with_parallax(bg_canvas, bg, time as f32, vw, vh, cam_x, cam_y);
                    }
                    let snapshot = bg_surface.image_snapshot();
                    let mut paint = skia_safe::Paint::default();
                    paint.set_alpha_f(crossfade);
                    canvas.save();
                    if scale_factor != 1.0 { canvas.reset_matrix(); }
                    canvas.draw_image(&snapshot, (0.0, 0.0), Some(&paint));
                    canvas.restore();
                    if scale_factor != 1.0 { canvas.scale((scale_factor, scale_factor)); }
                }
            }
        } else {
            // Single active scene — just draw its backgrounds
            for bg in active_bgs {
                draw_world_bg_with_parallax(canvas, bg, time as f32, vw, vh, cam_x, cam_y);
            }
        }
    } else {
        // No active scene — draw view-level backgrounds
        for bg in &view.animated_background {
            draw_world_bg_with_parallax(canvas, bg, time as f32, vw, vh, cam_x, cam_y);
        }
    }

    // 3. Apply world camera transform
    canvas.save();
    canvas.translate((viewport_cx - cam_x, viewport_cy - cam_y));

    // 4. Render each visible scene at its world position
    for vis in &visible {
        let scene = &view.scenes[vis.scene_idx];
        // Use world-position if specified, otherwise fall back to horizontal grid
        let (wx, wy) = scene.world_position.as_ref()
            .map(|p| (p.x, p.y))
            .unwrap_or((vw / 2.0 + vis.scene_idx as f32 * vw, vh / 2.0));

        // Apply crossfade opacity via save_layer during camera pans
        let needs_opacity = vis.opacity < 1.0 - f32::EPSILON;
        if needs_opacity {
            let mut layer_paint = Paint::default();
            layer_paint.set_alpha_f(vis.opacity);
            canvas.save_layer_alpha_f(None, vis.opacity);
        }

        canvas.save();
        // Translate to scene's world position, offset so scene center = world position
        canvas.translate((wx - viewport_cx, wy - viewport_cy));

        // Use local_time for animations (clamped to 0 if pan hasn't finished)
        let anim_time = vis.local_time.max(0.0);
        let ctx = RenderContext {
            time: anim_time,
            scene_duration: scene.duration,
            frame_index: vis.local_frame,
            fps,
            video_width: config.width,
            video_height: config.height,
            stagger_offset: 0.0,
        };

        // Apply per-scene camera if present
        let has_camera = scene.camera.is_some();
        if let Some(ref camera) = scene.camera {
            apply_camera_transform(canvas, camera, anim_time as f32, vw, vh);
        }

        // World scenes: force content children into centered flex flow.
        // Decorative children (particles) are excluded from flex and rendered fullscreen.
        let world_default_layout = crate::schema::SceneLayout {
            direction: Some(crate::schema::CardDirection::Column),
            gap: Some(12.0),
            align_items: Some(crate::schema::CardAlign::Center),
            justify_content: Some(crate::schema::CardJustify::Center),
            padding: None,
        };
        let scene_layout = scene.layout.as_ref().unwrap_or(&world_default_layout);
        let layout = compute_root_layout_all_flow(&scene.children, config, Some(scene_layout));

        // Render: decorative children get fullscreen layout, content children get flex layout
        for (i, child) in scene.children.iter().enumerate() {
            if i >= layout.children.len() { break; }
            if child.is_decorative() {
                // Render particles fullscreen
                let deco_layout = LayoutNode::new(0.0, 0.0, vw, vh);
                canvas.save();
                render_component(canvas, child, &deco_layout, &ctx)?;
                canvas.restore();
            } else {
                let child_layout = &layout.children[i];
                canvas.save();
                canvas.translate((child_layout.x, child_layout.y));
                render_component(canvas, child, child_layout, &ctx)?;
                if !child.overlays.is_empty() {
                    render_overlays(canvas, &child.overlays, child_layout, &ctx)?;
                }
                canvas.restore();
            }
        }

        if has_camera {
            canvas.restore();
        }

        canvas.restore();

        // Restore opacity layer
        if needs_opacity {
            canvas.restore();
        }
    }

    // Restore world transform
    canvas.restore();

    // 5. Read pixels
    let row_bytes = scaled_w as usize * 4;
    let mut pixels = vec![0u8; row_bytes * scaled_h as usize];
    let dst_info = ImageInfo::new(
        (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
    );
    surface.read_pixels(&dst_info, &mut pixels, row_bytes, (0, 0))
        .then_some(()).ok_or(RustmotionError::PixelRead)?;

    Ok(pixels)
}

/// Render only the background (solid color + animated-background) of a scene.
pub fn render_scene_bg_scaled(
    config: &VideoConfig,
    scene: &Scene,
    frame_in_scene: u32,
    scale_factor: f32,
) -> Result<Vec<u8>> {
    let scaled_w = (config.width as f32 * scale_factor) as i32;
    let scaled_h = (config.height as f32 * scale_factor) as i32;
    let mut time = frame_in_scene as f64 / config.fps as f64;
    if let Some(freeze_at) = scene.freeze_at {
        if time > freeze_at { time = freeze_at; }
    }
    let info = ImageInfo::new(
        (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
    );
    let mut surface = surfaces::raster(&info, None, None)
        .ok_or(RustmotionError::SurfaceCreation)?;
    let canvas = surface.canvas();
    if scale_factor != 1.0 { canvas.scale((scale_factor, scale_factor)); }

    let bg = scene.background.as_deref().unwrap_or(&config.background);
    canvas.clear(color4f_from_hex(bg));
    for anim_bg in &scene.animated_background {
        draw_animated_background(canvas, anim_bg, time as f32, config.width as f32, config.height as f32);
    }

    let row_bytes = scaled_w as usize * 4;
    let mut pixels = vec![0u8; row_bytes * scaled_h as usize];
    let dst_info = ImageInfo::new(
        (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
    );
    surface.read_pixels(&dst_info, &mut pixels, row_bytes, (0, 0))
        .then_some(()).ok_or(RustmotionError::PixelRead)?;
    Ok(pixels)
}

/// Render only the children (foreground) of a scene on a transparent canvas.
pub fn render_scene_fg_scaled(
    config: &VideoConfig,
    scene: &Scene,
    frame_in_scene: u32,
    _scene_total_frames: u32,
    scale_factor: f32,
) -> Result<Vec<u8>> {
    let (children, layout) = prepare_scene(scene, config);
    let scaled_w = (config.width as f32 * scale_factor) as i32;
    let scaled_h = (config.height as f32 * scale_factor) as i32;
    let mut time = frame_in_scene as f64 / config.fps as f64;
    if let Some(freeze_at) = scene.freeze_at {
        if time > freeze_at { time = freeze_at; }
    }
    let info = ImageInfo::new(
        (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
    );
    let mut surface = surfaces::raster(&info, None, None)
        .ok_or(RustmotionError::SurfaceCreation)?;
    let canvas = surface.canvas();
    if scale_factor != 1.0 { canvas.scale((scale_factor, scale_factor)); }

    // Transparent background
    canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));

    let ctx = RenderContext {
        time,
        scene_duration: scene.duration,
        frame_index: frame_in_scene,
        fps: config.fps,
        video_width: config.width,
        video_height: config.height,
        stagger_offset: 0.0,
    };

    let has_camera = scene.camera.is_some();
    if let Some(ref camera) = scene.camera {
        apply_camera_transform(canvas, camera, time as f32, config.width as f32, config.height as f32);
    }
    render_children(canvas, children, &layout, &ctx)?;
    if has_camera { canvas.restore(); }

    let row_bytes = scaled_w as usize * 4;
    let mut pixels = vec![0u8; row_bytes * scaled_h as usize];
    let dst_info = ImageInfo::new(
        (scaled_w, scaled_h), ColorType::RGBA8888, skia_safe::AlphaType::Premul, None,
    );
    surface.read_pixels(&dst_info, &mut pixels, row_bytes, (0, 0))
        .then_some(()).ok_or(RustmotionError::PixelRead)?;
    Ok(pixels)
}

/// Draw an animated background (gradient, concentric circles, or grid dots).
fn draw_animated_background(
    canvas: &Canvas,
    bg: &AnimatedBackground,
    time: f32,
    width: f32,
    height: f32,
) {
    match bg.preset.as_deref() {
        Some("concentric_circles") => draw_bg_concentric_circles(canvas, bg, time, width, height),
        Some("grid_dots") => draw_bg_grid_dots(canvas, bg, time, width, height),
        Some("halo") => draw_bg_halo(canvas, bg, time, width, height),
        _ => draw_bg_gradient_shift(canvas, bg, time, width, height),
    }
}

/// Draw animated background with camera parallax for world views.
/// Offsets the grid pattern by (cam_x, cam_y) modulo spacing so that
/// the texture scrolls as the camera pans.
fn draw_world_bg_with_parallax(
    canvas: &Canvas,
    bg: &AnimatedBackground,
    time: f32,
    width: f32,
    height: f32,
    cam_x: f32,
    cam_y: f32,
) {
    match bg.preset.as_deref() {
        Some("halo") => {
            // Halo zones use normalized coordinates (0-1) relative to the drawn area.
            // To make them scroll with the world, we translate by the full camera offset
            // and draw on a surface large enough to cover the entire world extent.
            // We use a large virtual surface so zones are spread across the world.
            let world_w = width * 5.0;
            let world_h = height * 5.0;
            canvas.save();
            canvas.translate((-cam_x, -cam_y));
            draw_bg_halo(canvas, bg, time, world_w, world_h);
            canvas.restore();
        }
        _ => {
            // Grid-based backgrounds (grid_dots, concentric_circles, etc.)
            // use modulo offset for seamless tiling.
            let spacing = bg.spacing.max(20.0);
            let offset_x = -(cam_x % spacing);
            let offset_y = -(cam_y % spacing);
            canvas.save();
            canvas.translate((offset_x, offset_y));
            draw_animated_background(canvas, bg, time, width + spacing * 2.0, height + spacing * 2.0);
            canvas.restore();
        }
    }
}

fn draw_bg_gradient_shift(
    canvas: &Canvas,
    bg: &AnimatedBackground,
    time: f32,
    width: f32,
    height: f32,
) {
    use skia_safe::{gradient_shader::GradientShaderColors, Point};

    if bg.colors.len() < 2 {
        return;
    }

    let base_colors: Vec<skia_safe::Color4f> = bg.colors.iter().map(|c| color4f_from_hex(c)).collect();
    let angle = (bg.speed * time) % 360.0;
    let rad = angle.to_radians();

    // Interpolate in linear color space to reduce banding on dark gradients
    let linear_cs = skia_safe::ColorSpace::new_srgb_linear();

    // Subdivide color stops (16 intermediate steps between each pair) for smoother gradients
    let (colors, positions) = subdivide_gradient_stops(&base_colors, 16);

    let shader = match bg.gradient_type {
        GradientType::Linear => {
            let cx = width / 2.0;
            let cy = height / 2.0;
            let half_diag = (width.powi(2) + height.powi(2)).sqrt() / 2.0;
            let start = Point::new(cx - rad.cos() * half_diag, cy - rad.sin() * half_diag);
            let end = Point::new(cx + rad.cos() * half_diag, cy + rad.sin() * half_diag);
            skia_safe::shader::Shader::linear_gradient(
                (start, end),
                GradientShaderColors::ColorsInSpace(&colors, Some(linear_cs)),
                Some(&positions[..]),
                skia_safe::TileMode::Clamp,
                None,
                None,
            )
        }
        GradientType::Radial => {
            let center = Point::new(width / 2.0, height / 2.0);
            let radius = width.max(height) / 2.0;
            skia_safe::shader::Shader::radial_gradient(
                center,
                radius,
                GradientShaderColors::ColorsInSpace(&colors, Some(linear_cs)),
                Some(&positions[..]),
                skia_safe::TileMode::Clamp,
                None,
                None,
            )
        }
    };

    if let Some(shader) = shader {
        let mut paint = Paint::default();
        paint.set_shader(shader);
        paint.set_dither(true);
        canvas.draw_rect(skia_safe::Rect::from_wh(width, height), &paint);
    }
}

/// Subdivide gradient color stops by inserting intermediate interpolated colors.
/// Returns (colors, positions) with `subdivisions` extra stops between each original pair.
fn subdivide_gradient_stops(
    colors: &[skia_safe::Color4f],
    subdivisions: u32,
) -> (Vec<skia_safe::Color4f>, Vec<f32>) {
    let n = colors.len();
    if n < 2 {
        return (colors.to_vec(), vec![0.0]);
    }
    let total = (n - 1) * subdivisions as usize + n;
    let mut out_colors = Vec::with_capacity(total);
    let mut out_pos = Vec::with_capacity(total);
    let seg = (n - 1) as f32;

    for i in 0..n - 1 {
        let c0 = &colors[i];
        let c1 = &colors[i + 1];
        let steps = subdivisions + 1;
        for s in 0..steps {
            let t = s as f32 / steps as f32;
            let global_t = (i as f32 + t) / seg;
            out_colors.push(skia_safe::Color4f {
                r: c0.r + (c1.r - c0.r) * t,
                g: c0.g + (c1.g - c0.g) * t,
                b: c0.b + (c1.b - c0.b) * t,
                a: c0.a + (c1.a - c0.a) * t,
            });
            out_pos.push(global_t);
        }
    }
    // Last color
    out_colors.push(colors[n - 1]);
    out_pos.push(1.0);

    (out_colors, out_pos)
}

/// Soft colored glow zones (halo preset).
fn draw_bg_halo(
    canvas: &Canvas,
    bg: &AnimatedBackground,
    time: f32,
    width: f32,
    height: f32,
) {
    for (i, zone) in bg.zones.iter().enumerate() {
        let cx = zone.x * width;
        let cy = zone.y * height;
        let base_radius = zone.radius * width.max(height);
        // Each particle gets a unique phase and slightly different frequency
        let phase = (zone.x * 17.3 + zone.y * 31.7 + i as f32 * 0.73).fract() * std::f32::consts::TAU;
        let freq = bg.speed * (0.7 + (zone.x * 13.1 + zone.y * 7.9).fract() * 0.6);
        let breath = 1.0 + 0.15 * (time * freq + phase).sin();
        let radius = base_radius * breath;

        let color = color4f_from_hex(&zone.color);
        let mut paint = Paint::default();
        paint.set_anti_alias(true);
        paint.set_color4f(color, None);
        paint.set_mask_filter(skia_safe::MaskFilter::blur(
            skia_safe::BlurStyle::Normal,
            radius * 0.15,
            false,
        ));
        canvas.draw_circle((cx, cy), radius, &paint);
    }
}

/// Expanding concentric circles from center.
fn draw_bg_concentric_circles(
    canvas: &Canvas,
    bg: &AnimatedBackground,
    time: f32,
    width: f32,
    height: f32,
) {
    use skia_safe::PaintStyle;

    let color_str = bg.colors.first().map(|s| s.as_str()).unwrap_or("#FFFFFF20");
    let mut paint = super::renderer::paint_from_hex(color_str);
    paint.set_style(PaintStyle::Stroke);
    paint.set_stroke_width(bg.element_size);
    paint.set_anti_alias(true);

    let cx = width / 2.0;
    let cy = height / 2.0;
    let max_radius = (width.powi(2) + height.powi(2)).sqrt() / 2.0;
    let spacing = if let Some(count) = bg.count {
        if count > 0 {
            max_radius / count as f32
        } else {
            bg.spacing.max(20.0)
        }
    } else {
        bg.spacing.max(20.0)
    };
    let offset = (time * bg.speed) % spacing;

    let mut r = offset;
    while r < max_radius {
        // Fade out as circles expand
        let alpha = 1.0 - (r / max_radius).clamp(0.0, 1.0);
        paint.set_alpha_f(alpha * 0.3);
        canvas.draw_circle((cx, cy), r, &paint);
        r += spacing;
    }
}

/// Animated dot grid pattern.
fn draw_bg_grid_dots(
    canvas: &Canvas,
    bg: &AnimatedBackground,
    time: f32,
    width: f32,
    height: f32,
) {
    let color_str = bg.colors.first().map(|s| s.as_str()).unwrap_or("#FFFFFF15");
    let mut paint = super::renderer::paint_from_hex(color_str);
    paint.set_anti_alias(true);

    let spacing = bg.spacing.max(20.0);
    let dot_radius = bg.element_size / 2.0;
    let offset_y = (time * bg.speed * 0.5) % spacing;

    let mut y = -spacing + offset_y;
    while y < height + spacing {
        let mut x = 0.0_f32;
        while x < width + spacing {
            // Pulse: subtle size variation based on position + time
            let phase = (x * 0.01 + y * 0.01 + time * 2.0).sin() * 0.3 + 0.7;
            let r = dot_radius * phase;
            paint.set_alpha_f(phase * 0.4);
            canvas.draw_circle((x, y), r, &paint);
            x += spacing;
        }
        y += spacing;
    }
}

/// Draw an inner (inset) shadow inside an element.
///
/// Technique: clip to the element bounds, then draw a large inverted rect
/// with blur that bleeds inward from the edges.
fn draw_inner_shadow(
    canvas: &Canvas,
    width: f32,
    height: f32,
    corner_radius: f32,
    shadow: &InnerShadow,
) {
    use skia_safe::{PaintStyle, Path, Rect, RRect, ClipOp};

    let bounds = Rect::from_xywh(0.0, 0.0, width, height);
    let rrect = RRect::new_rect_xy(bounds, corner_radius, corner_radius);

    canvas.save();
    // Clip to element bounds
    canvas.clip_rrect(rrect, ClipOp::Intersect, true);

    // Draw a large rect around the element with a hole in the center,
    // offset by the shadow offset, with blur applied.
    let expand = shadow.blur * 3.0 + 50.0;
    let outer = Rect::from_xywh(
        -expand + shadow.offset_x,
        -expand + shadow.offset_y,
        width + expand * 2.0,
        height + expand * 2.0,
    );
    let inner = Rect::from_xywh(shadow.offset_x, shadow.offset_y, width, height);
    let inner_rrect = RRect::new_rect_xy(inner, corner_radius, corner_radius);

    let mut path = Path::new();
    path.add_rect(outer, None);
    path.add_rrect(inner_rrect, None);
    path.set_fill_type(skia_safe::PathFillType::EvenOdd);

    let mut paint = super::renderer::paint_from_hex(&shadow.color);
    paint.set_style(PaintStyle::Fill);
    if shadow.blur > 0.0 {
        paint.set_mask_filter(skia_safe::MaskFilter::blur(
            skia_safe::BlurStyle::Normal,
            shadow.blur / 2.0,
            false,
        ));
    }

    canvas.draw_path(&path, &paint);
    canvas.restore();
}

/// Draw a 3D-adaptive ground-plane shadow.
///
/// When a component has 3D rotation (rotate_x/rotate_y), the shadow is drawn
/// on the "ground plane" (before 3D transforms) with offset/blur adapted to
/// the tilt angle. This creates a realistic shadow that grows and shifts
/// as the element tilts away from the viewer.
fn draw_3d_shadow(
    canvas: &Canvas,
    width: f32,
    height: f32,
    corner_radius: f32,
    shadow: &crate::schema::CardShadow,
    props: &AnimatedProperties,
) {
    use skia_safe::{Rect, RRect};

    let rx = props.rotate_x * std::f32::consts::PI / 180.0;
    let ry = props.rotate_y * std::f32::consts::PI / 180.0;

    // Shadow offset shifts based on rotation angle (light from top-left)
    let depth_factor = (height * 0.4).min(80.0);
    let extra_offset_x = shadow.offset_x - ry.sin() * depth_factor;
    let extra_offset_y = shadow.offset_y + rx.sin() * depth_factor;

    // Shadow blur increases with tilt angle
    let tilt_magnitude = (rx.abs() + ry.abs()).min(1.0);
    let extra_blur = shadow.blur + tilt_magnitude * 20.0;

    // Shadow scales slightly based on perspective foreshortening
    let scale_x = 1.0 + ry.sin().abs() * 0.15;
    let scale_y = 1.0 + rx.sin().abs() * 0.15;
    let sw = width * scale_x;
    let sh = height * scale_y;
    let sx = extra_offset_x - (sw - width) / 2.0;
    let sy = extra_offset_y - (sh - height) / 2.0;

    let shadow_rect = Rect::from_xywh(sx, sy, sw, sh);
    let shadow_rrect = RRect::new_rect_xy(shadow_rect, corner_radius, corner_radius);

    let mut shadow_paint = super::renderer::paint_from_hex(&shadow.color);
    if extra_blur > 0.0 {
        shadow_paint.set_mask_filter(skia_safe::MaskFilter::blur(
            skia_safe::BlurStyle::Normal,
            extra_blur / 2.0,
            false,
        ));
    }
    canvas.draw_rrect(shadow_rrect, &shadow_paint);
}

/// Interpolate a camera property at a given time using its keyframes.
fn interpolate_camera_property(camera: &Camera, property: &str, time: f32) -> f32 {
    use super::animator::ease;

    // Find keyframe track for this property
    let track = camera.keyframes.iter().find(|k| k.property == property);
    let track = match track {
        Some(t) if !t.values.is_empty() => t,
        _ => {
            // Return static value
            return match property {
                "x" => camera.x,
                "y" => camera.y,
                "zoom" => camera.zoom,
                "rotation" => camera.rotation,
                _ => 0.0,
            };
        }
    };

    let points = &track.values;
    let t = time as f64;

    // Before first keyframe
    if t <= points[0].time {
        return points[0].value;
    }

    // After last keyframe
    if t >= points[points.len() - 1].time {
        return points[points.len() - 1].value;
    }

    // Find segment
    for i in 0..points.len() - 1 {
        let p0 = &points[i];
        let p1 = &points[i + 1];
        if t >= p0.time && t <= p1.time {
            let segment_t = if (p1.time - p0.time).abs() < 1e-9 {
                1.0
            } else {
                (t - p0.time) / (p1.time - p0.time)
            };
            let eased = ease(segment_t, &track.easing) as f32;
            return p0.value + (p1.value - p0.value) * eased;
        }
    }

    points[points.len() - 1].value
}

/// Apply camera transform to the canvas: translate, zoom, rotate around scene center.
fn apply_camera_transform(canvas: &Canvas, camera: &Camera, time: f32, width: f32, height: f32) {
    let x = interpolate_camera_property(camera, "x", time);
    let y = interpolate_camera_property(camera, "y", time);
    let zoom = interpolate_camera_property(camera, "zoom", time);
    let rotation = interpolate_camera_property(camera, "rotation", time);

    let cx = width / 2.0;
    let cy = height / 2.0;

    canvas.save();

    // 1. Translate to center
    canvas.translate((cx, cy));
    // 2. Apply rotation
    if rotation.abs() > 0.001 {
        canvas.rotate(rotation, None);
    }
    // 3. Apply zoom
    if (zoom - 1.0).abs() > 0.001 {
        canvas.scale((zoom, zoom));
    }
    // 4. Translate back from center + apply camera pan offset
    canvas.translate((-cx - x, -cy - y));
}