common.js (1768B)
1 'use strict'; 2 3 function registerPassthroughAnimator() { 4 return runInAnimationWorklet(` 5 registerAnimator('passthrough', class { 6 animate(currentTime, effect) { 7 effect.localTime = currentTime; 8 } 9 }); 10 `); 11 } 12 13 function registerConstantLocalTimeAnimator(localTime) { 14 return runInAnimationWorklet(` 15 registerAnimator('constant_time', class { 16 animate(currentTime, effect) { effect.localTime = ${localTime}; } 17 }); 18 `); 19 } 20 21 function runInAnimationWorklet(code) { 22 return CSS.animationWorklet.addModule( 23 URL.createObjectURL(new Blob([code], {type: 'text/javascript'})) 24 ); 25 } 26 27 function approxEquals(actual, expected){ 28 // precision in ms 29 const epsilon = 0.005; 30 const lowerBound = (expected - epsilon) < actual; 31 const upperBound = (expected + epsilon) > actual; 32 return lowerBound && upperBound; 33 } 34 35 function waitForAsyncAnimationFrames(count) { 36 // In Chrome, waiting for N+1 main thread frames guarantees that compositor has produced 37 // at least N frames. 38 // TODO(majidvp): re-evaluate this choice once other browsers have implemented 39 // AnimationWorklet. 40 return waitForAnimationFrames(count + 1); 41 } 42 43 async function waitForAnimationFrameWithCondition(condition) { 44 do { 45 await new Promise(window.requestAnimationFrame); 46 } while (!condition()) 47 } 48 49 async function waitForDocumentTimelineAdvance() { 50 const timeAtStart = document.timeline.currentTime; 51 do { 52 await new Promise(window.requestAnimationFrame); 53 } while (timeAtStart === document.timeline.currentTime) 54 } 55 56 // Wait until animation's effect has a non-null localTime. 57 async function waitForNotNullLocalTime(animation) { 58 await waitForAnimationFrameWithCondition(_ => { 59 return animation.effect.getComputedTiming().localTime !== null; 60 }); 61 }