Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
Last active November 6, 2022 09:19
Show Gist options
  • Save gordonbrander/4640bb7ec552658d224e to your computer and use it in GitHub Desktop.
Save gordonbrander/4640bb7ec552658d224e to your computer and use it in GitHub Desktop.
scene.js - basic Timeline-driven animation functions
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