anim-engine 0.4.0 → 0.5.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.
package/dist/index.d.ts CHANGED
@@ -7,4 +7,4 @@ export { createSpring } from './spring';
7
7
  export { lerpRgba, hexToRgba } from './lerp-rgba';
8
8
  export { getTicker } from './ticker';
9
9
  export { cubicBezier } from './domain';
10
- export type { EaseName, Interpolation, AnimationStatus, InterpolationStatus, DynamicValue, Animation, Keyframe, AnimationOptions, KeyframeAnimationOptions, TimelineLayer, Timeline, Ticker, LerpOptions, SmoothDampOptions, SpringOptions, RgbaTuple, } from './domain';
10
+ export type { EaseName, Interpolation, AnimationStatus, InterpolationStatus, DynamicValue, Animation, Keyframe, AnimationOptions, KeyframeAnimationOptions, TimelineLayer, Timeline, Ticker, LerpOptions, SmoothDampOptions, SpringOptions, RgbaTuple, ExternalTicker, } from './domain';
@@ -1,2 +1,14 @@
1
1
  import { Interpolation, LerpOptions } from '../domain';
2
- export declare const createLerp: (options: LerpOptions) => Interpolation;
2
+ /**
3
+ * Creates a linear interpolation that smoothly moves a value toward a
4
+ * target over a specified time constant. Uses exponential decay for a
5
+ * smooth, asymptotic approach.
6
+ *
7
+ * The interpolation automatically starts and runs until it reaches the
8
+ * target within the configured precision. The target is re-evaluated
9
+ * every frame, allowing it to change dynamically.
10
+ *
11
+ * @param options - Configuration options for the interpolation.
12
+ * @returns An {@link Interpolation} instance for controlling the lerp.
13
+ */
14
+ export declare const createLerp: ({ precision, onUpdate, onEnded, to, smoothTimeMs: rawSmoothTimeMs, ticker, }: LerpOptions) => Interpolation;
@@ -2,21 +2,29 @@ import { resolveValue } from "../domain/resolve-value.js";
2
2
  import { getTicker } from "../ticker/get-ticker.js";
3
3
  import { lerpStep } from "./step.js";
4
4
  //#region src/lerp/create-lerp.ts
5
- var createLerp = (options) => {
6
- const precision = options.precision ?? .01;
7
- const onUpdate = options.onUpdate;
8
- const onEnded = options.onEnded;
5
+ /**
6
+ * Creates a linear interpolation that smoothly moves a value toward a
7
+ * target over a specified time constant. Uses exponential decay for a
8
+ * smooth, asymptotic approach.
9
+ *
10
+ * The interpolation automatically starts and runs until it reaches the
11
+ * target within the configured precision. The target is re-evaluated
12
+ * every frame, allowing it to change dynamically.
13
+ *
14
+ * @param options - Configuration options for the interpolation.
15
+ * @returns An {@link Interpolation} instance for controlling the lerp.
16
+ */
17
+ var createLerp = ({ precision = .01, onUpdate, onEnded, to, smoothTimeMs: rawSmoothTimeMs, ticker = getTicker() }) => {
9
18
  const state = { current: 0 };
10
19
  let previousValue = 0;
11
20
  let currentVelocity = 0;
12
21
  let active = true;
13
- const ticker = getTicker();
14
- state.current = options.to();
22
+ state.current = to();
15
23
  previousValue = state.current;
16
24
  const update = (deltaMs) => {
17
25
  if (!active) return;
18
- const target = options.to();
19
- const smoothTimeMs = resolveValue(options.smoothTimeMs);
26
+ const target = to();
27
+ const smoothTimeMs = resolveValue(rawSmoothTimeMs);
20
28
  lerpStep(state, target, smoothTimeMs, deltaMs);
21
29
  currentVelocity = (state.current - previousValue) / (deltaMs / 1e3);
22
30
  previousValue = state.current;
@@ -28,7 +36,7 @@ var createLerp = (options) => {
28
36
  }
29
37
  };
30
38
  ticker.add(update);
31
- const start = () => {
39
+ const resume = () => {
32
40
  if (active) return;
33
41
  active = true;
34
42
  ticker.add(update);
@@ -37,20 +45,15 @@ var createLerp = (options) => {
37
45
  active = false;
38
46
  ticker.remove(update);
39
47
  };
40
- const kill = () => {
41
- active = false;
42
- ticker.remove(update);
43
- };
44
48
  return {
45
- start,
49
+ resume,
46
50
  stop,
47
- kill,
48
- setCurrentValue: (value) => {
51
+ setValue: (value) => {
49
52
  state.current = value;
50
53
  previousValue = value;
51
54
  currentVelocity = 0;
52
55
  },
53
- get currentValue() {
56
+ get value() {
54
57
  return state.current;
55
58
  },
56
59
  get velocity() {
@@ -1,2 +1,16 @@
1
1
  import { Interpolation, SmoothDampOptions } from '../domain';
2
- export declare const createSmoothDamp: (options: SmoothDampOptions) => Interpolation;
2
+ /**
3
+ * Creates a smooth damped interpolation that progressively moves a value
4
+ * toward a target with velocity that decreases as it approaches, producing
5
+ * a natural deceleration effect.
6
+ *
7
+ * Unlike a simple lerp, smooth damp respects an optional max speed and
8
+ * maintains velocity continuity, making it suitable for camera-relative
9
+ * movement, UI animations, and game object tracking.
10
+ *
11
+ * The target is re-evaluated every frame, allowing it to change dynamically.
12
+ *
13
+ * @param options - Configuration options for the smooth damp interpolation.
14
+ * @returns An {@link Interpolation} instance for controlling the smooth damp.
15
+ */
16
+ export declare const createSmoothDamp: ({ to, smoothTimeMs: rawSmoothTimeMs, maxSpeed: rawMaxSpeed, precision, onUpdate, onEnded, ticker, }: SmoothDampOptions) => Interpolation;
@@ -2,19 +2,42 @@ import { resolveValue } from "../domain/resolve-value.js";
2
2
  import { getTicker } from "../ticker/get-ticker.js";
3
3
  import { smoothDampStep } from "./step.js";
4
4
  //#region src/smooth-damp/create-smooth-damp.ts
5
- var createSmoothDamp = (options) => {
6
- const precision = options.precision ?? .01;
7
- const onUpdate = options.onUpdate;
8
- const onEnded = options.onEnded;
5
+ /**
6
+ * Creates a smooth damped interpolation that progressively moves a value
7
+ * toward a target with velocity that decreases as it approaches, producing
8
+ * a natural deceleration effect.
9
+ *
10
+ * Unlike a simple lerp, smooth damp respects an optional max speed and
11
+ * maintains velocity continuity, making it suitable for camera-relative
12
+ * movement, UI animations, and game object tracking.
13
+ *
14
+ * The target is re-evaluated every frame, allowing it to change dynamically.
15
+ *
16
+ * @param options - Configuration options for the smooth damp interpolation.
17
+ * @returns An {@link Interpolation} instance for controlling the smooth damp.
18
+ */
19
+ var createSmoothDamp = ({ to, smoothTimeMs: rawSmoothTimeMs, maxSpeed: rawMaxSpeed, precision = .01, onUpdate, onEnded, ticker = getTicker() }) => {
9
20
  const state = {
10
21
  current: 0,
11
22
  velocity: 0
12
23
  };
13
24
  let active = true;
14
- const ticker = getTicker();
15
- state.current = options.to();
25
+ state.current = to();
26
+ const update = (deltaMs) => {
27
+ if (!active) return;
28
+ const target = to();
29
+ const smoothTimeMs = resolveValue(rawSmoothTimeMs);
30
+ const maxSpeed = rawMaxSpeed !== void 0 ? resolveValue(rawMaxSpeed) : Infinity;
31
+ smoothDampStep(state, target, smoothTimeMs, maxSpeed, deltaMs);
32
+ onUpdate?.(state.current, state.velocity);
33
+ if (onEnded && Math.abs(state.current - target) < precision && Math.abs(state.velocity) < precision) {
34
+ state.current = target;
35
+ state.velocity = 0;
36
+ onEnded();
37
+ }
38
+ };
16
39
  ticker.add(update);
17
- const start = () => {
40
+ const resume = () => {
18
41
  if (active) return;
19
42
  active = true;
20
43
  ticker.add(update);
@@ -23,32 +46,14 @@ var createSmoothDamp = (options) => {
23
46
  active = false;
24
47
  ticker.remove(update);
25
48
  };
26
- const kill = () => {
27
- active = false;
28
- ticker.remove(update);
29
- };
30
- function update(deltaMs) {
31
- if (!active) return;
32
- const target = options.to();
33
- const smoothTimeMs = resolveValue(options.smoothTimeMs);
34
- const maxSpeed = options.maxSpeed !== void 0 ? resolveValue(options.maxSpeed) : Infinity;
35
- smoothDampStep(state, target, smoothTimeMs, maxSpeed, deltaMs);
36
- onUpdate?.(state.current, state.velocity);
37
- if (onEnded && Math.abs(state.current - target) < precision && Math.abs(state.velocity) < precision) {
38
- state.current = target;
39
- state.velocity = 0;
40
- onEnded();
41
- }
42
- }
43
49
  return {
44
- start,
50
+ resume,
45
51
  stop,
46
- kill,
47
- setCurrentValue: (value) => {
52
+ setValue: (value) => {
48
53
  state.current = value;
49
54
  state.velocity = 0;
50
55
  },
51
- get currentValue() {
56
+ get value() {
52
57
  return state.current;
53
58
  },
54
59
  get velocity() {
@@ -1,2 +1,13 @@
1
1
  import { Interpolation, SpringOptions } from '../domain';
2
- export declare const createSpring: (options: SpringOptions) => Interpolation;
2
+ /**
3
+ * Creates a spring-based interpolation that simulates mass-spring-damper
4
+ * physics, producing bouncy or elastic movement toward the target value.
5
+ *
6
+ * Uses Verlet integration for stable, energy-conserving simulation.
7
+ * Higher stiffness produces faster, snappier motion; higher damping
8
+ * reduces oscillation. The target is re-evaluated every frame.
9
+ *
10
+ * @param options - Configuration options for the spring interpolation.
11
+ * @returns An {@link Interpolation} instance for controlling the spring.
12
+ */
13
+ export declare const createSpring: ({ precision, onUpdate, onEnded, to: rawTo, stiffness: rawStiffness, damping: rawDamping, mass: rawMass, ticker, }: SpringOptions) => Interpolation;
@@ -2,22 +2,25 @@ import { resolveValue } from "../domain/resolve-value.js";
2
2
  import { getTicker } from "../ticker/get-ticker.js";
3
3
  import { verletStep } from "./verlet.js";
4
4
  //#region src/spring/create-spring.ts
5
- var createSpring = (options) => {
6
- const precision = options.precision ?? .01;
7
- const onUpdate = options.onUpdate;
8
- const onEnded = options.onEnded;
9
- const rawTo = options.to;
10
- const rawStiffness = options.stiffness ?? 180;
11
- const rawDamping = options.damping ?? 12;
12
- const rawMass = options.mass ?? 1;
5
+ /**
6
+ * Creates a spring-based interpolation that simulates mass-spring-damper
7
+ * physics, producing bouncy or elastic movement toward the target value.
8
+ *
9
+ * Uses Verlet integration for stable, energy-conserving simulation.
10
+ * Higher stiffness produces faster, snappier motion; higher damping
11
+ * reduces oscillation. The target is re-evaluated every frame.
12
+ *
13
+ * @param options - Configuration options for the spring interpolation.
14
+ * @returns An {@link Interpolation} instance for controlling the spring.
15
+ */
16
+ var createSpring = ({ precision = .01, onUpdate, onEnded, to: rawTo, stiffness: rawStiffness = 180, damping: rawDamping = 12, mass: rawMass = 1, ticker = getTicker() }) => {
13
17
  const state = {
14
18
  current: rawTo(),
15
19
  velocity: 0
16
20
  };
17
21
  let active = true;
18
- const ticker = getTicker();
19
22
  ticker.add(update);
20
- const start = () => {
23
+ const resume = () => {
21
24
  if (active) return;
22
25
  active = true;
23
26
  ticker.add(update);
@@ -26,10 +29,6 @@ var createSpring = (options) => {
26
29
  active = false;
27
30
  ticker.remove(update);
28
31
  };
29
- const kill = () => {
30
- active = false;
31
- ticker.remove(update);
32
- };
33
32
  function update(deltaMs) {
34
33
  if (!active) return;
35
34
  const target = rawTo();
@@ -45,14 +44,13 @@ var createSpring = (options) => {
45
44
  }
46
45
  }
47
46
  return {
48
- start,
47
+ resume,
49
48
  stop,
50
- kill,
51
- setCurrentValue: (value) => {
49
+ setValue: (value) => {
52
50
  state.current = value;
53
51
  state.velocity = 0;
54
52
  },
55
- get currentValue() {
53
+ get value() {
56
54
  return state.current;
57
55
  },
58
56
  get velocity() {
@@ -1,5 +1,13 @@
1
1
  import { Ticker } from '../domain';
2
- /** Returns the default ticker singleton. Created lazily on first access. */
2
+ /**
3
+ * Returns the application-wide default ticker singleton.
4
+ *
5
+ * The ticker is created lazily on first access and reused for all
6
+ * subsequent calls. It drives all animations and interpolations that
7
+ * don't specify an external ticker.
8
+ *
9
+ * @returns The default {@link Ticker} instance.
10
+ */
3
11
  export declare const getTicker: () => Ticker;
4
12
  /**
5
13
  * Create a ticker that drives active animations.
@@ -1,6 +1,14 @@
1
1
  //#region src/ticker/get-ticker.ts
2
2
  var singleton;
3
- /** Returns the default ticker singleton. Created lazily on first access. */
3
+ /**
4
+ * Returns the application-wide default ticker singleton.
5
+ *
6
+ * The ticker is created lazily on first access and reused for all
7
+ * subsequent calls. It drives all animations and interpolations that
8
+ * don't specify an external ticker.
9
+ *
10
+ * @returns The default {@link Ticker} instance.
11
+ */
4
12
  var getTicker = () => {
5
13
  if (!singleton) singleton = createTicker();
6
14
  return singleton;
@@ -1,6 +1,16 @@
1
- import { TimelineLayer, Timeline } from '../domain';
2
- export declare const createTimeline: (layers: TimelineLayer[], options?: {
3
- onStarted?: () => void;
4
- onProgress?: (progress: number) => void;
5
- onEnded?: () => void;
6
- }) => Timeline;
1
+ import { TimelineLayer, Timeline, TimelineCallbacks, ExternalTicker } from '../domain';
2
+ /**
3
+ * Creates a timeline animation that sequences multiple keyframe animation
4
+ * layers, each starting at a specified time or gap after the previous layer.
5
+ *
6
+ * The timeline aggregates all layers into a single controllable animation
7
+ * with its own play/pause/resume/stop semantics and returns the combined
8
+ * values and velocities of all layers on each update.
9
+ *
10
+ * @param layers - An array of {@link TimelineLayer} configs describing each
11
+ * animation in the sequence and when it should play.
12
+ * @param callbacks - Optional callback hooks and configuration.
13
+ * @param ticker - An optional external ticker. Defaults to the global ticker.
14
+ * @returns A {@link Timeline} instance for controlling the sequenced animation.
15
+ */
16
+ export declare const createTimeline: (layers: TimelineLayer[], { onStarted, onUpdate, onEnded, onProgress }?: TimelineCallbacks, ticker?: ExternalTicker) => Timeline;
@@ -38,17 +38,42 @@ var buildFromConfigs = (rawLayers) => {
38
38
  totalDurationMs: Math.max(0, ...activeLayers.map((l) => l.endAt))
39
39
  };
40
40
  };
41
- var createTimeline = (layers, options) => {
42
- const { onStarted, onEnded, onProgress = noOp } = options ?? {};
41
+ /**
42
+ * Creates a timeline animation that sequences multiple keyframe animation
43
+ * layers, each starting at a specified time or gap after the previous layer.
44
+ *
45
+ * The timeline aggregates all layers into a single controllable animation
46
+ * with its own play/pause/resume/stop semantics and returns the combined
47
+ * values and velocities of all layers on each update.
48
+ *
49
+ * @param layers - An array of {@link TimelineLayer} configs describing each
50
+ * animation in the sequence and when it should play.
51
+ * @param callbacks - Optional callback hooks and configuration.
52
+ * @param ticker - An optional external ticker. Defaults to the global ticker.
53
+ * @returns A {@link Timeline} instance for controlling the sequenced animation.
54
+ */
55
+ var createTimeline = (layers, { onStarted, onUpdate, onEnded, onProgress = noOp } = {}, ticker = getTicker()) => {
43
56
  const rawLayers = layers;
44
57
  let state = buildFromConfigs(rawLayers);
45
58
  let activeLayers = state.activeLayers;
46
59
  let totalDurationMs = state.totalDurationMs;
60
+ let valuesCache = [];
61
+ let velocitiesCache = [];
62
+ let valuesDirty = true;
63
+ const syncValues = () => {
64
+ if (!valuesDirty) return;
65
+ valuesDirty = false;
66
+ valuesCache.length = activeLayers.length;
67
+ velocitiesCache.length = activeLayers.length;
68
+ for (let i = 0; i < activeLayers.length; i++) {
69
+ valuesCache[i] = activeLayers[i].runner.value;
70
+ velocitiesCache[i] = activeLayers[i].runner.velocity;
71
+ }
72
+ };
47
73
  let timelineStatus = "stopped";
48
74
  let elapsedMs = 0;
49
75
  let resolvePromise;
50
76
  let remainingLayers = activeLayers.length;
51
- const ticker = getTicker();
52
77
  const finish = () => {
53
78
  timelineStatus = "stopped";
54
79
  ticker.remove(update);
@@ -71,15 +96,22 @@ var createTimeline = (layers, options) => {
71
96
  }
72
97
  }
73
98
  }
99
+ valuesDirty = true;
100
+ if (onUpdate) {
101
+ syncValues();
102
+ onUpdate(valuesCache, velocitiesCache);
103
+ }
74
104
  onProgress(totalDurationMs > 0 ? Math.min(elapsedMs / totalDurationMs, 1) : 1);
75
105
  if (remainingLayers <= 0) finish();
76
106
  };
77
107
  const play = () => {
78
- if (timelineStatus === "dead") throw new Error("Cannot play a dead timeline");
79
108
  state = buildFromConfigs(rawLayers);
80
109
  activeLayers = state.activeLayers;
81
110
  totalDurationMs = state.totalDurationMs;
82
111
  remainingLayers = activeLayers.length;
112
+ valuesCache = [];
113
+ velocitiesCache = [];
114
+ syncValues();
83
115
  elapsedMs = 0;
84
116
  const promise = new Promise((resolve) => {
85
117
  resolvePromise = resolve;
@@ -111,17 +143,13 @@ var createTimeline = (layers, options) => {
111
143
  layer.runner.evaluate(1);
112
144
  layer.runner.onEnded?.();
113
145
  }
146
+ syncValues();
114
147
  timelineStatus = "stopped";
115
148
  ticker.remove(update);
116
149
  onEnded?.();
117
150
  resolvePromise?.();
118
151
  resolvePromise = void 0;
119
152
  };
120
- const kill = () => {
121
- timelineStatus = "dead";
122
- ticker.remove(update);
123
- resolvePromise = void 0;
124
- };
125
153
  const setProgress = (value) => {
126
154
  if (timelineStatus === "playing") pause();
127
155
  elapsedMs = Math.max(0, Math.min(1, value)) * totalDurationMs;
@@ -136,6 +164,7 @@ var createTimeline = (layers, options) => {
136
164
  layer.runner.evaluate(localProgress);
137
165
  layer.ended = localProgress >= 1;
138
166
  }
167
+ syncValues();
139
168
  remainingLayers = activeLayers.filter((l) => !l.ended).length;
140
169
  };
141
170
  return {
@@ -144,11 +173,18 @@ var createTimeline = (layers, options) => {
144
173
  resume,
145
174
  stop,
146
175
  skipToEnd,
147
- kill,
148
176
  setProgress,
149
177
  get progress() {
150
178
  return totalDurationMs > 0 ? Math.min(elapsedMs / totalDurationMs, 1) : 1;
151
179
  },
180
+ get values() {
181
+ syncValues();
182
+ return valuesCache;
183
+ },
184
+ get velocities() {
185
+ syncValues();
186
+ return velocitiesCache;
187
+ },
152
188
  get status() {
153
189
  return timelineStatus;
154
190
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anim-engine",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "private": false,
5
5
  "description": "JavaScript library for animating numbers",
6
6
  "license": "MIT",
@@ -11,7 +11,8 @@
11
11
  "files": [
12
12
  "dist",
13
13
  "README.md",
14
- "LICENSE.txt"
14
+ "LICENSE.txt",
15
+ "SKILL.md"
15
16
  ],
16
17
  "type": "module",
17
18
  "sideEffects": false,