anim-engine 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +639 -0
  3. package/dist/animation/create-animation.d.ts +24 -0
  4. package/dist/animation/create-animation.js +336 -0
  5. package/dist/animation/update.d.ts +12 -0
  6. package/dist/animation/update.js +22 -0
  7. package/dist/benchmarks/easing.bench.d.ts +1 -0
  8. package/dist/benchmarks/vs-gsap.bench.d.ts +1 -0
  9. package/dist/color/lerp-oklab.d.ts +26 -0
  10. package/dist/color/lerp-oklab.js +182 -0
  11. package/dist/easing/easing.d.ts +16 -0
  12. package/dist/easing/easing.js +154 -0
  13. package/dist/index.d.ts +17 -0
  14. package/dist/index.js +10 -0
  15. package/dist/lerp/create-lerp.d.ts +9 -0
  16. package/dist/lerp/create-lerp.js +65 -0
  17. package/dist/lerp/step.d.ts +14 -0
  18. package/dist/lerp/step.js +14 -0
  19. package/dist/shared/internal.d.ts +4 -0
  20. package/dist/shared/types.d.ts +29 -0
  21. package/dist/smooth-clamp/smooth-clamp.d.ts +13 -0
  22. package/dist/smooth-clamp/smooth-clamp.js +22 -0
  23. package/dist/smooth-damp/create-smooth-damp.d.ts +10 -0
  24. package/dist/smooth-damp/create-smooth-damp.js +63 -0
  25. package/dist/smooth-damp/step.d.ts +13 -0
  26. package/dist/smooth-damp/step.js +26 -0
  27. package/dist/spring/create-spring.d.ts +11 -0
  28. package/dist/spring/create-spring.js +67 -0
  29. package/dist/spring/verlet.d.ts +13 -0
  30. package/dist/spring/verlet.js +9 -0
  31. package/dist/ticker/get-ticker.d.ts +5 -0
  32. package/dist/ticker/get-ticker.js +10 -0
  33. package/dist/ticker/ticker.d.ts +22 -0
  34. package/dist/ticker/ticker.js +74 -0
  35. package/dist/timeline/create-timeline.d.ts +25 -0
  36. package/dist/timeline/create-timeline.js +139 -0
  37. package/package.json +47 -0
@@ -0,0 +1,74 @@
1
+ //#region src/ticker/ticker.ts
2
+ /**
3
+ * Create a ticker that drives active animations.
4
+ *
5
+ * Does NOT auto-start. The user must explicitly call either:
6
+ * - `start()` — begins a `requestAnimationFrame` loop
7
+ * - `update(deltaMs)` — drive manually from a game loop
8
+ *
9
+ * `add()` and `remove()` register/unregister animations without
10
+ * side effects on the rAF loop.
11
+ *
12
+ * Uses a flat array with undefined-tombstone removal for safe concurrent
13
+ * modification during iteration. Compacted after each frame.
14
+ */
15
+ var createTicker = () => {
16
+ const activeAnimations = [];
17
+ let needsCompact = false;
18
+ let animationFrameRequestId = void 0;
19
+ let previousFrameTime = void 0;
20
+ const start = () => {
21
+ if (animationFrameRequestId !== void 0) return;
22
+ previousFrameTime = void 0;
23
+ const tick = (now) => {
24
+ if (previousFrameTime !== void 0) {
25
+ const delta = now - previousFrameTime;
26
+ update(delta);
27
+ }
28
+ previousFrameTime = now;
29
+ animationFrameRequestId = requestAnimationFrame(tick);
30
+ };
31
+ animationFrameRequestId = requestAnimationFrame(tick);
32
+ };
33
+ const stop = () => {
34
+ if (animationFrameRequestId !== void 0) {
35
+ cancelAnimationFrame(animationFrameRequestId);
36
+ animationFrameRequestId = void 0;
37
+ }
38
+ previousFrameTime = void 0;
39
+ };
40
+ const update = (deltaMs) => {
41
+ for (let i = 0; i < activeAnimations.length; i++) {
42
+ const anim = activeAnimations[i];
43
+ if (anim) anim(deltaMs);
44
+ }
45
+ if (needsCompact) {
46
+ let writeIndex = 0;
47
+ for (let readIndex = 0; readIndex < activeAnimations.length; readIndex++) {
48
+ const anim = activeAnimations[readIndex];
49
+ if (anim !== void 0) activeAnimations[writeIndex++] = anim;
50
+ }
51
+ activeAnimations.length = writeIndex;
52
+ needsCompact = false;
53
+ }
54
+ };
55
+ const add = (handler) => {
56
+ activeAnimations.push(handler);
57
+ };
58
+ const remove = (handler) => {
59
+ const index = activeAnimations.indexOf(handler);
60
+ if (index >= 0) {
61
+ activeAnimations[index] = void 0;
62
+ needsCompact = true;
63
+ }
64
+ };
65
+ return {
66
+ start,
67
+ stop,
68
+ update,
69
+ add,
70
+ remove
71
+ };
72
+ };
73
+ //#endregion
74
+ export { createTicker };
@@ -0,0 +1,25 @@
1
+ import { Animation, AnimationStatus } from '../shared/types';
2
+ export type TimelineLayer = {
3
+ at: number;
4
+ animation: Animation | Animation[];
5
+ } | {
6
+ gap: number;
7
+ animation: Animation | Animation[];
8
+ };
9
+ export type Timeline = {
10
+ play: () => Promise<Timeline>;
11
+ pause: () => void;
12
+ resume: () => void;
13
+ stop: () => void;
14
+ skipToEnd: () => void;
15
+ kill: () => void;
16
+ setProgress: (value: number) => void;
17
+ progress: number;
18
+ status: AnimationStatus;
19
+ durationMs: number;
20
+ };
21
+ export declare const createTimeline: (layers: TimelineLayer[], options?: {
22
+ onStarted?: () => void;
23
+ onProgress?: (progress: number) => void;
24
+ onEnded?: () => void;
25
+ }) => Timeline;
@@ -0,0 +1,139 @@
1
+ import { getTicker } from "../ticker/get-ticker.js";
2
+ //#region src/timeline/create-timeline.ts
3
+ var createTimeline = (layers, options) => {
4
+ const { onStarted, onProgress, onEnded } = options ?? {};
5
+ const batches = [];
6
+ let lastBatchEnd = 0;
7
+ for (const layer of layers) {
8
+ const anims = Array.isArray(layer.animation) ? layer.animation : [layer.animation];
9
+ const startAt = "at" in layer ? layer.at : lastBatchEnd + layer.gap;
10
+ const endAt = startAt + Math.max(...anims.map((a) => a.durationMs));
11
+ batches.push({
12
+ animations: anims,
13
+ startAt,
14
+ endAt,
15
+ started: false
16
+ });
17
+ lastBatchEnd = endAt;
18
+ }
19
+ batches.sort((a, b) => a.startAt - b.startAt);
20
+ const totalDurationMs = Math.max(0, ...batches.map((b) => b.endAt));
21
+ let status = "stopped";
22
+ let elapsedMs = 0;
23
+ let resolvePromise;
24
+ let pendingAnimations = 0;
25
+ const ticker = getTicker();
26
+ const play = () => {
27
+ if (status === "dead") throw new Error("Cannot play a dead timeline");
28
+ elapsedMs = 0;
29
+ pendingAnimations = 0;
30
+ batches.forEach((b) => {
31
+ b.started = false;
32
+ });
33
+ const promise = new Promise((resolve) => {
34
+ resolvePromise = resolve;
35
+ });
36
+ status = "playing";
37
+ onStarted?.();
38
+ ticker.add(update);
39
+ return promise;
40
+ };
41
+ const pause = () => {
42
+ if (status !== "playing") return;
43
+ status = "paused";
44
+ ticker.remove(update);
45
+ for (const batch of batches) if (batch.started) {
46
+ for (const anim of batch.animations) if (anim.status === "playing") anim.pause();
47
+ }
48
+ };
49
+ const resume = () => {
50
+ if (status !== "paused") return;
51
+ status = "playing";
52
+ for (const batch of batches) if (batch.started) {
53
+ for (const anim of batch.animations) if (anim.status === "paused") anim.resume();
54
+ }
55
+ ticker.add(update);
56
+ };
57
+ const stop = () => {
58
+ if (status !== "playing" && status !== "paused") return;
59
+ status = "stopped";
60
+ ticker.remove(update);
61
+ for (const batch of batches) if (batch.started) for (const anim of batch.animations) anim.stop();
62
+ resolvePromise?.(timeline);
63
+ resolvePromise = void 0;
64
+ };
65
+ const skipToEnd = () => {
66
+ status = "stopped";
67
+ ticker.remove(update);
68
+ for (const batch of batches) for (const anim of batch.animations) anim.skipToEnd();
69
+ onEnded?.();
70
+ resolvePromise?.(timeline);
71
+ resolvePromise = void 0;
72
+ };
73
+ const kill = () => {
74
+ status = "dead";
75
+ ticker.remove(update);
76
+ for (const batch of batches) for (const anim of batch.animations) anim.kill();
77
+ resolvePromise = void 0;
78
+ };
79
+ const update = (deltaMs) => {
80
+ if (status !== "playing") return;
81
+ elapsedMs += deltaMs;
82
+ for (const batch of batches) if (!batch.started && elapsedMs >= batch.startAt) {
83
+ batch.started = true;
84
+ pendingAnimations += batch.animations.length;
85
+ for (const anim of batch.animations) anim.play().then(() => {
86
+ pendingAnimations--;
87
+ checkComplete();
88
+ });
89
+ }
90
+ onProgress?.(totalDurationMs > 0 ? Math.min(elapsedMs / totalDurationMs, 1) : 0);
91
+ if (batches.every((b) => b.started) && pendingAnimations <= 0) finish();
92
+ };
93
+ const checkComplete = () => {
94
+ if (pendingAnimations <= 0 && status === "playing" && batches.every((b) => b.started)) finish();
95
+ };
96
+ const finish = () => {
97
+ status = "stopped";
98
+ ticker.remove(update);
99
+ onEnded?.();
100
+ resolvePromise?.(timeline);
101
+ resolvePromise = void 0;
102
+ };
103
+ const setProgress = (value) => {
104
+ if (status === "playing") pause();
105
+ elapsedMs = Math.max(0, Math.min(1, value)) * totalDurationMs;
106
+ for (const batch of batches) if (elapsedMs < batch.startAt) {
107
+ for (const anim of batch.animations) anim.setProgress(0);
108
+ batch.started = false;
109
+ } else {
110
+ batch.started = true;
111
+ const batchElapsed = elapsedMs - batch.startAt;
112
+ for (const anim of batch.animations) {
113
+ const localProgress = anim.durationMs > 0 ? Math.min(batchElapsed / anim.durationMs, 1) : 1;
114
+ anim.setProgress(localProgress);
115
+ }
116
+ }
117
+ };
118
+ const timeline = {
119
+ play,
120
+ pause,
121
+ resume,
122
+ stop,
123
+ skipToEnd,
124
+ kill,
125
+ setProgress,
126
+ get progress() {
127
+ return totalDurationMs > 0 ? Math.min(elapsedMs / totalDurationMs, 1) : 0;
128
+ },
129
+ get status() {
130
+ return status;
131
+ },
132
+ get durationMs() {
133
+ return totalDurationMs;
134
+ }
135
+ };
136
+ return timeline;
137
+ };
138
+ //#endregion
139
+ export { createTimeline };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "anim-engine",
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "description": "JavaScript library for animating numbers",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/LukeCarlThompson/anim-engine",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "LICENSE.txt"
12
+ ],
13
+ "type": "module",
14
+ "sideEffects": false,
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "scripts": {
18
+ "dev": "npm run lint && npm run test:watch && vite",
19
+ "build": "npm run lint && npm run test && tsc && vite build",
20
+ "preview": "vite preview",
21
+ "check:types": "tsc --noEmit",
22
+ "lint": "oxlint -c .oxlintrc.json src/",
23
+ "test": "vitest run",
24
+ "test:watch": "vitest",
25
+ "storybook": "storybook dev -p 6006",
26
+ "build-storybook": "npm run lint && npm run test && storybook build",
27
+ "prepublishOnly": "npm run build",
28
+ "bench": "vitest bench src/benchmarks/ --run",
29
+ "bench:baseline": "vitest bench src/benchmarks/ --run --outputJson baselines/benchmark.json",
30
+ "format": "oxfmt src/",
31
+ "format:check": "oxfmt --check src/",
32
+ "fmt": "oxfmt src/"
33
+ },
34
+ "devDependencies": {
35
+ "@storybook/addon-docs": "10.4.6",
36
+ "@storybook/html": "10.4.6",
37
+ "@storybook/html-vite": "10.4.6",
38
+ "gsap": "3.15.0",
39
+ "oxfmt": "0.57.0",
40
+ "oxlint": "1.72.0",
41
+ "storybook": "10.4.6",
42
+ "typescript": "6.0.3",
43
+ "vite": "8.1.3",
44
+ "vite-plugin-dts": "5.0.3",
45
+ "vitest": "4.1.9"
46
+ }
47
+ }