Last active
November 6, 2022 09:19
-
-
Save gordonbrander/4640bb7ec552658d224e to your computer and use it in GitHub Desktop.
scene.js - basic Timeline-driven animation functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const progress = (t, start, end) => | |
t < start ? 0 : | |
t > end ? 1 : | |
(t / (end - start)); | |
export const tween = (start, end, step, state, t) => | |
step(state, progress(t, start, end)); | |
export const event = (time, step, state, t) => | |
t < time ? step(state, 0) : step(state, 1); | |
export const scene = callback => duration => { | |
const start = performance.now(); | |
const frame = (t) => { | |
const elapsed = (t - start); | |
callback(elapsed); | |
if (elapsed < duration) requestAnimationFrame(frame); | |
}; | |
requestAnimationFrame(frame); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment