@xtia/timeline 1.1.14 → 1.1.16

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.
@@ -1,4 +1,5 @@
1
1
  import { Easer, easers } from "./easing";
2
+ import { Path, XY } from "./path";
2
3
  import { BlendableWith, Tweenable } from "./tween";
3
4
  type Handler<T> = (value: T) => void;
4
5
  export type ListenFunc<T> = (handler: Handler<T>) => UnsubscribeFunc;
@@ -187,6 +188,7 @@ export declare class RangeProgression extends Emitter<number> {
187
188
  * @returns Listenable: emits non-repeating values
188
189
  */
189
190
  dedupe(): RangeProgression;
191
+ path(segments: Path): Emitter<XY>;
190
192
  private _dedupe?;
191
193
  /**
192
194
  * Creates a chainable progress emitter that offsets its parent's values by the given delta, wrapping at 1
@@ -1,4 +1,5 @@
1
1
  import { easers } from "./easing";
2
+ import { createPathEmitter } from "./path";
2
3
  import { createTween } from "./tween";
3
4
  import { clamp } from "./utils";
4
5
  export class Emitter {
@@ -138,10 +139,14 @@ export class RangeProgression extends Emitter {
138
139
  * @returns Listenable: emits the sampled values
139
140
  */
140
141
  sample(source) {
142
+ if (source.length === 0) {
143
+ throw new Error("Sample source is empty");
144
+ }
145
+ const sourceArray = Array.from(source);
141
146
  const listen = this.transform((value, emit) => {
142
147
  const clampedProgress = clamp(value);
143
- const index = Math.floor(clampedProgress * (source.length - 1));
144
- emit(source[index]);
148
+ const index = Math.floor(clampedProgress * (sourceArray.length - 1));
149
+ emit(sourceArray[index]);
145
150
  });
146
151
  return new Emitter(listen);
147
152
  }
@@ -236,6 +241,29 @@ export class RangeProgression extends Emitter {
236
241
  }
237
242
  return this._dedupe;
238
243
  }
244
+ path(segments) {
245
+ const pathEvaluator = createPathEmitter(segments);
246
+ let parentUnsubscribe = null;
247
+ let pathUnsubscribe = null;
248
+ const { listen, emit } = createListenable(() => {
249
+ // onAddFirst - when first listener subscribes
250
+ pathUnsubscribe = pathEvaluator.listen(emit);
251
+ parentUnsubscribe = this.listen((timeValue) => {
252
+ pathEvaluator.seek(timeValue);
253
+ });
254
+ }, () => {
255
+ // onRemoveLast - when last listener unsubscribes
256
+ if (pathUnsubscribe) {
257
+ pathUnsubscribe();
258
+ pathUnsubscribe = null;
259
+ }
260
+ if (parentUnsubscribe) {
261
+ parentUnsubscribe();
262
+ parentUnsubscribe = null;
263
+ }
264
+ });
265
+ return new Emitter(listen);
266
+ }
239
267
  /**
240
268
  * Creates a chainable progress emitter that offsets its parent's values by the given delta, wrapping at 1
241
269
  *
@@ -0,0 +1,36 @@
1
+ import { Easer, easers } from "./easing";
2
+ import { ListenFunc } from "./emitters";
3
+ export type XY = [number, number];
4
+ type SegmentEvaluator = (t: number) => XY;
5
+ type LineSegment = {
6
+ type: "line";
7
+ from?: XY;
8
+ to: XY;
9
+ speed?: number;
10
+ ease?: Easer | keyof typeof easers;
11
+ };
12
+ type CurveSegment = {
13
+ type: "curve";
14
+ from?: XY;
15
+ to: XY;
16
+ control1: XY;
17
+ control2: XY;
18
+ speed?: number;
19
+ ease?: Easer | keyof typeof easers;
20
+ };
21
+ type StaticSegment = LineSegment | CurveSegment;
22
+ type Segment = StaticSegment | CustomSegment;
23
+ type CustomSegment = {
24
+ get: SegmentEvaluator;
25
+ length?: number;
26
+ };
27
+ type FirstSegment = CustomSegment | (StaticSegment & {
28
+ from: XY;
29
+ });
30
+ export type Path = [FirstSegment | XY, ...(Segment | XY)[]] | XY[];
31
+ type PathEvaluator = {
32
+ listen: ListenFunc<XY>;
33
+ seek: (n: number) => void;
34
+ };
35
+ export declare function createPathEmitter(input: Path): PathEvaluator;
36
+ export {};
@@ -0,0 +1,75 @@
1
+ import { createListenable } from "./emitters";
2
+ import { Timeline } from "./timeline";
3
+ export function createPathEmitter(input) {
4
+ const { listen, emit } = createListenable();
5
+ const tl = new Timeline();
6
+ const firstItem = input[0];
7
+ let getCurrentPosition;
8
+ let items;
9
+ if (Array.isArray(firstItem)) {
10
+ // first is XY - use it as starting position and exclude it from iteration
11
+ items = input.slice(1);
12
+ getCurrentPosition = () => firstItem;
13
+ }
14
+ else {
15
+ items = input;
16
+ getCurrentPosition = () => [0, 0];
17
+ }
18
+ items.forEach(item => {
19
+ const speed = typeof item === 'object' && !Array.isArray(item) && "speed" in item ? item.speed ?? 1 : 1;
20
+ if (Array.isArray(item)) { // XY
21
+ const start = getCurrentPosition();
22
+ const length = distance(start, item);
23
+ tl.end.range(length / speed).tween(start, item).apply(emit);
24
+ getCurrentPosition = () => item;
25
+ }
26
+ else if ("get" in item) { // custom segment
27
+ const length = item.length ?? estimateLength(item.get);
28
+ tl.end.range(length / speed).map(v => item.get(v)).apply(emit);
29
+ getCurrentPosition = () => item.get(1);
30
+ }
31
+ else
32
+ switch (item.type) { // static segment
33
+ case "line": {
34
+ const start = item.from ?? getCurrentPosition();
35
+ const length = distance(start, item.to);
36
+ tl.end.range(length / speed).ease(item.ease).tween(start, item.to).apply(emit);
37
+ getCurrentPosition = () => item.to;
38
+ break;
39
+ }
40
+ case "curve": {
41
+ const start = item.from ?? getCurrentPosition();
42
+ const curve = createCurve(start, item.to, item.control1, item.control2);
43
+ const length = estimateLength(curve);
44
+ tl.end.range(length / speed).ease(item.ease).map(curve).apply(emit);
45
+ getCurrentPosition = () => item.to;
46
+ }
47
+ }
48
+ });
49
+ return { listen, seek: t => tl.seek(t * tl.end.position) };
50
+ }
51
+ function createCurve(start, end, control1, control2) {
52
+ return (t) => {
53
+ const x = (1 - t) ** 3 * start[0] +
54
+ 3 * (1 - t) ** 2 * t * control1[0] +
55
+ 3 * (1 - t) * t ** 2 * control2[0] +
56
+ t ** 3 * end[0];
57
+ const y = (1 - t) ** 3 * start[1] +
58
+ 3 * (1 - t) ** 2 * t * control1[1] +
59
+ 3 * (1 - t) * t ** 2 * control2[1] +
60
+ t ** 3 * end[1];
61
+ return [x, y];
62
+ };
63
+ }
64
+ function estimateLength(curve, samples = 100) {
65
+ let length = 0;
66
+ let prev = curve(0);
67
+ for (let i = 1; i <= samples; i++) {
68
+ const t = i / samples;
69
+ const current = curve(t);
70
+ length += Math.sqrt((current[0] - prev[0]) ** 2 + (current[1] - prev[1]) ** 2);
71
+ prev = current;
72
+ }
73
+ return length;
74
+ }
75
+ const distance = (a, b) => Math.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2);
package/internal/tween.js CHANGED
@@ -132,21 +132,18 @@ function parseColour(code) {
132
132
  rawHex += "ff";
133
133
  return [...rawHex.matchAll(/../g)].map(hex => parseInt(hex[0], 16));
134
134
  }
135
- function blendColours(from, to, bias) {
136
- const blended = from.map((val, i) => clamp(blendNumbers(val, to[i], bias), 0, 255));
137
- if (blended[3] === 255) {
138
- return "#" +
139
- Math.round(blended[0]).toString(16).padStart(2, "0") +
140
- Math.round(blended[1]).toString(16).padStart(2, "0") +
141
- Math.round(blended[2]).toString(16).padStart(2, "0");
142
- }
143
- else {
144
- return "#" +
145
- Math.round(blended[0]).toString(16).padStart(2, "0") +
146
- Math.round(blended[1]).toString(16).padStart(2, "0") +
147
- Math.round(blended[2]).toString(16).padStart(2, "0") +
148
- Math.round(blended[3]).toString(16).padStart(2, "0");
149
- }
135
+ const hexLookup = new Array(256);
136
+ for (let i = 0; i < 256; i++) {
137
+ hexLookup[i] = i.toString(16).padStart(2, "0");
138
+ }
139
+ function blendColours([fromR, fromG, fromB, fromA], [toR, toG, toB, toA], bias) {
140
+ const r = Math.max(0, Math.min(255, Math.round(fromR + bias * (toR - fromR))));
141
+ const g = Math.max(0, Math.min(255, Math.round(fromG + bias * (toG - fromG))));
142
+ const b = Math.max(0, Math.min(255, Math.round(fromB + bias * (toB - fromB))));
143
+ const a = Math.max(0, Math.min(255, Math.round(fromA + bias * (toA - fromA))));
144
+ return a === 255
145
+ ? "#" + hexLookup[r] + hexLookup[g] + hexLookup[b]
146
+ : "#" + hexLookup[r] + hexLookup[g] + hexLookup[b] + hexLookup[a];
150
147
  }
151
148
  const tweenableTokenRegex = /(#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b|[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)/g;
152
149
  function tokenise(s) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtia/timeline",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "repository": {
5
5
  "url": "https://github.com/tiadrop/timeline",
6
6
  "type": "github"