@open-take/compositor 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdp-CzT5BKNY.js +3 -0
- package/dist/cdp-QXbQfXna.js +373 -0
- package/dist/cdp-QXbQfXna.js.map +1 -0
- package/dist/index.d.ts +4327 -195
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26282 -160
- package/dist/index.js.map +1 -1
- package/dist/server-841PMF2H.js +163 -0
- package/dist/server-841PMF2H.js.map +1 -0
- package/dist/server-C2aVv4-t.js +3 -0
- package/kit/audio.mjs +2366 -0
- package/kit/camera.js +587 -0
- package/kit/cursor.js +264 -0
- package/kit/cut.js +427 -0
- package/kit/draw.js +346 -0
- package/kit/look.js +589 -0
- package/kit/map.js +170 -0
- package/kit/media.js +3568 -0
- package/kit/motion.js +1115 -0
- package/kit/seams.js +1006 -0
- package/kit/stage.js +923 -0
- package/kit/time.js +812 -0
- package/kit/world.js +1072 -0
- package/package.json +7 -3
- package/src/ffmpeg.ts +63 -0
- package/src/film/beats.ts +1285 -0
- package/src/film/carry.ts +221 -0
- package/src/film/changes.ts +330 -0
- package/src/film/check.ts +996 -0
- package/src/film/clock.ts +2530 -0
- package/src/film/components-probe.ts +809 -0
- package/src/film/components.ts +1566 -0
- package/src/film/frames.ts +389 -0
- package/src/film/gallery.ts +3276 -0
- package/src/film/kit.ts +515 -0
- package/src/film/layout.ts +701 -0
- package/src/film/listen.ts +1979 -0
- package/src/film/look.ts +3829 -0
- package/src/film/materials.ts +976 -0
- package/src/film/moving.ts +248 -0
- package/src/film/part-files.ts +729 -0
- package/src/film/picture.ts +74 -0
- package/src/film/probe-text.ts +843 -0
- package/src/film/probe.ts +3456 -0
- package/src/film/provenance.ts +596 -0
- package/src/film/render.ts +2447 -0
- package/src/film/report.ts +1631 -0
- package/src/film/sheet.ts +156 -0
- package/src/film/study-declared.ts +123 -0
- package/src/film/study-picture.ts +791 -0
- package/src/film/study-sound.ts +416 -0
- package/src/film/study.ts +864 -0
- package/src/film/tuning.ts +155 -0
- package/src/film/viewer.ts +174 -0
- package/src/html/cdp.ts +575 -0
- package/src/html/server.ts +208 -0
- package/src/index.ts +202 -0
- package/src/launch-render.ts +3 -2
- package/src/math.ts +3 -3
- package/src/mjpeg-matroska.ts +228 -0
- package/src/render.ts +6 -4
- package/src/state-diff.ts +215 -0
- package/src/state-index.ts +99 -0
- package/src/types.ts +59 -1
- package/starter/PLAN.md +84 -0
- package/starter/START.md +460 -0
- package/starter/lib/anim.js +760 -0
- package/starter/lib/devices.js +1402 -0
- package/starter/lib/handoff.js +421 -0
- package/starter/lib/hud.js +1879 -0
- package/starter/lib/measure.mjs +154 -0
- package/starter/lib/moves.js +1095 -0
- package/starter/lib/palette.mjs +803 -0
- package/starter/lib/physics-seeds.mjs +198 -0
- package/starter/lib/physics.js +738 -0
- package/starter/lib/sampler.mjs +242 -0
- package/starter/lib/solid.js +727 -0
- package/starter/lib/space.js +1227 -0
- package/starter/lib/swarm.js +1398 -0
- package/starter/reference/motion.md +276 -0
- package/starter/reference/structure.md +466 -0
- package/starter/reference/travel/motion.md +433 -0
- package/starter/reference/travel/sound.md +241 -0
- package/starter/reference/travel/structure.md +518 -0
- package/starter/samples/NOTICE.txt +55 -0
- package/starter/samples/celli/celli_24_1.opus +0 -0
- package/starter/samples/celli/celli_24_3.opus +0 -0
- package/starter/samples/celli/celli_28_1.opus +0 -0
- package/starter/samples/celli/celli_28_3.opus +0 -0
- package/starter/samples/celli/celli_31_1.opus +0 -0
- package/starter/samples/celli/celli_31_3.opus +0 -0
- package/starter/samples/celli/celli_35_1.opus +0 -0
- package/starter/samples/celli/celli_35_3.opus +0 -0
- package/starter/samples/celli/celli_38_1.opus +0 -0
- package/starter/samples/celli/celli_38_3.opus +0 -0
- package/starter/samples/celli/celli_41_1.opus +0 -0
- package/starter/samples/celli/celli_41_3.opus +0 -0
- package/starter/samples/celli/celli_45_1.opus +0 -0
- package/starter/samples/celli/celli_45_3.opus +0 -0
- package/starter/samples/celli/celli_48_1.opus +0 -0
- package/starter/samples/celli/celli_48_3.opus +0 -0
- package/starter/samples/celli/celli_52_1.opus +0 -0
- package/starter/samples/celli/celli_52_3.opus +0 -0
- package/starter/samples/celli/celli_55_1.opus +0 -0
- package/starter/samples/celli/celli_55_3.opus +0 -0
- package/starter/samples/celli/celli_59_1.opus +0 -0
- package/starter/samples/celli/celli_59_3.opus +0 -0
- package/starter/samples/celli/celli_62_1.opus +0 -0
- package/starter/samples/celli/celli_62_3.opus +0 -0
- package/starter/samples/celli/celli_65_1.opus +0 -0
- package/starter/samples/celli/celli_65_3.opus +0 -0
- package/starter/samples/crash/crash_f_1.flac +0 -0
- package/starter/samples/crash/crash_fff_1.flac +0 -0
- package/starter/samples/ebass/ebass_0_x.flac +0 -0
- package/starter/samples/ebass/ebass_10_x.flac +0 -0
- package/starter/samples/ebass/ebass_11_x.flac +0 -0
- package/starter/samples/ebass/ebass_1_x.flac +0 -0
- package/starter/samples/ebass/ebass_2_x.flac +0 -0
- package/starter/samples/ebass/ebass_3_x.flac +0 -0
- package/starter/samples/ebass/ebass_4_x.flac +0 -0
- package/starter/samples/ebass/ebass_5_x.flac +0 -0
- package/starter/samples/ebass/ebass_6_x.flac +0 -0
- package/starter/samples/ebass/ebass_7_x.flac +0 -0
- package/starter/samples/ebass/ebass_8_x.flac +0 -0
- package/starter/samples/ebass/ebass_9_x.flac +0 -0
- package/starter/samples/ek_clap/ek_clap_1_1.flac +0 -0
- package/starter/samples/ek_hat/ek_hat_1_1.flac +0 -0
- package/starter/samples/ek_hat/ek_hat_1_2.flac +0 -0
- package/starter/samples/ek_kick/ek_kick_1_1.flac +0 -0
- package/starter/samples/ek_kick/ek_kick_1_2.flac +0 -0
- package/starter/samples/glock/glock_67_x.flac +0 -0
- package/starter/samples/glock/glock_72_x.flac +0 -0
- package/starter/samples/glock/glock_79_x.flac +0 -0
- package/starter/samples/glock/glock_84_x.flac +0 -0
- package/starter/samples/glock/glock_91_x.flac +0 -0
- package/starter/samples/glock/glock_96_x.flac +0 -0
- package/starter/samples/harp/harp_101_f.opus +0 -0
- package/starter/samples/harp/harp_28_f.opus +0 -0
- package/starter/samples/harp/harp_31_mp.opus +0 -0
- package/starter/samples/harp/harp_35_mf.opus +0 -0
- package/starter/samples/harp/harp_38_mf.opus +0 -0
- package/starter/samples/harp/harp_41_mf.opus +0 -0
- package/starter/samples/harp/harp_45_mf.opus +0 -0
- package/starter/samples/harp/harp_48_mf.opus +0 -0
- package/starter/samples/harp/harp_52_mf.opus +0 -0
- package/starter/samples/harp/harp_55_mf.opus +0 -0
- package/starter/samples/harp/harp_59_mf.opus +0 -0
- package/starter/samples/harp/harp_62_mf.opus +0 -0
- package/starter/samples/harp/harp_65_mf.opus +0 -0
- package/starter/samples/harp/harp_69_mf.opus +0 -0
- package/starter/samples/harp/harp_72_mf.opus +0 -0
- package/starter/samples/harp/harp_76_mf.opus +0 -0
- package/starter/samples/harp/harp_79_mf.opus +0 -0
- package/starter/samples/harp/harp_83_mf.opus +0 -0
- package/starter/samples/harp/harp_86_mf.opus +0 -0
- package/starter/samples/harp/harp_89_mf.opus +0 -0
- package/starter/samples/harp/harp_93_mf.opus +0 -0
- package/starter/samples/harp/harp_95_mf.opus +0 -0
- package/starter/samples/harp/harp_98_f.opus +0 -0
- package/starter/samples/manifest.json +2951 -0
- package/starter/samples/marimba/marimba_29_x.flac +0 -0
- package/starter/samples/marimba/marimba_36_x.flac +0 -0
- package/starter/samples/marimba/marimba_43_x.flac +0 -0
- package/starter/samples/marimba/marimba_47_x.flac +0 -0
- package/starter/samples/marimba/marimba_53_x.flac +0 -0
- package/starter/samples/marimba/marimba_60_x.flac +0 -0
- package/starter/samples/marimba/marimba_67_x.flac +0 -0
- package/starter/samples/marimba/marimba_71_x.flac +0 -0
- package/starter/samples/marimba/marimba_77_x.flac +0 -0
- package/starter/samples/marimba/marimba_84_x.flac +0 -0
- package/starter/samples/piano/piano_102_12.opus +0 -0
- package/starter/samples/piano/piano_102_16.opus +0 -0
- package/starter/samples/piano/piano_102_4.opus +0 -0
- package/starter/samples/piano/piano_102_8.opus +0 -0
- package/starter/samples/piano/piano_105_12.opus +0 -0
- package/starter/samples/piano/piano_105_16.opus +0 -0
- package/starter/samples/piano/piano_105_4.opus +0 -0
- package/starter/samples/piano/piano_105_8.opus +0 -0
- package/starter/samples/piano/piano_108_12.opus +0 -0
- package/starter/samples/piano/piano_108_16.opus +0 -0
- package/starter/samples/piano/piano_108_4.opus +0 -0
- package/starter/samples/piano/piano_108_8.opus +0 -0
- package/starter/samples/piano/piano_21_12.opus +0 -0
- package/starter/samples/piano/piano_21_16.opus +0 -0
- package/starter/samples/piano/piano_21_4.opus +0 -0
- package/starter/samples/piano/piano_21_8.opus +0 -0
- package/starter/samples/piano/piano_24_12.opus +0 -0
- package/starter/samples/piano/piano_24_16.opus +0 -0
- package/starter/samples/piano/piano_24_4.opus +0 -0
- package/starter/samples/piano/piano_24_8.opus +0 -0
- package/starter/samples/piano/piano_27_12.opus +0 -0
- package/starter/samples/piano/piano_27_16.opus +0 -0
- package/starter/samples/piano/piano_27_4.opus +0 -0
- package/starter/samples/piano/piano_27_8.opus +0 -0
- package/starter/samples/piano/piano_30_12.opus +0 -0
- package/starter/samples/piano/piano_30_16.opus +0 -0
- package/starter/samples/piano/piano_30_4.opus +0 -0
- package/starter/samples/piano/piano_30_8.opus +0 -0
- package/starter/samples/piano/piano_33_12.opus +0 -0
- package/starter/samples/piano/piano_33_16.opus +0 -0
- package/starter/samples/piano/piano_33_4.opus +0 -0
- package/starter/samples/piano/piano_33_8.opus +0 -0
- package/starter/samples/piano/piano_36_12.opus +0 -0
- package/starter/samples/piano/piano_36_16.opus +0 -0
- package/starter/samples/piano/piano_36_4.opus +0 -0
- package/starter/samples/piano/piano_36_8.opus +0 -0
- package/starter/samples/piano/piano_39_12.opus +0 -0
- package/starter/samples/piano/piano_39_16.opus +0 -0
- package/starter/samples/piano/piano_39_4.opus +0 -0
- package/starter/samples/piano/piano_39_8.opus +0 -0
- package/starter/samples/piano/piano_42_12.opus +0 -0
- package/starter/samples/piano/piano_42_16.opus +0 -0
- package/starter/samples/piano/piano_42_4.opus +0 -0
- package/starter/samples/piano/piano_42_8.opus +0 -0
- package/starter/samples/piano/piano_45_12.opus +0 -0
- package/starter/samples/piano/piano_45_16.opus +0 -0
- package/starter/samples/piano/piano_45_4.opus +0 -0
- package/starter/samples/piano/piano_45_8.opus +0 -0
- package/starter/samples/piano/piano_48_12.opus +0 -0
- package/starter/samples/piano/piano_48_16.opus +0 -0
- package/starter/samples/piano/piano_48_4.opus +0 -0
- package/starter/samples/piano/piano_48_8.opus +0 -0
- package/starter/samples/piano/piano_51_12.opus +0 -0
- package/starter/samples/piano/piano_51_16.opus +0 -0
- package/starter/samples/piano/piano_51_4.opus +0 -0
- package/starter/samples/piano/piano_51_8.opus +0 -0
- package/starter/samples/piano/piano_54_12.opus +0 -0
- package/starter/samples/piano/piano_54_16.opus +0 -0
- package/starter/samples/piano/piano_54_4.opus +0 -0
- package/starter/samples/piano/piano_54_8.opus +0 -0
- package/starter/samples/piano/piano_57_12.opus +0 -0
- package/starter/samples/piano/piano_57_16.opus +0 -0
- package/starter/samples/piano/piano_57_4.opus +0 -0
- package/starter/samples/piano/piano_57_8.opus +0 -0
- package/starter/samples/piano/piano_60_12.opus +0 -0
- package/starter/samples/piano/piano_60_16.opus +0 -0
- package/starter/samples/piano/piano_60_4.opus +0 -0
- package/starter/samples/piano/piano_60_8.opus +0 -0
- package/starter/samples/piano/piano_63_12.opus +0 -0
- package/starter/samples/piano/piano_63_16.opus +0 -0
- package/starter/samples/piano/piano_63_4.opus +0 -0
- package/starter/samples/piano/piano_63_8.opus +0 -0
- package/starter/samples/piano/piano_66_12.opus +0 -0
- package/starter/samples/piano/piano_66_16.opus +0 -0
- package/starter/samples/piano/piano_66_4.opus +0 -0
- package/starter/samples/piano/piano_66_8.opus +0 -0
- package/starter/samples/piano/piano_69_12.opus +0 -0
- package/starter/samples/piano/piano_69_16.opus +0 -0
- package/starter/samples/piano/piano_69_4.opus +0 -0
- package/starter/samples/piano/piano_69_8.opus +0 -0
- package/starter/samples/piano/piano_72_12.opus +0 -0
- package/starter/samples/piano/piano_72_16.opus +0 -0
- package/starter/samples/piano/piano_72_4.opus +0 -0
- package/starter/samples/piano/piano_72_8.opus +0 -0
- package/starter/samples/piano/piano_75_12.opus +0 -0
- package/starter/samples/piano/piano_75_16.opus +0 -0
- package/starter/samples/piano/piano_75_4.opus +0 -0
- package/starter/samples/piano/piano_75_8.opus +0 -0
- package/starter/samples/piano/piano_78_12.opus +0 -0
- package/starter/samples/piano/piano_78_16.opus +0 -0
- package/starter/samples/piano/piano_78_4.opus +0 -0
- package/starter/samples/piano/piano_78_8.opus +0 -0
- package/starter/samples/piano/piano_81_12.opus +0 -0
- package/starter/samples/piano/piano_81_16.opus +0 -0
- package/starter/samples/piano/piano_81_4.opus +0 -0
- package/starter/samples/piano/piano_81_8.opus +0 -0
- package/starter/samples/piano/piano_84_12.opus +0 -0
- package/starter/samples/piano/piano_84_16.opus +0 -0
- package/starter/samples/piano/piano_84_4.opus +0 -0
- package/starter/samples/piano/piano_84_8.opus +0 -0
- package/starter/samples/piano/piano_87_12.opus +0 -0
- package/starter/samples/piano/piano_87_16.opus +0 -0
- package/starter/samples/piano/piano_87_4.opus +0 -0
- package/starter/samples/piano/piano_87_8.opus +0 -0
- package/starter/samples/piano/piano_90_12.opus +0 -0
- package/starter/samples/piano/piano_90_16.opus +0 -0
- package/starter/samples/piano/piano_90_4.opus +0 -0
- package/starter/samples/piano/piano_90_8.opus +0 -0
- package/starter/samples/piano/piano_93_12.opus +0 -0
- package/starter/samples/piano/piano_93_16.opus +0 -0
- package/starter/samples/piano/piano_93_4.opus +0 -0
- package/starter/samples/piano/piano_93_8.opus +0 -0
- package/starter/samples/piano/piano_96_12.opus +0 -0
- package/starter/samples/piano/piano_96_16.opus +0 -0
- package/starter/samples/piano/piano_96_4.opus +0 -0
- package/starter/samples/piano/piano_96_8.opus +0 -0
- package/starter/samples/piano/piano_99_12.opus +0 -0
- package/starter/samples/piano/piano_99_16.opus +0 -0
- package/starter/samples/piano/piano_99_4.opus +0 -0
- package/starter/samples/piano/piano_99_8.opus +0 -0
- package/starter/samples/piano/piano_rel_102_x.opus +0 -0
- package/starter/samples/piano/piano_rel_105_x.opus +0 -0
- package/starter/samples/piano/piano_rel_108_x.opus +0 -0
- package/starter/samples/piano/piano_rel_21_x.opus +0 -0
- package/starter/samples/piano/piano_rel_24_x.opus +0 -0
- package/starter/samples/piano/piano_rel_27_x.opus +0 -0
- package/starter/samples/piano/piano_rel_30_x.opus +0 -0
- package/starter/samples/piano/piano_rel_33_x.opus +0 -0
- package/starter/samples/piano/piano_rel_36_x.opus +0 -0
- package/starter/samples/piano/piano_rel_39_x.opus +0 -0
- package/starter/samples/piano/piano_rel_42_x.opus +0 -0
- package/starter/samples/piano/piano_rel_45_x.opus +0 -0
- package/starter/samples/piano/piano_rel_48_x.opus +0 -0
- package/starter/samples/piano/piano_rel_51_x.opus +0 -0
- package/starter/samples/piano/piano_rel_54_x.opus +0 -0
- package/starter/samples/piano/piano_rel_57_x.opus +0 -0
- package/starter/samples/piano/piano_rel_60_x.opus +0 -0
- package/starter/samples/piano/piano_rel_63_x.opus +0 -0
- package/starter/samples/piano/piano_rel_66_x.opus +0 -0
- package/starter/samples/piano/piano_rel_69_x.opus +0 -0
- package/starter/samples/piano/piano_rel_72_x.opus +0 -0
- package/starter/samples/piano/piano_rel_75_x.opus +0 -0
- package/starter/samples/piano/piano_rel_78_x.opus +0 -0
- package/starter/samples/piano/piano_rel_81_x.opus +0 -0
- package/starter/samples/piano/piano_rel_84_x.opus +0 -0
- package/starter/samples/piano/piano_rel_87_x.opus +0 -0
- package/starter/samples/piano/piano_rel_90_x.opus +0 -0
- package/starter/samples/piano/piano_rel_93_x.opus +0 -0
- package/starter/samples/piano/piano_rel_96_x.opus +0 -0
- package/starter/samples/piano/piano_rel_99_x.opus +0 -0
- package/starter/samples/pizz/pizz_24_2.flac +0 -0
- package/starter/samples/pizz/pizz_28_2.flac +0 -0
- package/starter/samples/pizz/pizz_31_2.flac +0 -0
- package/starter/samples/pizz/pizz_35_2.flac +0 -0
- package/starter/samples/pizz/pizz_38_2.flac +0 -0
- package/starter/samples/pizz/pizz_41_2.flac +0 -0
- package/starter/samples/pizz/pizz_45_2.flac +0 -0
- package/starter/samples/pizz/pizz_48_2.flac +0 -0
- package/starter/samples/pizz/pizz_52_2.flac +0 -0
- package/starter/samples/pizz/pizz_57_2.flac +0 -0
- package/starter/samples/pizz/pizz_60_2.flac +0 -0
- package/starter/samples/pizz/pizz_64_2.flac +0 -0
- package/starter/samples/pizz/pizz_67_2.flac +0 -0
- package/starter/samples/pizz/pizz_71_2.flac +0 -0
- package/starter/samples/pizz/pizz_74_2.flac +0 -0
- package/starter/samples/pk_hat/pk_hat_2_1.flac +0 -0
- package/starter/samples/pk_hat/pk_hat_2_2.flac +0 -0
- package/starter/samples/pk_hat/pk_hat_6_1.flac +0 -0
- package/starter/samples/pk_hat/pk_hat_6_2.flac +0 -0
- package/starter/samples/pk_kick/pk_kick_3_1.flac +0 -0
- package/starter/samples/pk_kick/pk_kick_3_2.flac +0 -0
- package/starter/samples/pk_kick/pk_kick_7_1.flac +0 -0
- package/starter/samples/pk_kick/pk_kick_7_2.flac +0 -0
- package/starter/samples/pk_ohat/pk_ohat_3_1.flac +0 -0
- package/starter/samples/pk_ohat/pk_ohat_3_2.flac +0 -0
- package/starter/samples/pk_ohat/pk_ohat_6_1.flac +0 -0
- package/starter/samples/pk_ohat/pk_ohat_6_2.flac +0 -0
- package/starter/samples/pk_rim/pk_rim_2_1.flac +0 -0
- package/starter/samples/pk_rim/pk_rim_2_2.flac +0 -0
- package/starter/samples/pk_rim/pk_rim_5_1.flac +0 -0
- package/starter/samples/pk_rim/pk_rim_5_2.flac +0 -0
- package/starter/samples/pk_snare/pk_snare_3_1.flac +0 -0
- package/starter/samples/pk_snare/pk_snare_3_2.flac +0 -0
- package/starter/samples/pk_snare/pk_snare_7_1.flac +0 -0
- package/starter/samples/pk_snare/pk_snare_7_2.flac +0 -0
- package/starter/samples/shaker/shaker_1_1.flac +0 -0
- package/starter/samples/shaker/shaker_2_1.flac +0 -0
- package/starter/samples/snap/snap_r_1.flac +0 -0
- package/starter/samples/snap/snap_r_2.flac +0 -0
- package/starter/samples/snap/snap_r_3.flac +0 -0
- package/starter/samples/snap/snap_r_4.flac +0 -0
- package/starter/samples/snap/snap_r_5.flac +0 -0
- package/starter/samples/snap/snap_r_6.flac +0 -0
- package/starter/samples/tom/tom_3_1.flac +0 -0
- package/starter/samples/tom/tom_3_2.flac +0 -0
- package/starter/samples/tom/tom_4_1.flac +0 -0
- package/starter/samples/tom/tom_4_2.flac +0 -0
- package/starter/samples/vibes/vibes_41_1.opus +0 -0
- package/starter/samples/vibes/vibes_41_2.opus +0 -0
- package/starter/samples/vibes/vibes_45_1.opus +0 -0
- package/starter/samples/vibes/vibes_45_2.opus +0 -0
- package/starter/samples/vibes/vibes_48_1.opus +0 -0
- package/starter/samples/vibes/vibes_48_2.opus +0 -0
- package/starter/samples/vibes/vibes_52_1.opus +0 -0
- package/starter/samples/vibes/vibes_52_2.opus +0 -0
- package/starter/samples/vibes/vibes_55_1.opus +0 -0
- package/starter/samples/vibes/vibes_55_2.opus +0 -0
- package/starter/samples/vibes/vibes_59_1.opus +0 -0
- package/starter/samples/vibes/vibes_59_2.opus +0 -0
- package/starter/samples/vibes/vibes_62_1.opus +0 -0
- package/starter/samples/vibes/vibes_62_2.opus +0 -0
- package/starter/samples/vibes/vibes_65_1.opus +0 -0
- package/starter/samples/vibes/vibes_65_2.opus +0 -0
- package/starter/samples/vibes/vibes_69_1.opus +0 -0
- package/starter/samples/vibes/vibes_69_2.opus +0 -0
- package/starter/samples/vibes/vibes_72_1.opus +0 -0
- package/starter/samples/vibes/vibes_72_2.opus +0 -0
- package/starter/samples/vibes/vibes_76_1.opus +0 -0
- package/starter/samples/vibes/vibes_76_2.opus +0 -0
- package/starter/samples/violas/violas_36_1.opus +0 -0
- package/starter/samples/violas/violas_36_2.opus +0 -0
- package/starter/samples/violas/violas_38_1.opus +0 -0
- package/starter/samples/violas/violas_38_2.opus +0 -0
- package/starter/samples/violas/violas_40_1.opus +0 -0
- package/starter/samples/violas/violas_40_2.opus +0 -0
- package/starter/samples/violas/violas_43_1.opus +0 -0
- package/starter/samples/violas/violas_43_2.opus +0 -0
- package/starter/samples/violas/violas_47_1.opus +0 -0
- package/starter/samples/violas/violas_47_2.opus +0 -0
- package/starter/samples/violas/violas_50_1.opus +0 -0
- package/starter/samples/violas/violas_50_2.opus +0 -0
- package/starter/samples/violas/violas_53_1.opus +0 -0
- package/starter/samples/violas/violas_53_2.opus +0 -0
- package/starter/samples/violas/violas_57_1.opus +0 -0
- package/starter/samples/violas/violas_57_2.opus +0 -0
- package/starter/samples/violas/violas_60_1.opus +0 -0
- package/starter/samples/violas/violas_60_2.opus +0 -0
- package/starter/samples/violas/violas_64_1.opus +0 -0
- package/starter/samples/violas/violas_64_2.opus +0 -0
- package/starter/samples/violas/violas_67_1.opus +0 -0
- package/starter/samples/violas/violas_67_2.opus +0 -0
- package/starter/samples/violas/violas_71_1.opus +0 -0
- package/starter/samples/violas/violas_71_2.opus +0 -0
- package/starter/samples/violas/violas_74_1.opus +0 -0
- package/starter/samples/violas/violas_74_2.opus +0 -0
- package/starter/samples/violins/violins_43_1.opus +0 -0
- package/starter/samples/violins/violins_43_2.opus +0 -0
- package/starter/samples/violins/violins_45_1.opus +0 -0
- package/starter/samples/violins/violins_45_2.opus +0 -0
- package/starter/samples/violins/violins_47_1.opus +0 -0
- package/starter/samples/violins/violins_47_2.opus +0 -0
- package/starter/samples/violins/violins_50_1.opus +0 -0
- package/starter/samples/violins/violins_50_2.opus +0 -0
- package/starter/samples/violins/violins_54_1.opus +0 -0
- package/starter/samples/violins/violins_54_2.opus +0 -0
- package/starter/samples/violins/violins_57_1.opus +0 -0
- package/starter/samples/violins/violins_57_2.opus +0 -0
- package/starter/samples/violins/violins_60_1.opus +0 -0
- package/starter/samples/violins/violins_60_2.opus +0 -0
- package/starter/samples/violins/violins_64_1.opus +0 -0
- package/starter/samples/violins/violins_64_2.opus +0 -0
- package/starter/samples/violins/violins_67_1.opus +0 -0
- package/starter/samples/violins/violins_67_2.opus +0 -0
- package/starter/samples/violins/violins_71_1.opus +0 -0
- package/starter/samples/violins/violins_71_2.opus +0 -0
- package/starter/samples/violins/violins_74_1.opus +0 -0
- package/starter/samples/violins/violins_74_2.opus +0 -0
- package/starter/samples/woodblock/woodblock_f_1.flac +0 -0
- package/starter/samples/woodblock/woodblock_f_2.flac +0 -0
package/kit/motion.js
ADDED
|
@@ -0,0 +1,1115 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Motion for a film page — open-take's film kit. Plain functions of time:
|
|
3
|
+
// give one the film's time t (s) and when the move starts, read back numbers,
|
|
4
|
+
// and put them wherever the film needs them (CSS, canvas, WebGL). Nothing
|
|
5
|
+
// here owns an element, keeps state or runs on its own clock, so a frame
|
|
6
|
+
// rendered twice or out of order comes out the same.
|
|
7
|
+
//
|
|
8
|
+
// - springs that land: a damped spring in closed form, under-, critically
|
|
9
|
+
// or over-damped, with or without an overshoot;
|
|
10
|
+
// - entrances that read as arriving, not fading: the opacity is tied to the
|
|
11
|
+
// travel and finishes first; a drop stretches with its speed and squashes
|
|
12
|
+
// as it brakes; text can rise out of its own line or be typed;
|
|
13
|
+
// - handoffs, where one picture becomes the next: a box growing into
|
|
14
|
+
// another, a circle opening from a point, a zoom through a rect, items
|
|
15
|
+
// gathering into one place;
|
|
16
|
+
// - contact: a button that sinks and comes back, a hit that kicks;
|
|
17
|
+
// - numbers and parts: a number counting in the product's own format, a
|
|
18
|
+
// line of text scrambling into place, digits rolling like an odometer or
|
|
19
|
+
// folding over like a split-flap board, a part's pieces arriving one
|
|
20
|
+
// after another (assemble, which also writes its numbers onto elements
|
|
21
|
+
// when it is given elements — from t alone).
|
|
22
|
+
//
|
|
23
|
+
// Every default below says where it comes from; none of them is a style.
|
|
24
|
+
// Change any of them per call, or write the move yourself.
|
|
25
|
+
//
|
|
26
|
+
// import { rise, morphRect, rectClip, press } from "./kit/motion.js";
|
|
27
|
+
// const w = rise(t, 2.0); // { x, y, opacity, p }
|
|
28
|
+
// title.style.transform = `translate(${w.x}px, ${w.y}px)`;
|
|
29
|
+
// title.style.opacity = String(w.opacity);
|
|
30
|
+
// const r = morphRect(t, 4, 4.5, buttonBox, { x: 0, y: 0, w: 1920, h: 1080 });
|
|
31
|
+
// card.style.clipPath = rectClip(r, { width: 1920, height: 1080 });
|
|
32
|
+
// const b = press(t, 6.1); // { scale, y }
|
|
33
|
+
|
|
34
|
+
/** @typedef {{ x: number, y: number }} Pt a point, in CSS px */
|
|
35
|
+
/** @typedef {{ x: number, y: number, w: number, h: number, r: number }} Rect a box with a corner radius, in CSS px */
|
|
36
|
+
/** @typedef {{ x: number, y: number, w?: number, h?: number, width?: number, height?: number, r?: number }} RectLike a Rect, or a kit Box ({ x, y, width, height }) */
|
|
37
|
+
/** @typedef {{ x?: number, y?: number, w?: number, h?: number, width?: number, height?: number }} Frame an element's box: its size, and where it sits if not at 0, 0 */
|
|
38
|
+
/**
|
|
39
|
+
* @typedef {object} SpringOpts
|
|
40
|
+
* @property {number} [from] where it starts (default 0)
|
|
41
|
+
* @property {number} [to] where it comes to rest (default 1)
|
|
42
|
+
* @property {number} [start] when it is let go (s, default 0)
|
|
43
|
+
* @property {number} [velocity] its speed when let go, in units a second (default 0)
|
|
44
|
+
* @property {number} [freq] natural frequency in Hz (default 1.6 — see DEFAULT_FREQ)
|
|
45
|
+
* @property {number} [stiffness] instead of freq: k for a unit mass (ω0 = √k)
|
|
46
|
+
* @property {number} [damping] damping RATIO ζ: < 1 swings past the rest, 1 lands
|
|
47
|
+
* without, > 1 creeps in (default 0.7). Not a coefficient: framer-motion's
|
|
48
|
+
* { stiffness: 300, damping: 20 } is ratio 20 / (2·√300) ≈ 0.58 — or pass it as `friction`
|
|
49
|
+
* @property {number} [friction] instead of damping: the coefficient c for a unit mass (ζ = c / 2√k)
|
|
50
|
+
* @property {number} [overshoot] instead of damping: how far past the rest the
|
|
51
|
+
* first swing goes, as a share of the travel (0.1 = 10 %)
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
// --- small helpers ----------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
/** @param {number} x */
|
|
57
|
+
const clamp01 = (x) => Math.max(0, Math.min(1, x));
|
|
58
|
+
/** @param {number} a @param {number} b @param {number} p */
|
|
59
|
+
const lerp = (a, b, p) => a + (b - a) * p;
|
|
60
|
+
|
|
61
|
+
/** smootherstep, 6p⁵ − 15p⁴ + 10p³ (the camera's `smooth`) */
|
|
62
|
+
/** @param {number} p */
|
|
63
|
+
export const smooth = (p) => {
|
|
64
|
+
const x = clamp01(p);
|
|
65
|
+
return x * x * x * (x * (x * 6 - 15) + 10);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** leaves from rest and is fastest at the end: how things get out of the way */
|
|
69
|
+
/** @param {number} p */
|
|
70
|
+
export const easeIn = (p) => {
|
|
71
|
+
const x = clamp01(p);
|
|
72
|
+
return x * x * x;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** fastest at the start and lands softly */
|
|
76
|
+
/** @param {number} p */
|
|
77
|
+
export const easeOut = (p) => 1 - easeIn(1 - p);
|
|
78
|
+
|
|
79
|
+
/** a Rect from a Rect or a kit Box */
|
|
80
|
+
/** @param {RectLike} b @returns {Rect} */
|
|
81
|
+
export function rectOf(b) {
|
|
82
|
+
return { x: b.x, y: b.y, w: b.w ?? b.width ?? 0, h: b.h ?? b.height ?? 0, r: b.r ?? 0 };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// --- springs ----------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
// 1.6 Hz with ζ 0.7: settles to within 2 % in ≈ 4 / (ζ·ω0) ≈ 0.57 s, with a
|
|
88
|
+
// first swing of ≈ 4.6 % of the travel. That is the take camera's push-in
|
|
89
|
+
// (0.73 s) scaled down for things smaller than the picture, which travel
|
|
90
|
+
// less and so should take less time; the swing is about the least an eye
|
|
91
|
+
// notices at 60 fps on a 24 px rise (≈ 1 px), so the landing is felt, not seen.
|
|
92
|
+
const DEFAULT_FREQ = 1.6;
|
|
93
|
+
const DEFAULT_DAMPING = 0.7;
|
|
94
|
+
|
|
95
|
+
/** the damping ratio whose first swing past the rest is `os` of the travel:
|
|
96
|
+
* for an underdamped step, os = exp(−ζπ / √(1−ζ²)), solved for ζ */
|
|
97
|
+
/** @param {number} os */
|
|
98
|
+
export function dampingFor(os) {
|
|
99
|
+
if (!(os > 0)) return 1;
|
|
100
|
+
const l = Math.log(Math.min(os, 0.999));
|
|
101
|
+
return -l / Math.sqrt(Math.PI * Math.PI + l * l);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @param {SpringOpts} o */
|
|
105
|
+
function springParams(o) {
|
|
106
|
+
const w0 = o.stiffness != null ? Math.sqrt(o.stiffness) : 2 * Math.PI * (o.freq ?? DEFAULT_FREQ);
|
|
107
|
+
const zeta =
|
|
108
|
+
o.overshoot != null
|
|
109
|
+
? dampingFor(o.overshoot)
|
|
110
|
+
: o.friction != null
|
|
111
|
+
? o.friction / (2 * w0)
|
|
112
|
+
: (o.damping ?? DEFAULT_DAMPING);
|
|
113
|
+
if (!(w0 > 0)) throw new Error(`spring: the frequency must be above 0 (ω0 ${w0})`);
|
|
114
|
+
if (!(zeta >= 0)) throw new Error(`spring: the damping must be 0 or more (ζ ${zeta})`);
|
|
115
|
+
return { w0, zeta };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** displacement from the rest and its speed, τ s after release, for
|
|
119
|
+
* x(0) = x0, x'(0) = v0: the closed-form solutions of x'' + 2ζω0x' + ω0²x = 0 */
|
|
120
|
+
/** @param {number} tau @param {number} x0 @param {number} v0 @param {number} w0 @param {number} zeta @returns {[number, number]} */
|
|
121
|
+
function springState(tau, x0, v0, w0, zeta) {
|
|
122
|
+
if (tau <= 0) return [x0, v0];
|
|
123
|
+
if (Math.abs(zeta - 1) < 1e-6) {
|
|
124
|
+
// critically damped: x = e^(−ω0τ)·(x0 + (v0 + ω0x0)·τ)
|
|
125
|
+
const e = Math.exp(-w0 * tau);
|
|
126
|
+
const b = v0 + w0 * x0;
|
|
127
|
+
return [e * (x0 + b * tau), e * (b - w0 * (x0 + b * tau))];
|
|
128
|
+
}
|
|
129
|
+
if (zeta < 1) {
|
|
130
|
+
const wd = w0 * Math.sqrt(1 - zeta * zeta);
|
|
131
|
+
const a = zeta * w0;
|
|
132
|
+
const e = Math.exp(-a * tau);
|
|
133
|
+
const B = (v0 + a * x0) / wd;
|
|
134
|
+
const c = Math.cos(wd * tau);
|
|
135
|
+
const s = Math.sin(wd * tau);
|
|
136
|
+
return [e * (x0 * c + B * s), e * ((B * wd - a * x0) * c - (x0 * wd + a * B) * s)];
|
|
137
|
+
}
|
|
138
|
+
// overdamped: two decaying exponentials, r = −ω0·(ζ ∓ √(ζ²−1))
|
|
139
|
+
const q = Math.sqrt(zeta * zeta - 1);
|
|
140
|
+
const r1 = -w0 * (zeta - q);
|
|
141
|
+
const r2 = -w0 * (zeta + q);
|
|
142
|
+
const c2 = (v0 - r1 * x0) / (r2 - r1);
|
|
143
|
+
const c1 = x0 - c2;
|
|
144
|
+
const e1 = Math.exp(r1 * tau);
|
|
145
|
+
const e2 = Math.exp(r2 * tau);
|
|
146
|
+
return [c1 * e1 + c2 * e2, r1 * c1 * e1 + r2 * c2 * e2];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** where a spring is at t (s): `from` until `start`, then on its way to `to` */
|
|
150
|
+
/** @param {number} t @param {SpringOpts} [opts] */
|
|
151
|
+
export function spring(t, opts = {}) {
|
|
152
|
+
const { from = 0, to = 1, start = 0, velocity = 0 } = opts;
|
|
153
|
+
const { w0, zeta } = springParams(opts);
|
|
154
|
+
return to + springState(t - start, from - to, velocity, w0, zeta)[0];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** how fast a spring moves at t (units a second) */
|
|
158
|
+
/** @param {number} t @param {SpringOpts} [opts] */
|
|
159
|
+
export function springVel(t, opts = {}) {
|
|
160
|
+
const { from = 0, to = 1, start = 0, velocity = 0 } = opts;
|
|
161
|
+
const { w0, zeta } = springParams(opts);
|
|
162
|
+
if (t < start) return 0;
|
|
163
|
+
return springState(t - start, from - to, velocity, w0, zeta)[1];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** how long a spring takes to stay within `tol` of the travel (s) — to plan
|
|
167
|
+
* what comes next; the spring itself keeps going (by microns) after it.
|
|
168
|
+
* Infinity for an undamped spring, which never does */
|
|
169
|
+
/** @param {SpringOpts} [opts] @param {number} [tol] */
|
|
170
|
+
export function springSettle(opts = {}, tol = 0.01) {
|
|
171
|
+
const { from = 0, to = 1, velocity = 0 } = opts;
|
|
172
|
+
const { w0, zeta } = springParams(opts);
|
|
173
|
+
const span = Math.abs(from - to) || Math.abs(velocity) / w0 || 1;
|
|
174
|
+
// the envelope decays at ζω0 (swinging) or the slow root (creeping); with
|
|
175
|
+
// no damping it never settles. Look as far as the envelope needs.
|
|
176
|
+
const rate = zeta < 1 ? zeta * w0 : w0 * (zeta - Math.sqrt(zeta * zeta - 1));
|
|
177
|
+
if (!(rate > 0)) return Infinity;
|
|
178
|
+
const horizon = (Math.log(1 / tol) + 8) / rate + 8 / w0;
|
|
179
|
+
let last = 0;
|
|
180
|
+
const step = horizon / 30000;
|
|
181
|
+
for (let tau = 0; tau < horizon; tau += step) {
|
|
182
|
+
const [x, v] = springState(tau, from - to, velocity, w0, zeta);
|
|
183
|
+
if (Math.abs(x) > tol * span || Math.abs(v) / w0 > tol * span) last = tau;
|
|
184
|
+
else if (tau - last > 4 / w0) break; // quiet for well over a swing
|
|
185
|
+
}
|
|
186
|
+
return last;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** a spring step squeezed to land exactly at p = 1: an ease for a move that
|
|
190
|
+
* must end on time (a handoff) and still land like a spring. ζ 1 by default:
|
|
191
|
+
* no swing; a lower damping (or `overshoot`) swings past and back */
|
|
192
|
+
/** @param {number} p @param {{ damping?: number, overshoot?: number }} [opts] */
|
|
193
|
+
export function settle(p, opts = {}) {
|
|
194
|
+
if (p <= 0) return 0;
|
|
195
|
+
if (p >= 1) return 1;
|
|
196
|
+
const zeta = opts.overshoot != null ? dampingFor(opts.overshoot) : (opts.damping ?? 1);
|
|
197
|
+
// ω0 = 1, run to the 0.1 % band (ln 1000 over the slowest decay rate: ζ
|
|
198
|
+
// when it swings, the slow root ζ − √(ζ² − 1) when it creeps), then
|
|
199
|
+
// normalised so the end value is exactly 1 — no snap. A swinging one is
|
|
200
|
+
// run on to the next turn past the rest: it ends at rest there (no speed
|
|
201
|
+
// to snap off) and the end value is ≥ 1, so the normalising never divides
|
|
202
|
+
// by ≈ 0, even with next to no damping
|
|
203
|
+
const rate = zeta < 1 ? zeta : zeta - Math.sqrt(zeta * zeta - 1);
|
|
204
|
+
let T = Math.log(1000) / Math.max(0.05, rate);
|
|
205
|
+
if (zeta < 1 - 1e-6) {
|
|
206
|
+
const half = Math.PI / Math.sqrt(1 - zeta * zeta); // its turns are half a period apart
|
|
207
|
+
T = (2 * Math.ceil((T / half - 1) / 2) + 1) * half; // the first odd turn at or after T
|
|
208
|
+
}
|
|
209
|
+
const end = 1 + springState(T, -1, 0, 1, zeta)[0];
|
|
210
|
+
return (1 + springState(p * T, -1, 0, 1, zeta)[0]) / end;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// --- entrances and exits ----------------------------------------------------
|
|
214
|
+
|
|
215
|
+
// The opacity finishes once 60 % of the travel is covered: the element is
|
|
216
|
+
// solid while it is still visibly moving, so the eye reads a landing; an
|
|
217
|
+
// opacity that runs as long as the travel reads as a fade with a drift.
|
|
218
|
+
const LEAD = 0.6;
|
|
219
|
+
// An exit lasts 0.32 s: about half an entrance's settle (0.57 s) — leaving
|
|
220
|
+
// should not hold the eye — and still ≈ 19 frames, so it is seen to go.
|
|
221
|
+
const EXIT_DUR = 0.32;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Rises (or slides) into place: the position springs from an offset to 0 and
|
|
225
|
+
* the opacity, tied to the travel, arrives first.
|
|
226
|
+
* @param {number} t
|
|
227
|
+
* @param {number} start
|
|
228
|
+
* @param {SpringOpts & { dx?: number, dy?: number, lead?: number }} [opts]
|
|
229
|
+
* dx/dy: the offset it comes from (default 0, 24 px below — about one line
|
|
230
|
+
* of 20 px text, far enough to read as a move); lead: the share of the travel
|
|
231
|
+
* by which it is solid (default 0.6)
|
|
232
|
+
*/
|
|
233
|
+
export function rise(t, start, opts = {}) {
|
|
234
|
+
const { dx = 0, dy = 24, lead = LEAD } = opts;
|
|
235
|
+
const p = spring(t, { ...opts, from: 0, to: 1, start });
|
|
236
|
+
return { x: dx * (1 - p), y: dy * (1 - p), opacity: easeOut(p / lead), p };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Leaves the way a rise came, continued: it accelerates away and the opacity
|
|
241
|
+
* trails the position (the reverse of a rise: it moves, then goes).
|
|
242
|
+
* @param {number} t
|
|
243
|
+
* @param {number} start
|
|
244
|
+
* @param {{ dx?: number, dy?: number, dur?: number, lead?: number }} [opts] dy default −24 (up and out)
|
|
245
|
+
*/
|
|
246
|
+
export function riseOut(t, start, opts = {}) {
|
|
247
|
+
const { dx = 0, dy = -24, dur = EXIT_DUR, lead = LEAD } = opts;
|
|
248
|
+
const p = easeIn((t - start) / dur);
|
|
249
|
+
return { x: dx * p, y: dy * p, opacity: 1 - smooth((p - (1 - lead)) / lead), p };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Pops into being: the scale springs up past full size and back.
|
|
254
|
+
* @param {number} t
|
|
255
|
+
* @param {number} start
|
|
256
|
+
* @param {SpringOpts & { scale?: number }} [opts] scale: where it starts
|
|
257
|
+
* (default 0.6 — big enough that a 40 % growth reads as a pop, not a zoom);
|
|
258
|
+
* default overshoot 0.12 (≈ 5 % past full size at 0.6 → 1, visible)
|
|
259
|
+
*/
|
|
260
|
+
export function pop(t, start, opts = {}) {
|
|
261
|
+
const { scale: s0 = 0.6 } = opts;
|
|
262
|
+
const o = opts.damping == null && opts.friction == null ? { overshoot: 0.12, ...opts } : opts;
|
|
263
|
+
const p = spring(t, { freq: 2.2, ...o, from: 0, to: 1, start });
|
|
264
|
+
// the pop is a quick event; its opacity is done by a third of the growth
|
|
265
|
+
return { scale: lerp(s0, 1, p), opacity: easeOut(p / 0.35), p };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Pops out: a small swell (anticipation), then it shrinks away.
|
|
270
|
+
* @param {number} t
|
|
271
|
+
* @param {number} start
|
|
272
|
+
* @param {{ scale?: number, dur?: number, swell?: number }} [opts] scale: where it ends (default 0.6);
|
|
273
|
+
* swell: how far past full size it grows first (default 0.04)
|
|
274
|
+
*/
|
|
275
|
+
export function popOut(t, start, opts = {}) {
|
|
276
|
+
const { scale: s1 = 0.6, dur = EXIT_DUR * 0.8, swell = 0.04 } = opts;
|
|
277
|
+
const u = clamp01((t - start) / dur);
|
|
278
|
+
// the first quarter swells, the rest shrinks from the swell, accelerating
|
|
279
|
+
const scale =
|
|
280
|
+
u < 0.25 ? 1 + swell * smooth(u / 0.25) : lerp(1 + swell, s1, easeIn((u - 0.25) / 0.75));
|
|
281
|
+
return { scale, opacity: 1 - smooth((u - 0.4) / 0.6), p: u };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Drops in from above and lands: it stretches along its fall with its speed
|
|
286
|
+
* and squashes as it sinks into the landing, keeping its area (sx = 1 / sy).
|
|
287
|
+
* @param {number} t
|
|
288
|
+
* @param {number} start
|
|
289
|
+
* @param {SpringOpts & { height?: number, squash?: number }} [opts] height:
|
|
290
|
+
* how far above it starts (default 120 px); squash: how much it deforms at
|
|
291
|
+
* full speed and at the bottom of the landing (default 0.18 — a cartoon's is ≈ 0.3–0.5, text
|
|
292
|
+
* stays legible to ≈ 0.2); default damping 0.45 (one clear bounce)
|
|
293
|
+
*/
|
|
294
|
+
export function drop(t, start, opts = {}) {
|
|
295
|
+
const { height = 120, squash = 0.18 } = opts;
|
|
296
|
+
const o = { freq: 1.8, damping: 0.45, ...opts, from: -height, to: 0, start };
|
|
297
|
+
const { w0, zeta } = springParams(o);
|
|
298
|
+
const [x, v] = springState(t - start, -height, 0, w0, zeta);
|
|
299
|
+
// Stretch: the speed against ω0·h (the most a spring released from h can
|
|
300
|
+
// reach). Squash: how far it is pressed past its rest (into the landing),
|
|
301
|
+
// against how deep the first swing goes — 1 at the bottom. Both are
|
|
302
|
+
// shares, so the look does not depend on the drop's size or frequency; the
|
|
303
|
+
// squash is 0 at the rest and follows the depth both ways, so the shape
|
|
304
|
+
// never jumps. An overdamped drop never passes its rest: no squash.
|
|
305
|
+
const h = Math.max(1e-9, Math.abs(height));
|
|
306
|
+
const stretch = Math.abs(v) / (w0 * h);
|
|
307
|
+
const os = zeta < 1 ? Math.exp((-zeta * Math.PI) / Math.sqrt(1 - zeta * zeta)) : 0;
|
|
308
|
+
const past = height >= 0 ? x : -x; // > 0 beyond the rest, on the far side from where it fell
|
|
309
|
+
const braking = os > 0 && past > 0 ? past / (os * h) : 0;
|
|
310
|
+
const sy = Math.max(0.2, 1 + squash * (stretch - braking));
|
|
311
|
+
const started = t >= start;
|
|
312
|
+
return {
|
|
313
|
+
y: x,
|
|
314
|
+
sx: 1 / sy,
|
|
315
|
+
sy,
|
|
316
|
+
opacity: started ? clamp01(Math.abs(x + height) / (h * 0.15)) : 0,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Falls away below, stretching as it speeds up.
|
|
322
|
+
* @param {number} t
|
|
323
|
+
* @param {number} start
|
|
324
|
+
* @param {{ depth?: number, dur?: number, squash?: number }} [opts] depth: how far it falls (default 160 px)
|
|
325
|
+
*/
|
|
326
|
+
export function dropOut(t, start, opts = {}) {
|
|
327
|
+
const { depth = 160, dur = EXIT_DUR * 1.25, squash = 0.18 } = opts;
|
|
328
|
+
const u = clamp01((t - start) / dur);
|
|
329
|
+
const p = u * u; // falling: constant acceleration
|
|
330
|
+
const sy = 1 + squash * u; // speed grows linearly with time
|
|
331
|
+
return { y: depth * p, sx: 1 / sy, sy, opacity: 1 - smooth((u - 0.5) / 0.5) };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Rises out of its own line: it slides up from below a mask the size of its
|
|
336
|
+
* own box, so it needs no wrapper. Put `y` in a translate and `clip` in
|
|
337
|
+
* clip-path on the same element.
|
|
338
|
+
* @param {number} t
|
|
339
|
+
* @param {number} start
|
|
340
|
+
* @param {SpringOpts & { height: number }} opts height: the element's own height (px).
|
|
341
|
+
* Default damping 0.85: a swing past the line would be cut by the mask's top
|
|
342
|
+
*/
|
|
343
|
+
export function maskRise(t, start, opts) {
|
|
344
|
+
const { height } = opts;
|
|
345
|
+
const p = spring(t, { damping: 0.85, freq: 1.5, ...opts, from: 0, to: 1, start });
|
|
346
|
+
const y = height * (1 - p);
|
|
347
|
+
// translated down by y, the part of the element still inside its own box is its top h − y
|
|
348
|
+
return { y, clip: `inset(0px 0px ${Math.max(0, Math.min(height, y)).toFixed(2)}px 0px)`, p };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Sinks back into its line: the reverse of maskRise (or, with `up`, leaves through the top).
|
|
353
|
+
* @param {number} t
|
|
354
|
+
* @param {number} start
|
|
355
|
+
* @param {{ height: number, dur?: number, up?: boolean }} opts
|
|
356
|
+
*/
|
|
357
|
+
export function maskOut(t, start, opts) {
|
|
358
|
+
const { height, dur = EXIT_DUR, up = false } = opts;
|
|
359
|
+
const p = easeIn((t - start) / dur);
|
|
360
|
+
const d = Math.min(height, height * p);
|
|
361
|
+
return up
|
|
362
|
+
? { y: -d, clip: `inset(${d.toFixed(2)}px 0px 0px 0px)`, p }
|
|
363
|
+
: { y: d, clip: `inset(0px 0px ${d.toFixed(2)}px 0px)`, p };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Typing: 16 characters a second by default. A fast real typist is ≈ 8 cps
|
|
367
|
+
// (100 wpm); a film shows the typing, not the typist, so it runs at about
|
|
368
|
+
// twice that — a 40-character query takes 2.5 s, a sentence a viewer can
|
|
369
|
+
// follow being written.
|
|
370
|
+
const TYPE_CPS = 16;
|
|
371
|
+
|
|
372
|
+
/** a small seeded random generator (mulberry32), so a cadence is the same on every frame */
|
|
373
|
+
/** @param {number} seed */
|
|
374
|
+
function rng(seed) {
|
|
375
|
+
let a = seed >>> 0;
|
|
376
|
+
return () => {
|
|
377
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
378
|
+
let x = a;
|
|
379
|
+
x = Math.imul(x ^ (x >>> 15), x | 1);
|
|
380
|
+
x ^= x + Math.imul(x ^ (x >>> 7), x | 61);
|
|
381
|
+
return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** @type {Map<string, number[]>} */
|
|
386
|
+
const typeCache = new Map();
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* When each character of `text` appears (s, absolute), typed from `start`.
|
|
390
|
+
* Also the times to put key sounds on. With `human`, the gaps vary: a word
|
|
391
|
+
* boundary or punctuation takes longer, gaps inside a word are uneven
|
|
392
|
+
* (a seeded, so repeatable, spread); the average rate stays `cps`.
|
|
393
|
+
* @param {string} text
|
|
394
|
+
* @param {number} start
|
|
395
|
+
* @param {{ cps?: number, human?: boolean, seed?: number }} [opts]
|
|
396
|
+
*/
|
|
397
|
+
export function typeTimes(text, start, opts = {}) {
|
|
398
|
+
const { cps = TYPE_CPS, human = false, seed = 1 } = opts;
|
|
399
|
+
if (!(cps > 0)) throw new Error(`type: cps must be above 0 (${cps})`);
|
|
400
|
+
const chars = Array.from(text);
|
|
401
|
+
const key = `${text}\u0000${cps}\u0000${human}\u0000${seed}`;
|
|
402
|
+
let gaps = typeCache.get(key);
|
|
403
|
+
if (!gaps) {
|
|
404
|
+
const rand = rng(seed);
|
|
405
|
+
gaps = chars.map((ch, i) => {
|
|
406
|
+
if (!human || i === 0) return 1;
|
|
407
|
+
const prev = /** @type {string} */ (chars[i - 1]);
|
|
408
|
+
// a lognormal-ish spread inside a word (×0.6–1.5), longer after a word
|
|
409
|
+
// (×1.8) and after punctuation (×2.6): where a typist's hand pauses
|
|
410
|
+
let g = Math.exp((rand() + rand() - 1) * 0.45);
|
|
411
|
+
if (prev === " ") g *= 1.8;
|
|
412
|
+
else if (/[.,;:!?]/.test(prev)) g *= 2.6;
|
|
413
|
+
else if (ch === " ") g *= 0.85;
|
|
414
|
+
return g;
|
|
415
|
+
});
|
|
416
|
+
// normalise the gaps after the first key to the asked rate
|
|
417
|
+
const rest = gaps.slice(1).reduce((s, g) => s + g, 0);
|
|
418
|
+
const scale = rest > 0 ? (gaps.length - 1) / rest : 1;
|
|
419
|
+
gaps = gaps.map((g, i) => (i === 0 ? 0 : g * scale));
|
|
420
|
+
if (typeCache.size > 256) typeCache.clear();
|
|
421
|
+
typeCache.set(key, gaps);
|
|
422
|
+
}
|
|
423
|
+
/** @type {number[]} */
|
|
424
|
+
const times = [];
|
|
425
|
+
let at = start;
|
|
426
|
+
for (const g of gaps) {
|
|
427
|
+
at += g / cps;
|
|
428
|
+
times.push(at);
|
|
429
|
+
}
|
|
430
|
+
return times;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Text typed over time: `text` is what shows at t. The first character
|
|
435
|
+
* appears at `start`.
|
|
436
|
+
* @param {number} t
|
|
437
|
+
* @param {number} start
|
|
438
|
+
* @param {string} text
|
|
439
|
+
* @param {{ cps?: number, human?: boolean, seed?: number }} [opts]
|
|
440
|
+
*/
|
|
441
|
+
export function type(t, start, text, opts = {}) {
|
|
442
|
+
const times = typeTimes(text, start, opts);
|
|
443
|
+
let n = 0;
|
|
444
|
+
while (n < times.length && /** @type {number} */ (times[n]) <= t + 1e-9) n++;
|
|
445
|
+
const chars = Array.from(text);
|
|
446
|
+
return {
|
|
447
|
+
text: chars.slice(0, n).join(""),
|
|
448
|
+
count: n,
|
|
449
|
+
done: n === chars.length,
|
|
450
|
+
/** when the last shown character appeared (s), for a caret's blink or a key sound */
|
|
451
|
+
lastAt: n > 0 ? /** @type {number} */ (times[n - 1]) : null,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Text deleted from the end, as a held backspace does: steady and faster
|
|
457
|
+
* than typing (default 30 cps — a key's auto-repeat rate).
|
|
458
|
+
* @param {number} t
|
|
459
|
+
* @param {number} start
|
|
460
|
+
* @param {string} text
|
|
461
|
+
* @param {{ cps?: number }} [opts]
|
|
462
|
+
*/
|
|
463
|
+
export function untype(t, start, text, opts = {}) {
|
|
464
|
+
const { cps = 30 } = opts;
|
|
465
|
+
const chars = Array.from(text);
|
|
466
|
+
const gone = t < start ? 0 : Math.min(chars.length, Math.floor((t - start) * cps + 1e-9) + 1);
|
|
467
|
+
return {
|
|
468
|
+
text: chars.slice(0, chars.length - gone).join(""),
|
|
469
|
+
count: chars.length - gone,
|
|
470
|
+
done: gone === chars.length,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// --- handoffs ---------------------------------------------------------------
|
|
475
|
+
|
|
476
|
+
/** progress through a window [start, end] through an ease */
|
|
477
|
+
/** @param {number} t @param {number} start @param {number} end @param {(p: number) => number} ease */
|
|
478
|
+
const windowP = (t, start, end, ease) =>
|
|
479
|
+
end > start ? ease(clamp01((t - start) / (end - start))) : t >= end ? 1 : 0;
|
|
480
|
+
|
|
481
|
+
/** the default handoff ease: a critically damped spring landing exactly on `end` */
|
|
482
|
+
/** @param {number} p */
|
|
483
|
+
const handoffEase = (p) => settle(p);
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* One box grows (or shrinks) into another between `start` and `end`: a
|
|
487
|
+
* button becoming the page, a card becoming a panel. The sizes change in
|
|
488
|
+
* log space (a 10× growth looks steady instead of fast-then-slow), the
|
|
489
|
+
* centre and the corner radius change with the same progress.
|
|
490
|
+
* @param {number} t
|
|
491
|
+
* @param {number} start
|
|
492
|
+
* @param {number} end
|
|
493
|
+
* @param {RectLike} from
|
|
494
|
+
* @param {RectLike} to
|
|
495
|
+
* @param {{ ease?: (p: number) => number, log?: boolean }} [opts] log: false for linear sizes
|
|
496
|
+
* @returns {Rect & { p: number }}
|
|
497
|
+
*/
|
|
498
|
+
export function morphRect(t, start, end, from, to, opts = {}) {
|
|
499
|
+
const { ease = handoffEase, log = true } = opts;
|
|
500
|
+
const a = rectOf(from);
|
|
501
|
+
const b = rectOf(to);
|
|
502
|
+
const p = windowP(t, start, end, ease);
|
|
503
|
+
const size = /** @param {number} u @param {number} v */ (u, v) =>
|
|
504
|
+
log && u > 0 && v > 0 ? u * (v / u) ** p : lerp(u, v, p);
|
|
505
|
+
const w = size(a.w, b.w);
|
|
506
|
+
const h = size(a.h, b.h);
|
|
507
|
+
const cx = lerp(a.x + a.w / 2, b.x + b.w / 2, p);
|
|
508
|
+
const cy = lerp(a.y + a.h / 2, b.y + b.h / 2, p);
|
|
509
|
+
return { x: cx - w / 2, y: cy - h / 2, w, h, r: lerp(a.r, b.r, p), p };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* A rect as a CSS clip-path for an element whose box is `frame` (its own
|
|
514
|
+
* size, or, with x/y, where it sits in the rect's coordinates).
|
|
515
|
+
* @param {RectLike} rect
|
|
516
|
+
* @param {Frame} frame
|
|
517
|
+
*/
|
|
518
|
+
export function rectClip(rect, frame) {
|
|
519
|
+
const i = inset(rect, frame);
|
|
520
|
+
const px = /** @param {number} v */ (v) => `${v.toFixed(2)}px`;
|
|
521
|
+
const round = i.r > 0 ? ` round ${px(i.r)}` : "";
|
|
522
|
+
return `inset(${px(i.top)} ${px(i.right)} ${px(i.bottom)} ${px(i.left)}${round})`;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* How far a rect sits inside a frame on each side (px) — the numbers of a
|
|
527
|
+
* CSS inset(), for a canvas or anything else.
|
|
528
|
+
* @param {RectLike} rect
|
|
529
|
+
* @param {Frame} frame
|
|
530
|
+
*/
|
|
531
|
+
export function inset(rect, frame) {
|
|
532
|
+
const r = rectOf(rect);
|
|
533
|
+
const f = rectOf({ x: 0, y: 0, ...frame });
|
|
534
|
+
const fx = f.x;
|
|
535
|
+
const fy = f.y;
|
|
536
|
+
return {
|
|
537
|
+
top: r.y - fy,
|
|
538
|
+
right: fx + f.w - (r.x + r.w),
|
|
539
|
+
bottom: fy + f.h - (r.y + r.h),
|
|
540
|
+
left: r.x - fx,
|
|
541
|
+
r: r.r,
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* A circle opening from a point until it covers the frame: the radius
|
|
547
|
+
* reaches the farthest corner, so at `end` nothing of the frame is left out.
|
|
548
|
+
* Ease is on the radius, smootherstep by default: the area (∝ r²) then
|
|
549
|
+
* opens slowly, grows fast through the middle and arrives without a snap.
|
|
550
|
+
* @param {number} t
|
|
551
|
+
* @param {number} start
|
|
552
|
+
* @param {number} end
|
|
553
|
+
* @param {Pt} at the centre
|
|
554
|
+
* @param {{ width: number, height: number }} frame
|
|
555
|
+
* @param {{ ease?: (p: number) => number, from?: number, to?: number, close?: boolean }} [opts]
|
|
556
|
+
* from/to: radii instead of 0 → covering; close: run it backwards (covering → 0)
|
|
557
|
+
*/
|
|
558
|
+
export function iris(t, start, end, at, frame, opts = {}) {
|
|
559
|
+
const { ease = smooth, close = false } = opts;
|
|
560
|
+
const cover = Math.max(
|
|
561
|
+
Math.hypot(at.x, at.y),
|
|
562
|
+
Math.hypot(frame.width - at.x, at.y),
|
|
563
|
+
Math.hypot(at.x, frame.height - at.y),
|
|
564
|
+
Math.hypot(frame.width - at.x, frame.height - at.y),
|
|
565
|
+
);
|
|
566
|
+
const r0 = opts.from ?? 0;
|
|
567
|
+
const r1 = opts.to ?? cover;
|
|
568
|
+
const p = windowP(t, start, end, ease);
|
|
569
|
+
const r = close ? lerp(r1, r0, p) : lerp(r0, r1, p);
|
|
570
|
+
return { r, clip: `circle(${r.toFixed(2)}px at ${at.x.toFixed(2)}px ${at.y.toFixed(2)}px)`, p };
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* The picture flies into a rect until it fills the view: one scale about a
|
|
575
|
+
* fixed point, changing in log space, so every step of the zoom looks the
|
|
576
|
+
* same size — a straight flight in, not a lurch then a crawl. Put
|
|
577
|
+
* `transform` on the layer (transform-origin 0 0) whose coordinates `rect` is in.
|
|
578
|
+
* @param {number} t
|
|
579
|
+
* @param {number} start
|
|
580
|
+
* @param {number} end
|
|
581
|
+
* @param {RectLike} rect what to fly into
|
|
582
|
+
* @param {{ width: number, height: number }} view the picture
|
|
583
|
+
* @param {{ ease?: (p: number) => number, fit?: "cover" | "contain", through?: number }} [opts]
|
|
584
|
+
* fit: the rect covers the view at the end (default — what a handoff into it
|
|
585
|
+
* needs) or fits inside it; through: keep zooming past it by this factor
|
|
586
|
+
*/
|
|
587
|
+
export function zoomThrough(t, start, end, rect, view, opts = {}) {
|
|
588
|
+
const { ease = smooth, fit = "cover", through = 1 } = opts;
|
|
589
|
+
const r = rectOf(rect);
|
|
590
|
+
const sx = view.width / Math.max(1e-9, r.w);
|
|
591
|
+
const sy = view.height / Math.max(1e-9, r.h);
|
|
592
|
+
const S = (fit === "cover" ? Math.max(sx, sy) : Math.min(sx, sy)) * through;
|
|
593
|
+
// the end transform maps the rect's centre c to the view's centre v:
|
|
594
|
+
// x' = S·(x − c) + v. A pure scale about f that ends there needs
|
|
595
|
+
// f = S·(f − c) + v, so f = (S·c − v) / (S − 1).
|
|
596
|
+
const c = { x: r.x + r.w / 2, y: r.y + r.h / 2 };
|
|
597
|
+
const v = { x: view.width / 2, y: view.height / 2 };
|
|
598
|
+
const p = windowP(t, start, end, ease);
|
|
599
|
+
const scale = S ** p;
|
|
600
|
+
/** @type {Pt} */
|
|
601
|
+
let f;
|
|
602
|
+
let tx;
|
|
603
|
+
let ty;
|
|
604
|
+
if (Math.abs(S - 1) < 1e-9) {
|
|
605
|
+
// no zoom: a slide from c to v
|
|
606
|
+
f = c;
|
|
607
|
+
tx = (v.x - c.x) * p;
|
|
608
|
+
ty = (v.y - c.y) * p;
|
|
609
|
+
} else {
|
|
610
|
+
f = { x: (S * c.x - v.x) / (S - 1), y: (S * c.y - v.y) / (S - 1) };
|
|
611
|
+
tx = (1 - scale) * f.x;
|
|
612
|
+
ty = (1 - scale) * f.y;
|
|
613
|
+
}
|
|
614
|
+
return {
|
|
615
|
+
scale,
|
|
616
|
+
x: tx,
|
|
617
|
+
y: ty,
|
|
618
|
+
origin: f,
|
|
619
|
+
transform: `translate(${tx.toFixed(3)}px, ${ty.toFixed(3)}px) scale(${scale.toFixed(5)})`,
|
|
620
|
+
p,
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Items converge on one place, one after another: a ripple of arrivals.
|
|
626
|
+
* Each leaves `stagger` s after the one before (in `order`) and travels
|
|
627
|
+
* `dur` s; with `scale`, it ends at that size.
|
|
628
|
+
* @param {number} t
|
|
629
|
+
* @param {number} start
|
|
630
|
+
* @param {Pt[]} from where each item starts
|
|
631
|
+
* @param {Pt} to where they gather
|
|
632
|
+
* @param {{ dur?: number, stagger?: number, ease?: (p: number) => number, scale?: number, order?: "index" | "far" | "near" }} [opts]
|
|
633
|
+
* stagger default 0.04 s: under a frame apart they arrive as one block, over
|
|
634
|
+
* ≈ 0.1 s they read as a queue; 0.04 (2–3 frames) reads as a ripple.
|
|
635
|
+
* dur default 0.5 s; ease default smootherstep; order: by index, farthest first, or nearest first
|
|
636
|
+
*/
|
|
637
|
+
export function gather(t, start, from, to, opts = {}) {
|
|
638
|
+
const { dur = 0.5, stagger = 0.04, ease = smooth, scale = 1, order = "index" } = opts;
|
|
639
|
+
const rank = from.map((_, i) => i);
|
|
640
|
+
if (order !== "index") {
|
|
641
|
+
const d = from.map((q) => Math.hypot(q.x - to.x, q.y - to.y));
|
|
642
|
+
rank.sort((i, j) => (order === "far" ? (d[j] ?? 0) - (d[i] ?? 0) : (d[i] ?? 0) - (d[j] ?? 0)));
|
|
643
|
+
}
|
|
644
|
+
/** @type {number[]} */
|
|
645
|
+
const slot = new Array(from.length).fill(0);
|
|
646
|
+
rank.forEach((i, k) => {
|
|
647
|
+
slot[i] = k;
|
|
648
|
+
});
|
|
649
|
+
return from.map((q, i) => {
|
|
650
|
+
const p = ease(clamp01((t - start - (slot[i] ?? 0) * stagger) / dur));
|
|
651
|
+
return { x: lerp(q.x, to.x, p), y: lerp(q.y, to.y, p), scale: lerp(1, scale, p), p };
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Hops from one point to another along an arc (a parabola — the path of a
|
|
657
|
+
* thrown thing), `height` px above the straight line at its middle.
|
|
658
|
+
* @param {number} t
|
|
659
|
+
* @param {number} start
|
|
660
|
+
* @param {number} end
|
|
661
|
+
* @param {Pt} from
|
|
662
|
+
* @param {Pt} to
|
|
663
|
+
* @param {{ height?: number, ease?: (p: number) => number }} [opts] default height 80 px; ease: along the path (default smootherstep)
|
|
664
|
+
*/
|
|
665
|
+
export function hop(t, start, end, from, to, opts = {}) {
|
|
666
|
+
const { height = 80, ease = smooth } = opts;
|
|
667
|
+
const p = windowP(t, start, end, ease);
|
|
668
|
+
return {
|
|
669
|
+
x: lerp(from.x, to.x, p),
|
|
670
|
+
// 4h·p(1−p) peaks at h in the middle
|
|
671
|
+
y: lerp(from.y, to.y, p) - 4 * height * p * (1 - p),
|
|
672
|
+
p,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
// --- contact ----------------------------------------------------------------
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* A button pressed at `at`: it sinks fast and comes back on a spring with a
|
|
680
|
+
* small swing. Use `scale` (about its centre) and/or `y`.
|
|
681
|
+
* @param {number} t
|
|
682
|
+
* @param {number} at when it is pressed (s)
|
|
683
|
+
* @param {{ depth?: number, sink?: number, hold?: number, freq?: number, damping?: number, drop?: number }} [opts]
|
|
684
|
+
* depth: how much smaller it gets (default 0.06 — a 200 px button loses
|
|
685
|
+
* 12 px, plainly seen; most interfaces use 0.02–0.05, a film reads smaller);
|
|
686
|
+
* sink: how long going down takes (default 0.07 s ≈ 4 frames, a click's
|
|
687
|
+
* travel); hold: how long it stays down before release (default 0.05 s);
|
|
688
|
+
* drop: how many px it moves down at the bottom (default 2)
|
|
689
|
+
*/
|
|
690
|
+
export function press(t, at, opts = {}) {
|
|
691
|
+
const { depth = 0.06, sink = 0.07, hold = 0.05, freq = 3, damping = 0.5, drop = 2 } = opts;
|
|
692
|
+
const up = at + sink + hold;
|
|
693
|
+
let d; // 0 at rest, 1 fully down
|
|
694
|
+
if (t < at) d = 0;
|
|
695
|
+
else if (t < up) d = easeOut((t - at) / sink);
|
|
696
|
+
else d = spring(t, { from: 1, to: 0, start: up, freq, damping });
|
|
697
|
+
return { scale: 1 - depth * d, y: drop * d, down: d };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* A hit at `at`: a kick that rings down — an offset along `angle` and a
|
|
702
|
+
* scale bump, both shaped as a struck spring (0 at the hit, out to `amount`,
|
|
703
|
+
* back through 0, smaller each time).
|
|
704
|
+
* @param {number} t
|
|
705
|
+
* @param {number} at
|
|
706
|
+
* @param {{ amount?: number, angle?: number, scale?: number, freq?: number, damping?: number }} [opts]
|
|
707
|
+
* amount: the first swing (default 10 px); angle: its direction in degrees
|
|
708
|
+
* (default 90, down — what is hit gives way); scale: the scale bump at the
|
|
709
|
+
* first swing (default 0.03); freq default 7 Hz and damping 0.35: the first
|
|
710
|
+
* swing peaks ≈ 30 ms after the hit (2 frames — instant to the eye) and
|
|
711
|
+
* the ring is under 5 % of that swing in ≈ 0.23 s, so it reads as one hit, not a wobble
|
|
712
|
+
*/
|
|
713
|
+
export function impact(t, at, opts = {}) {
|
|
714
|
+
const { amount = 10, angle = 90, scale = 0.03, freq = 7, damping = 0.35 } = opts;
|
|
715
|
+
const w0 = 2 * Math.PI * freq;
|
|
716
|
+
const zeta = Math.min(damping, 0.999);
|
|
717
|
+
const wd = w0 * Math.sqrt(1 - zeta * zeta);
|
|
718
|
+
// the impulse response e^(−ζω0τ)·sin(ωdτ), normalised by its first peak
|
|
719
|
+
// (at τp = atan(ωd / ζω0) / ωd) so `amount` is exactly the first swing
|
|
720
|
+
const tp = Math.atan2(wd, zeta * w0) / wd;
|
|
721
|
+
const peak = Math.exp(-zeta * w0 * tp) * Math.sin(wd * tp);
|
|
722
|
+
const tau = t - at;
|
|
723
|
+
const k = tau <= 0 ? 0 : (Math.exp(-zeta * w0 * tau) * Math.sin(wd * tau)) / peak;
|
|
724
|
+
const rad = (angle * Math.PI) / 180;
|
|
725
|
+
return { x: amount * k * Math.cos(rad), y: amount * k * Math.sin(rad), scale: 1 + scale * k, k };
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// --- numbers and parts ------------------------------------------------------
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* @typedef {object} NumberFormat how a number is written: what comes before
|
|
732
|
+
* and after it, the mark between thousands ("" for none) and before the
|
|
733
|
+
* decimals, how many decimals
|
|
734
|
+
* @property {string} prefix e.g. "$"
|
|
735
|
+
* @property {string} suffix e.g. "%", "+", " 萬"
|
|
736
|
+
* @property {string} sep e.g. ",", ".", " ", ""
|
|
737
|
+
* @property {string} point e.g. ".", ","
|
|
738
|
+
* @property {number} decimals
|
|
739
|
+
* @property {string} [minus] the sign a negative number is written with ("-", "−")
|
|
740
|
+
*/
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* A number as written, read back: "$4,466" → 4466 with prefix "$" and
|
|
744
|
+
* separator ","; "12.5%" → 12.5, one decimal, suffix "%"; "1.234,56 €" →
|
|
745
|
+
* 1234.56. One mark that occurs once with three digits after it is a
|
|
746
|
+
* thousands separator if it is "," (or a space or an apostrophe), a decimal
|
|
747
|
+
* point if it is "." — say `sep` / `point` when the product writes it the
|
|
748
|
+
* other way. A plain number has no prefix, suffix or separator.
|
|
749
|
+
* @param {string | number} s
|
|
750
|
+
* @param {Partial<NumberFormat>} [o] what to take as given instead of reading it
|
|
751
|
+
* @returns {{ value: number } & NumberFormat}
|
|
752
|
+
*/
|
|
753
|
+
export function readNumber(s, o = {}) {
|
|
754
|
+
if (typeof s === "number") {
|
|
755
|
+
if (!Number.isFinite(s)) throw new Error(`count: ${s} is not a number`);
|
|
756
|
+
const dec = String(s).split(".")[1]?.length ?? 0;
|
|
757
|
+
return {
|
|
758
|
+
value: s,
|
|
759
|
+
prefix: o.prefix ?? "",
|
|
760
|
+
suffix: o.suffix ?? "",
|
|
761
|
+
sep: o.sep ?? "",
|
|
762
|
+
point: o.point ?? ".",
|
|
763
|
+
decimals: o.decimals ?? dec,
|
|
764
|
+
minus: o.minus ?? "-",
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
const m = /^(.*?)([-−]?)(\d(?:[\d.,'’ ]*\d)?)(.*)$/su.exec(String(s));
|
|
768
|
+
if (!m) throw new Error(`count: "${s}" has no number in it`);
|
|
769
|
+
const [, before = "", minus = "", body = "", after = ""] = m;
|
|
770
|
+
const marks = [...new Set(body.replace(/\d/g, ""))];
|
|
771
|
+
let sep = "";
|
|
772
|
+
let point = ".";
|
|
773
|
+
if (o.sep != null || o.point != null) {
|
|
774
|
+
sep = o.sep ?? "";
|
|
775
|
+
point = o.point ?? (sep === "." ? "," : ".");
|
|
776
|
+
} else if (marks.length >= 2) {
|
|
777
|
+
// the last mark is the decimal point, the other the thousands
|
|
778
|
+
point = /** @type {string} */ (body.replace(/\d/g, "").slice(-1));
|
|
779
|
+
sep = /** @type {string} */ (marks.find((c) => c !== point));
|
|
780
|
+
} else if (marks.length === 1) {
|
|
781
|
+
const c = /** @type {string} */ (marks[0]);
|
|
782
|
+
const groups = body.split(c);
|
|
783
|
+
const last = groups[groups.length - 1] ?? "";
|
|
784
|
+
if (groups.length > 2 || (c !== "." && c !== ",")) sep = c;
|
|
785
|
+
else if (c === ".") point = c;
|
|
786
|
+
else if (last.length === 3) sep = c;
|
|
787
|
+
else point = c;
|
|
788
|
+
}
|
|
789
|
+
const esc = (/** @type {string} */ c) => c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
790
|
+
const plain = (sep ? body.replace(new RegExp(esc(sep), "g"), "") : body).replace(point, ".");
|
|
791
|
+
const value = Number(plain) * (minus ? -1 : 1);
|
|
792
|
+
if (!Number.isFinite(value)) throw new Error(`count: "${s}" does not read as a number`);
|
|
793
|
+
const dec = plain.includes(".") ? (plain.split(".")[1]?.length ?? 0) : 0;
|
|
794
|
+
return {
|
|
795
|
+
value,
|
|
796
|
+
prefix: o.prefix ?? before,
|
|
797
|
+
suffix: o.suffix ?? after,
|
|
798
|
+
sep,
|
|
799
|
+
point,
|
|
800
|
+
decimals: o.decimals ?? dec,
|
|
801
|
+
minus: o.minus ?? (minus || "-"),
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* A number written the way `f` writes them.
|
|
807
|
+
* @param {number} v
|
|
808
|
+
* @param {NumberFormat} f
|
|
809
|
+
*/
|
|
810
|
+
export function writeNumber(v, f) {
|
|
811
|
+
const fixed = Math.abs(v).toFixed(Math.max(0, Math.min(20, f.decimals)));
|
|
812
|
+
const [int = "0", frac] = fixed.split(".");
|
|
813
|
+
const grouped = f.sep ? int.replace(/\B(?=(\d{3})+(?!\d))/g, f.sep) : int;
|
|
814
|
+
const neg = v < 0 && Number(fixed) !== 0 ? (f.minus ?? "-") : "";
|
|
815
|
+
return `${f.prefix}${neg}${grouped}${frac != null ? f.point + frac : ""}${f.suffix}`;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// A count runs 1 s by default: at 60 fps that is 60 frames of numbers
|
|
819
|
+
// changing — enough for the eye to see a run, not a flicker — and it lands
|
|
820
|
+
// on a critically damped spring, so it slows into its last value and never
|
|
821
|
+
// shows a digit past it.
|
|
822
|
+
const COUNT_DUR = 1;
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* A number running from one value to another, written the product's way:
|
|
826
|
+
* the format is read from `to` ("$4,466": prefix "$", "," between
|
|
827
|
+
* thousands, no decimals) — or from `from` for what `to` does not show —
|
|
828
|
+
* and every value along the way is written in it. `from` may be a number
|
|
829
|
+
* (0) or a string as the product wrote it. `steps`: how many values it
|
|
830
|
+
* shows along the way (an odometer feel); `ease`: the run's shape (default:
|
|
831
|
+
* lands without overshoot); `dur` s long (default 1).
|
|
832
|
+
*
|
|
833
|
+
* part.text(count(t, 4, "$4,212", "$4,466").text);
|
|
834
|
+
* @param {number} t
|
|
835
|
+
* @param {number} start
|
|
836
|
+
* @param {string | number} from
|
|
837
|
+
* @param {string | number} to
|
|
838
|
+
* @param {{ dur?: number, ease?: (p: number) => number, steps?: number } & Partial<NumberFormat>} [opts]
|
|
839
|
+
* @returns {{ text: string, value: number, p: number }}
|
|
840
|
+
*/
|
|
841
|
+
export function count(t, start, from, to, opts = {}) {
|
|
842
|
+
const { dur = COUNT_DUR, ease = settle, steps, ...given } = opts;
|
|
843
|
+
if (!(dur > 0)) throw new Error(`count: dur must be above 0 (${dur})`);
|
|
844
|
+
const b = readNumber(to, given);
|
|
845
|
+
const a = readNumber(from, typeof from === "number" ? { ...b, ...given } : given);
|
|
846
|
+
/** @type {NumberFormat} */
|
|
847
|
+
const f = {
|
|
848
|
+
prefix: b.prefix || a.prefix,
|
|
849
|
+
suffix: b.suffix || a.suffix,
|
|
850
|
+
sep: b.sep || a.sep,
|
|
851
|
+
point: b.point,
|
|
852
|
+
decimals: given.decimals ?? Math.max(b.decimals, typeof from === "number" ? 0 : a.decimals),
|
|
853
|
+
minus: b.minus,
|
|
854
|
+
};
|
|
855
|
+
let p = clamp01(ease(clamp01((t - start) / dur)));
|
|
856
|
+
if (steps != null && steps > 0) p = Math.floor(p * steps + 1e-9) / steps;
|
|
857
|
+
const k = 10 ** f.decimals;
|
|
858
|
+
const value = Math.round((a.value + (b.value - a.value) * p) * k) / k;
|
|
859
|
+
return { text: writeNumber(value, f), value, p };
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// --- a line, a figure, landing -----------------------------------------------
|
|
863
|
+
|
|
864
|
+
const GLYPHS = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789#$%&*+?<>/";
|
|
865
|
+
|
|
866
|
+
/** a seeded integer in [0, n) for (a, b, seed): the same every render */
|
|
867
|
+
/** @param {number} a @param {number} b @param {number} seed @param {number} n */
|
|
868
|
+
function pick(a, b, seed, n) {
|
|
869
|
+
let h = Math.imul(a ^ 0x9e3779b9, 0x85ebca6b) ^ Math.imul(b + 0x632be5ab, 0xc2b2ae35) ^ seed;
|
|
870
|
+
h ^= h >>> 15;
|
|
871
|
+
h = Math.imul(h, 0x2c1b3c6d);
|
|
872
|
+
h ^= h >>> 12;
|
|
873
|
+
return (h >>> 0) % n;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* A line resolving out of random glyphs: each character cycles through
|
|
878
|
+
* `glyphs` until its lock time, then holds — left to right (or `from:
|
|
879
|
+
* "right"`), `each` s apart, the first after `lead` s. The glyph shown
|
|
880
|
+
* changes `rate` times a second and is seeded by the character and that
|
|
881
|
+
* step, so any frame renders the same. Spaces stay spaces; characters
|
|
882
|
+
* outside the glyph set's script (Chinese, say) cycle through `glyphs` too
|
|
883
|
+
* unless `keep` says which to leave. Defaults from films measured: the first
|
|
884
|
+
* locks after 0.2 s, then one every 0.05 s, glyphs changing 30 times a second.
|
|
885
|
+
*
|
|
886
|
+
* el.textContent = scramble(t, 3, "$990").text;
|
|
887
|
+
* @param {number} t
|
|
888
|
+
* @param {number} start
|
|
889
|
+
* @param {string} text what it lands on
|
|
890
|
+
* @param {{ each?: number, lead?: number, rate?: number, glyphs?: string, seed?: number,
|
|
891
|
+
* from?: "left" | "right", keep?: RegExp }} [opts]
|
|
892
|
+
* @returns {{ text: string, locked: number, done: boolean }}
|
|
893
|
+
*/
|
|
894
|
+
export function scramble(t, start, text, opts = {}) {
|
|
895
|
+
const { each = 0.05, lead = 0.2, rate = 30, glyphs = GLYPHS, seed = 1, from = "left" } = opts;
|
|
896
|
+
const keep = opts.keep ?? /\s/;
|
|
897
|
+
const chars = Array.from(text);
|
|
898
|
+
const n = chars.length;
|
|
899
|
+
if (t < start) return { text: "", locked: 0, done: false };
|
|
900
|
+
const step = Math.floor((t - start) * rate);
|
|
901
|
+
let locked = 0;
|
|
902
|
+
const out = chars.map((c, i) => {
|
|
903
|
+
const k = from === "right" ? n - 1 - i : i;
|
|
904
|
+
if (keep.test(c) || t >= start + lead + k * each) {
|
|
905
|
+
locked++;
|
|
906
|
+
return c;
|
|
907
|
+
}
|
|
908
|
+
return glyphs[pick(i, step, seed, glyphs.length)];
|
|
909
|
+
});
|
|
910
|
+
return { text: out.join(""), locked, done: locked === n };
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/** an ease-out that runs past the end by `overshoot` of the travel and comes
|
|
914
|
+
* back (the "back" ease, its constant solved for the overshoot asked): a
|
|
915
|
+
* roll that slows into place, unlike a spring step, which is most of the
|
|
916
|
+
* way there in its first fifth */
|
|
917
|
+
/** @param {number} overshoot */
|
|
918
|
+
function backOut(overshoot) {
|
|
919
|
+
const f = (/** @type {number} */ c, /** @type {number} */ p) =>
|
|
920
|
+
1 + (c + 1) * (p - 1) ** 3 + c * (p - 1) ** 2;
|
|
921
|
+
// the peak of f is at p = 1 − 2c / (3(c + 1)); bisect c for the overshoot
|
|
922
|
+
const peak = (/** @type {number} */ c) => f(c, 1 - (2 * c) / (3 * (c + 1))) - 1;
|
|
923
|
+
let lo = 0;
|
|
924
|
+
let hi = 10;
|
|
925
|
+
if (!(overshoot > 0)) return (/** @type {number} */ p) => 1 - (1 - clamp01(p)) ** 3;
|
|
926
|
+
for (let i = 0; i < 40; i++) {
|
|
927
|
+
const mid = (lo + hi) / 2;
|
|
928
|
+
if (peak(mid) < overshoot) lo = mid;
|
|
929
|
+
else hi = mid;
|
|
930
|
+
}
|
|
931
|
+
const c = (lo + hi) / 2;
|
|
932
|
+
return (/** @type {number} */ p) => (p <= 0 ? 0 : p >= 1 ? 1 : f(c, p));
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Digits rolling into place like an odometer: each digit column spins from
|
|
937
|
+
* its old digit to its new one (going round `laps` extra times), the
|
|
938
|
+
* columns landing left to right `each` s apart, each over `dur` s, with a
|
|
939
|
+
* small overshoot. A column's `pos` is how far down its strip of digits it
|
|
940
|
+
* is (in digit heights; 0–9 then 0 again …): translate a strip of
|
|
941
|
+
* "0123456789" stacked `laps + 2` times by −pos em. `blur` is its speed in
|
|
942
|
+
* digit heights a second, for a vertical motion blur. Non-digits (",", "$")
|
|
943
|
+
* come back as fixed columns. The format of `to` is kept: "1,240" lands as
|
|
944
|
+
* "1,240". From films measured: columns 0.15 s apart, ≈5 % overshoot.
|
|
945
|
+
* @param {number} t
|
|
946
|
+
* @param {number} start
|
|
947
|
+
* @param {string | number} to
|
|
948
|
+
* @param {{ from?: string | number, each?: number, dur?: number, laps?: number, overshoot?: number,
|
|
949
|
+
* order?: "left" | "right" }} [opts]
|
|
950
|
+
* @returns {{ columns: { char: string, digit: boolean, pos: number, value: number, speed: number }[], text: string, done: boolean }}
|
|
951
|
+
*/
|
|
952
|
+
export function odometer(t, start, to, opts = {}) {
|
|
953
|
+
const { each = 0.15, dur = 0.6, laps = 1, overshoot = 0.05, order = "left" } = opts;
|
|
954
|
+
const target = String(to);
|
|
955
|
+
const fromStr = opts.from == null ? "" : String(opts.from);
|
|
956
|
+
const digitsTo = Array.from(target).filter((c) => /\d/.test(c)).length;
|
|
957
|
+
// the old value's digits, right-aligned against the new ones
|
|
958
|
+
const fromDigits = Array.from(fromStr).filter((c) => /\d/.test(c));
|
|
959
|
+
const pad = Array(Math.max(0, digitsTo - fromDigits.length)).fill("0");
|
|
960
|
+
const old = [...pad, ...fromDigits].slice(-digitsTo || 0);
|
|
961
|
+
const ease = backOut(overshoot);
|
|
962
|
+
let d = -1;
|
|
963
|
+
const chars = Array.from(target);
|
|
964
|
+
const columns = chars.map((c) => {
|
|
965
|
+
if (!/\d/.test(c)) return { char: c, digit: false, pos: 0, value: 0, speed: 0 };
|
|
966
|
+
d++;
|
|
967
|
+
const k = order === "left" ? d : digitsTo - 1 - d;
|
|
968
|
+
const a = Number(old[d] ?? 0);
|
|
969
|
+
const b = Number(c);
|
|
970
|
+
const span = ((b - a + 10) % 10) + laps * 10;
|
|
971
|
+
const t0 = start + k * each;
|
|
972
|
+
const p = dur > 0 ? ease(clamp01((t - t0) / dur)) : t >= t0 ? 1 : 0;
|
|
973
|
+
const h = 1 / 240;
|
|
974
|
+
const p2 = dur > 0 ? ease(clamp01((t + h - t0) / dur)) : p;
|
|
975
|
+
const pos = a + span * p;
|
|
976
|
+
return {
|
|
977
|
+
char: c,
|
|
978
|
+
digit: true,
|
|
979
|
+
pos,
|
|
980
|
+
value: ((Math.round(pos) % 10) + 10) % 10,
|
|
981
|
+
speed: (span * (p2 - p)) / h,
|
|
982
|
+
};
|
|
983
|
+
});
|
|
984
|
+
const text = columns.map((c) => (c.digit ? String(c.value) : c.char)).join("");
|
|
985
|
+
return { columns, text, done: t >= start + Math.max(0, digitsTo - 1) * each + dur };
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* A split-flap board turning from one value to the next: per character
|
|
990
|
+
* tile, the old character's top half folds down (`top` 1 → 0, a scaleY
|
|
991
|
+
* about the tile's middle) in the first half of `dur`, then the new
|
|
992
|
+
* character's bottom half folds open (`bottom` 0 → 1). Draw each tile as
|
|
993
|
+
* four halves: the new top behind, the old bottom behind, then the folding
|
|
994
|
+
* old top and new bottom. Tiles turn `each` s apart, left to right; the
|
|
995
|
+
* shorter string is padded with spaces on the left. From a film's board:
|
|
996
|
+
* 0.26 s a tile, 0.2 s apart.
|
|
997
|
+
* @param {number} t
|
|
998
|
+
* @param {number} start
|
|
999
|
+
* @param {string | number} from
|
|
1000
|
+
* @param {string | number} to
|
|
1001
|
+
* @param {{ each?: number, dur?: number }} [opts]
|
|
1002
|
+
* @returns {{ tiles: { old: string, now: string, top: number, bottom: number, p: number }[], done: boolean }}
|
|
1003
|
+
*/
|
|
1004
|
+
export function flap(t, start, from, to, opts = {}) {
|
|
1005
|
+
const { each = 0.2, dur = 0.26 } = opts;
|
|
1006
|
+
const a = Array.from(String(from));
|
|
1007
|
+
const b = Array.from(String(to));
|
|
1008
|
+
const n = Math.max(a.length, b.length);
|
|
1009
|
+
while (a.length < n) a.unshift(" ");
|
|
1010
|
+
while (b.length < n) b.unshift(" ");
|
|
1011
|
+
const tiles = b.map((now, i) => {
|
|
1012
|
+
const old = a[i] ?? " ";
|
|
1013
|
+
if (old === now) return { old, now, top: 0, bottom: 1, p: 1 };
|
|
1014
|
+
const p = dur > 0 ? clamp01((t - (start + i * each)) / dur) : t >= start + i * each ? 1 : 0;
|
|
1015
|
+
// gravity on the fold: it leaves slowly and slaps down
|
|
1016
|
+
const top = p < 0.5 ? 1 - easeIn(p / 0.5) : 0;
|
|
1017
|
+
const bottom = p < 0.5 ? 0 : easeOut((p - 0.5) / 0.5);
|
|
1018
|
+
return { old, now, top, bottom, p };
|
|
1019
|
+
});
|
|
1020
|
+
return { tiles, done: tiles.every((x) => x.p >= 1) };
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
// Parts arrive 0.06 s apart by default: ≈ 4 frames at 60 fps, so each one is
|
|
1024
|
+
// seen to come on its own (gather's 0.04 reads as one ripple), while five of
|
|
1025
|
+
// them are all moving within a third of a second — one gesture, not a queue.
|
|
1026
|
+
const EACH = 0.06;
|
|
1027
|
+
|
|
1028
|
+
/** @param {unknown} x @returns {x is HTMLElement} */
|
|
1029
|
+
const isEl = (x) =>
|
|
1030
|
+
!!x &&
|
|
1031
|
+
typeof x === "object" &&
|
|
1032
|
+
/** @type {{ nodeType?: unknown }} */ (x).nodeType === 1 &&
|
|
1033
|
+
"style" in /** @type {object} */ (x);
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* Parts arriving one after another — a list's rows, a card's stat pills, a
|
|
1037
|
+
* page's sections: `move` (default rise) run on each, `each` s after the one
|
|
1038
|
+
* before, in `order` ("index", or "reverse"). `with`: the move's own options.
|
|
1039
|
+
* Returns the move's values, one a part; given elements (a lifted part's
|
|
1040
|
+
* `parts()`), it also puts them on each: translate (x, y), scale (scale or
|
|
1041
|
+
* sx / sy), opacity and clip — an inline element is made inline-block so it
|
|
1042
|
+
* can move — and gives a move that needs one (maskRise, maskOut) the
|
|
1043
|
+
* element's height. What the product gave the element is kept: its own
|
|
1044
|
+
* transform stays under the move's, its own opacity is multiplied by the
|
|
1045
|
+
* move's, and at rest the transform is the product's again.
|
|
1046
|
+
*
|
|
1047
|
+
* assemble(t, 5, card.parts(), { each: 0.08, move: pop });
|
|
1048
|
+
* const v = assemble(t, 5, 4); // four sets of numbers, for a canvas
|
|
1049
|
+
* @param {number} t
|
|
1050
|
+
* @param {number} start
|
|
1051
|
+
* @param {number | ArrayLike<unknown>} items how many, or the elements
|
|
1052
|
+
* @param {{ each?: number, move?: (t: number, start: number, o?: any) => Record<string, unknown>,
|
|
1053
|
+
* with?: Record<string, unknown>, order?: "index" | "reverse" }} [opts]
|
|
1054
|
+
*/
|
|
1055
|
+
export function assemble(t, start, items, opts = {}) {
|
|
1056
|
+
const { each = EACH, with: w = {}, order = "index" } = opts;
|
|
1057
|
+
/** @type {(t: number, start: number, o?: any) => Record<string, unknown>} */
|
|
1058
|
+
const move = opts.move ?? rise;
|
|
1059
|
+
const list = typeof items === "number" ? Array.from({ length: items }) : Array.from(items);
|
|
1060
|
+
const n = list.length;
|
|
1061
|
+
return list.map((item, i) => {
|
|
1062
|
+
const k = order === "reverse" ? n - 1 - i : i;
|
|
1063
|
+
const el = isEl(item) ? item : null;
|
|
1064
|
+
// only the masks take the element's height: drop's `height` is how far it falls
|
|
1065
|
+
const own =
|
|
1066
|
+
el && w.height == null && (move === maskRise || move === maskOut)
|
|
1067
|
+
? { height: el.offsetHeight }
|
|
1068
|
+
: {};
|
|
1069
|
+
const v = move(t, start + k * each, { ...own, ...w });
|
|
1070
|
+
if (el) putOn(el, v);
|
|
1071
|
+
return v;
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/** what the product gave an element before assemble first moved it: its
|
|
1076
|
+
* own transform and opacity (computed), and its inline transform
|
|
1077
|
+
* @type {WeakMap<object, { transform: string, opacity: number, inline: string }>} */
|
|
1078
|
+
const ownStyle = new WeakMap();
|
|
1079
|
+
|
|
1080
|
+
/** a move's numbers onto an element's style, over what the product gave it
|
|
1081
|
+
* @param {HTMLElement} el @param {Record<string, unknown>} v */
|
|
1082
|
+
function putOn(el, v) {
|
|
1083
|
+
const num = (/** @type {unknown} */ x) =>
|
|
1084
|
+
typeof x === "number" && Number.isFinite(x) ? x : null;
|
|
1085
|
+
const view = el.ownerDocument?.defaultView;
|
|
1086
|
+
let base = ownStyle.get(el);
|
|
1087
|
+
if (!base) {
|
|
1088
|
+
const cs = view?.getComputedStyle(el);
|
|
1089
|
+
const tf = cs?.transform;
|
|
1090
|
+
const op = Number(cs?.opacity);
|
|
1091
|
+
base = {
|
|
1092
|
+
transform: tf && tf !== "none" ? tf : "",
|
|
1093
|
+
opacity: Number.isFinite(op) ? op : 1,
|
|
1094
|
+
inline: el.style.transform ?? "",
|
|
1095
|
+
};
|
|
1096
|
+
ownStyle.set(el, base);
|
|
1097
|
+
if (cs?.display === "inline") el.style.display = "inline-block";
|
|
1098
|
+
}
|
|
1099
|
+
const x = num(v.x) ?? 0;
|
|
1100
|
+
const y = num(v.y) ?? 0;
|
|
1101
|
+
const s = num(v.scale);
|
|
1102
|
+
const sx = num(v.sx) ?? s ?? 1;
|
|
1103
|
+
const sy = num(v.sy) ?? s ?? 1;
|
|
1104
|
+
const parts = [];
|
|
1105
|
+
// what rounds to nothing is nothing (a spring's last ten-thousandth of a px)
|
|
1106
|
+
if (Math.abs(x) >= 5e-4 || Math.abs(y) >= 5e-4)
|
|
1107
|
+
parts.push(`translate(${x.toFixed(3)}px, ${y.toFixed(3)}px)`);
|
|
1108
|
+
if (Math.abs(sx - 1) >= 5e-6 || Math.abs(sy - 1) >= 5e-6)
|
|
1109
|
+
parts.push(`scale(${sx.toFixed(5)}, ${sy.toFixed(5)})`);
|
|
1110
|
+
// the move about the element's own place, the product's transform under it
|
|
1111
|
+
el.style.transform = parts.length ? [...parts, base.transform].join(" ").trim() : base.inline;
|
|
1112
|
+
const o = num(v.opacity);
|
|
1113
|
+
if (o != null) el.style.opacity = String(clamp01(o) * base.opacity);
|
|
1114
|
+
if (typeof v.clip === "string") el.style.clipPath = v.clip;
|
|
1115
|
+
}
|