@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
|
@@ -0,0 +1,2530 @@
|
|
|
1
|
+
// The film clock: the page-side half of `open-take film render`, installed in
|
|
2
|
+
// the film's page before any of its own scripts run.
|
|
3
|
+
//
|
|
4
|
+
// A film is any web page. It either draws itself for a time (`window.__render(t)`,
|
|
5
|
+
// t in seconds, any order — the page is a pure function of time) or it simply
|
|
6
|
+
// animates: CSS animations and transitions, the Web Animations API, a
|
|
7
|
+
// requestAnimationFrame loop, GSAP, three.js, a <video>. For the second kind this
|
|
8
|
+
// clock replaces every time source the page can read — performance.now, Date,
|
|
9
|
+
// timers, requestAnimationFrame — with film time, and pauses every running
|
|
10
|
+
// animation and video so each frame shows exactly the instant asked for. Such a
|
|
11
|
+
// page keeps its own state, so it renders front to back (time only moves
|
|
12
|
+
// forward); Math.random is seeded so every render worker draws the same numbers.
|
|
13
|
+
//
|
|
14
|
+
// The renderer drives the page through `window.__ot`:
|
|
15
|
+
// prepare() → the page's ready promise, fonts, images; what it declared
|
|
16
|
+
// seek(t) → the page shows film time t (seconds)
|
|
17
|
+
// motion(t, half) → how far (px) anything visible moves across [t-half, t+half],
|
|
18
|
+
// and which side of that shutter to expose (one that cuts is left out)
|
|
19
|
+
// and, before each draw, sets `window.__filmShare`: the share of the frame's
|
|
20
|
+
// shutter that draw stands for (1 when the frame is one draw; 1/n of it, or of
|
|
21
|
+
// its half, when it is n samples) — what a page that blurs itself across its
|
|
22
|
+
// own shutter exposes, so its samples and the renderer's tile the shutter.
|
|
23
|
+
//
|
|
24
|
+
// The page may declare itself with `window.__film = { duration, width, height,
|
|
25
|
+
// fps, audio, ready, motion, marks }` (every field optional); `motion` speaks
|
|
26
|
+
// for what the page draws (a canvas), and the larger of it and how far its
|
|
27
|
+
// elements move is the frame's motion. `__duration` and
|
|
28
|
+
// `__ready` are read too. `marks` — [{ at, until?, what? }] — are the moments
|
|
29
|
+
// the author means to look the way they do (a white flash on the beat): `film
|
|
30
|
+
// check` lists what it finds there as marked, not as a defect.
|
|
31
|
+
//
|
|
32
|
+
// The clock and its canvas matcher are serialized into the page
|
|
33
|
+
// (Function#toString): they take anything they need as arguments.
|
|
34
|
+
|
|
35
|
+
type FilmClockOptions = {
|
|
36
|
+
/** Math.random's seed — the same in every worker */
|
|
37
|
+
seed: number;
|
|
38
|
+
/** what Date says at film time 0 (ms since the epoch) */
|
|
39
|
+
epochMs: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type FilmPageInfo = {
|
|
43
|
+
/** "render": the page draws any time on request (window.__render);
|
|
44
|
+
* "clock": it animates on its own and renders front to back */
|
|
45
|
+
mode: "render" | "clock";
|
|
46
|
+
duration: number | null;
|
|
47
|
+
width: number | null;
|
|
48
|
+
height: number | null;
|
|
49
|
+
fps: number | null;
|
|
50
|
+
audio: string | null;
|
|
51
|
+
/** the page has <audio> elements — they are not recorded */
|
|
52
|
+
audioElements: number;
|
|
53
|
+
/** libraries found running on their own tickers in clock mode */
|
|
54
|
+
tickers: ("GSAP" | "anime.js")[];
|
|
55
|
+
/** window.__film.marks as the page has them (see filmMarks) */
|
|
56
|
+
marks: unknown;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** A moment the author means to look the way it does, in seconds of film
|
|
60
|
+
* time: `film check` lists what it finds there as marked, not as a defect. */
|
|
61
|
+
export type FilmMark = { at: number; until: number; what: string | null };
|
|
62
|
+
|
|
63
|
+
/** window.__film.marks, checked: a list of { at, until?, what? } in seconds;
|
|
64
|
+
* a mark without `until` is the moment `at`. */
|
|
65
|
+
export function filmMarks(raw: unknown): FilmMark[] {
|
|
66
|
+
if (raw == null) return [];
|
|
67
|
+
const usage = "[{ at: <seconds>, until?: <seconds>, what?: <text> }]";
|
|
68
|
+
if (!Array.isArray(raw)) throw new Error(`film: window.__film.marks is a list — ${usage}`);
|
|
69
|
+
return raw.map((m, i) => {
|
|
70
|
+
const { at, until, what } = (m ?? {}) as Record<string, unknown>;
|
|
71
|
+
if (typeof at !== "number" || !Number.isFinite(at) || at < 0)
|
|
72
|
+
throw new Error(
|
|
73
|
+
`film: window.__film.marks[${i}].at is the mark's time in seconds — ${usage}`,
|
|
74
|
+
);
|
|
75
|
+
if (until != null && (typeof until !== "number" || !Number.isFinite(until) || until < at))
|
|
76
|
+
throw new Error(
|
|
77
|
+
`film: window.__film.marks[${i}].until is a time in seconds from at (${at}) on — ${usage}`,
|
|
78
|
+
);
|
|
79
|
+
return {
|
|
80
|
+
at,
|
|
81
|
+
until: (until as number | null | undefined) ?? at,
|
|
82
|
+
what: what == null ? null : String(what),
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type CanvasThumb = { pixels: Uint8Array; width: number; height: number; cutPixels: Uint8ClampedArray };
|
|
88
|
+
export type TextCause = { key: string; label: string };
|
|
89
|
+
|
|
90
|
+
// An eighth of the grey range represents coverage. Opaque black can then
|
|
91
|
+
// be followed against a transparent surround.
|
|
92
|
+
export function canvasGrey(r: number, g: number, b: number, alpha: number): number {
|
|
93
|
+
return (((77 * r + 150 * g + 29 * b) / 256) * 7 / 8 + 32) * alpha / 255;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function canvasAxisSize(
|
|
97
|
+
width: number, height: number, { a, b, c, d }: Pick<DOMMatrixReadOnly, "a" | "b" | "c" | "d">, zoom: number,
|
|
98
|
+
): [number, number] {
|
|
99
|
+
return [width * Math.hypot(a, b) * zoom, height * Math.hypot(c, d) * zoom];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function gsapTickerRunning(gsap: {
|
|
103
|
+
ticker?: { add?: unknown; _listeners?: unknown[]; active?: boolean };
|
|
104
|
+
globalTimeline?: { getChildren?: () => { paused?: () => boolean }[] };
|
|
105
|
+
} | undefined): boolean {
|
|
106
|
+
const ticker = gsap?.ticker;
|
|
107
|
+
if (typeof ticker?.add !== "function" || ticker.active === false) return false;
|
|
108
|
+
// GSAP 3 has one internal listener. The timeline is also useful on
|
|
109
|
+
// builds that do not expose their ticker listeners.
|
|
110
|
+
return (ticker._listeners?.length ?? 0) > 1 ||
|
|
111
|
+
gsap?.globalTimeline?.getChildren?.().some((child) => !child.paused?.()) === true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The largest credible shift between two grey thumbnails, in CSS px.
|
|
115
|
+
* Follow textured patches where the picture changed, so still background
|
|
116
|
+
* cannot outvote a small moving object; take the fastest of them, so a part
|
|
117
|
+
* that turns or grows while it moves is spaced by its fastest edge. 0 when
|
|
118
|
+
* nothing changed enough to follow (the caller's own test decides then);
|
|
119
|
+
* `fallback` when something changed and no patch can be followed. */
|
|
120
|
+
export function canvasDisplacement(
|
|
121
|
+
a: Uint8Array,
|
|
122
|
+
b: Uint8Array,
|
|
123
|
+
width: number,
|
|
124
|
+
height: number,
|
|
125
|
+
cssWidth: number,
|
|
126
|
+
cssHeight: number,
|
|
127
|
+
fallback = 12,
|
|
128
|
+
): number {
|
|
129
|
+
if (!a.some((v, i) => v !== b[i])) return 0;
|
|
130
|
+
|
|
131
|
+
// A patch is 7×7 pixels read every other pixel: 16 values. On the
|
|
132
|
+
// 192-pixel thumbnail one pixel is 5 CSS px of a 960-pixel canvas, so a
|
|
133
|
+
// patch spans 35 CSS px — a button's worth, small enough to stay rigid
|
|
134
|
+
// on a part that turns.
|
|
135
|
+
const radius = 3;
|
|
136
|
+
// The plain search reaches 32 pixels (160 CSS px at 960) at every pixel;
|
|
137
|
+
// the wide one 64 at every other pixel, for what it cannot find there.
|
|
138
|
+
const reach = 32;
|
|
139
|
+
const wide = 64;
|
|
140
|
+
const offsets: [number, number][] = [];
|
|
141
|
+
for (let y = -radius; y <= radius; y += 2)
|
|
142
|
+
for (let x = -radius; x <= radius; x += 2) offsets.push([x, y]);
|
|
143
|
+
const n = offsets.length;
|
|
144
|
+
|
|
145
|
+
// Where the picture changed and holds texture to follow. 48 over 16 values
|
|
146
|
+
// is 3 grey levels a value: one pixel a level off, or a 4×4 square
|
|
147
|
+
// flickering by 4 levels, stays under it and is no motion (the canvas
|
|
148
|
+
// motion tests). A range of 16 levels is the faintest edge on those tests'
|
|
149
|
+
// disc and pan that the matcher still places to the pixel.
|
|
150
|
+
// At a stride of 5 the 7 px patches overlap by 2, so the test's 6 px
|
|
151
|
+
// disc can still leave an edge in a patch.
|
|
152
|
+
const tiles: { x: number; y: number; score: number; reverse: boolean }[] = [];
|
|
153
|
+
for (let y = radius; y < height - radius; y += 5)
|
|
154
|
+
for (let x = radius; x < width - radius; x += 5) {
|
|
155
|
+
let alo = 255, ahi = 0, blo = 255, bhi = 0, diff = 0;
|
|
156
|
+
for (const [i, j] of offsets) {
|
|
157
|
+
const p = (y + j) * width + x + i;
|
|
158
|
+
const av = a[p]!;
|
|
159
|
+
const bv = b[p]!;
|
|
160
|
+
alo = Math.min(alo, av);
|
|
161
|
+
ahi = Math.max(ahi, av);
|
|
162
|
+
blo = Math.min(blo, bv);
|
|
163
|
+
bhi = Math.max(bhi, bv);
|
|
164
|
+
diff += Math.abs(av - bv);
|
|
165
|
+
}
|
|
166
|
+
const ar = ahi - alo;
|
|
167
|
+
const br = bhi - blo;
|
|
168
|
+
if (!(diff >= 48 && Math.max(ar, br) >= 16)) continue;
|
|
169
|
+
// What moves changes a run of pixels alike — an edge, a texture
|
|
170
|
+
// shifted; grain changes each pixel on its own. The change at each
|
|
171
|
+
// pixel of the patch against its right and lower neighbours: grain's
|
|
172
|
+
// stays under a third of its own size, and it is no motion.
|
|
173
|
+
let dd = 0, dh = 0, dv = 0;
|
|
174
|
+
for (let j = -radius; j <= radius; j++)
|
|
175
|
+
for (let i = -radius; i <= radius; i++) {
|
|
176
|
+
const p = (y + j) * width + x + i;
|
|
177
|
+
const d = a[p]! - b[p]!;
|
|
178
|
+
dd += d * d;
|
|
179
|
+
if (i < radius) dh += d * (a[p + 1]! - b[p + 1]!);
|
|
180
|
+
if (j < radius) dv += d * (a[p + width]! - b[p + width]!);
|
|
181
|
+
}
|
|
182
|
+
if (Math.max(dh, dv) < dd / 2) continue;
|
|
183
|
+
tiles.push({ x, y, score: diff * Math.max(ar, br), reverse: br > ar });
|
|
184
|
+
}
|
|
185
|
+
if (!tiles.length) return 0;
|
|
186
|
+
tiles.sort((l, r) => r.score - l.score);
|
|
187
|
+
|
|
188
|
+
const sample = (img: Uint8Array, x: number, y: number): number => {
|
|
189
|
+
const cx = Math.min(width - 1.001, Math.max(0, x));
|
|
190
|
+
const cy = Math.min(height - 1.001, Math.max(0, y));
|
|
191
|
+
const x0 = Math.floor(cx), y0 = Math.floor(cy);
|
|
192
|
+
const fx = cx - x0, fy = cy - y0;
|
|
193
|
+
const p = y0 * width + x0;
|
|
194
|
+
return (img[p]! * (1 - fx) + img[p + 1]! * fx) * (1 - fy) +
|
|
195
|
+
(img[p + width]! * (1 - fx) + img[p + width + 1]! * fx) * fy;
|
|
196
|
+
};
|
|
197
|
+
const css = (f: { dx: number; dy: number }) =>
|
|
198
|
+
Math.hypot(f.dx * cssWidth / width, f.dy * cssHeight / height);
|
|
199
|
+
// Check a single move over a whole part before using its farthest edge:
|
|
200
|
+
// short patches can match the wrong repeat of a texture when it turns.
|
|
201
|
+
const bounds = (includes: (i: number) => boolean) => {
|
|
202
|
+
let left = width, right = -1, top = height, bottom = -1;
|
|
203
|
+
for (let y = 0; y < height; y++)
|
|
204
|
+
for (let x = 0; x < width; x++) {
|
|
205
|
+
if (!includes(y * width + x)) continue;
|
|
206
|
+
left = Math.min(left, x); right = Math.max(right, x);
|
|
207
|
+
top = Math.min(top, y); bottom = Math.max(bottom, y);
|
|
208
|
+
}
|
|
209
|
+
return { left, right, top, bottom, x: (left + right) / 2, y: (top + bottom) / 2,
|
|
210
|
+
w: right + 1 - left, h: bottom + 1 - top };
|
|
211
|
+
};
|
|
212
|
+
const enclosed = (p: ReturnType<typeof bounds>) => p.left > 0 && p.right < width - 1 &&
|
|
213
|
+
p.top > 0 && p.bottom < height - 1 && p.w >= 2 * radius && p.h >= 2 * radius;
|
|
214
|
+
const bg = a[0]!;
|
|
215
|
+
const corners = [0, width - 1, (height - 1) * width, width * height - 1];
|
|
216
|
+
const flat = corners.every((i) => a[i] === bg && b[i] === bg);
|
|
217
|
+
const changed = bounds((i) => Math.abs(a[i]! - b[i]!) >= 16);
|
|
218
|
+
const before = flat ? bounds((i) => Math.abs(a[i]! - bg) >= 16) : changed;
|
|
219
|
+
const after = flat ? bounds((i) => Math.abs(b[i]! - bg) >= 16) : changed;
|
|
220
|
+
// A flat surround exposes the two outlines. Against a textured
|
|
221
|
+
// surround we can still check for a turn centred on the changed part.
|
|
222
|
+
const p = enclosed(before) && enclosed(after) ? before : changed;
|
|
223
|
+
const q = p === before ? after : changed;
|
|
224
|
+
if (enclosed(p) && enclosed(q)) {
|
|
225
|
+
const scale = Math.sqrt(q.w * q.h / (p.w * p.h));
|
|
226
|
+
let best = Infinity, bestPx = 0, bestTurn = 0, still = 0, n = 0;
|
|
227
|
+
for (let y = p.top; y <= p.bottom; y += 2)
|
|
228
|
+
for (let x = p.left; x <= p.right; x += 2) {
|
|
229
|
+
still += Math.abs(a[y * width + x]! - b[y * width + x]!);
|
|
230
|
+
n++;
|
|
231
|
+
}
|
|
232
|
+
const fit = (turn: number, scale: number) => {
|
|
233
|
+
const c = scale * Math.cos(turn), s = scale * Math.sin(turn);
|
|
234
|
+
let error = 0, px = 0;
|
|
235
|
+
for (let y = p.top; y <= p.bottom && error < best; y += 2)
|
|
236
|
+
for (let x = p.left; x <= p.right; x += 2) {
|
|
237
|
+
const dx = x - p.x, dy = y - p.y;
|
|
238
|
+
const u = q.x + c * dx - s * dy, v = q.y + s * dx + c * dy;
|
|
239
|
+
const value = a[y * width + x]!;
|
|
240
|
+
error += Math.abs(value - sample(b, u, v));
|
|
241
|
+
if ((p !== changed && Math.abs(value - bg) >= 16) ||
|
|
242
|
+
(p === changed && Math.abs(value - b[y * width + x]!) >= 16))
|
|
243
|
+
px = Math.max(px, css({ dx: u - x, dy: v - y }));
|
|
244
|
+
}
|
|
245
|
+
if (error < best) { best = error; bestPx = px; bestTurn = turn; }
|
|
246
|
+
};
|
|
247
|
+
fit(0, scale);
|
|
248
|
+
let lo = 255, hi = 0;
|
|
249
|
+
for (let y = Math.ceil(p.y - p.h / 4); y <= p.y + p.h / 4; y++)
|
|
250
|
+
for (let x = Math.ceil(p.x - p.w / 4); x <= p.x + p.w / 4; x++) {
|
|
251
|
+
const v = a[y * width + x]!;
|
|
252
|
+
lo = Math.min(lo, v);
|
|
253
|
+
hi = Math.max(hi, v);
|
|
254
|
+
}
|
|
255
|
+
// A plain disc can look better after an arbitrary turn because its
|
|
256
|
+
// edge lands differently on the grid. Require the same 16-level
|
|
257
|
+
// texture range inside it before following a turn. Five degrees
|
|
258
|
+
// moves the 25 px test disc's rim about 2 px.
|
|
259
|
+
if (hi - lo >= 16)
|
|
260
|
+
for (let angle = 5; angle < 360; angle += 5) fit(angle * Math.PI / 180, scale);
|
|
261
|
+
const turn = bestTurn;
|
|
262
|
+
// Quantised edges put the span of the test disc off by up to 2 px.
|
|
263
|
+
const unit = 1 / Math.min(q.w, q.h);
|
|
264
|
+
for (const ds of [-2, -1, 1, 2]) fit(turn, scale * (1 + ds * unit));
|
|
265
|
+
if (bestPx > 0 && best <= Math.min(0.3 * still, n * 9)) return bestPx;
|
|
266
|
+
}
|
|
267
|
+
// A part that turns or grows between the two pictures turns or grows its
|
|
268
|
+
// patches too. Turns of 20° and 40° each way and a growth of 18 % each
|
|
269
|
+
// way: with the interpolation's slack they match turns to about 50°,
|
|
270
|
+
// where a 3D part tumbling at 30 rad/s turns 14° over the half shutter
|
|
271
|
+
// the probe compares at 60 fps.
|
|
272
|
+
const shapes: [number, number][] = [[1, 0]];
|
|
273
|
+
for (const turn of [20, -20, 40, -40]) {
|
|
274
|
+
const r = (turn * Math.PI) / 180;
|
|
275
|
+
shapes.push([Math.cos(r), Math.sin(r)]);
|
|
276
|
+
}
|
|
277
|
+
shapes.push([1.18, 0], [1 / 1.18, 0]);
|
|
278
|
+
|
|
279
|
+
const grid = 2 * wide + 1;
|
|
280
|
+
const errors = new Float32Array(grid * grid);
|
|
281
|
+
const at = (dx: number, dy: number) => (dy + wide) * grid + dx + wide;
|
|
282
|
+
// Where the patch of `from` around (x, y) went in `to`, or null.
|
|
283
|
+
// With `home`, only within three pixels of it: the way back.
|
|
284
|
+
const follow = (from: Uint8Array, to: Uint8Array, x: number, y: number,
|
|
285
|
+
home?: { dx: number; dy: number }) => {
|
|
286
|
+
const hx = home ? Math.round(home.dx) : 0, hy = home ? Math.round(home.dy) : 0;
|
|
287
|
+
const patch = offsets.map(([i, j]) => from[(y + j) * width + x + i]!);
|
|
288
|
+
const x0 = Math.max(-wide, radius - x), x1 = Math.min(wide, width - radius - 1 - x);
|
|
289
|
+
const y0 = Math.max(-wide, radius - y), y1 = Math.min(wide, height - radius - 1 - y);
|
|
290
|
+
// shape: [cos·scale, sin·scale] applied to the patch's offsets
|
|
291
|
+
const cost = (dx: number, dy: number, [c, s]: [number, number]): number => {
|
|
292
|
+
let sum = 0;
|
|
293
|
+
if (c === 1 && s === 0) {
|
|
294
|
+
const p = (y + dy) * width + x + dx;
|
|
295
|
+
for (let i = 0; i < n; i++)
|
|
296
|
+
sum += Math.abs(patch[i]! - to[p + offsets[i]![1] * width + offsets[i]![0]]!);
|
|
297
|
+
} else
|
|
298
|
+
for (let i = 0; i < n; i++) {
|
|
299
|
+
const [ox, oy] = offsets[i]!;
|
|
300
|
+
sum += Math.abs(patch[i]! - sample(to, x + dx + c * ox - s * oy, y + dy + s * ox + c * oy));
|
|
301
|
+
}
|
|
302
|
+
return sum;
|
|
303
|
+
};
|
|
304
|
+
// The best place within `span` at every `step` pixels, kept in errors.
|
|
305
|
+
// A stripe or a checker repeats: of the places that match about as well
|
|
306
|
+
// as the best (an eighth and a level a value allow for rounding on
|
|
307
|
+
// the shifted discs), take the nearest
|
|
308
|
+
// one that is a low point of its own, the slower reading of the two.
|
|
309
|
+
const search = (span: number, step: number, shape: [number, number]) => {
|
|
310
|
+
const ylo = Math.max(y0, hy - span), yhi = Math.min(y1, hy + span);
|
|
311
|
+
const xlo = Math.max(x0, hx - span), xhi = Math.min(x1, hx + span);
|
|
312
|
+
const on = (d: number) => step === 1 || d % step === 0;
|
|
313
|
+
let best = Infinity;
|
|
314
|
+
for (let dy = ylo; dy <= yhi; dy++)
|
|
315
|
+
for (let dx = xlo; dx <= xhi; dx++) {
|
|
316
|
+
if (!on(dx) || !on(dy)) continue;
|
|
317
|
+
const e = cost(dx, dy, shape);
|
|
318
|
+
errors[at(dx, dy)] = e;
|
|
319
|
+
best = Math.min(best, e);
|
|
320
|
+
}
|
|
321
|
+
const near = best * 1.125 + n;
|
|
322
|
+
let bx = 0, by = 0, pick = Infinity, pickError = Infinity;
|
|
323
|
+
for (let dy = ylo; dy <= yhi; dy++)
|
|
324
|
+
for (let dx = xlo; dx <= xhi; dx++) {
|
|
325
|
+
if (!on(dx) || !on(dy)) continue;
|
|
326
|
+
const e = errors[at(dx, dy)]!;
|
|
327
|
+
if (e > near) continue;
|
|
328
|
+
let low = true;
|
|
329
|
+
for (let j = -step; j <= step && low; j += step)
|
|
330
|
+
for (let i = -step; i <= step; i += step) {
|
|
331
|
+
const u = dx + i, v = dy + j;
|
|
332
|
+
if ((i || j) && u >= xlo && u <= xhi && v >= ylo && v <= yhi &&
|
|
333
|
+
errors[at(u, v)]! < e) {
|
|
334
|
+
low = false;
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const d = Math.hypot(dx - hx, dy - hy);
|
|
339
|
+
if (low && (d < pick || (d === pick && e < pickError))) {
|
|
340
|
+
pick = d;
|
|
341
|
+
pickError = e;
|
|
342
|
+
bx = dx;
|
|
343
|
+
by = dy;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return { best: pickError, bx, by };
|
|
347
|
+
};
|
|
348
|
+
// Credible: it explains most of what changed — a mean error of 9 levels
|
|
349
|
+
// a value where a patch that moved off its place differs by tens on the
|
|
350
|
+
// test pages, and under 0.3 of the error at no shift.
|
|
351
|
+
const credible = (best: number, bx: number, by: number, slack: number) =>
|
|
352
|
+
best <= n * 9 * slack && (!(bx || by) || best <= 0.3 * slack * cost(0, 0, [1, 0]));
|
|
353
|
+
const refine = (lo: number, hi: number, mid: number): number => {
|
|
354
|
+
const bend = lo + hi - 2 * mid;
|
|
355
|
+
return bend > 0 ? Math.max(-0.5, Math.min(0.5, (lo - hi) / (2 * bend))) : 0;
|
|
356
|
+
};
|
|
357
|
+
const plain = search(home ? 3 : reach, 1, [1, 0]);
|
|
358
|
+
if (credible(plain.best, plain.bx, plain.by, 1)) {
|
|
359
|
+
const { best, bx, by } = plain;
|
|
360
|
+
const sx = bx > x0 && bx < x1 ? refine(errors[at(bx - 1, by)]!, errors[at(bx + 1, by)]!, best) : 0;
|
|
361
|
+
const sy = by > y0 && by < y1 ? refine(errors[at(bx, by - 1)]!, errors[at(bx, by + 1)]!, best) : 0;
|
|
362
|
+
return { dx: bx + sx, dy: by + sy };
|
|
363
|
+
}
|
|
364
|
+
// Farther, or turned or grown: every other pixel over the wide reach,
|
|
365
|
+
// then to the pixel around the best of them. These matches blur
|
|
366
|
+
// through interpolation, so they get a third more slack.
|
|
367
|
+
let pick: { best: number; bx: number; by: number; shape: [number, number] } | null = null;
|
|
368
|
+
for (const shape of shapes) {
|
|
369
|
+
// turned or grown within the plain reach, unchanged out to the wide one
|
|
370
|
+
const s = home ? search(3, 1, shape) : search(shape === shapes[0] ? wide : reach, 2, shape);
|
|
371
|
+
if (!credible(s.best, s.bx, s.by, 1.35)) continue;
|
|
372
|
+
const d = Math.hypot(s.bx - hx, s.by - hy);
|
|
373
|
+
const pd = pick ? Math.hypot(pick.bx - hx, pick.by - hy) : Infinity;
|
|
374
|
+
if (d < pd || (d === pd && s.best < pick!.best))
|
|
375
|
+
pick = { ...s, shape };
|
|
376
|
+
}
|
|
377
|
+
if (!pick) return null;
|
|
378
|
+
let { best, bx, by } = pick;
|
|
379
|
+
for (let dy = pick.by - 1; dy <= pick.by + 1; dy++)
|
|
380
|
+
for (let dx = pick.bx - 1; dx <= pick.bx + 1; dx++) {
|
|
381
|
+
if (dx < x0 || dx > x1 || dy < y0 || dy > y1) continue;
|
|
382
|
+
const e = cost(dx, dy, pick.shape);
|
|
383
|
+
if (e < best) {
|
|
384
|
+
best = e;
|
|
385
|
+
bx = dx;
|
|
386
|
+
by = dy;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return { dx: bx, dy: by };
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
const found: { x: number; y: number; dx: number; dy: number; sure: boolean }[] = [];
|
|
393
|
+
// a patch's neighbours that share at most one column or row of its values
|
|
394
|
+
const APART: [number, number][] = [[2 * radius, 0], [-2 * radius, 0], [0, 2 * radius], [0, -2 * radius]];
|
|
395
|
+
const inside = (u: number, v: number) =>
|
|
396
|
+
u >= radius && v >= radius && u < width - radius && v < height - radius;
|
|
397
|
+
// The 12 patches that changed most: more never changed the answer on the
|
|
398
|
+
// test pages and each costs two searches.
|
|
399
|
+
for (const { x, y, reverse } of tiles.slice(0, 12)) {
|
|
400
|
+
const from = reverse ? b : a;
|
|
401
|
+
const to = reverse ? a : b;
|
|
402
|
+
const there = follow(from, to, x, y);
|
|
403
|
+
if (!there) continue;
|
|
404
|
+
// Followed back from where it landed, it must come home within two
|
|
405
|
+
// pixels: a new picture rarely matches both ways.
|
|
406
|
+
const u = Math.round(x + there.dx), v = Math.round(y + there.dy);
|
|
407
|
+
if (!inside(u, v)) continue;
|
|
408
|
+
const back = follow(to, from, u, v, { dx: -there.dx, dy: -there.dy });
|
|
409
|
+
if (!back || Math.hypot(u + back.dx - x, v + back.dy - y) > 2) continue;
|
|
410
|
+
// Grain that changes every frame matches somewhere by chance, and both
|
|
411
|
+
// ways: the same pixels matched back are the same chance. What moved
|
|
412
|
+
// carries its neighbourhood with it — a patch beside it, sharing a
|
|
413
|
+
// column of it at most, lands with it.
|
|
414
|
+
const sure = APART.some(([i, j]) => {
|
|
415
|
+
if (!inside(x + i, y + j)) return false;
|
|
416
|
+
const t = follow(from, to, x + i, y + j);
|
|
417
|
+
return !!t && Math.hypot(t.dx - there.dx, t.dy - there.dy) <= 2;
|
|
418
|
+
});
|
|
419
|
+
const sign = reverse ? -1 : 1;
|
|
420
|
+
found.push({ x, y, dx: sign * there.dx, dy: sign * there.dy, sure });
|
|
421
|
+
}
|
|
422
|
+
// For a shift under half a thumbnail pixel, fitting the frame's grey
|
|
423
|
+
// gradients is steadier than snapping the edges to the patch grid.
|
|
424
|
+
// Downweight residuals over 20 levels: the 0.4 px test disc's hard
|
|
425
|
+
// silhouette can cross a pixel even where its texture moved only a
|
|
426
|
+
// fraction. The small shift explains over 60% of the weighted error.
|
|
427
|
+
if (found.every(({ dx, dy }) => Math.hypot(dx, dy) < radius)) {
|
|
428
|
+
let xx = 0, yy = 0, xy = 0, ex = 0, ey = 0, ee = 0;
|
|
429
|
+
for (let y = 1; y < height - 1; y++)
|
|
430
|
+
for (let x = 1; x < width - 1; x++) {
|
|
431
|
+
const p = y * width + x;
|
|
432
|
+
const e = a[p]! - b[p]!;
|
|
433
|
+
const dx = (b[p + 1]! - b[p - 1]! + a[p + 1]! - a[p - 1]!) / 4;
|
|
434
|
+
const dy = (b[p + width]! - b[p - width]! + a[p + width]! - a[p - width]!) / 4;
|
|
435
|
+
const weight = 1 / Math.max(1, (e / 20) ** 2);
|
|
436
|
+
xx += weight * dx * dx; yy += weight * dy * dy; xy += weight * dx * dy;
|
|
437
|
+
ex += weight * e * dx; ey += weight * e * dy; ee += weight * e * e;
|
|
438
|
+
}
|
|
439
|
+
const det = xx * yy - xy * xy;
|
|
440
|
+
if (det > 0) {
|
|
441
|
+
const dx = (yy * ex - xy * ey) / det;
|
|
442
|
+
const dy = (xx * ey - xy * ex) / det;
|
|
443
|
+
if (Math.hypot(dx, dy) <= 0.5 && dx * ex + dy * ey >= 0.6 * ee)
|
|
444
|
+
return css({ dx, dy });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
// Nothing followed for certain.
|
|
448
|
+
const unfollowed = (): number => {
|
|
449
|
+
// Grain alone — each pixel off on its own, no two neighbours alike — is
|
|
450
|
+
// no motion: 0, and the caller's own test decides.
|
|
451
|
+
let dd = 0, dh = 0, dv = 0;
|
|
452
|
+
for (let y = 0; y < height - 1; y++)
|
|
453
|
+
for (let x = 0; x < width - 1; x++) {
|
|
454
|
+
const p = y * width + x;
|
|
455
|
+
const d = a[p]! - b[p]!;
|
|
456
|
+
dd += d * d;
|
|
457
|
+
dh += d * (a[p + 1]! - b[p + 1]!);
|
|
458
|
+
dv += d * (a[p + width]! - b[p + width]!);
|
|
459
|
+
}
|
|
460
|
+
if (dd > 0 && Math.max(dh, dv) < 0.25 * dd) return 0;
|
|
461
|
+
// A local change in both directions can be a turn that lost its
|
|
462
|
+
// texture between samples. Use the radius of the changed box. A
|
|
463
|
+
// flash or a replacement across the whole canvas keeps the cut signal.
|
|
464
|
+
if (enclosed(changed) && a.some((v, i) => v - b[i]! >= 16) &&
|
|
465
|
+
b.some((v, i) => v - a[i]! >= 16))
|
|
466
|
+
return Math.max(fallback, css({ dx: changed.w / 2, dy: changed.h / 2 }));
|
|
467
|
+
return fallback;
|
|
468
|
+
};
|
|
469
|
+
if (!found.length) return unfollowed();
|
|
470
|
+
// A repeated texture can be followed both ways to the wrong repeat. The
|
|
471
|
+
// fastest patch counts when another moved alike: within two pixels, and
|
|
472
|
+
// half the distance between them up to 12 pixels (two patches apart),
|
|
473
|
+
// which a part turning by up to 29° between the two pictures keeps
|
|
474
|
+
// (2·sin 14.5° = 0.5) — a 3D part tumbling at 30 rad/s turns about 14°
|
|
475
|
+
// over the half-shutter the probe compares at 60 fps.
|
|
476
|
+
// Two patches that overlap share their chance matches too: another
|
|
477
|
+
// counts once it is a patch apart (its values at most a column of these).
|
|
478
|
+
let px = -1;
|
|
479
|
+
for (const f of found)
|
|
480
|
+
if (found.some((g) => g !== f && Math.hypot(f.x - g.x, f.y - g.y) >= 2 * radius &&
|
|
481
|
+
Math.hypot(f.dx - g.dx, f.dy - g.dy) <= 2 + 0.5 * Math.min(12, Math.hypot(f.x - g.x, f.y - g.y))))
|
|
482
|
+
px = Math.max(px, css(f));
|
|
483
|
+
if (px >= 0) return px;
|
|
484
|
+
// Patches that each moved their own way: at least the fixed distance —
|
|
485
|
+
// those found at both ends or beside a neighbour; chance matches in grain
|
|
486
|
+
// are none.
|
|
487
|
+
const sure = found.filter((f) => f.sure);
|
|
488
|
+
if (!sure.length) return unfollowed();
|
|
489
|
+
const alone = sure.map(css).sort((l, r) => l - r);
|
|
490
|
+
// found where they were: they did not move
|
|
491
|
+
if (sure.every((f) => Math.hypot(f.dx, f.dy) < 1)) return alone[alone.length >> 1]!;
|
|
492
|
+
return Math.max(fallback, alone[alone.length >> 1]!);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export function filmClockSource(opts: FilmClockOptions): string {
|
|
496
|
+
// a transpiler that keeps function names (tsx/esbuild) calls a module-level
|
|
497
|
+
// `__name` helper inside the body; the page has none, so it gets a no-op
|
|
498
|
+
const helpers = [canvasDisplacement, canvasGrey, canvasAxisSize, gsapTickerRunning]
|
|
499
|
+
.map((fn) => `(${fn.toString()})`).join(", ");
|
|
500
|
+
return `(() => { const __name = (f) => f; (${installFilmClock.toString()})(${JSON.stringify(opts)}, ${helpers}); })();`;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function installFilmClock(
|
|
504
|
+
opts: FilmClockOptions,
|
|
505
|
+
displacement: typeof canvasDisplacement,
|
|
506
|
+
grey: typeof canvasGrey,
|
|
507
|
+
axisSize: typeof canvasAxisSize,
|
|
508
|
+
gsapRunning: typeof gsapTickerRunning,
|
|
509
|
+
): void {
|
|
510
|
+
// biome-ignore lint/suspicious/noExplicitAny: the page's globals are untyped
|
|
511
|
+
const w = window as any;
|
|
512
|
+
if (w.__ot) return;
|
|
513
|
+
const realSetTimeout = w.setTimeout.bind(w) as typeof setTimeout;
|
|
514
|
+
const RealDate = w.Date as DateConstructor;
|
|
515
|
+
const realPause = HTMLMediaElement.prototype.pause;
|
|
516
|
+
// motion (px) credited to a playing video or a canvas the probe cannot
|
|
517
|
+
// read, and the old canvas-change signal used to choose a shutter side
|
|
518
|
+
const OPAQUE_PX = 12;
|
|
519
|
+
const STEP_MS = 1000 / 60;
|
|
520
|
+
const MAX_JUMP_MS = 10_000;
|
|
521
|
+
// how long the ready wait holds the clock for loading that does not end
|
|
522
|
+
const LOADING_PATIENCE_MS = 10_000;
|
|
523
|
+
// how many times one instant is drawn while new frames keep showing up in it
|
|
524
|
+
const MAX_DRAWS = 32;
|
|
525
|
+
const warn = console.warn.bind(console);
|
|
526
|
+
|
|
527
|
+
let now = 0; // the page's clock, ms
|
|
528
|
+
// the timers' own clock: equal to the page's, except while a __render holds
|
|
529
|
+
// its instant — then timers still run in due order and schedule from their
|
|
530
|
+
// own due time (a timer set inside a timer lands after the earlier ones)
|
|
531
|
+
let holding = false;
|
|
532
|
+
let timerNow = 0;
|
|
533
|
+
const schedNow = () => (holding ? timerNow : clock());
|
|
534
|
+
// film time 0 is where the page became ready: a ready promise that waits on
|
|
535
|
+
// a timer advances the page's clock before the film starts
|
|
536
|
+
let base = 0;
|
|
537
|
+
let mode: "render" | "clock" = "clock";
|
|
538
|
+
// A frame inside the film (an <iframe> of a saved screen, say) shows its
|
|
539
|
+
// page as it loaded, on the film's clock. Its timers and animation frames
|
|
540
|
+
// run where the page put it — its first frame, as a browser runs before
|
|
541
|
+
// painting — and not again: what a frame's own script would do later
|
|
542
|
+
// depends on how the film is sought, and what moves inside a frame, the
|
|
543
|
+
// page moves (it is same-origin). Its CSS animations and videos play on
|
|
544
|
+
// film time. While it loads it reads the time it was put on the page;
|
|
545
|
+
// once it shows, the time of the page that holds it (ms on the top page's
|
|
546
|
+
// clock).
|
|
547
|
+
// biome-ignore lint/suspicious/noExplicitAny: the holding page's film clock
|
|
548
|
+
let holder: any = null;
|
|
549
|
+
try {
|
|
550
|
+
const up = w.parent;
|
|
551
|
+
if (up && up !== w && up.__ot) {
|
|
552
|
+
holder = up.__ot;
|
|
553
|
+
now = holder.bornOf(w.frameElement);
|
|
554
|
+
base = holder.origin();
|
|
555
|
+
}
|
|
556
|
+
} catch {
|
|
557
|
+
/* a cross-origin parent: this frame keeps its own clock */
|
|
558
|
+
}
|
|
559
|
+
// whether the page has had its first animation frame (see firstFrame)
|
|
560
|
+
let started = false;
|
|
561
|
+
/** the time this page reads, and plays its animations and videos at */
|
|
562
|
+
const clock = (): number => (holder && started ? holder.clock() : now);
|
|
563
|
+
/** this page, or the page that holds it, draws any moment on request */
|
|
564
|
+
const drawn = (): boolean => mode === "render" || !!holder?.drawn();
|
|
565
|
+
// requestAnimationFrame runs on one grid of film time (see advance): the
|
|
566
|
+
// grid's instants, and the next one to run
|
|
567
|
+
const gridTime = (k: number) => base + (k + 0.5) * STEP_MS;
|
|
568
|
+
const gridAfter = (t: number) => Math.floor((t - base) / STEP_MS - 0.5 + 1e-9) + 1;
|
|
569
|
+
let grid = gridAfter(now);
|
|
570
|
+
|
|
571
|
+
// --- inline styles the page sets that do not take ----------------------------
|
|
572
|
+
// `el.style.transform = \`scale(${x})\`` with x NaN, undefined or an object
|
|
573
|
+
// is not CSS: the browser ignores the assignment and the element keeps what
|
|
574
|
+
// it had, silently. Every inline-style setter is wrapped to notice an
|
|
575
|
+
// assignment after which the property does not hold what was set — the
|
|
576
|
+
// value is passed on unchanged, so the page draws exactly as it would — and
|
|
577
|
+
// the first such value for each element and property is kept, with the film
|
|
578
|
+
// time it was set at, for the renderer to name (styleRejects). Reading the
|
|
579
|
+
// property back costs two string reads a set; a value is only parsed again
|
|
580
|
+
// (on a detached declaration, cached) when the property reads the same
|
|
581
|
+
// before and after — set again unchanged, or ignored. cssText, the style
|
|
582
|
+
// attribute and the styles of CSS rules (but their setProperty) are not
|
|
583
|
+
// watched.
|
|
584
|
+
type StyleReject = {
|
|
585
|
+
el: string;
|
|
586
|
+
prop: string;
|
|
587
|
+
value: string;
|
|
588
|
+
kept: string;
|
|
589
|
+
/** "ignored": the property did not take it; "suspect": a custom property
|
|
590
|
+
* took it as written, but it reads NaN, undefined or [object …] */
|
|
591
|
+
how: "ignored" | "suspect";
|
|
592
|
+
t: number | null;
|
|
593
|
+
};
|
|
594
|
+
let begun = false;
|
|
595
|
+
const rejected: StyleReject[] = [];
|
|
596
|
+
const rejectedSeen = new WeakMap<CSSStyleDeclaration, Set<string>>();
|
|
597
|
+
const SUSPECT = /\bNaN\b|\bundefined\b|\[object [A-Za-z]+\]/;
|
|
598
|
+
const filmT = (): number | null => {
|
|
599
|
+
if (holder) {
|
|
600
|
+
try {
|
|
601
|
+
return (holder.clock() - holder.origin()) / 1000;
|
|
602
|
+
} catch {
|
|
603
|
+
return null;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return begun ? (now - base) / 1000 : null;
|
|
607
|
+
};
|
|
608
|
+
const noteStyle = (r: StyleReject) => {
|
|
609
|
+
if (rejected.length < 200) rejected.push(r);
|
|
610
|
+
};
|
|
611
|
+
(() => {
|
|
612
|
+
const proto = w.CSSStyleDeclaration?.prototype as CSSStyleDeclaration | undefined;
|
|
613
|
+
if (!proto) return;
|
|
614
|
+
const realGet = proto.getPropertyValue;
|
|
615
|
+
const realSetProperty = proto.setProperty;
|
|
616
|
+
const realRemove = proto.removeProperty;
|
|
617
|
+
// a declaration of its own, taken before `style` hands out proxies
|
|
618
|
+
const scratch = document.createElement("div").style;
|
|
619
|
+
const takesCache = new Map<string, boolean>();
|
|
620
|
+
/** whether a fresh declaration holds `value` for `prop` once it is set */
|
|
621
|
+
const takes = (prop: string, value: string): boolean => {
|
|
622
|
+
const key = `${prop}\n${value}`;
|
|
623
|
+
const known = takesCache.get(key);
|
|
624
|
+
if (known !== undefined) return known;
|
|
625
|
+
realSetProperty.call(scratch, prop, value);
|
|
626
|
+
const ok = realGet.call(scratch, prop) !== "";
|
|
627
|
+
realRemove.call(scratch, prop);
|
|
628
|
+
if (takesCache.size > 4000) takesCache.clear();
|
|
629
|
+
takesCache.set(key, ok);
|
|
630
|
+
return ok;
|
|
631
|
+
};
|
|
632
|
+
// the element each inline declaration belongs to (see the style getter)
|
|
633
|
+
const owners = new WeakMap<CSSStyleDeclaration, Element>();
|
|
634
|
+
/** a short name for the element (or rule) a declaration belongs to */
|
|
635
|
+
const nameOf = (decl: CSSStyleDeclaration): string => {
|
|
636
|
+
const rule = decl.parentRule as CSSStyleRule | null;
|
|
637
|
+
if (rule) return `the rule ${rule.selectorText ?? rule.cssText.slice(0, 60)}`;
|
|
638
|
+
const el = owners.get(decl);
|
|
639
|
+
if (!el) return "an element";
|
|
640
|
+
const one = (e: Element) => {
|
|
641
|
+
let s = e.tagName.toLowerCase();
|
|
642
|
+
if (e.id) return `${s}#${e.id}`;
|
|
643
|
+
const cls = Array.from(e.classList).slice(0, 2);
|
|
644
|
+
if (cls.length) s += `.${cls.join(".")}`;
|
|
645
|
+
const p = e.parentElement;
|
|
646
|
+
if (p) {
|
|
647
|
+
const same = Array.from(p.children).filter((c) => c.tagName === e.tagName);
|
|
648
|
+
if (same.length > 1) s += `:nth-of-type(${same.indexOf(e) + 1})`;
|
|
649
|
+
}
|
|
650
|
+
return s;
|
|
651
|
+
};
|
|
652
|
+
const path: string[] = [];
|
|
653
|
+
for (let e: Element | null = el; e && path.length < 3; e = e.parentElement) {
|
|
654
|
+
if (e === document.body || e === document.documentElement) break;
|
|
655
|
+
path.unshift(one(e));
|
|
656
|
+
if (e.id) break;
|
|
657
|
+
}
|
|
658
|
+
return (
|
|
659
|
+
(path.join(" > ") || el.tagName.toLowerCase()) +
|
|
660
|
+
(el.isConnected ? "" : " (not on the page)")
|
|
661
|
+
);
|
|
662
|
+
};
|
|
663
|
+
const check = (
|
|
664
|
+
decl: CSSStyleDeclaration,
|
|
665
|
+
prop: string,
|
|
666
|
+
value: unknown,
|
|
667
|
+
before: string,
|
|
668
|
+
after: string,
|
|
669
|
+
) => {
|
|
670
|
+
const custom = prop.startsWith("--");
|
|
671
|
+
if (!custom && after !== before) return;
|
|
672
|
+
let seen = rejectedSeen.get(decl);
|
|
673
|
+
if (seen?.has(prop)) return;
|
|
674
|
+
const str = String(value);
|
|
675
|
+
let how: StyleReject["how"];
|
|
676
|
+
if (custom) {
|
|
677
|
+
if (!SUSPECT.test(str)) return;
|
|
678
|
+
how = "suspect";
|
|
679
|
+
} else {
|
|
680
|
+
if (takes(prop, str)) return;
|
|
681
|
+
how = "ignored";
|
|
682
|
+
}
|
|
683
|
+
if (!seen) {
|
|
684
|
+
seen = new Set();
|
|
685
|
+
rejectedSeen.set(decl, seen);
|
|
686
|
+
}
|
|
687
|
+
seen.add(prop);
|
|
688
|
+
const r: StyleReject = { el: nameOf(decl), prop, value: str, kept: before, how, t: filmT() };
|
|
689
|
+
if (holder?.noteStyle) holder.noteStyle(r);
|
|
690
|
+
else noteStyle(r);
|
|
691
|
+
};
|
|
692
|
+
const kebab = (name: string) =>
|
|
693
|
+
name === "cssFloat"
|
|
694
|
+
? "float"
|
|
695
|
+
: name.includes("-")
|
|
696
|
+
? name
|
|
697
|
+
: name
|
|
698
|
+
.replace(/^webkit(?=[A-Z])/, "-webkit")
|
|
699
|
+
.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
|
|
700
|
+
const kebabs = new Map<string, string>();
|
|
701
|
+
// the declaration behind a proxy (see the style getter below): a native
|
|
702
|
+
// method called on the proxy itself, `CSSStyleDeclaration.prototype
|
|
703
|
+
// .getPropertyValue.call(el.style, …)`, reaches the declaration
|
|
704
|
+
const targets = new WeakMap<object, CSSStyleDeclaration>();
|
|
705
|
+
const self = (o: CSSStyleDeclaration) => targets.get(o) ?? o;
|
|
706
|
+
for (const name of [
|
|
707
|
+
"getPropertyValue",
|
|
708
|
+
"getPropertyPriority",
|
|
709
|
+
"removeProperty",
|
|
710
|
+
"item",
|
|
711
|
+
] as const) {
|
|
712
|
+
const real = proto[name] as (...a: unknown[]) => unknown;
|
|
713
|
+
if (typeof real !== "function") continue;
|
|
714
|
+
// biome-ignore lint/suspicious/noExplicitAny: the prototype's own methods
|
|
715
|
+
(proto as any)[name] = function (this: CSSStyleDeclaration, ...a: unknown[]) {
|
|
716
|
+
return real.apply(self(this), a);
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
// setProperty, on every declaration
|
|
720
|
+
proto.setProperty = function (
|
|
721
|
+
this: CSSStyleDeclaration,
|
|
722
|
+
...args: [string, string | null, string?]
|
|
723
|
+
) {
|
|
724
|
+
const d = self(this);
|
|
725
|
+
const [p, v] = args;
|
|
726
|
+
if (v == null || v === "" || typeof p !== "string") return realSetProperty.apply(d, args);
|
|
727
|
+
const prop = p.startsWith("--") ? p : p.toLowerCase();
|
|
728
|
+
const before = realGet.call(d, prop);
|
|
729
|
+
realSetProperty.apply(d, args);
|
|
730
|
+
const after = realGet.call(d, prop);
|
|
731
|
+
if (after === before || prop.startsWith("--")) check(d, prop, v, before, after);
|
|
732
|
+
};
|
|
733
|
+
// `el.style.transform = …`: Chrome keeps each property on the declaration
|
|
734
|
+
// itself, where no setter can be wrapped. An element's `style` is handed
|
|
735
|
+
// out as a proxy of its declaration — one per declaration, so it is the
|
|
736
|
+
// same object every time — whose set notices a property that did not take;
|
|
737
|
+
// everything else (reads, methods, `in`, keys) reaches the declaration as
|
|
738
|
+
// it is.
|
|
739
|
+
const proxies = new WeakMap<CSSStyleDeclaration, CSSStyleDeclaration>();
|
|
740
|
+
const proxyOf = (decl: CSSStyleDeclaration, el: Element): CSSStyleDeclaration => {
|
|
741
|
+
const had = proxies.get(decl);
|
|
742
|
+
if (had) return had;
|
|
743
|
+
owners.set(decl, el);
|
|
744
|
+
const methods = new Map<PropertyKey, unknown>();
|
|
745
|
+
const p = new Proxy(decl, {
|
|
746
|
+
get(t, k) {
|
|
747
|
+
const v = Reflect.get(t, k, t);
|
|
748
|
+
if (typeof v !== "function") return v;
|
|
749
|
+
let m = methods.get(k);
|
|
750
|
+
if (!m || (m as { real?: unknown }).real !== v) {
|
|
751
|
+
const bound = (...a: unknown[]) => (v as (...x: unknown[]) => unknown).apply(t, a);
|
|
752
|
+
(bound as unknown as { real: unknown }).real = v;
|
|
753
|
+
m = bound;
|
|
754
|
+
methods.set(k, bound);
|
|
755
|
+
}
|
|
756
|
+
return m;
|
|
757
|
+
},
|
|
758
|
+
set(t, k, v) {
|
|
759
|
+
if (
|
|
760
|
+
typeof k !== "string" ||
|
|
761
|
+
k === "cssText" ||
|
|
762
|
+
v == null ||
|
|
763
|
+
v === "" ||
|
|
764
|
+
typeof Reflect.get(t, k, t) !== "string"
|
|
765
|
+
)
|
|
766
|
+
return Reflect.set(t, k, v, t);
|
|
767
|
+
let prop = kebabs.get(k);
|
|
768
|
+
if (prop === undefined) {
|
|
769
|
+
prop = kebab(k);
|
|
770
|
+
kebabs.set(k, prop);
|
|
771
|
+
}
|
|
772
|
+
const before = realGet.call(t, prop);
|
|
773
|
+
const ok = Reflect.set(t, k, v, t);
|
|
774
|
+
const after = realGet.call(t, prop);
|
|
775
|
+
if (after === before) check(t, prop, v, before, after);
|
|
776
|
+
return ok;
|
|
777
|
+
},
|
|
778
|
+
});
|
|
779
|
+
proxies.set(decl, p);
|
|
780
|
+
targets.set(p, decl);
|
|
781
|
+
return p;
|
|
782
|
+
};
|
|
783
|
+
for (const C of [w.HTMLElement, w.SVGElement, w.MathMLElement]) {
|
|
784
|
+
const d = C && Object.getOwnPropertyDescriptor(C.prototype, "style");
|
|
785
|
+
if (!d?.get || !d.configurable) continue;
|
|
786
|
+
const get = d.get;
|
|
787
|
+
Object.defineProperty(C.prototype, "style", {
|
|
788
|
+
...d,
|
|
789
|
+
get(this: Element) {
|
|
790
|
+
const decl = get.call(this) as CSSStyleDeclaration;
|
|
791
|
+
return decl ? proxyOf(decl, this) : decl;
|
|
792
|
+
},
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
})();
|
|
796
|
+
|
|
797
|
+
// when each <iframe> and <video> was put on the page: it loads in real
|
|
798
|
+
// time, after the clock has moved on, and must start where it was inserted.
|
|
799
|
+
// Lazy loading waits for a viewport a film never scrolls into, so what is
|
|
800
|
+
// lazy loads at once. Records are drained synchronously after every
|
|
801
|
+
// callback that could insert.
|
|
802
|
+
const inserted = new WeakMap<Element, number>();
|
|
803
|
+
// frames put on the page that have not shown yet (see after), and how many
|
|
804
|
+
// have
|
|
805
|
+
const unshown = new Set<HTMLIFrameElement>();
|
|
806
|
+
let shown = 0;
|
|
807
|
+
const noteInserted = (records: MutationRecord[]) => {
|
|
808
|
+
for (const r of records) {
|
|
809
|
+
for (const n of Array.from(r.addedNodes)) {
|
|
810
|
+
if (!(n instanceof Element)) continue;
|
|
811
|
+
const els = Array.from(n.querySelectorAll("iframe,video,img"));
|
|
812
|
+
if (n.matches("iframe,video,img")) els.push(n);
|
|
813
|
+
for (const el of els) {
|
|
814
|
+
if (!(el instanceof HTMLImageElement) && !inserted.has(el)) inserted.set(el, clock());
|
|
815
|
+
if (el instanceof HTMLIFrameElement) unshown.add(el);
|
|
816
|
+
const lazy = el as HTMLImageElement | HTMLIFrameElement;
|
|
817
|
+
if (lazy.loading === "lazy") lazy.loading = "eager";
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
const watcher = new MutationObserver(noteInserted);
|
|
823
|
+
watcher.observe(document, { childList: true, subtree: true });
|
|
824
|
+
const drain = () => noteInserted(watcher.takeRecords());
|
|
825
|
+
|
|
826
|
+
// an error in the page's own callback fails the render, named, instead of
|
|
827
|
+
// leaving a frame half drawn: rethrown on a clean stack the renderer watches
|
|
828
|
+
const report = (e: unknown) => {
|
|
829
|
+
realSetTimeout(() => {
|
|
830
|
+
throw e;
|
|
831
|
+
}, 0);
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
// In a browser every timer and animation-frame callback is a task of its
|
|
835
|
+
// own: the promises it settles run before anything else does, so an `await`
|
|
836
|
+
// resumes at the time its timer was due. A message on the clock's own
|
|
837
|
+
// channel is such a task boundary.
|
|
838
|
+
const channel = new MessageChannel();
|
|
839
|
+
const flushed: (() => void)[] = [];
|
|
840
|
+
channel.port1.onmessage = () => flushed.shift()?.();
|
|
841
|
+
const flush = () =>
|
|
842
|
+
new Promise<void>((ok) => {
|
|
843
|
+
flushed.push(ok);
|
|
844
|
+
channel.port2.postMessage(null);
|
|
845
|
+
});
|
|
846
|
+
/** after each of the page's callbacks: what it settled runs on, at its
|
|
847
|
+
* time; an animation it started, here or in a frame, runs from then; and a
|
|
848
|
+
* frame it put on the page loads and shows before the clock moves on */
|
|
849
|
+
async function after(): Promise<void> {
|
|
850
|
+
await flush();
|
|
851
|
+
stamp();
|
|
852
|
+
await showFrames();
|
|
853
|
+
}
|
|
854
|
+
/** animations not seen before start now */
|
|
855
|
+
function stamp(): void {
|
|
856
|
+
drain();
|
|
857
|
+
if (!drawn()) for (const a of document.getAnimations()) if (!born.has(a)) born.set(a, clock());
|
|
858
|
+
for (const f of Array.from(document.querySelectorAll("iframe"))) clockOf(f)?.stamp();
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// --- random: seeded (mulberry32) -----------------------------------------
|
|
862
|
+
let seed = opts.seed >>> 0;
|
|
863
|
+
/** a page that draws any moment on request draws each moment with the same
|
|
864
|
+
* numbers, whichever worker draws it and in whatever order */
|
|
865
|
+
const reseed = (tSec: number) => {
|
|
866
|
+
let h = (opts.seed ^ Math.round(tSec * 1e6)) >>> 0;
|
|
867
|
+
h = Math.imul(h ^ (h >>> 16), 0x45d9f3b) >>> 0;
|
|
868
|
+
seed = (h ^ (h >>> 16)) >>> 0;
|
|
869
|
+
};
|
|
870
|
+
Math.random = () => {
|
|
871
|
+
seed = (seed + 0x6d2b79f5) >>> 0;
|
|
872
|
+
let t = seed;
|
|
873
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
874
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
875
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
// --- time sources ---------------------------------------------------------
|
|
879
|
+
performance.now = () => clock();
|
|
880
|
+
class FilmDate extends RealDate {
|
|
881
|
+
constructor(...args: unknown[]) {
|
|
882
|
+
if (args.length === 0) super(opts.epochMs + clock());
|
|
883
|
+
// biome-ignore lint/suspicious/noExplicitAny: Date's overloads
|
|
884
|
+
else super(...(args as [any]));
|
|
885
|
+
}
|
|
886
|
+
static override now() {
|
|
887
|
+
return opts.epochMs + clock();
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
w.Date = FilmDate;
|
|
891
|
+
|
|
892
|
+
type Timer = { due: number; fn: unknown; args: unknown[]; every: number };
|
|
893
|
+
const timers = new Map<number, Timer>();
|
|
894
|
+
let nextTimer = 1;
|
|
895
|
+
w.setTimeout = (fn: unknown, delay?: number, ...args: unknown[]) => {
|
|
896
|
+
const id = nextTimer++;
|
|
897
|
+
timers.set(id, { due: schedNow() + Math.max(0, Number(delay) || 0), fn, args, every: 0 });
|
|
898
|
+
return id;
|
|
899
|
+
};
|
|
900
|
+
w.setInterval = (fn: unknown, delay?: number, ...args: unknown[]) => {
|
|
901
|
+
const id = nextTimer++;
|
|
902
|
+
const every = Math.max(1, Number(delay) || 0);
|
|
903
|
+
timers.set(id, { due: schedNow() + every, fn, args, every });
|
|
904
|
+
return id;
|
|
905
|
+
};
|
|
906
|
+
w.clearTimeout = w.clearInterval = (id: number) => {
|
|
907
|
+
timers.delete(id);
|
|
908
|
+
};
|
|
909
|
+
w.requestIdleCallback = (fn: (d: unknown) => void) =>
|
|
910
|
+
w.setTimeout(() => fn({ didTimeout: false, timeRemaining: () => 50 }), 0);
|
|
911
|
+
w.cancelIdleCallback = (id: number) => timers.delete(id);
|
|
912
|
+
|
|
913
|
+
let frames = new Map<number, (t: number) => void>();
|
|
914
|
+
let nextFrame = 1;
|
|
915
|
+
w.requestAnimationFrame = (fn: (t: number) => void) => {
|
|
916
|
+
const id = nextFrame++;
|
|
917
|
+
frames.set(id, fn);
|
|
918
|
+
return id;
|
|
919
|
+
};
|
|
920
|
+
w.cancelAnimationFrame = (id: number) => {
|
|
921
|
+
frames.delete(id);
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
// --- loading: what the page asked for that is still on its way. Before the
|
|
925
|
+
// film starts the clock moves on only while nothing is (see pump) ----------
|
|
926
|
+
let loading = 0;
|
|
927
|
+
const track = <T>(p: Promise<T>): Promise<T> => {
|
|
928
|
+
loading++;
|
|
929
|
+
const done = () => {
|
|
930
|
+
loading--;
|
|
931
|
+
};
|
|
932
|
+
p.then(done, done);
|
|
933
|
+
return p;
|
|
934
|
+
};
|
|
935
|
+
if (typeof w.fetch === "function") {
|
|
936
|
+
const realFetch = w.fetch.bind(w);
|
|
937
|
+
w.fetch = (...a: unknown[]) => track(realFetch(...a));
|
|
938
|
+
}
|
|
939
|
+
// biome-ignore lint/suspicious/noExplicitAny: the body readers, by name
|
|
940
|
+
const body = Response.prototype as any;
|
|
941
|
+
for (const k of ["arrayBuffer", "blob", "bytes", "formData", "json", "text"]) {
|
|
942
|
+
const real = body[k];
|
|
943
|
+
if (typeof real === "function")
|
|
944
|
+
body[k] = function (this: Response, ...a: unknown[]) {
|
|
945
|
+
return track(real.apply(this, a));
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
const realSend = XMLHttpRequest.prototype.send;
|
|
949
|
+
XMLHttpRequest.prototype.send = function (this: XMLHttpRequest, ...a: unknown[]) {
|
|
950
|
+
loading++;
|
|
951
|
+
let counted = true;
|
|
952
|
+
const done = () => {
|
|
953
|
+
if (counted) loading--;
|
|
954
|
+
counted = false;
|
|
955
|
+
};
|
|
956
|
+
// added first: a synchronous request ends inside send
|
|
957
|
+
this.addEventListener("loadend", done, { once: true });
|
|
958
|
+
try {
|
|
959
|
+
// biome-ignore lint/suspicious/noExplicitAny: send's one optional body
|
|
960
|
+
return realSend.apply(this, a as any);
|
|
961
|
+
} catch (e) {
|
|
962
|
+
this.removeEventListener("loadend", done);
|
|
963
|
+
done();
|
|
964
|
+
throw e;
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
// a body read as a stream counts while each read waits
|
|
968
|
+
// biome-ignore lint/suspicious/noExplicitAny: the stream readers, by name
|
|
969
|
+
const streams: any[] = [
|
|
970
|
+
w.ReadableStreamDefaultReader?.prototype,
|
|
971
|
+
w.ReadableStreamBYOBReader?.prototype,
|
|
972
|
+
];
|
|
973
|
+
for (const proto of streams) {
|
|
974
|
+
const real = proto?.read;
|
|
975
|
+
if (typeof real === "function")
|
|
976
|
+
proto.read = function (this: unknown, ...a: unknown[]) {
|
|
977
|
+
return track(real.apply(this, a));
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
const stream = ReadableStream.prototype;
|
|
981
|
+
const realPipe = stream.pipeTo;
|
|
982
|
+
stream.pipeTo = function (this: ReadableStream, ...a: Parameters<typeof realPipe>) {
|
|
983
|
+
return track(realPipe.apply(this, a));
|
|
984
|
+
};
|
|
985
|
+
for (const k of ["values", Symbol.asyncIterator]) {
|
|
986
|
+
// biome-ignore lint/suspicious/noExplicitAny: the stream's iterator, by name
|
|
987
|
+
const proto = stream as any;
|
|
988
|
+
const real = proto[k];
|
|
989
|
+
if (typeof real !== "function") continue;
|
|
990
|
+
proto[k] = function (this: ReadableStream, ...a: unknown[]) {
|
|
991
|
+
const it = real.apply(this, a);
|
|
992
|
+
const next = it.next.bind(it);
|
|
993
|
+
it.next = () => track(next());
|
|
994
|
+
return it;
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
// an image made in script (a texture, say) counts while it loads; the
|
|
998
|
+
// document's own images are looked at directly (see busy)
|
|
999
|
+
for (const k of ["src", "srcset"]) {
|
|
1000
|
+
const d = Object.getOwnPropertyDescriptor(HTMLImageElement.prototype, k);
|
|
1001
|
+
const set = d?.set;
|
|
1002
|
+
if (!set) continue;
|
|
1003
|
+
Object.defineProperty(HTMLImageElement.prototype, k, {
|
|
1004
|
+
...d,
|
|
1005
|
+
set(this: HTMLImageElement, v: unknown) {
|
|
1006
|
+
set.call(this, v);
|
|
1007
|
+
if (this.isConnected || this.complete) return;
|
|
1008
|
+
loading++;
|
|
1009
|
+
let counted = true;
|
|
1010
|
+
const done = () => {
|
|
1011
|
+
if (counted) loading--;
|
|
1012
|
+
counted = false;
|
|
1013
|
+
};
|
|
1014
|
+
this.addEventListener("load", done, { once: true });
|
|
1015
|
+
this.addEventListener("error", done, { once: true });
|
|
1016
|
+
},
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
const realDecode = HTMLImageElement.prototype.decode;
|
|
1020
|
+
HTMLImageElement.prototype.decode = function (this: HTMLImageElement) {
|
|
1021
|
+
return track(realDecode.call(this));
|
|
1022
|
+
};
|
|
1023
|
+
if (typeof w.createImageBitmap === "function") {
|
|
1024
|
+
const realBitmap = w.createImageBitmap.bind(w);
|
|
1025
|
+
w.createImageBitmap = (...a: unknown[]) => track(realBitmap(...a));
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
/** run every timer due by `until`, in due order. The clock moves to each
|
|
1029
|
+
* timer's time — unless `hold`: a __render draws one instant, so the timers
|
|
1030
|
+
* it waits on run at that instant (a page reading the clock reads t). */
|
|
1031
|
+
async function runTimers(until: number, hold = false): Promise<void> {
|
|
1032
|
+
for (let guard = 0; guard < 100_000; guard++) {
|
|
1033
|
+
let pickId = 0;
|
|
1034
|
+
let pick: Timer | undefined;
|
|
1035
|
+
for (const [id, t] of timers)
|
|
1036
|
+
if (t.due <= until && (!pick || t.due < pick.due)) {
|
|
1037
|
+
pick = t;
|
|
1038
|
+
pickId = id;
|
|
1039
|
+
}
|
|
1040
|
+
if (!pick) return;
|
|
1041
|
+
if (hold) timerNow = Math.max(timerNow, pick.due);
|
|
1042
|
+
else {
|
|
1043
|
+
now = Math.max(now, pick.due);
|
|
1044
|
+
// what the callback reads of an animation is where it is at its time
|
|
1045
|
+
if (clock() !== syncedAt) syncAnimations();
|
|
1046
|
+
}
|
|
1047
|
+
if (pick.every) pick.due += pick.every;
|
|
1048
|
+
else timers.delete(pickId);
|
|
1049
|
+
try {
|
|
1050
|
+
if (typeof pick.fn === "function") pick.fn(...pick.args);
|
|
1051
|
+
// setTimeout("code") is part of the timer API this clock stands in for
|
|
1052
|
+
// biome-ignore lint/complexity/noCommaOperator: an indirect (global) eval, as the browser's own
|
|
1053
|
+
// biome-ignore lint/security/noGlobalEval: the page's own string handler, run as the browser would
|
|
1054
|
+
else (0, eval)(String(pick.fn));
|
|
1055
|
+
} catch (e) {
|
|
1056
|
+
report(e);
|
|
1057
|
+
}
|
|
1058
|
+
await after();
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/** the page's animation frame: its callbacks, then the ones it asked the
|
|
1063
|
+
* frames inside it for (see asked) */
|
|
1064
|
+
async function runFrames(only?: (fn: unknown) => boolean, at = clock()): Promise<void> {
|
|
1065
|
+
let batch = frames;
|
|
1066
|
+
if (only) {
|
|
1067
|
+
batch = new Map([...frames].filter(([, fn]) => only(fn)));
|
|
1068
|
+
for (const id of batch.keys()) frames.delete(id);
|
|
1069
|
+
} else frames = new Map();
|
|
1070
|
+
for (const fn of batch.values()) {
|
|
1071
|
+
try {
|
|
1072
|
+
fn(at);
|
|
1073
|
+
} catch (e) {
|
|
1074
|
+
report(e);
|
|
1075
|
+
}
|
|
1076
|
+
await after();
|
|
1077
|
+
}
|
|
1078
|
+
await eachFrame(async (f) => clockOf(f)?.runAsked(at));
|
|
1079
|
+
// what those put on this page shows before the clock moves on, too
|
|
1080
|
+
await showFrames();
|
|
1081
|
+
}
|
|
1082
|
+
// A frame runs its own animation frames only where it was put on the page,
|
|
1083
|
+
// but a page may ask one for its next animation frame — to wait until it
|
|
1084
|
+
// has drawn, say. Such a callback belongs to the page (it comes from the
|
|
1085
|
+
// page's own script, not the frame's) and runs on the page's animation
|
|
1086
|
+
// frames, at the page's time.
|
|
1087
|
+
const asked = (fn: unknown) => !(fn instanceof w.Function);
|
|
1088
|
+
const askedHere = (): boolean =>
|
|
1089
|
+
[...frames.values()].some(asked) ||
|
|
1090
|
+
Array.from(document.querySelectorAll("iframe")).some((f) => clockOf(f)?.asked());
|
|
1091
|
+
/** an animation frame is waited for: the page's own, or one it asked a frame for */
|
|
1092
|
+
const framesDue = () => frames.size > 0 || askedHere();
|
|
1093
|
+
|
|
1094
|
+
// --- animations: every one paused and set from film time -------------------
|
|
1095
|
+
const born = new WeakMap<Animation, number>();
|
|
1096
|
+
let syncedAt = Number.NaN;
|
|
1097
|
+
function syncAnimations(): void {
|
|
1098
|
+
const t = clock();
|
|
1099
|
+
syncedAt = t;
|
|
1100
|
+
// a page that draws any moment on request jumps around in time: its
|
|
1101
|
+
// animations (and its frames') all run from film time 0. In one that
|
|
1102
|
+
// animates on its own an animation runs from the callback that started
|
|
1103
|
+
// it, or from where it was first seen; one that started before the film
|
|
1104
|
+
// did starts with it
|
|
1105
|
+
const fromZero = drawn();
|
|
1106
|
+
for (const a of document.getAnimations()) {
|
|
1107
|
+
let b = base;
|
|
1108
|
+
if (!fromZero) {
|
|
1109
|
+
if (!born.has(a)) born.set(a, t);
|
|
1110
|
+
b = Math.max(base, born.get(a)!);
|
|
1111
|
+
}
|
|
1112
|
+
if (a.playState !== "paused") a.pause();
|
|
1113
|
+
a.currentTime = Math.max(0, (t - b) * (a.playbackRate || 1));
|
|
1114
|
+
}
|
|
1115
|
+
for (const svg of Array.from(document.querySelectorAll("svg"))) {
|
|
1116
|
+
if (svg.ownerSVGElement) continue;
|
|
1117
|
+
if (!svg.querySelector("animate,animateTransform,animateMotion,set")) continue;
|
|
1118
|
+
svg.pauseAnimations();
|
|
1119
|
+
svg.setCurrentTime((t - base) / 1000);
|
|
1120
|
+
}
|
|
1121
|
+
// a page that reads a frame's animation reads it at its time too
|
|
1122
|
+
for (const f of Array.from(document.querySelectorAll("iframe"))) clockOf(f)?.sync();
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// --- media: a video plays on film time, seeked frame by frame ---------------
|
|
1126
|
+
// A video the page plays — or that plays by itself (autoplay), from where it
|
|
1127
|
+
// was put on the page — plays on film time: where it is follows from when
|
|
1128
|
+
// it started. One the page pauses stays where it was; one the page sets to
|
|
1129
|
+
// a time goes there. That place is what the page reads of the video, at
|
|
1130
|
+
// once; the element is sought there before a frame is taken (see syncMedia).
|
|
1131
|
+
type Media = {
|
|
1132
|
+
/** taken over: muted, its own playback stopped (see syncMedia) */
|
|
1133
|
+
seen: boolean;
|
|
1134
|
+
/** the page has played, paused or set it */
|
|
1135
|
+
touched: boolean;
|
|
1136
|
+
playing: boolean;
|
|
1137
|
+
/** playing: the time it was at 0 */
|
|
1138
|
+
born: number;
|
|
1139
|
+
/** paused: where it stays */
|
|
1140
|
+
at: number;
|
|
1141
|
+
};
|
|
1142
|
+
const media = new WeakMap<HTMLMediaElement, Media>();
|
|
1143
|
+
const mediaOf = (el: HTMLMediaElement): Media => {
|
|
1144
|
+
let m = media.get(el);
|
|
1145
|
+
if (!m) {
|
|
1146
|
+
m = { seen: false, touched: false, playing: false, born: 0, at: Number.NaN };
|
|
1147
|
+
media.set(el, m);
|
|
1148
|
+
}
|
|
1149
|
+
return m;
|
|
1150
|
+
};
|
|
1151
|
+
// what plays by itself: its autoplay is switched off when it is taken over
|
|
1152
|
+
const autoplay = new WeakSet<HTMLMediaElement>();
|
|
1153
|
+
/** a video that will play by itself, untouched by the page */
|
|
1154
|
+
const autoplays = (el: HTMLMediaElement, m: Media) =>
|
|
1155
|
+
!m.seen && !m.touched && (el.autoplay || autoplay.has(el));
|
|
1156
|
+
/** how many times a video was loaded: what was asked of it before it was
|
|
1157
|
+
* loaded again (a seek, a frame to show) is not done to what it loads */
|
|
1158
|
+
const loads = new WeakMap<HTMLMediaElement, number>();
|
|
1159
|
+
/** a video loaded again (load(), a new source) starts over where it is
|
|
1160
|
+
* loaded: at 0, playing by itself from there if it does. One that had
|
|
1161
|
+
* nothing to play keeps playing if the page had played it, as the element
|
|
1162
|
+
* does; one that had something stops. The frame it showed goes with what
|
|
1163
|
+
* it had: it shows what it loads */
|
|
1164
|
+
function startOver(el: HTMLMediaElement, hadNothing: boolean): void {
|
|
1165
|
+
const playing = hadNothing && !!media.get(el)?.playing;
|
|
1166
|
+
media.delete(el);
|
|
1167
|
+
loads.set(el, (loads.get(el) ?? 0) + 1);
|
|
1168
|
+
seeking.delete(el);
|
|
1169
|
+
lastAsked.delete(el);
|
|
1170
|
+
unframe(el);
|
|
1171
|
+
inserted.set(el, clock());
|
|
1172
|
+
// from where it got it — in a film that draws itself, from film time 0,
|
|
1173
|
+
// as what plays by itself does there
|
|
1174
|
+
if (playing)
|
|
1175
|
+
Object.assign(mediaOf(el), { touched: true, playing: true, born: drawn() ? base : clock() });
|
|
1176
|
+
}
|
|
1177
|
+
const realLoad = HTMLMediaElement.prototype.load;
|
|
1178
|
+
HTMLMediaElement.prototype.load = function (this: HTMLMediaElement) {
|
|
1179
|
+
startOver(this, this.networkState === HTMLMediaElement.NETWORK_EMPTY);
|
|
1180
|
+
return realLoad.call(this);
|
|
1181
|
+
};
|
|
1182
|
+
// a new source loads it again too — at once, so what the page sets on the
|
|
1183
|
+
// video right after applies to the new one; one the element refuses
|
|
1184
|
+
// changes nothing (the element's own checks come first)
|
|
1185
|
+
const source = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "src")!;
|
|
1186
|
+
Object.defineProperty(HTMLMediaElement.prototype, "src", {
|
|
1187
|
+
...source,
|
|
1188
|
+
set(this: HTMLMediaElement, v: string) {
|
|
1189
|
+
const hadNothing = this.networkState === HTMLMediaElement.NETWORK_EMPTY;
|
|
1190
|
+
source.set!.call(this, v);
|
|
1191
|
+
startOver(this, hadNothing);
|
|
1192
|
+
},
|
|
1193
|
+
});
|
|
1194
|
+
// so does a stream, a MediaSource or a blob given as its source object
|
|
1195
|
+
const sourceObject = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "srcObject");
|
|
1196
|
+
if (sourceObject?.set)
|
|
1197
|
+
Object.defineProperty(HTMLMediaElement.prototype, "srcObject", {
|
|
1198
|
+
...sourceObject,
|
|
1199
|
+
set(this: HTMLMediaElement, v: unknown) {
|
|
1200
|
+
const hadNothing = this.networkState === HTMLMediaElement.NETWORK_EMPTY;
|
|
1201
|
+
sourceObject.set!.call(this, v);
|
|
1202
|
+
startOver(this, hadNothing);
|
|
1203
|
+
},
|
|
1204
|
+
});
|
|
1205
|
+
const realSetAttribute = Element.prototype.setAttribute;
|
|
1206
|
+
Element.prototype.setAttribute = function (this: Element, name: string, value: string) {
|
|
1207
|
+
const n = `${name}`; // as the element converts it: once, and an error as its own
|
|
1208
|
+
const isSource = this instanceof HTMLMediaElement && n.toLowerCase() === "src";
|
|
1209
|
+
const hadNothing =
|
|
1210
|
+
isSource && (this as HTMLMediaElement).networkState === HTMLMediaElement.NETWORK_EMPTY;
|
|
1211
|
+
realSetAttribute.call(this, n, value);
|
|
1212
|
+
if (isSource) startOver(this as HTMLMediaElement, hadNothing);
|
|
1213
|
+
};
|
|
1214
|
+
const realRemoveAttribute = Element.prototype.removeAttribute;
|
|
1215
|
+
Element.prototype.removeAttribute = function (this: Element, name: string) {
|
|
1216
|
+
const n = `${name}`;
|
|
1217
|
+
const isSource =
|
|
1218
|
+
this instanceof HTMLMediaElement && n.toLowerCase() === "src" && this.hasAttribute("src");
|
|
1219
|
+
const hadNothing =
|
|
1220
|
+
isSource && (this as HTMLMediaElement).networkState === HTMLMediaElement.NETWORK_EMPTY;
|
|
1221
|
+
realRemoveAttribute.call(this, n);
|
|
1222
|
+
if (isSource) startOver(this as HTMLMediaElement, hadNothing);
|
|
1223
|
+
};
|
|
1224
|
+
/** when a video that plays by itself started: where it was put on the page */
|
|
1225
|
+
const autoBorn = (el: HTMLMediaElement) =>
|
|
1226
|
+
drawn() ? base : Math.max(base, inserted.get(el) ?? clock());
|
|
1227
|
+
/** where a video is on film time; null for one the page has left to itself */
|
|
1228
|
+
function position(el: HTMLMediaElement): number | null {
|
|
1229
|
+
const m = mediaOf(el);
|
|
1230
|
+
const d = el.duration;
|
|
1231
|
+
const known = Number.isFinite(d) && d > 0;
|
|
1232
|
+
if (!m.playing && !autoplays(el, m)) {
|
|
1233
|
+
if (!m.touched || !Number.isFinite(m.at)) return null;
|
|
1234
|
+
return Math.max(0, known ? Math.min(m.at, d) : m.at);
|
|
1235
|
+
}
|
|
1236
|
+
let at = (clock() - (m.playing ? m.born : autoBorn(el))) / 1000;
|
|
1237
|
+
if (known) at = el.loop ? at % d : Math.min(at, d - 0.001);
|
|
1238
|
+
return Math.max(0, at);
|
|
1239
|
+
}
|
|
1240
|
+
// a video sought to its frame (below) reads as seeking until that frame is
|
|
1241
|
+
// there, as one sought itself does until it has decoded it. Seeks are
|
|
1242
|
+
// numbered — the last one asked of each video, and the one to a frame that
|
|
1243
|
+
// is under way: a later seek, to a frame or the element's own, ends that
|
|
1244
|
+
// one, as the element's own does
|
|
1245
|
+
const lastSeek = new WeakMap<HTMLMediaElement, number>();
|
|
1246
|
+
const seeking = new WeakMap<HTMLMediaElement, number>();
|
|
1247
|
+
let seekCount = 0;
|
|
1248
|
+
const realSeeking = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "seeking")!;
|
|
1249
|
+
Object.defineProperty(HTMLMediaElement.prototype, "seeking", {
|
|
1250
|
+
...realSeeking,
|
|
1251
|
+
get(this: HTMLMediaElement) {
|
|
1252
|
+
return seeking.has(this) || realSeeking.get!.call(this);
|
|
1253
|
+
},
|
|
1254
|
+
});
|
|
1255
|
+
const seekTo = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "currentTime")!;
|
|
1256
|
+
Object.defineProperty(HTMLMediaElement.prototype, "currentTime", {
|
|
1257
|
+
...seekTo,
|
|
1258
|
+
get(this: HTMLMediaElement) {
|
|
1259
|
+
return position(this) ?? seekTo.get!.call(this);
|
|
1260
|
+
},
|
|
1261
|
+
set(this: HTMLMediaElement, v: number) {
|
|
1262
|
+
// a video shown as its frames is not sought: its frame at the new time
|
|
1263
|
+
// is shown, and the page told, as a real seek would (see showFrame)
|
|
1264
|
+
const virtual =
|
|
1265
|
+
this instanceof HTMLVideoElement &&
|
|
1266
|
+
asFrame.has(this) &&
|
|
1267
|
+
!readers.has(this) &&
|
|
1268
|
+
Number.isFinite(Number(v));
|
|
1269
|
+
// the element's own checks first: a time it refuses changes nothing
|
|
1270
|
+
if (!virtual) seekTo.set!.call(this, v);
|
|
1271
|
+
let n = Number(v);
|
|
1272
|
+
if (!Number.isFinite(n)) return;
|
|
1273
|
+
// as the element does: a time before 0 is 0, one past its end its end
|
|
1274
|
+
n = Math.max(0, n);
|
|
1275
|
+
if (Number.isFinite(this.duration) && this.duration > 0) n = Math.min(n, this.duration);
|
|
1276
|
+
const m = mediaOf(this);
|
|
1277
|
+
// a playing video (or one about to play by itself) plays on from there
|
|
1278
|
+
if (m.playing || autoplays(this, m)) {
|
|
1279
|
+
m.born = clock() - n * 1000;
|
|
1280
|
+
m.playing = true;
|
|
1281
|
+
} else m.at = n;
|
|
1282
|
+
m.touched = true;
|
|
1283
|
+
const seek = ++seekCount;
|
|
1284
|
+
lastSeek.set(this, seek);
|
|
1285
|
+
if (!virtual) seeking.delete(this);
|
|
1286
|
+
else {
|
|
1287
|
+
// as a real seek does: seeking at once, then seeked once the frame is
|
|
1288
|
+
// there — the events in a later task, so a listener added right after
|
|
1289
|
+
// the set still hears them. One loaded or sought again in the
|
|
1290
|
+
// meantime says no more: loading or a later seek ends it
|
|
1291
|
+
const el = this as HTMLVideoElement;
|
|
1292
|
+
const load = loads.get(el);
|
|
1293
|
+
seeking.set(el, seek);
|
|
1294
|
+
realSetTimeout(() => {
|
|
1295
|
+
if (loads.get(el) !== load) return;
|
|
1296
|
+
el.dispatchEvent(new Event("seeking"));
|
|
1297
|
+
void showFrame(el, position(el) ?? n).then((shown) => {
|
|
1298
|
+
if (loads.get(el) !== load || lastSeek.get(el) !== seek) return;
|
|
1299
|
+
seeking.delete(el);
|
|
1300
|
+
// read in the meantime, or its frame did not come: a real seek,
|
|
1301
|
+
// which tells the page itself — of the video itself, not the
|
|
1302
|
+
// frame it showed
|
|
1303
|
+
if (!shown) {
|
|
1304
|
+
unframe(el);
|
|
1305
|
+
return void seekTo.set!.call(el, n);
|
|
1306
|
+
}
|
|
1307
|
+
el.dispatchEvent(new Event("timeupdate"));
|
|
1308
|
+
el.dispatchEvent(new Event("seeked"));
|
|
1309
|
+
});
|
|
1310
|
+
}, 0);
|
|
1311
|
+
}
|
|
1312
|
+
},
|
|
1313
|
+
});
|
|
1314
|
+
HTMLMediaElement.prototype.play = function (this: HTMLMediaElement) {
|
|
1315
|
+
const m = mediaOf(this);
|
|
1316
|
+
if (!m.playing) {
|
|
1317
|
+
m.born = clock() - (position(this) ?? seekTo.get!.call(this)) * 1000;
|
|
1318
|
+
m.playing = true;
|
|
1319
|
+
}
|
|
1320
|
+
m.touched = true;
|
|
1321
|
+
return Promise.resolve();
|
|
1322
|
+
};
|
|
1323
|
+
HTMLMediaElement.prototype.pause = function (this: HTMLMediaElement) {
|
|
1324
|
+
const m = mediaOf(this);
|
|
1325
|
+
const at = position(this);
|
|
1326
|
+
if (at !== null) m.at = at;
|
|
1327
|
+
m.playing = false;
|
|
1328
|
+
m.touched = true;
|
|
1329
|
+
return realPause.call(this);
|
|
1330
|
+
};
|
|
1331
|
+
// --- a video shown as its frames --------------------------------------------
|
|
1332
|
+
// A film-timed <video> is shown as its decoded frame at the film's time —
|
|
1333
|
+
// the frame's image as the element's CSS `content`, so the page's own size,
|
|
1334
|
+
// fit, corners and transforms all hold — and is not sought: a video element
|
|
1335
|
+
// composited under a slow push shimmers, and seeking it for every frame and
|
|
1336
|
+
// every motion probe is most of a recorded film's render. The frames come
|
|
1337
|
+
// from the renderer (the __otFrames binding, answered by framesReady); a
|
|
1338
|
+
// canvas, WebGL or a VideoFrame the page makes of the video gets the frame
|
|
1339
|
+
// too. With no renderer to ask (a layout reading), for a source it does not
|
|
1340
|
+
// decode (or not in time), or for a video the page reads frame by frame
|
|
1341
|
+
// itself (a frame callback, a stream), the element is sought as before.
|
|
1342
|
+
type Frames = { base: string; times: number[]; ext: string; bright?: boolean } | null;
|
|
1343
|
+
const framesFor = new Map<string, Promise<Frames>>();
|
|
1344
|
+
const framesWaiting = new Map<string, (f: Frames) => void>();
|
|
1345
|
+
const readers = new WeakSet<HTMLMediaElement>();
|
|
1346
|
+
const asFrame = new WeakMap<HTMLMediaElement, string>();
|
|
1347
|
+
/** What a video shown as its frames shows: the frame's image, when that
|
|
1348
|
+
* frame starts, and whether the image is brighter than the video decodes
|
|
1349
|
+
* (frames.ts). Kept on the element under a key every page and frame
|
|
1350
|
+
* shares, so the page around a frame reads the frame's videos too. */
|
|
1351
|
+
type Shown = { im: HTMLImageElement; at: number; bright: boolean; plain?: HTMLCanvasElement };
|
|
1352
|
+
const SHOWN = Symbol.for("open-take frame shown");
|
|
1353
|
+
const shownOf = (x: unknown): Shown | null =>
|
|
1354
|
+
x !== null && typeof x === "object"
|
|
1355
|
+
? ((x as Record<symbol, Shown | undefined>)[SHOWN] ?? null)
|
|
1356
|
+
: null;
|
|
1357
|
+
/** show the video itself again */
|
|
1358
|
+
function unframe(v: HTMLMediaElement): void {
|
|
1359
|
+
if (asFrame.has(v)) {
|
|
1360
|
+
v.style.removeProperty("content");
|
|
1361
|
+
asFrame.delete(v);
|
|
1362
|
+
}
|
|
1363
|
+
delete (v as unknown as Record<symbol, unknown>)[SHOWN];
|
|
1364
|
+
}
|
|
1365
|
+
/** the page reads this video's pixels: it is sought from now on */
|
|
1366
|
+
const reads = (v: unknown) => {
|
|
1367
|
+
if (!(v instanceof HTMLMediaElement) || readers.has(v)) return;
|
|
1368
|
+
readers.add(v);
|
|
1369
|
+
unframe(v);
|
|
1370
|
+
};
|
|
1371
|
+
// Chrome on macOS shows video through Apple's gamma, but hands a canvas
|
|
1372
|
+
// the pixels as they decode: a frame written as shown (frames.ts) goes to
|
|
1373
|
+
// a canvas with that undone
|
|
1374
|
+
let undo: Uint8Array | null = null;
|
|
1375
|
+
function pixelsOf(f: Shown): HTMLImageElement | HTMLCanvasElement {
|
|
1376
|
+
if (!f.bright || !f.im.naturalWidth) return f.im;
|
|
1377
|
+
if (f.plain) return f.plain;
|
|
1378
|
+
if (!undo) {
|
|
1379
|
+
undo = new Uint8Array(256);
|
|
1380
|
+
for (let v = 0; v < 256; v++) {
|
|
1381
|
+
const c = v / 255;
|
|
1382
|
+
const light = c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
1383
|
+
undo[v] = Math.round(255 * light ** (1 / 1.961));
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
const c = document.createElement("canvas");
|
|
1387
|
+
c.width = f.im.naturalWidth;
|
|
1388
|
+
c.height = f.im.naturalHeight;
|
|
1389
|
+
const g = c.getContext("2d", { willReadFrequently: true })!;
|
|
1390
|
+
g.drawImage(f.im, 0, 0);
|
|
1391
|
+
const d = g.getImageData(0, 0, c.width, c.height);
|
|
1392
|
+
const p = d.data;
|
|
1393
|
+
for (let i = 0; i < p.length; i += 4) {
|
|
1394
|
+
p[i] = undo[p[i]!]!;
|
|
1395
|
+
p[i + 1] = undo[p[i + 1]!]!;
|
|
1396
|
+
p[i + 2] = undo[p[i + 2]!]!;
|
|
1397
|
+
}
|
|
1398
|
+
g.putImageData(d, 0, 0);
|
|
1399
|
+
f.plain = c;
|
|
1400
|
+
return c;
|
|
1401
|
+
}
|
|
1402
|
+
// where the page reads a video's pixels: a video shown as its frames — here
|
|
1403
|
+
// or in a frame inside the page — is handed over as its frame (the same
|
|
1404
|
+
// pixels, the same size); one not shown so yet is read as it is, sought as
|
|
1405
|
+
// before
|
|
1406
|
+
const passFrames = (owner: Record<string, unknown> | undefined, name: string) => {
|
|
1407
|
+
const real = owner?.[name];
|
|
1408
|
+
if (!owner || typeof real !== "function") return;
|
|
1409
|
+
owner[name] = function (this: unknown, ...a: unknown[]) {
|
|
1410
|
+
for (let i = 0; i < a.length; i++) {
|
|
1411
|
+
const f = shownOf(a[i]);
|
|
1412
|
+
if (f) a[i] = pixelsOf(f);
|
|
1413
|
+
}
|
|
1414
|
+
return (real as (...a: unknown[]) => unknown).apply(this, a);
|
|
1415
|
+
};
|
|
1416
|
+
};
|
|
1417
|
+
// what needs the element's own decoding: a frame callback, a stream
|
|
1418
|
+
const needsDecoding = (owner: Record<string, unknown> | undefined, name: string) => {
|
|
1419
|
+
const real = owner?.[name];
|
|
1420
|
+
if (!owner || typeof real !== "function") return;
|
|
1421
|
+
owner[name] = function (this: unknown, ...a: unknown[]) {
|
|
1422
|
+
reads(this);
|
|
1423
|
+
return (real as (...a: unknown[]) => unknown).apply(this, a);
|
|
1424
|
+
};
|
|
1425
|
+
};
|
|
1426
|
+
for (const ctx of [w.CanvasRenderingContext2D, w.OffscreenCanvasRenderingContext2D])
|
|
1427
|
+
passFrames(ctx?.prototype, "drawImage");
|
|
1428
|
+
for (const gl of [w.WebGLRenderingContext, w.WebGL2RenderingContext])
|
|
1429
|
+
for (const f of ["texImage2D", "texSubImage2D", "texImage3D", "texSubImage3D"])
|
|
1430
|
+
passFrames(gl?.prototype, f);
|
|
1431
|
+
passFrames(w, "createImageBitmap");
|
|
1432
|
+
needsDecoding(w.HTMLVideoElement?.prototype, "requestVideoFrameCallback");
|
|
1433
|
+
needsDecoding(w.HTMLMediaElement?.prototype, "captureStream");
|
|
1434
|
+
// a VideoFrame of a video shown as its frames holds the frame it shows, at
|
|
1435
|
+
// that frame's time unless the page gives one
|
|
1436
|
+
if (typeof w.VideoFrame === "function")
|
|
1437
|
+
w.VideoFrame = new Proxy(w.VideoFrame, {
|
|
1438
|
+
construct(target, args, nt) {
|
|
1439
|
+
const f = shownOf(args[0]);
|
|
1440
|
+
if (f) {
|
|
1441
|
+
const init = (args[1] ?? {}) as { timestamp?: number };
|
|
1442
|
+
args = [pixelsOf(f), { ...init, timestamp: init.timestamp ?? Math.round(f.at * 1e6) }];
|
|
1443
|
+
}
|
|
1444
|
+
return Reflect.construct(target, args, nt);
|
|
1445
|
+
},
|
|
1446
|
+
});
|
|
1447
|
+
// A call from the renderer gives up at 120 s, and a page waits for frames
|
|
1448
|
+
// inside one: in all, one call waits for frames at most FRAMES_WAIT_MS (a
|
|
1449
|
+
// frame inside the page counts with the page), and a video whose frames
|
|
1450
|
+
// have not come by then is sought, that once. One whose answer never comes
|
|
1451
|
+
// is sought from then on.
|
|
1452
|
+
const FRAMES_WAIT_MS = 75_000;
|
|
1453
|
+
let framesUntil = 0;
|
|
1454
|
+
/** the real time by which the frames of this call must be there */
|
|
1455
|
+
const framesBy = (): number =>
|
|
1456
|
+
holder?.framesBy?.() ?? (framesUntil || RealDate.now() + FRAMES_WAIT_MS);
|
|
1457
|
+
/** the renderer's answers that have come */
|
|
1458
|
+
const answered = new Map<string, Frames>();
|
|
1459
|
+
/** the frames of the video at `src`, asked of the renderer once */
|
|
1460
|
+
const framesOf = (src: string): Promise<Frames> => {
|
|
1461
|
+
let p = framesFor.get(src);
|
|
1462
|
+
if (!p) {
|
|
1463
|
+
p = new Promise<Frames>((ok) => {
|
|
1464
|
+
framesWaiting.set(src, ok);
|
|
1465
|
+
realSetTimeout(() => framesReady(src, null), FRAMES_WAIT_MS);
|
|
1466
|
+
});
|
|
1467
|
+
void p.then((f) => answered.set(src, f));
|
|
1468
|
+
framesFor.set(src, p);
|
|
1469
|
+
try {
|
|
1470
|
+
w.__otFrames(JSON.stringify({ src }));
|
|
1471
|
+
} catch {
|
|
1472
|
+
framesReady(src, null);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
return p;
|
|
1476
|
+
};
|
|
1477
|
+
/** the frames of the video at `src`; null when they do not come in time */
|
|
1478
|
+
const framesNow = (src: string): Frames | Promise<Frames> =>
|
|
1479
|
+
answered.has(src)
|
|
1480
|
+
? (answered.get(src) ?? null)
|
|
1481
|
+
: Promise.race([
|
|
1482
|
+
framesOf(src),
|
|
1483
|
+
new Promise<Frames>((ok) => realSetTimeout(() => ok(null), framesBy() - RealDate.now())),
|
|
1484
|
+
]);
|
|
1485
|
+
/** the renderer's answer, passed on to the frames inside the film */
|
|
1486
|
+
function framesReady(src: string, f: Frames): void {
|
|
1487
|
+
const ok = framesWaiting.get(src);
|
|
1488
|
+
framesWaiting.delete(src);
|
|
1489
|
+
ok?.(f);
|
|
1490
|
+
for (const el of Array.from(document.querySelectorAll("iframe"))) {
|
|
1491
|
+
try {
|
|
1492
|
+
// biome-ignore lint/suspicious/noExplicitAny: another document's globals
|
|
1493
|
+
(el.contentWindow as any)?.__ot?.framesReady?.(src, f);
|
|
1494
|
+
} catch {
|
|
1495
|
+
// another origin: it has no frames of ours to wait for
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
/** the frame showing at `t` seconds: the last that starts by then — give
|
|
1500
|
+
* or take the film's own rounding, and no more: a 29.97 fps video's second
|
|
1501
|
+
* frame starts 33 µs after a 30 fps film's */
|
|
1502
|
+
const frameAt = (times: number[], t: number): number => {
|
|
1503
|
+
let lo = 0;
|
|
1504
|
+
let hi = times.length - 1;
|
|
1505
|
+
while (lo < hi) {
|
|
1506
|
+
const mid = (lo + hi + 1) >> 1;
|
|
1507
|
+
if (times[mid]! <= t + 1e-9) lo = mid;
|
|
1508
|
+
else hi = mid - 1;
|
|
1509
|
+
}
|
|
1510
|
+
return lo;
|
|
1511
|
+
};
|
|
1512
|
+
// decoded frames, a few held: the ones shown and the next ones fetched
|
|
1513
|
+
// ahead. One that fails, or has not come in 10 s (a service worker of the
|
|
1514
|
+
// page's can hold its request), is null — its video is sought — and not
|
|
1515
|
+
// kept
|
|
1516
|
+
const DECODE_MS = 10_000;
|
|
1517
|
+
const decoded = new Map<string, Promise<HTMLImageElement | null>>();
|
|
1518
|
+
const decodeFrame = (url: string): Promise<HTMLImageElement | null> => {
|
|
1519
|
+
let p = decoded.get(url);
|
|
1520
|
+
if (p) decoded.delete(url);
|
|
1521
|
+
else {
|
|
1522
|
+
const im = new Image();
|
|
1523
|
+
im.src = url;
|
|
1524
|
+
// one the page's policy refuses, or that does not decode, is null too
|
|
1525
|
+
const coming = new Promise<HTMLImageElement | null>((ok) => {
|
|
1526
|
+
let done = false;
|
|
1527
|
+
realSetTimeout(() => {
|
|
1528
|
+
if (done) return;
|
|
1529
|
+
done = true;
|
|
1530
|
+
im.removeAttribute("src"); // asked no longer
|
|
1531
|
+
ok(null);
|
|
1532
|
+
}, DECODE_MS);
|
|
1533
|
+
im.decode().then(
|
|
1534
|
+
() => {
|
|
1535
|
+
if (!done) ok(im);
|
|
1536
|
+
done = true;
|
|
1537
|
+
},
|
|
1538
|
+
() => {
|
|
1539
|
+
if (!done) ok(null);
|
|
1540
|
+
done = true;
|
|
1541
|
+
},
|
|
1542
|
+
);
|
|
1543
|
+
});
|
|
1544
|
+
void coming.then((x) => {
|
|
1545
|
+
if (!x && decoded.get(url) === coming) decoded.delete(url);
|
|
1546
|
+
});
|
|
1547
|
+
p = coming;
|
|
1548
|
+
}
|
|
1549
|
+
decoded.set(url, p);
|
|
1550
|
+
while (decoded.size > 8) decoded.delete(decoded.keys().next().value!);
|
|
1551
|
+
return p;
|
|
1552
|
+
};
|
|
1553
|
+
const frameUrl = (f: NonNullable<Frames>, i: number) =>
|
|
1554
|
+
`${f.base}${String(i).padStart(6, "0")}.${f.ext}`;
|
|
1555
|
+
/** the last frame asked of each video: only it is shown. One asked
|
|
1556
|
+
* earlier that comes later is not — not over a later frame, nor over the
|
|
1557
|
+
* video itself once a later one was not there and it was sought — and
|
|
1558
|
+
* answers as the last one does */
|
|
1559
|
+
const lastAsked = new WeakMap<HTMLMediaElement, { n: number; shown: Promise<boolean> }>();
|
|
1560
|
+
let askCount = 0;
|
|
1561
|
+
/** show the video at `at` seconds as its frame; false when it cannot be,
|
|
1562
|
+
* or when it is read or loaded again while its frame comes — said at once
|
|
1563
|
+
* when there is no frame to show */
|
|
1564
|
+
function showFrame(el: HTMLVideoElement, at: number): Promise<boolean> {
|
|
1565
|
+
const n = ++askCount;
|
|
1566
|
+
const load = loads.get(el);
|
|
1567
|
+
const shown = frameOf(el, at).then((frame): boolean | Promise<boolean> => {
|
|
1568
|
+
const last = lastAsked.get(el);
|
|
1569
|
+
if (last && last.n !== n) return last.shown;
|
|
1570
|
+
if (!frame || readers.has(el) || loads.get(el) !== load) return false;
|
|
1571
|
+
if (asFrame.get(el) !== frame.url) {
|
|
1572
|
+
el.style.setProperty("content", `url("${frame.url}")`, "important");
|
|
1573
|
+
asFrame.set(el, frame.url);
|
|
1574
|
+
}
|
|
1575
|
+
if (shownOf(el)?.im !== frame.shown.im)
|
|
1576
|
+
Object.defineProperty(el, SHOWN, {
|
|
1577
|
+
value: frame.shown,
|
|
1578
|
+
configurable: true,
|
|
1579
|
+
writable: true,
|
|
1580
|
+
});
|
|
1581
|
+
return true;
|
|
1582
|
+
});
|
|
1583
|
+
lastAsked.set(el, { n, shown });
|
|
1584
|
+
return shown;
|
|
1585
|
+
}
|
|
1586
|
+
/** the video's frame at `at` seconds, decoded; null when it has none: no
|
|
1587
|
+
* renderer to ask, a source that is not a file the page serves (a stream
|
|
1588
|
+
* or a blob as its source object), a video read or loaded again */
|
|
1589
|
+
async function frameOf(
|
|
1590
|
+
el: HTMLVideoElement,
|
|
1591
|
+
at: number,
|
|
1592
|
+
): Promise<{ url: string; shown: Shown } | null> {
|
|
1593
|
+
if (typeof w.__otFrames !== "function" || readers.has(el) || el.srcObject) return null;
|
|
1594
|
+
const src = el.currentSrc || el.src;
|
|
1595
|
+
if (!src?.startsWith(`${location.origin}/`)) return null;
|
|
1596
|
+
const load = loads.get(el);
|
|
1597
|
+
const f = await framesNow(src);
|
|
1598
|
+
if (!f?.times.length || readers.has(el) || loads.get(el) !== load) return null;
|
|
1599
|
+
const i = frameAt(f.times, at);
|
|
1600
|
+
const url = frameUrl(f, i);
|
|
1601
|
+
const im = await decodeFrame(url);
|
|
1602
|
+
for (let k = 1; k <= 2 && i + k < f.times.length; k++) void decodeFrame(frameUrl(f, i + k));
|
|
1603
|
+
if (!im) return null;
|
|
1604
|
+
return { url, shown: { im, at: f.times[i]!, bright: !!f.bright } };
|
|
1605
|
+
}
|
|
1606
|
+
function seekVideo(el: HTMLVideoElement, at: number): Promise<void> {
|
|
1607
|
+
return new Promise((ok) => {
|
|
1608
|
+
let finished = false;
|
|
1609
|
+
const done = () => {
|
|
1610
|
+
if (finished) return;
|
|
1611
|
+
finished = true;
|
|
1612
|
+
el.removeEventListener("seeked", done);
|
|
1613
|
+
ok();
|
|
1614
|
+
};
|
|
1615
|
+
const go = () => {
|
|
1616
|
+
el.addEventListener("seeked", done);
|
|
1617
|
+
seekTo.set!.call(el, at);
|
|
1618
|
+
};
|
|
1619
|
+
if (el.readyState >= 1) go();
|
|
1620
|
+
else el.addEventListener("loadedmetadata", go, { once: true });
|
|
1621
|
+
realSetTimeout(done, 10_000);
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
async function syncMedia(): Promise<void> {
|
|
1625
|
+
const waits: Promise<void>[] = [];
|
|
1626
|
+
for (const el of Array.from(document.querySelectorAll("video"))) {
|
|
1627
|
+
const m = mediaOf(el);
|
|
1628
|
+
if (!m.seen) {
|
|
1629
|
+
if (autoplays(el, m)) {
|
|
1630
|
+
m.born = autoBorn(el);
|
|
1631
|
+
m.playing = true;
|
|
1632
|
+
autoplay.add(el);
|
|
1633
|
+
}
|
|
1634
|
+
m.seen = true;
|
|
1635
|
+
el.muted = true;
|
|
1636
|
+
el.autoplay = false;
|
|
1637
|
+
realPause.call(el);
|
|
1638
|
+
}
|
|
1639
|
+
// a stream (a MediaStream as its source object) has no film time to
|
|
1640
|
+
// seek: it shows what it is sent — waiting on a seek it cannot make
|
|
1641
|
+
// cost 10 s an instant. A MediaSource (a worker's handle) is sought
|
|
1642
|
+
const object = el.srcObject as { getTracks?: unknown } | null;
|
|
1643
|
+
if (object && typeof object.getTracks === "function") continue;
|
|
1644
|
+
const at = position(el);
|
|
1645
|
+
if (at === null) continue;
|
|
1646
|
+
const load = loads.get(el);
|
|
1647
|
+
waits.push(
|
|
1648
|
+
showFrame(el, at).then((shown) => {
|
|
1649
|
+
// loaded again meanwhile: `at` was where the old one was
|
|
1650
|
+
if (shown || loads.get(el) !== load) return;
|
|
1651
|
+
unframe(el);
|
|
1652
|
+
if (Math.abs(seekTo.get!.call(el) - at) > 0.0005 || el.readyState < 2)
|
|
1653
|
+
return seekVideo(el, at);
|
|
1654
|
+
}),
|
|
1655
|
+
);
|
|
1656
|
+
}
|
|
1657
|
+
await Promise.all(waits);
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
async function settle(): Promise<void> {
|
|
1661
|
+
await syncMedia();
|
|
1662
|
+
const pending = Array.from(document.images).filter((i) => !i.complete);
|
|
1663
|
+
if (pending.length)
|
|
1664
|
+
await Promise.race([
|
|
1665
|
+
Promise.all(pending.map((i) => i.decode().catch(() => {}))),
|
|
1666
|
+
new Promise((ok) => realSetTimeout(ok, 15_000)),
|
|
1667
|
+
]);
|
|
1668
|
+
if (document.fonts.status !== "loaded")
|
|
1669
|
+
await Promise.race([document.fonts.ready, new Promise((ok) => realSetTimeout(ok, 15_000))]);
|
|
1670
|
+
await followFrames();
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
// same-origin frames on the page: loaded before the frame is taken, and
|
|
1674
|
+
// their animations and videos at the film's time
|
|
1675
|
+
async function followFrames(): Promise<void> {
|
|
1676
|
+
unshown.clear();
|
|
1677
|
+
await eachFrame(async (f) => {
|
|
1678
|
+
if (await loaded(f)) await clockOf(f)?.follow(base);
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
/** frames the page put on it since the last look load and show — their
|
|
1683
|
+
* first frame — before its clock moves on: however the film is sought, a
|
|
1684
|
+
* frame starts at the same moment of the film */
|
|
1685
|
+
async function showFrames(): Promise<void> {
|
|
1686
|
+
drain(); // what was put here since this page last looked
|
|
1687
|
+
while (unshown.size) {
|
|
1688
|
+
const fresh = [...unshown];
|
|
1689
|
+
unshown.clear();
|
|
1690
|
+
await Promise.all(
|
|
1691
|
+
fresh.map(async (f) => {
|
|
1692
|
+
if (!f.isConnected || !(await loaded(f))) return;
|
|
1693
|
+
await clockOf(f)?.follow(base);
|
|
1694
|
+
shown++;
|
|
1695
|
+
}),
|
|
1696
|
+
);
|
|
1697
|
+
}
|
|
1698
|
+
// one the page put into a frame of its own shows too
|
|
1699
|
+
await eachFrame(async (f) => clockOf(f)?.showFrames());
|
|
1700
|
+
}
|
|
1701
|
+
/** how many frames have shown, here and inside the frames */
|
|
1702
|
+
const shownHere = (): number =>
|
|
1703
|
+
Array.from(document.querySelectorAll("iframe")).reduce(
|
|
1704
|
+
(n, f) => n + (clockOf(f)?.shown() ?? 0),
|
|
1705
|
+
shown,
|
|
1706
|
+
);
|
|
1707
|
+
|
|
1708
|
+
function eachFrame(fn: (f: HTMLIFrameElement) => Promise<unknown>): Promise<unknown> {
|
|
1709
|
+
return Promise.all(Array.from(document.querySelectorAll("iframe")).map(fn));
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
/** a frame's own film clock; null when it is cross-origin or has none */
|
|
1713
|
+
// biome-ignore lint/suspicious/noExplicitAny: the frame's own film clock
|
|
1714
|
+
function clockOf(f: HTMLIFrameElement): any {
|
|
1715
|
+
try {
|
|
1716
|
+
// biome-ignore lint/suspicious/noExplicitAny: the frame's own film clock
|
|
1717
|
+
return (f.contentWindow as any)?.__ot ?? null;
|
|
1718
|
+
} catch {
|
|
1719
|
+
return null; // cross-origin: not ours to drive
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
/** a frame's document is still to come (a frame with a source shows an
|
|
1724
|
+
* empty one until that loads) */
|
|
1725
|
+
const awaiting = (f: HTMLIFrameElement, doc: Document) => {
|
|
1726
|
+
const src = f.getAttribute("src");
|
|
1727
|
+
return (
|
|
1728
|
+
doc.readyState !== "complete" ||
|
|
1729
|
+
(doc.URL === "about:blank" && (f.hasAttribute("srcdoc") || (!!src && src !== "about:blank")))
|
|
1730
|
+
);
|
|
1731
|
+
};
|
|
1732
|
+
|
|
1733
|
+
/** wait for a frame's document; false when it is not ours to drive */
|
|
1734
|
+
async function loaded(f: HTMLIFrameElement): Promise<boolean> {
|
|
1735
|
+
const doc = f.contentDocument; // null when cross-origin
|
|
1736
|
+
if (!doc) return false;
|
|
1737
|
+
if (awaiting(f, doc))
|
|
1738
|
+
await new Promise<void>((ok) => {
|
|
1739
|
+
const done = () => ok();
|
|
1740
|
+
f.addEventListener("load", done, { once: true });
|
|
1741
|
+
realSetTimeout(done, 15_000);
|
|
1742
|
+
});
|
|
1743
|
+
return !!f.contentDocument;
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
/** a frame inside the film, as the page that holds it moves (`origin` is
|
|
1747
|
+
* where film time 0 is): its first frame where it was put on the page,
|
|
1748
|
+
* then its animations and videos at the film's time */
|
|
1749
|
+
async function follow(origin: number): Promise<void> {
|
|
1750
|
+
base = origin;
|
|
1751
|
+
if (!started) await firstFrame();
|
|
1752
|
+
syncAnimations();
|
|
1753
|
+
await settle();
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
/** the page's first animation frame, which a browser runs before it first
|
|
1757
|
+
* paints: where the page becomes ready, or where a frame inside it starts */
|
|
1758
|
+
async function firstFrame(): Promise<void> {
|
|
1759
|
+
await runTimers(now);
|
|
1760
|
+
syncAnimations();
|
|
1761
|
+
await runFrames();
|
|
1762
|
+
syncAnimations();
|
|
1763
|
+
started = true;
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
/** when the page's clock has something to run next: a timer, or an
|
|
1767
|
+
* animation frame someone asked for */
|
|
1768
|
+
function nextEvent(): number {
|
|
1769
|
+
let t = Number.POSITIVE_INFINITY;
|
|
1770
|
+
for (const x of timers.values()) t = Math.min(t, x.due);
|
|
1771
|
+
if (framesDue()) t = Math.min(t, gridTime(grid));
|
|
1772
|
+
return t;
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
/** something the page asked for is still on its way: a fetch, an image, a
|
|
1776
|
+
* font, a video's first frame, a frame's document, or the like in a frame */
|
|
1777
|
+
function busy(): boolean {
|
|
1778
|
+
if (loading > 0 || document.fonts.status === "loading") return true;
|
|
1779
|
+
if (Array.from(document.images).some((i) => !i.complete)) return true;
|
|
1780
|
+
for (const v of Array.from(document.querySelectorAll("video")))
|
|
1781
|
+
if (v.readyState < 2 && v.networkState === v.NETWORK_LOADING) return true;
|
|
1782
|
+
for (const f of Array.from(document.querySelectorAll("iframe"))) {
|
|
1783
|
+
const doc = f.contentDocument;
|
|
1784
|
+
if (doc && (awaiting(f, doc) || clockOf(f)?.busy())) return true;
|
|
1785
|
+
}
|
|
1786
|
+
return false;
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
// --- moving time -----------------------------------------------------------
|
|
1790
|
+
/** requestAnimationFrame runs 60 times a second of film time, on one grid
|
|
1791
|
+
* from film time 0 — as on a 60 Hz screen — however the film is sought: in
|
|
1792
|
+
* order, between a shutter's samples, or straight from a fresh page, a page
|
|
1793
|
+
* that moves a step a frame is where a browser would have it. The grid
|
|
1794
|
+
* lies halfway between 60 fps frames, so no frame's shutter spans two of
|
|
1795
|
+
* its instants: what a page draws itself in requestAnimationFrame is sharp;
|
|
1796
|
+
* what CSS, the Web Animations API or __render move is blurred. */
|
|
1797
|
+
async function advance(to: number): Promise<void> {
|
|
1798
|
+
if (to < now - 1e-6)
|
|
1799
|
+
throw new Error(
|
|
1800
|
+
`film clock: cannot go back from ${((now - base) / 1000).toFixed(3)}s to ${((to - base) / 1000).toFixed(3)}s — ` +
|
|
1801
|
+
"this film has no window.__render(t), so it keeps its own state and renders front to back",
|
|
1802
|
+
);
|
|
1803
|
+
syncAnimations(); // what is on the page now starts now
|
|
1804
|
+
for (let g = gridTime(grid); g <= to + 1e-6; g = gridTime(++grid)) {
|
|
1805
|
+
await runTimers(g);
|
|
1806
|
+
now = g;
|
|
1807
|
+
if (framesDue()) {
|
|
1808
|
+
syncAnimations();
|
|
1809
|
+
await runFrames();
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
await runTimers(to);
|
|
1813
|
+
now = to;
|
|
1814
|
+
syncAnimations();
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
/** await a promise the page returned while its timers and frames still run.
|
|
1818
|
+
* Real waiting comes first (a fetch, an image, a font). A promise that waits
|
|
1819
|
+
* on the page's own clock (a timer, an animation frame) would never settle
|
|
1820
|
+
* on a clock that only moves when asked: when real waiting does not settle
|
|
1821
|
+
* it, the clock runs the next thing due — at most MAX_JUMP_MS past where
|
|
1822
|
+
* the wait began.
|
|
1823
|
+
* Before the film starts (`hold` false: the ready wait) the clock moves
|
|
1824
|
+
* with it, but only while nothing the page asked for is still on its way:
|
|
1825
|
+
* however long loading took, the page reaches film time 0 — where it
|
|
1826
|
+
* became ready — in the same state. Inside a __render (`hold` true) the
|
|
1827
|
+
* clock stays at the instant being drawn; timers run in due order on their
|
|
1828
|
+
* own clock. */
|
|
1829
|
+
async function pump<T>(p: PromiseLike<T>, hold = false): Promise<T> {
|
|
1830
|
+
let settled = false;
|
|
1831
|
+
let value: T | undefined;
|
|
1832
|
+
let error: unknown;
|
|
1833
|
+
let failed = false;
|
|
1834
|
+
Promise.resolve(p).then(
|
|
1835
|
+
(v) => {
|
|
1836
|
+
settled = true;
|
|
1837
|
+
value = v;
|
|
1838
|
+
},
|
|
1839
|
+
(e) => {
|
|
1840
|
+
settled = true;
|
|
1841
|
+
failed = true;
|
|
1842
|
+
error = e;
|
|
1843
|
+
},
|
|
1844
|
+
);
|
|
1845
|
+
const start = now;
|
|
1846
|
+
const began = RealDate.now();
|
|
1847
|
+
let impatient = false;
|
|
1848
|
+
if (hold) {
|
|
1849
|
+
holding = true;
|
|
1850
|
+
timerNow = now;
|
|
1851
|
+
}
|
|
1852
|
+
try {
|
|
1853
|
+
for (let i = 0, stepped = false; !settled; i++) {
|
|
1854
|
+
if (RealDate.now() - began > 45_000)
|
|
1855
|
+
throw new Error(
|
|
1856
|
+
"film clock: the page's promise did not settle within 45 s" +
|
|
1857
|
+
(document.querySelector("iframe")
|
|
1858
|
+
? " (a frame inside the film runs its own timers and animation frames only where it was put on the page)"
|
|
1859
|
+
: ""),
|
|
1860
|
+
);
|
|
1861
|
+
if (hold) {
|
|
1862
|
+
await runTimers(now, true);
|
|
1863
|
+
await runFrames();
|
|
1864
|
+
} else await advance(now);
|
|
1865
|
+
if (settled) break;
|
|
1866
|
+
await new Promise((ok) => realSetTimeout(ok, stepped || i < 20 ? 0 : 15));
|
|
1867
|
+
stepped = false;
|
|
1868
|
+
if (settled || i < 3) continue;
|
|
1869
|
+
if (hold) {
|
|
1870
|
+
let due = Number.POSITIVE_INFINITY;
|
|
1871
|
+
for (const t of timers.values()) due = Math.min(due, t.due);
|
|
1872
|
+
if (due - start <= MAX_JUMP_MS) await runTimers(due, true);
|
|
1873
|
+
} else {
|
|
1874
|
+
const stillLoading = busy();
|
|
1875
|
+
if (stillLoading && RealDate.now() - began < LOADING_PATIENCE_MS) continue;
|
|
1876
|
+
const next = nextEvent();
|
|
1877
|
+
if (next - start <= MAX_JUMP_MS) {
|
|
1878
|
+
if (stillLoading && !impatient)
|
|
1879
|
+
warn(
|
|
1880
|
+
`film clock: the page was still loading after ${LOADING_PATIENCE_MS / 1000} s while it waited on its own clock; its clock went on, so renders of it may start differently`,
|
|
1881
|
+
);
|
|
1882
|
+
impatient ||= stillLoading;
|
|
1883
|
+
await advance(Math.max(now, next));
|
|
1884
|
+
await followFrames();
|
|
1885
|
+
stepped = true;
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
} finally {
|
|
1890
|
+
holding = false;
|
|
1891
|
+
}
|
|
1892
|
+
if (failed) throw error;
|
|
1893
|
+
return value as T;
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
async function seek(tSec: number): Promise<void> {
|
|
1897
|
+
const t = base + tSec * 1000;
|
|
1898
|
+
if (mode === "render") {
|
|
1899
|
+
now = t;
|
|
1900
|
+
// the frames the page draws into have shown before it does (their first
|
|
1901
|
+
// frame would otherwise come after what it writes), and are at t — the
|
|
1902
|
+
// videos in them too, for a page that draws one; a frame it puts on the
|
|
1903
|
+
// page while drawing loads, and the instant is drawn again — into that
|
|
1904
|
+
// one too — until no new one shows up
|
|
1905
|
+
await showFrames();
|
|
1906
|
+
await followFrames();
|
|
1907
|
+
syncAnimations();
|
|
1908
|
+
for (let pass = 0; ; pass++) {
|
|
1909
|
+
if (pass === MAX_DRAWS)
|
|
1910
|
+
throw new Error(
|
|
1911
|
+
`film clock: drawing ${tSec}s put a new frame on the page ${MAX_DRAWS} times over — draw the same frames for the same t`,
|
|
1912
|
+
);
|
|
1913
|
+
const before = shownHere();
|
|
1914
|
+
reseed(tSec);
|
|
1915
|
+
const r = w.__render(tSec);
|
|
1916
|
+
if (r && typeof r.then === "function") await pump(r, true);
|
|
1917
|
+
drain();
|
|
1918
|
+
await showFrames();
|
|
1919
|
+
if (shownHere() === before) break;
|
|
1920
|
+
syncAnimations();
|
|
1921
|
+
}
|
|
1922
|
+
syncAnimations();
|
|
1923
|
+
} else await advance(t);
|
|
1924
|
+
await settle();
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
// --- motion: how far anything visible moves over the shutter ---------------
|
|
1928
|
+
const thumbCanvas = document.createElement("canvas");
|
|
1929
|
+
const CUT_WIDTH = 48;
|
|
1930
|
+
const CUT_HEIGHT = 27;
|
|
1931
|
+
let thumbCtx: CanvasRenderingContext2D | null = null;
|
|
1932
|
+
function readThumb(c: HTMLCanvasElement): CanvasThumb | null {
|
|
1933
|
+
if (!c.width || !c.height) return null;
|
|
1934
|
+
const scale = Math.min(1, 192 / Math.max(c.width, c.height));
|
|
1935
|
+
const width = Math.max(1, Math.round(c.width * scale));
|
|
1936
|
+
const height = Math.max(1, Math.round(c.height * scale));
|
|
1937
|
+
const stride = Math.max(width, CUT_WIDTH);
|
|
1938
|
+
const rows = height + CUT_HEIGHT;
|
|
1939
|
+
if (thumbCanvas.width !== stride) thumbCanvas.width = stride;
|
|
1940
|
+
if (thumbCanvas.height !== rows) thumbCanvas.height = rows;
|
|
1941
|
+
try {
|
|
1942
|
+
thumbCtx ??= thumbCanvas.getContext("2d", { willReadFrequently: true });
|
|
1943
|
+
if (!thumbCtx) return null;
|
|
1944
|
+
thumbCtx.clearRect(0, 0, stride, rows);
|
|
1945
|
+
thumbCtx.drawImage(c, 0, 0, width, height);
|
|
1946
|
+
// Keep the original 48×27 RGBA comparison for choosing the shutter
|
|
1947
|
+
// side. Both drawings share one read back from the thumbnail canvas.
|
|
1948
|
+
thumbCtx.drawImage(c, 0, height, CUT_WIDTH, CUT_HEIGHT);
|
|
1949
|
+
const data = thumbCtx.getImageData(0, 0, stride, rows).data;
|
|
1950
|
+
const pixels = new Uint8Array(width * height);
|
|
1951
|
+
for (let y = 0; y < height; y++)
|
|
1952
|
+
for (let x = 0, j = 4 * y * stride; x < width; x++, j += 4)
|
|
1953
|
+
pixels[y * width + x] = grey(data[j]!, data[j + 1]!, data[j + 2]!, data[j + 3]!);
|
|
1954
|
+
const cutPixels = new Uint8ClampedArray(CUT_WIDTH * CUT_HEIGHT * 4);
|
|
1955
|
+
for (let y = 0; y < CUT_HEIGHT; y++) {
|
|
1956
|
+
const from = 4 * (y + height) * stride;
|
|
1957
|
+
cutPixels.set(data.subarray(from, from + CUT_WIDTH * 4), y * CUT_WIDTH * 4);
|
|
1958
|
+
}
|
|
1959
|
+
return { pixels, width, height, cutPixels };
|
|
1960
|
+
} catch {
|
|
1961
|
+
thumbCanvas.width = stride; // clears the taint for the next canvas
|
|
1962
|
+
return null; // a tainted or lost canvas: counted as moving
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
let probing = false;
|
|
1967
|
+
const gpuCanvases = new WeakSet<HTMLCanvasElement>();
|
|
1968
|
+
const gpuPictures = new WeakMap<HTMLCanvasElement, CanvasThumb | null>();
|
|
1969
|
+
type PendingTexture = { texture: object; clearing: boolean };
|
|
1970
|
+
let pendingTextures = new WeakMap<object, Map<HTMLCanvasElement, PendingTexture>>();
|
|
1971
|
+
// A WebGPU canvas's drawing buffer can be gone by the time an async
|
|
1972
|
+
// __render returns. Read it in the task that submitted the draw, before
|
|
1973
|
+
// the browser presents it. Keep the first picture for canvases drawn once
|
|
1974
|
+
// at load, then read only while probing. The screenshots use the page's
|
|
1975
|
+
// own presentation.
|
|
1976
|
+
type GPUContext = { canvas: HTMLCanvasElement | OffscreenCanvas };
|
|
1977
|
+
type GPUConfig = { device: { queue: object } };
|
|
1978
|
+
const contexts = w.GPUCanvasContext?.prototype;
|
|
1979
|
+
const queues = w.GPUQueue?.prototype;
|
|
1980
|
+
if (contexts && queues) {
|
|
1981
|
+
const configure = contexts.configure;
|
|
1982
|
+
const unconfigure = contexts.unconfigure;
|
|
1983
|
+
const getCurrentTexture = contexts.getCurrentTexture;
|
|
1984
|
+
const submit = queues.submit;
|
|
1985
|
+
const configured = new WeakMap<GPUContext, object>();
|
|
1986
|
+
const forget = (context: GPUContext) => {
|
|
1987
|
+
if (!(context.canvas instanceof HTMLCanvasElement)) return;
|
|
1988
|
+
const queue = configured.get(context);
|
|
1989
|
+
if (queue) pendingTextures.get(queue)?.delete(context.canvas);
|
|
1990
|
+
gpuPictures.delete(context.canvas);
|
|
1991
|
+
};
|
|
1992
|
+
contexts.configure = function (this: GPUContext, config: GPUConfig) {
|
|
1993
|
+
configure.call(this, config);
|
|
1994
|
+
forget(this);
|
|
1995
|
+
configured.set(this, config.device.queue);
|
|
1996
|
+
if (this.canvas instanceof HTMLCanvasElement) {
|
|
1997
|
+
gpuCanvases.add(this.canvas);
|
|
1998
|
+
}
|
|
1999
|
+
};
|
|
2000
|
+
contexts.unconfigure = function (this: GPUContext) {
|
|
2001
|
+
unconfigure.call(this);
|
|
2002
|
+
forget(this);
|
|
2003
|
+
configured.delete(this);
|
|
2004
|
+
};
|
|
2005
|
+
contexts.getCurrentTexture = function (this: GPUContext) {
|
|
2006
|
+
const texture = getCurrentTexture.call(this);
|
|
2007
|
+
const queue = configured.get(this);
|
|
2008
|
+
if (queue && this.canvas instanceof HTMLCanvasElement &&
|
|
2009
|
+
(probing || !gpuPictures.has(this.canvas))) {
|
|
2010
|
+
let wanted = pendingTextures.get(queue);
|
|
2011
|
+
if (!wanted) {
|
|
2012
|
+
wanted = new Map();
|
|
2013
|
+
pendingTextures.set(queue, wanted);
|
|
2014
|
+
}
|
|
2015
|
+
if (wanted.get(this.canvas)?.texture !== texture)
|
|
2016
|
+
wanted.set(this.canvas, { texture, clearing: false });
|
|
2017
|
+
}
|
|
2018
|
+
return texture;
|
|
2019
|
+
};
|
|
2020
|
+
queues.submit = function (this: object, commands: Iterable<unknown>) {
|
|
2021
|
+
submit.call(this, commands);
|
|
2022
|
+
const wanted = pendingTextures.get(this);
|
|
2023
|
+
if (!wanted) return;
|
|
2024
|
+
// A frame can be drawn in several submits (a clear pass, then the
|
|
2025
|
+
// scene loaded over it, then an overlay): read after each one, so the
|
|
2026
|
+
// last submit before the browser presents the texture is the picture.
|
|
2027
|
+
// Expire only this texture. The next draw can acquire its texture
|
|
2028
|
+
// before the timer for the previous one runs.
|
|
2029
|
+
for (const [canvas, frame] of wanted) {
|
|
2030
|
+
gpuPictures.set(canvas, readThumb(canvas));
|
|
2031
|
+
if (frame.clearing) continue;
|
|
2032
|
+
frame.clearing = true;
|
|
2033
|
+
realSetTimeout(() => {
|
|
2034
|
+
if (wanted.get(canvas) === frame) wanted.delete(canvas);
|
|
2035
|
+
}, 0);
|
|
2036
|
+
}
|
|
2037
|
+
};
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
function thumb(c: HTMLCanvasElement): CanvasThumb | null {
|
|
2041
|
+
return gpuCanvases.has(c) ? (gpuPictures.get(c) ?? null) : readThumb(c);
|
|
2042
|
+
}
|
|
2043
|
+
type Snapshot = {
|
|
2044
|
+
rects: Map<Element, DOMRect>;
|
|
2045
|
+
/** what a still element shows — an image's source, a leaf's text: when it
|
|
2046
|
+
* changes, the picture cut there, however small the element */
|
|
2047
|
+
shows: Map<Element, string>;
|
|
2048
|
+
thumbs: Map<HTMLCanvasElement, CanvasThumb | null>;
|
|
2049
|
+
video: boolean;
|
|
2050
|
+
};
|
|
2051
|
+
function showing(el: Element): string | null {
|
|
2052
|
+
// by tag, not instanceof: a saved screen's elements belong to its frame's realm
|
|
2053
|
+
if (el.tagName === "IMG") {
|
|
2054
|
+
const img = el as HTMLImageElement;
|
|
2055
|
+
return img.currentSrc || img.src;
|
|
2056
|
+
}
|
|
2057
|
+
return el.childElementCount === 0 ? (el.textContent ?? "") : null;
|
|
2058
|
+
}
|
|
2059
|
+
function snapshot(): Snapshot {
|
|
2060
|
+
const vw = innerWidth;
|
|
2061
|
+
const vh = innerHeight;
|
|
2062
|
+
const rects = new Map<Element, DOMRect>();
|
|
2063
|
+
const shows = new Map<Element, string>();
|
|
2064
|
+
const thumbs = new Map<HTMLCanvasElement, CanvasThumb | null>();
|
|
2065
|
+
let video = false;
|
|
2066
|
+
for (const el of Array.from(document.querySelectorAll("body *"))) {
|
|
2067
|
+
const r = el.getBoundingClientRect();
|
|
2068
|
+
if (
|
|
2069
|
+
r.width < 1 ||
|
|
2070
|
+
r.height < 1 ||
|
|
2071
|
+
r.right <= 0 ||
|
|
2072
|
+
r.bottom <= 0 ||
|
|
2073
|
+
r.left >= vw ||
|
|
2074
|
+
r.top >= vh
|
|
2075
|
+
)
|
|
2076
|
+
continue;
|
|
2077
|
+
if (!el.checkVisibility({ opacityProperty: true, visibilityProperty: true })) continue;
|
|
2078
|
+
rects.set(el, r);
|
|
2079
|
+
const s = showing(el);
|
|
2080
|
+
if (s !== null) shows.set(el, s);
|
|
2081
|
+
if (el instanceof HTMLCanvasElement) thumbs.set(el, thumb(el));
|
|
2082
|
+
else if (el instanceof HTMLVideoElement && media.get(el)?.playing) video = true;
|
|
2083
|
+
else if (el instanceof HTMLIFrameElement) framed(el, r, rects, shows, vw, vh);
|
|
2084
|
+
}
|
|
2085
|
+
return { rects, shows, thumbs, video };
|
|
2086
|
+
}
|
|
2087
|
+
|
|
2088
|
+
/** the elements inside a same-origin frame (a saved screen lifted into the
|
|
2089
|
+
* film), in the film's coordinates — what moves inside it moves on screen */
|
|
2090
|
+
function framed(
|
|
2091
|
+
f: HTMLIFrameElement,
|
|
2092
|
+
box: DOMRect,
|
|
2093
|
+
rects: Map<Element, DOMRect>,
|
|
2094
|
+
shows: Map<Element, string>,
|
|
2095
|
+
vw: number,
|
|
2096
|
+
vh: number,
|
|
2097
|
+
): void {
|
|
2098
|
+
let doc: Document | null = null;
|
|
2099
|
+
try {
|
|
2100
|
+
doc = f.contentDocument;
|
|
2101
|
+
} catch {
|
|
2102
|
+
return; // cross-origin
|
|
2103
|
+
}
|
|
2104
|
+
if (!doc?.body) return;
|
|
2105
|
+
// the frame's own viewport, scaled by whatever transform the film put on it
|
|
2106
|
+
const sx = box.width / (f.clientWidth || box.width);
|
|
2107
|
+
const sy = box.height / (f.clientHeight || box.height);
|
|
2108
|
+
for (const el of Array.from(doc.body.querySelectorAll("*"))) {
|
|
2109
|
+
const r = el.getBoundingClientRect();
|
|
2110
|
+
if (r.width < 1 || r.height < 1) continue;
|
|
2111
|
+
const m = new DOMRect(
|
|
2112
|
+
box.left + r.left * sx,
|
|
2113
|
+
box.top + r.top * sy,
|
|
2114
|
+
r.width * sx,
|
|
2115
|
+
r.height * sy,
|
|
2116
|
+
);
|
|
2117
|
+
if (m.right <= Math.max(0, box.left) || m.bottom <= Math.max(0, box.top)) continue;
|
|
2118
|
+
if (m.left >= Math.min(vw, box.right) || m.top >= Math.min(vh, box.bottom)) continue;
|
|
2119
|
+
if (!el.checkVisibility({ opacityProperty: true, visibilityProperty: true })) continue;
|
|
2120
|
+
rects.set(el, m);
|
|
2121
|
+
const s = showing(el);
|
|
2122
|
+
if (s !== null) shows.set(el, s);
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
type Change = {
|
|
2126
|
+
/** the farthest any element that is in both snapshots travelled */
|
|
2127
|
+
px: number;
|
|
2128
|
+
/** visible area (px²) of elements that are in only one of them */
|
|
2129
|
+
churn: number;
|
|
2130
|
+
/** elements inside the picture that appeared, vanished or changed what
|
|
2131
|
+
* they show: cuts, however small (one sliding in from an edge is motion) */
|
|
2132
|
+
cuts: number;
|
|
2133
|
+
/** how far the text that moved at all moved, the median (px): Chrome
|
|
2134
|
+
* draws glyphs on whole device pixels, so text moving under one a frame
|
|
2135
|
+
* steps */
|
|
2136
|
+
text: number;
|
|
2137
|
+
/** the element at the middle of the moving text speeds */
|
|
2138
|
+
textElement: Element | null;
|
|
2139
|
+
/** the picture as a whole, fitted to the elements in both: b = s·a + t
|
|
2140
|
+
* (a camera's zoom and pan), from n elements; null with too few */
|
|
2141
|
+
view: {
|
|
2142
|
+
s: number;
|
|
2143
|
+
tx: number;
|
|
2144
|
+
ty: number;
|
|
2145
|
+
n: number;
|
|
2146
|
+
share: number;
|
|
2147
|
+
rigid: number;
|
|
2148
|
+
who: string;
|
|
2149
|
+
} | null;
|
|
2150
|
+
};
|
|
2151
|
+
/** the zoom and pan that carry most elements from a to b: least squares on
|
|
2152
|
+
* their centres, again without those it fits worst (a cursor, a caption
|
|
2153
|
+
* moving on its own) */
|
|
2154
|
+
// the element the fit followed, by where it sits in the page (and the frame
|
|
2155
|
+
// it is in): the same in every browser that probes, frame after frame
|
|
2156
|
+
const idOf = (el: Element | null): string => {
|
|
2157
|
+
if (!el) return "";
|
|
2158
|
+
const path: number[] = [];
|
|
2159
|
+
let n: Element | null = el;
|
|
2160
|
+
while (n) {
|
|
2161
|
+
const parent: Element | null = n.parentElement;
|
|
2162
|
+
if (parent) path.unshift(Array.prototype.indexOf.call(parent.children, n));
|
|
2163
|
+
else {
|
|
2164
|
+
const host: Element | null = n.ownerDocument.defaultView?.frameElement ?? null;
|
|
2165
|
+
path.unshift(-1);
|
|
2166
|
+
n = host;
|
|
2167
|
+
continue;
|
|
2168
|
+
}
|
|
2169
|
+
n = parent;
|
|
2170
|
+
}
|
|
2171
|
+
return path.join(".");
|
|
2172
|
+
};
|
|
2173
|
+
function fitView(
|
|
2174
|
+
pairs: [number, number, number, number][],
|
|
2175
|
+
share: number,
|
|
2176
|
+
el: Element | null,
|
|
2177
|
+
): Change["view"] {
|
|
2178
|
+
const fit = (ps: [number, number, number, number][]) => {
|
|
2179
|
+
const n = ps.length;
|
|
2180
|
+
let mx = 0,
|
|
2181
|
+
my = 0,
|
|
2182
|
+
nx = 0,
|
|
2183
|
+
ny = 0;
|
|
2184
|
+
for (const [ax, ay, bx, by] of ps) {
|
|
2185
|
+
mx += ax;
|
|
2186
|
+
my += ay;
|
|
2187
|
+
nx += bx;
|
|
2188
|
+
ny += by;
|
|
2189
|
+
}
|
|
2190
|
+
mx /= n;
|
|
2191
|
+
my /= n;
|
|
2192
|
+
nx /= n;
|
|
2193
|
+
ny /= n;
|
|
2194
|
+
let num = 0,
|
|
2195
|
+
den = 0;
|
|
2196
|
+
for (const [ax, ay, bx, by] of ps) {
|
|
2197
|
+
num += (ax - mx) * (bx - nx) + (ay - my) * (by - ny);
|
|
2198
|
+
den += (ax - mx) ** 2 + (ay - my) ** 2;
|
|
2199
|
+
}
|
|
2200
|
+
const sc = den > 1e-6 ? num / den : 1;
|
|
2201
|
+
return { s: sc, tx: nx - sc * mx, ty: ny - sc * my };
|
|
2202
|
+
};
|
|
2203
|
+
if (pairs.length < 2) return null;
|
|
2204
|
+
let f = fit(pairs);
|
|
2205
|
+
const err = (q: [number, number, number, number]) =>
|
|
2206
|
+
Math.hypot(f.s * q[0] + f.tx - q[2], f.s * q[1] + f.ty - q[3]);
|
|
2207
|
+
const errs = pairs.map(err).sort((x, y) => x - y);
|
|
2208
|
+
const cut = Math.max(0.5, errs[Math.floor(errs.length * 0.75)]! * 2);
|
|
2209
|
+
const kept = pairs.filter((q) => err(q) <= cut);
|
|
2210
|
+
if (kept.length >= 2) f = fit(kept);
|
|
2211
|
+
const used = kept.length >= 2 ? kept : pairs;
|
|
2212
|
+
// how much of the elements' movement the zoom and pan leave unexplained:
|
|
2213
|
+
// 0 for a camera, near 1 for shapes changing on their own
|
|
2214
|
+
let res = 0,
|
|
2215
|
+
mov = 0;
|
|
2216
|
+
for (const q of used) {
|
|
2217
|
+
res += (f.s * q[0] + f.tx - q[2]) ** 2 + (f.s * q[1] + f.ty - q[3]) ** 2;
|
|
2218
|
+
mov += (q[2] - q[0]) ** 2 + (q[3] - q[1]) ** 2;
|
|
2219
|
+
}
|
|
2220
|
+
return {
|
|
2221
|
+
...f,
|
|
2222
|
+
n: used.length,
|
|
2223
|
+
share,
|
|
2224
|
+
rigid: mov > 0 ? Math.sqrt(res / mov) : 0,
|
|
2225
|
+
who: idOf(el),
|
|
2226
|
+
};
|
|
2227
|
+
}
|
|
2228
|
+
function moved(a: Snapshot, b: Snapshot): Change {
|
|
2229
|
+
let px = 0;
|
|
2230
|
+
let churn = 0;
|
|
2231
|
+
let cuts = 0;
|
|
2232
|
+
const text: { px: number; el: Element }[] = [];
|
|
2233
|
+
// the largest element that moves: under a camera, the picture itself
|
|
2234
|
+
let big: [number, number, number, number][] = [];
|
|
2235
|
+
let bigEl: Element | null = null;
|
|
2236
|
+
let movingArea = 0;
|
|
2237
|
+
const vw = innerWidth;
|
|
2238
|
+
const vh = innerHeight;
|
|
2239
|
+
const area = (r: DOMRect) =>
|
|
2240
|
+
Math.max(0, Math.min(vw, r.right) - Math.max(0, r.left)) *
|
|
2241
|
+
Math.max(0, Math.min(vh, r.bottom) - Math.max(0, r.top));
|
|
2242
|
+
const inside = (r: DOMRect) => r.left > 0 && r.top > 0 && r.right < vw && r.bottom < vh;
|
|
2243
|
+
for (const [el, ra] of a.rects) {
|
|
2244
|
+
const rb = b.rects.get(el);
|
|
2245
|
+
if (!rb) {
|
|
2246
|
+
churn += area(ra); // vanished: a cut or a fade, not motion
|
|
2247
|
+
if (inside(ra)) cuts++;
|
|
2248
|
+
continue;
|
|
2249
|
+
}
|
|
2250
|
+
const shown = a.shows.get(el);
|
|
2251
|
+
if (shown !== b.shows.get(el)) cuts++;
|
|
2252
|
+
const d = Math.max(
|
|
2253
|
+
Math.abs(ra.left - rb.left),
|
|
2254
|
+
Math.abs(ra.top - rb.top),
|
|
2255
|
+
Math.abs(ra.right - rb.right),
|
|
2256
|
+
Math.abs(ra.bottom - rb.bottom),
|
|
2257
|
+
);
|
|
2258
|
+
if (d > px) px = d;
|
|
2259
|
+
// text, not a picture: Chrome resamples an image wherever it lands, and
|
|
2260
|
+
// puts glyphs on whole device pixels
|
|
2261
|
+
if (shown?.trim() && el.tagName !== "IMG" && d > 1e-4)
|
|
2262
|
+
text.push({ px: Math.hypot(ra.left - rb.left, ra.top - rb.top), el });
|
|
2263
|
+
// what moves: what holds still (a caption over a moving picture) is not the picture's move
|
|
2264
|
+
// its two corners, so that one picture alone (a screenshot past the
|
|
2265
|
+
// frame's edges under a zoom) gives the zoom as well as the pan
|
|
2266
|
+
if (d > 1e-3) {
|
|
2267
|
+
if (area(rb) > movingArea) {
|
|
2268
|
+
movingArea = area(rb);
|
|
2269
|
+
bigEl = el;
|
|
2270
|
+
big = [
|
|
2271
|
+
[ra.left, ra.top, rb.left, rb.top],
|
|
2272
|
+
[ra.right, ra.bottom, rb.right, rb.bottom],
|
|
2273
|
+
];
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
for (const [el, rb] of b.rects)
|
|
2278
|
+
if (!a.rects.has(el)) {
|
|
2279
|
+
churn += area(rb);
|
|
2280
|
+
if (inside(rb)) cuts++;
|
|
2281
|
+
}
|
|
2282
|
+
if (a.video || b.video) px = Math.max(px, OPAQUE_PX);
|
|
2283
|
+
text.sort((l, r) => l.px - r.px);
|
|
2284
|
+
const middle = text.length >> 1;
|
|
2285
|
+
const upper = text[middle];
|
|
2286
|
+
const lower = text[middle - 1];
|
|
2287
|
+
const even = text.length % 2 === 0;
|
|
2288
|
+
return {
|
|
2289
|
+
px,
|
|
2290
|
+
churn,
|
|
2291
|
+
cuts,
|
|
2292
|
+
text: !upper ? 0 : even ? (lower!.px + upper.px) / 2 : upper.px,
|
|
2293
|
+
textElement: (even && lower?.px ? lower : upper)?.el ?? null,
|
|
2294
|
+
view: fitView(big, Math.min(1, movingArea / (vw * vh)), bigEl),
|
|
2295
|
+
};
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2298
|
+
// A change on just one side still uses the old fixed-distance signal.
|
|
2299
|
+
// The measured displacement is for spacing samples on the chosen span.
|
|
2300
|
+
const canvasChanged = (a: CanvasThumb, b: CanvasThumb): boolean => {
|
|
2301
|
+
let diff = 0;
|
|
2302
|
+
for (let i = 0; i < a.cutPixels.length; i++)
|
|
2303
|
+
diff += Math.abs(a.cutPixels[i]! - b.cutPixels[i]!);
|
|
2304
|
+
return diff / a.cutPixels.length > 0.5;
|
|
2305
|
+
};
|
|
2306
|
+
// The canvas's own width and height on screen, along its own axes: its
|
|
2307
|
+
// layout box scaled by the transforms on it and its ancestors. The
|
|
2308
|
+
// bounding box swaps the two for a canvas turned 90°.
|
|
2309
|
+
function drawnSize(el: HTMLCanvasElement, box: DOMRect): [number, number] {
|
|
2310
|
+
if (!el.offsetWidth || !el.offsetHeight) return [box.width, box.height];
|
|
2311
|
+
let m = new DOMMatrix();
|
|
2312
|
+
for (let node: Element | null = el; node; node = node.parentElement) {
|
|
2313
|
+
const t = getComputedStyle(node).transform;
|
|
2314
|
+
if (t && t !== "none") m = new DOMMatrix(t).multiply(m);
|
|
2315
|
+
}
|
|
2316
|
+
return axisSize(el.offsetWidth, el.offsetHeight, m, el.currentCSSZoom ?? 1);
|
|
2317
|
+
}
|
|
2318
|
+
function canvasMotion(a: Snapshot, b: Snapshot, measure: boolean): number {
|
|
2319
|
+
let px = 0;
|
|
2320
|
+
for (const [el, ta] of a.thumbs) {
|
|
2321
|
+
if (!b.thumbs.has(el)) continue;
|
|
2322
|
+
const tb = b.thumbs.get(el);
|
|
2323
|
+
if (!ta || !tb) {
|
|
2324
|
+
px = Math.max(px, OPAQUE_PX);
|
|
2325
|
+
} else if (measure) {
|
|
2326
|
+
const [cssWidth, cssHeight] = drawnSize(el, b.rects.get(el)!);
|
|
2327
|
+
const d = ta.width === tb.width && ta.height === tb.height
|
|
2328
|
+
? displacement(ta.pixels, tb.pixels, ta.width, ta.height, cssWidth, cssHeight)
|
|
2329
|
+
: OPAQUE_PX;
|
|
2330
|
+
// A change of colour alone can leave the grey thumbnails equal.
|
|
2331
|
+
px = Math.max(px, d || (canvasChanged(ta, tb) ? OPAQUE_PX : 0));
|
|
2332
|
+
} else if (canvasChanged(ta, tb)) return OPAQUE_PX;
|
|
2333
|
+
}
|
|
2334
|
+
return px;
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
function textLabel(el: Element | null, picture: Snapshot): TextCause | null {
|
|
2338
|
+
if (!el) return null;
|
|
2339
|
+
const text = (picture.shows.get(el) ?? "").trim().replace(/\s+/g, " ");
|
|
2340
|
+
const key = idOf(el);
|
|
2341
|
+
const name = el.id ? `#${el.id.replace(/\s+/g, " ").slice(0, 40)}` : ` [${key}]`;
|
|
2342
|
+
const label = `${el.tagName.toLowerCase()}${name} "${text.length > 32 ? `${text.slice(0, 31)}…` : text}"`;
|
|
2343
|
+
return { key, label };
|
|
2344
|
+
}
|
|
2345
|
+
/** how far anything moves while the shutter is open, and which part of the
|
|
2346
|
+
* shutter to expose: side 0 all of it, -1 its first half, 1 its second;
|
|
2347
|
+
* and how fast the text that moves there moves, px a second */
|
|
2348
|
+
async function motion(
|
|
2349
|
+
tSec: number,
|
|
2350
|
+
halfSec: number,
|
|
2351
|
+
): Promise<{
|
|
2352
|
+
px: number;
|
|
2353
|
+
side: number;
|
|
2354
|
+
text: number;
|
|
2355
|
+
textCause: TextCause | null;
|
|
2356
|
+
/** the picture's zoom and pan across the exposed span, as rates: s − 1
|
|
2357
|
+
* and t (px) a second, about the page's top left; null when not fitted */
|
|
2358
|
+
view: { ds: number; tx: number; ty: number; share: number; rigid: number; who: string } | null;
|
|
2359
|
+
}> {
|
|
2360
|
+
// The page's own motion(t, half) says how far its picture moves across
|
|
2361
|
+
// [t-half, t+half] (what a canvas draws, which no element shows) in place
|
|
2362
|
+
// of the canvas matcher; its elements are still measured, and where the
|
|
2363
|
+
// picture cuts is still found here, so a hook never opens the shutter across one.
|
|
2364
|
+
const hook = w.__film?.motion;
|
|
2365
|
+
const told =
|
|
2366
|
+
typeof hook === "function"
|
|
2367
|
+
? async (t: number, half: number) => {
|
|
2368
|
+
const px = Number(await hook(t, half));
|
|
2369
|
+
return Number.isFinite(px) && px > 0 ? px : 0;
|
|
2370
|
+
}
|
|
2371
|
+
: null;
|
|
2372
|
+
probing = true;
|
|
2373
|
+
let a: Snapshot;
|
|
2374
|
+
let mid: Snapshot;
|
|
2375
|
+
let b: Snapshot;
|
|
2376
|
+
try {
|
|
2377
|
+
await seek(Math.max(0, tSec - halfSec));
|
|
2378
|
+
a = snapshot();
|
|
2379
|
+
await seek(tSec);
|
|
2380
|
+
mid = snapshot();
|
|
2381
|
+
await seek(tSec + halfSec);
|
|
2382
|
+
b = snapshot();
|
|
2383
|
+
} finally {
|
|
2384
|
+
probing = false;
|
|
2385
|
+
pendingTextures = new WeakMap();
|
|
2386
|
+
}
|
|
2387
|
+
// A shutter never straddles a cut: when something changes in one half of
|
|
2388
|
+
// the shutter only — it jumps, a large part of the picture appears or
|
|
2389
|
+
// vanishes at once, or anything inside the picture appears, vanishes or
|
|
2390
|
+
// shows something else — the frame is exposed over the other half alone:
|
|
2391
|
+
// no double exposure, and what moves through that half keeps its blur.
|
|
2392
|
+
// Motion spreads over both halves.
|
|
2393
|
+
const first = moved(a, mid);
|
|
2394
|
+
const second = moved(mid, b);
|
|
2395
|
+
const screen = innerWidth * innerHeight;
|
|
2396
|
+
const firstPx = Math.max(first.px, canvasMotion(a, mid, false));
|
|
2397
|
+
const secondPx = Math.max(second.px, canvasMotion(mid, b, false));
|
|
2398
|
+
const onlyIn = (x: Change, y: Change, xPx: number, yPx: number) =>
|
|
2399
|
+
(xPx > 1 && yPx < 0.1 * xPx) ||
|
|
2400
|
+
(x.churn > 0.25 * screen && y.churn < 0.05 * screen) ||
|
|
2401
|
+
(x.cuts > 0 && y.cuts === 0);
|
|
2402
|
+
// with a hook, it is asked for the half that is exposed
|
|
2403
|
+
const quarter = halfSec / 2;
|
|
2404
|
+
const rate = (v: Change["view"], sec: number) =>
|
|
2405
|
+
v
|
|
2406
|
+
? {
|
|
2407
|
+
ds: (v.s - 1) / sec,
|
|
2408
|
+
tx: v.tx / sec,
|
|
2409
|
+
ty: v.ty / sec,
|
|
2410
|
+
share: v.share,
|
|
2411
|
+
rigid: v.rigid,
|
|
2412
|
+
who: v.who,
|
|
2413
|
+
}
|
|
2414
|
+
: null;
|
|
2415
|
+
const side = onlyIn(first, second, firstPx, secondPx)
|
|
2416
|
+
? 1
|
|
2417
|
+
: onlyIn(second, first, secondPx, firstPx) ? -1 : 0;
|
|
2418
|
+
const change = side === 1 ? second : side === -1 ? first : moved(a, b);
|
|
2419
|
+
const start = side === 1 ? mid : a;
|
|
2420
|
+
const end = side === -1 ? mid : b;
|
|
2421
|
+
const half = side === 0 ? halfSec : quarter;
|
|
2422
|
+
const textCause = change.text > 0 ? textLabel(change.textElement, end) : null;
|
|
2423
|
+
return {
|
|
2424
|
+
// a hook speaks for what the page draws; its elements outside a canvas
|
|
2425
|
+
// are still measured here, and the larger of the two is the motion
|
|
2426
|
+
px: Math.max(
|
|
2427
|
+
change.px,
|
|
2428
|
+
told ? await told(tSec + side * quarter, half) : canvasMotion(start, end, true),
|
|
2429
|
+
),
|
|
2430
|
+
side,
|
|
2431
|
+
text: change.text / (2 * half),
|
|
2432
|
+
textCause,
|
|
2433
|
+
view: rate(change.view, 2 * half),
|
|
2434
|
+
};
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
// --- ready -----------------------------------------------------------------
|
|
2438
|
+
async function prepare(): Promise<unknown> {
|
|
2439
|
+
// a module page still in a top-level await (a fetch, a font) has declared
|
|
2440
|
+
// nothing yet: its length and soundtrack would be read as missing. Wait
|
|
2441
|
+
// for it, up to 5 s; a page that declares nothing goes on as before.
|
|
2442
|
+
const declared = () =>
|
|
2443
|
+
w.__film != null || typeof w.__render === "function" || w.__duration != null;
|
|
2444
|
+
if (!declared() && document.querySelector('script[type="module"]')) {
|
|
2445
|
+
const began = RealDate.now();
|
|
2446
|
+
await pump(
|
|
2447
|
+
new Promise<void>((done) => {
|
|
2448
|
+
const look = () =>
|
|
2449
|
+
declared() || RealDate.now() - began > 5_000 ? done() : w.setTimeout(look, 16);
|
|
2450
|
+
look();
|
|
2451
|
+
}),
|
|
2452
|
+
);
|
|
2453
|
+
}
|
|
2454
|
+
const film = w.__film ?? {};
|
|
2455
|
+
const ready = film.ready ?? w.__ready;
|
|
2456
|
+
if (ready) await pump(typeof ready === "function" ? ready() : ready);
|
|
2457
|
+
base = now;
|
|
2458
|
+
begun = true;
|
|
2459
|
+
grid = 0;
|
|
2460
|
+
mode = typeof w.__render === "function" ? "render" : "clock";
|
|
2461
|
+
if (mode === "clock") await firstFrame();
|
|
2462
|
+
await settle();
|
|
2463
|
+
const num = (v: unknown) => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : null);
|
|
2464
|
+
const tickers: FilmPageInfo["tickers"] = [];
|
|
2465
|
+
if (mode === "clock") {
|
|
2466
|
+
if (gsapRunning(w.gsap)) tickers.push("GSAP");
|
|
2467
|
+
// Anime v4's ESM and UMD builds register their engine in AnimeJS.
|
|
2468
|
+
// A build that does not expose its engine cannot be identified here.
|
|
2469
|
+
if (Array.isArray(w.AnimeJS) && w.AnimeJS.some(
|
|
2470
|
+
(entry: { engine?: { reqId?: number } } | null) => !!entry?.engine?.reqId,
|
|
2471
|
+
)) tickers.push("anime.js");
|
|
2472
|
+
}
|
|
2473
|
+
return {
|
|
2474
|
+
mode,
|
|
2475
|
+
tickers,
|
|
2476
|
+
duration: num(film.duration ?? w.__duration),
|
|
2477
|
+
width: num(film.width),
|
|
2478
|
+
height: num(film.height),
|
|
2479
|
+
fps: num(film.fps),
|
|
2480
|
+
audio: typeof film.audio === "string" ? film.audio : null,
|
|
2481
|
+
audioElements: document.querySelectorAll("audio").length,
|
|
2482
|
+
// plain values only, to cross into the renderer; checked there (filmMarks)
|
|
2483
|
+
marks: Array.isArray(film.marks)
|
|
2484
|
+
? // biome-ignore lint/suspicious/noExplicitAny: the page's own marks, untyped
|
|
2485
|
+
film.marks.map((m: any) => ({ at: m?.at, until: m?.until, what: m?.what }))
|
|
2486
|
+
: film.marks == null
|
|
2487
|
+
? null
|
|
2488
|
+
: String(film.marks),
|
|
2489
|
+
};
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
/** a call from the renderer: the frames it waits for count from now */
|
|
2493
|
+
const called =
|
|
2494
|
+
<A extends unknown[], R>(fn: (...a: A) => R) =>
|
|
2495
|
+
(...a: A): R => {
|
|
2496
|
+
framesUntil = RealDate.now() + FRAMES_WAIT_MS;
|
|
2497
|
+
return fn(...a);
|
|
2498
|
+
};
|
|
2499
|
+
w.__ot = {
|
|
2500
|
+
prepare: called(prepare),
|
|
2501
|
+
seek: called(seek),
|
|
2502
|
+
motion: called(motion),
|
|
2503
|
+
framesReady,
|
|
2504
|
+
// --- for the frames inside the film (times in ms on the top page's clock) ---
|
|
2505
|
+
framesBy,
|
|
2506
|
+
follow,
|
|
2507
|
+
stamp,
|
|
2508
|
+
showFrames,
|
|
2509
|
+
shown: shownHere,
|
|
2510
|
+
sync: syncAnimations,
|
|
2511
|
+
busy,
|
|
2512
|
+
asked: askedHere,
|
|
2513
|
+
runAsked: (at: number) => runFrames(asked, at),
|
|
2514
|
+
drawn,
|
|
2515
|
+
clock,
|
|
2516
|
+
origin: () => base,
|
|
2517
|
+
// inline styles the page set that did not take (see StyleReject): kept
|
|
2518
|
+
// here for the renderer, which drains them; a frame's go to its holder
|
|
2519
|
+
noteStyle,
|
|
2520
|
+
styleRejects: () => rejected.splice(0),
|
|
2521
|
+
// in a page that draws any moment on request, which moment put a frame on
|
|
2522
|
+
// the page first depends on the order they were drawn in: its frames
|
|
2523
|
+
// start at film time 0
|
|
2524
|
+
bornOf: (el: Element | null) => {
|
|
2525
|
+
if (drawn()) return base;
|
|
2526
|
+
drain();
|
|
2527
|
+
return (el && inserted.get(el)) ?? clock();
|
|
2528
|
+
},
|
|
2529
|
+
};
|
|
2530
|
+
}
|