Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/native. An initiative which aims to make it easy to create complex, light-weight, and perfor okikio.github.io/native
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222type PlaybackState = "idle" | "running" | "paused" | "finished";type PlaybackDatasetState = "play" | "pause" | "replay";
const PLAYBACK_STATES: Record<PlaybackState, PlaybackDatasetState> = { idle: "pause", running: "play", paused: "pause", finished: "replay",};
interface DemoElements { canvas: HTMLElement; boxes: [HTMLElement, HTMLElement]; controlButton: HTMLButtonElement; playback: HTMLElement; progressBar: HTMLInputElement; progressOutput: HTMLElement;}
class PlaybackTimeline { private readonly animations: [Animation, Animation]; private readonly totalDuration: number;
constructor(animations: [Animation, Animation]) { this.animations = animations; this.totalDuration = Math.max(...animations.map((animation) => this.getAnimationEndTime(animation))); this.pause(); this.setProgress(0); }
play() { this.animations.forEach((animation) => animation.play()); }
pause() { this.animations.forEach((animation) => animation.pause()); }
reset() { this.animations.forEach((animation) => { animation.cancel(); animation.pause(); animation.currentTime = 0; }); }
setProgress(percent: number) { const currentTime = this.totalDuration * (percent / 100); this.animations.forEach((animation) => { animation.pause(); animation.currentTime = currentTime; }); }
getProgress() { const currentTime = Math.max(...this.animations.map((animation) => this.toNumber(animation.currentTime))); return (currentTime / this.totalDuration) * 100; }
getState(): PlaybackState { const [firstAnimation] = this.animations; const state = firstAnimation.playState;
if (state === "running") { return "running"; }
if (state === "paused" && this.toNumber(firstAnimation.currentTime) > 0) { return "paused"; }
if (state === "finished") { return "finished"; }
return "idle"; }
private getAnimationEndTime(animation: Animation) { if (animation.effect instanceof KeyframeEffect) { return this.toNumber(animation.effect.getComputedTiming().endTime); }
return 0; }
private toNumber(value: CSSNumberish | null | undefined) { if (typeof value === "number") { return value; }
if (value && typeof value === "object" && "value" in value && typeof value.value === "number") { return value.value; }
return 0; }}
function getDemoElements(): DemoElements | null { const container = document.querySelector<HTMLElement>("#simple-demo"); const canvas = container?.querySelector<HTMLElement>(".playback-canvas"); const boxes = container?.querySelectorAll<HTMLElement>(".animated-box"); const controlButton = container?.querySelector<HTMLButtonElement>(".control-btn"); const playback = container?.querySelector<HTMLElement>(".playback"); const progressBar = container?.querySelector<HTMLInputElement>(".progress-bar"); const progressOutput = container?.querySelector<HTMLElement>(".progress-output");
if ( !(canvas instanceof HTMLElement) || !boxes || boxes.length < 2 || !(controlButton instanceof HTMLButtonElement) || !(playback instanceof HTMLElement) || !(progressBar instanceof HTMLInputElement) || !(progressOutput instanceof HTMLElement) ) { return null; }
return { canvas, boxes: [boxes[0], boxes[1]], controlButton, playback, progressBar, progressOutput, };}
function createAnimation(box: HTMLElement, x: number, y: number, delay = 0) { return box.animate( [ { transform: "translate(0px, 0px)" }, { transform: `translate(${x}px, ${y}px)` }, ], { delay, duration: 3000, direction: "alternate", easing: "cubic-bezier(0.34, 1.56, 0.64, 1)", fill: "both", iterations: 5, } );}
function updatePlaybackUi(elements: DemoElements, timeline: PlaybackTimeline) { const progress = Math.max(0, Math.min(100, timeline.getProgress())); elements.progressBar.value = `${progress}`; elements.progressOutput.textContent = `${Math.round(progress)}%`; elements.playback.dataset.state = PLAYBACK_STATES[timeline.getState()];}
export function startPlaybackDemo() { const elements = getDemoElements(); if (!elements) { return null; }
const [primaryBox, secondaryBox] = elements.boxes; const motionWidth = Math.max(elements.canvas.clientWidth - primaryBox.offsetWidth, 0); const motionHeight = Math.max(elements.canvas.clientHeight - primaryBox.offsetHeight, 0);
const timeline = new PlaybackTimeline([ createAnimation(primaryBox, motionWidth, motionHeight), createAnimation(secondaryBox, motionWidth, motionHeight, 400), ]);
let previousState = timeline.getState(); let rafId = 0;
const tick = () => { updatePlaybackUi(elements, timeline);
if (timeline.getState() === "running") { rafId = window.requestAnimationFrame(tick); } };
const handleClick = () => { const state = timeline.getState();
if (state === "running") { timeline.pause(); } else if (state === "finished") { timeline.reset(); timeline.play(); } else { timeline.play(); }
window.cancelAnimationFrame(rafId); rafId = window.requestAnimationFrame(tick); previousState = timeline.getState(); updatePlaybackUi(elements, timeline); };
const handleInput = () => { timeline.setProgress(Number(elements.progressBar.value)); updatePlaybackUi(elements, timeline); };
const handleChange = () => { if (previousState === "running") { timeline.play(); rafId = window.requestAnimationFrame(tick); } else { timeline.pause(); }
updatePlaybackUi(elements, timeline); };
elements.controlButton.addEventListener("click", handleClick); elements.progressBar.addEventListener("input", handleInput); elements.progressBar.addEventListener("change", handleChange); updatePlaybackUi(elements, timeline);
return timeline;}