From a9cdc16f2ac433da5e72093487663748f717c229 Mon Sep 17 00:00:00 2001 From: wryk Date: Thu, 2 Mar 2023 07:38:31 +0100 Subject: [PATCH] respect reduced motion for counter animation --- src/js/script.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/js/script.js b/src/js/script.js index 8b4f0ab..3aff925 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -67,22 +67,26 @@ document.addEventListener('alpine:init', () => { }) function animateRange(duration, start, end, callback) { - let startTimestamp = null; + if (matchMedia("(prefers-reduced-motion)").matches) { + callback(end) + } else { + let startTimestamp = null; - const step = (timestamp) => { - if (!startTimestamp) { - startTimestamp = timestamp + const step = (timestamp) => { + if (!startTimestamp) { + startTimestamp = timestamp + }; + + const progress = Math.min((timestamp - startTimestamp) / duration, 1) + callback(Math.floor(progress * (end - start) + start)) + + if (progress < 1) { + requestAnimationFrame(step) + } }; - const progress = Math.min((timestamp - startTimestamp) / duration, 1) - callback(Math.floor(progress * (end - start) + start)) - - if (progress < 1) { - requestAnimationFrame(step) - } - }; - - requestAnimationFrame(step); + requestAnimationFrame(step) + } } function formatNumber(number) {