Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/crutchcorn/bowtie-button-demo. A fun little animation library test crutchcorn.github.io/bowtie-button-demo
Something went wrong. Try again.
bowtie-button-demo script.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164const animDurationMs = 300;// The final frame is frame9, so there are nine intervals between frame0 and frame9.const frameIntervals = 9;
const el = document.querySelector('.bowtie-button');const bowties = el.querySelector('.bowties');
// The scale is deliberately non-uniform: it tightens the vertical tile spacing, and each frame SVG// pre-stretches its artwork to match so the bowties stay un-squished. See assets/README.md.//// This effect never plays on its own. A single requestAnimationFrame loop seeks it and swaps the SVG// frame from the same progress value, so rapid direction changes cannot leave two clocks out of step.const press = new Animation( new KeyframeEffect( bowties, [ { transform: 'scale(1, 1)', opacity: 1 }, { transform: 'scale(4, 2)', opacity: 0.24 }, ], { duration: animDurationMs, easing: 'ease-in-out', fill: 'both' } ), document.timeline);
let progress = 0;let targetProgress = 0;let previousTimestamp = null;let rafId = null;let renderedFrame = 0;
function render() { press.currentTime = progress * animDurationMs;
const frameNum = Math.min(frameIntervals, Math.floor(progress * frameIntervals)); if (frameNum === renderedFrame) return;
renderedFrame = frameNum; bowties.style.backgroundImage = `url("/bowtie-button-demo/assets/bowtie-frames.svg#frame${frameNum}")`;}
function advance(timestamp) { if (previousTimestamp === null) return;
const elapsedProgress = Math.max(0, timestamp - previousTimestamp) / animDurationMs; progress = targetProgress === 1 ? Math.min(targetProgress, progress + elapsedProgress) : Math.max(targetProgress, progress - elapsedProgress); if (Math.abs(progress - targetProgress) < 1e-12) progress = targetProgress; previousTimestamp = timestamp;}
function finishAtTarget() { if (rafId !== null) cancelAnimationFrame(rafId); previousTimestamp = null; rafId = null;
if (targetProgress !== 0) return;
// Once the press has fully unwound, hand opacity back to the CSS hover rule and let the stylesheet // own frame0 again. Canceling before this boundary is what used to expose an unsynchronised frame. press.cancel(); bowties.style.removeProperty('background-image'); renderedFrame = 0;}
function tick() { // Event handlers also use performance.now(); keeping every sample on that clock prevents a RAF // timestamp from moving previousTimestamp backwards around a rapid reversal. advance(performance.now()); render();
if (progress === targetProgress) { finishAtTarget(); return; }
rafId = requestAnimationFrame(tick);}
function setPressed(isPressed) { const nextTarget = isPressed ? 1 : 0; if (nextTarget === targetProgress) return;
const timestamp = performance.now(); // Account for time since the last paint in the old direction before reversing. This makes a rapid // release/re-press continuous even when both events land between animation frames. advance(timestamp); targetProgress = nextTarget; previousTimestamp = timestamp; render();
if (progress === targetProgress) { finishAtTarget(); return; }
if (rafId === null) rafId = requestAnimationFrame(tick);}
const activePointers = new Set();const activeKeys = new Set();
function syncPressedState() { setPressed(activePointers.size > 0 || activeKeys.size > 0);}
el.addEventListener('pointerdown', event => { if (event.button !== 0) return;
activePointers.add(event.pointerId); try { el.setPointerCapture(event.pointerId); } catch { // The persistent document listeners below are also a fallback when capture is unavailable. } syncPressedState();});
function releasePointer(event) { if (!activePointers.delete(event.pointerId)) return; syncPressedState();}
document.addEventListener('pointerup', releasePointer, true);document.addEventListener('pointercancel', releasePointer, true);el.addEventListener('lostpointercapture', releasePointer);
function isActivationKey(event) { return event.key === 'Enter' || event.key === ' ';}
function keyId(event) { return event.code || event.key;}
el.addEventListener('keydown', event => { if (!isActivationKey(event)) return;
activeKeys.add(keyId(event)); syncPressedState();});
document.addEventListener( 'keyup', event => { if (!isActivationKey(event) || !activeKeys.delete(keyId(event))) return; syncPressedState(); }, true);
function releaseAllInputs() { if (activePointers.size === 0 && activeKeys.size === 0) return;
activePointers.clear(); activeKeys.clear(); syncPressedState();}
window.addEventListener('blur', releaseAllInputs);document.addEventListener('visibilitychange', () => { if (document.hidden) releaseAllInputs();});