"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Easing: () => easing_exports, createAnimationController: () => createAnimationController }); module.exports = __toCommonJS(src_exports); // src/easing.ts var easing_exports = {}; __export(easing_exports, { inBack: () => inBack, inBounce: () => inBounce, inCirc: () => inCirc, inCubic: () => inCubic, inElastic: () => inElastic, inExpo: () => inExpo, inOutBack: () => inOutBack, inOutBounce: () => inOutBounce, inOutCirc: () => inOutCirc, inOutCubic: () => inOutCubic, inOutElastic: () => inOutElastic, inOutExpo: () => inOutExpo, inOutQuad: () => inOutQuad, inOutQuart: () => inOutQuart, inOutQuint: () => inOutQuint, inOutSine: () => inOutSine, inQuad: () => inQuad, inQuart: () => inQuart, inQuint: () => inQuint, inSine: () => inSine, linear: () => linear, outBack: () => outBack, outBounce: () => outBounce, outCirc: () => outCirc, outCubic: () => outCubic, outElastic: () => outElastic, outExpo: () => outExpo, outQuad: () => outQuad, outQuart: () => outQuart, outQuint: () => outQuint, outSine: () => outSine }); var linear = (t, b, c, d) => c * t / d + b; var inQuad = (t, b, c, d) => c * (t /= d) * t + b; var outQuad = (t, b, c, d) => -c * (t /= d) * (t - 2) + b; var inOutQuad = (t, b, c, d) => { if ((t /= d / 2) < 1) return c / 2 * t * t + b; return -c / 2 * (--t * (t - 2) - 1) + b; }; var inCubic = (t, b, c, d) => c * (t /= d) * t * t + b; var outCubic = (t, b, c, d) => c * ((t = t / d - 1) * t * t + 1) + b; var inOutCubic = (t, b, c, d) => { if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; return c / 2 * ((t -= 2) * t * t + 2) + b; }; var inQuart = (t, b, c, d) => c * (t /= d) * t * t * t + b; var outQuart = (t, b, c, d) => -c * ((t = t / d - 1) * t * t * t - 1) + b; var inOutQuart = (t, b, c, d) => { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b; return -c / 2 * ((t -= 2) * t * t * t - 2) + b; }; var inQuint = (t, b, c, d) => c * (t /= d) * t * t * t * t + b; var outQuint = (t, b, c, d) => c * ((t = t / d - 1) * t * t * t * t + 1) + b; var inOutQuint = (t, b, c, d) => { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b; return c / 2 * ((t -= 2) * t * t * t * t + 2) + b; }; var inSine = (t, b, c, d) => -c * Math.cos(t / d * (Math.PI / 2)) + c + b; var outSine = (t, b, c, d) => c * Math.sin(t / d * (Math.PI / 2)) + b; var inOutSine = (t, b, c, d) => -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b; var inExpo = (t, b, c, d) => t === 0 ? b : c * 2 ** (10 * (t / d - 1)) + b; var outExpo = (t, b, c, d) => t === d ? b + c : c * (-(2 ** (-10 * t / d)) + 1) + b; var inOutExpo = (t, b, c, d) => { if (t === 0) return b; if (t === d) return b + c; if ((t /= d / 2) < 1) return c / 2 * 2 ** (10 * (t - 1)) + b; return c / 2 * (-(2 ** (-10 * --t)) + 2) + b; }; var inCirc = (t, b, c, d) => -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b; var outCirc = (t, b, c, d) => c * Math.sqrt(1 - (t = t / d - 1) * t) + b; var inOutCirc = (t, b, c, d) => { if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b; return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b; }; var inElastic = (t, b, c, d) => { let s = 1.70158; let p = 0; let a = c; if (t === 0) return b; if ((t /= d) === 1) return b + c; p ||= d * 0.3; if (a < Math.abs(c)) { a = c; s = p / 4; } else s = p / (2 * Math.PI) * Math.asin(c / a); return -(a * 2 ** (10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; }; var outElastic = (t, b, c, d) => { let s = 1.70158; let p = 0; let a = c; if (t === 0) return b; if ((t /= d) === 1) return b + c; p ||= d * 0.3; if (a < Math.abs(c)) { a = c; s = p / 4; } else s = p / (2 * Math.PI) * Math.asin(c / a); return a * 2 ** (-10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b; }; var inOutElastic = (t, b, c, d) => { let s = 1.70158; let p = 0; let a = c; if (t === 0) return b; if ((t /= d / 2) === 2) return b + c; p ||= d * (0.3 * 1.5); if (a < Math.abs(c)) { a = c; s = p / 4; } else s = p / (2 * Math.PI) * Math.asin(c / a); if (t < 1) return -0.5 * (a * 2 ** (10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; return a * 2 ** (-10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * 0.5 + c + b; }; var inBack = (t, b, c, d) => { const s = 1.70158; return c * (t /= d) * t * ((s + 1) * t - s) + b; }; var outBack = (t, b, c, d) => { const s = 1.70158; return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b; }; var inOutBack = (t, b, c, d) => { let s = 1.70158; if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= 1.525) + 1) * t - s)) + b; return c / 2 * ((t -= 2) * t * (((s *= 1.525) + 1) * t + s) + 2) + b; }; var outBounce = (t, b, c, d) => { if ((t /= d) < 1 / 2.75) { return c * (7.5625 * t * t) + b; } if (t < 2 / 2.75) { return c * (7.5625 * (t -= 1.5 / 2.75) * t + 0.75) + b; } if (t < 2.5 / 2.75) { return c * (7.5625 * (t -= 2.25 / 2.75) * t + 0.9375) + b; } return c * (7.5625 * (t -= 2.625 / 2.75) * t + 0.984375) + b; }; var inBounce = (t, b, c, d) => c - outBounce(d - t, 0, c, d) + b; var inOutBounce = (t, b, c, d) => { if (t < d / 2) return inBounce(t * 2, 0, c, d) * 0.5 + b; return outBounce(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b; }; // src/utils.ts function restrictNumber(v, border1, border2) { const min = Math.min(border1, border2); const max = Math.max(border1, border2); return Math.min(Math.max(v, min), max); } function createClockingMechanism(clock) { if (clock) { let delayed; return { setInterval(callback, ms) { delayed?.clear(); delayed = clock.setInterval(callback, ms); }, clearInterval: () => delayed?.clear() }; } let intervalId; return { setInterval(callback, ms) { if (intervalId) { clearInterval(intervalId); } intervalId = setInterval(callback, ms); }, clearInterval() { if (intervalId) { clearInterval(intervalId); } intervalId = void 0; } }; } // src/animation.ts var createAnimationController = (config, hooks = {}) => { const { loop = false, fps = 60 } = config; const frameDuration = 1e3 / fps; const { setInterval: setInterval2, clearInterval: clearInterval2 } = createClockingMechanism(config.clock); let isAnimationActive = false; let _onFrame; let segments = config.segments; let currentSegmentIndex = 0; let currentAnimatedValue = segments[0].initialValue; const animateSegment = () => { if (!isAnimationActive) { return; } const { toValue, duration, easing = linear } = segments[currentSegmentIndex]; const segmentDuration = typeof duration === "function" ? duration() : duration; const beginningValue = currentSegmentIndex > 0 ? segments[currentSegmentIndex - 1].toValue : segments[0].initialValue; let currentTimeWithinSegment = 0; setInterval2(() => { currentTimeWithinSegment += frameDuration; currentAnimatedValue = restrictNumber( easing(currentTimeWithinSegment, beginningValue, toValue - beginningValue, segmentDuration), beginningValue, toValue ); _onFrame(currentAnimatedValue); if (currentTimeWithinSegment >= segmentDuration) { clearInterval2(); const isLastSegment = currentSegmentIndex === segments.length - 1; if (!isLastSegment) { currentSegmentIndex++; animateSegment(); return; } if (loop) { currentSegmentIndex = 0; hooks.onLoop?.(); animateSegment(); return; } hooks.onEnd?.(); } }, frameDuration); }; const resetAndStop = () => { clearInterval2(); currentSegmentIndex = 0; currentAnimatedValue = segments[0].initialValue; }; return { start(onFrame, withNewSegments) { _onFrame = onFrame; if (withNewSegments) { segments = withNewSegments; } isAnimationActive = true; resetAndStop(); hooks.onStart?.(); animateSegment(); }, stop() { isAnimationActive = false; resetAndStop(); hooks.onStop?.(); } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Easing, createAnimationController });