@xtia/timeline 1.1.13 → 1.1.15
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/README.md +2 -2
- package/internal/emitters.d.ts +2 -0
- package/internal/emitters.js +24 -0
- package/internal/path.d.ts +36 -0
- package/internal/path.js +75 -0
- package/internal/tween.js +13 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,8 +107,8 @@ const filenameEmitter = range
|
|
|
107
107
|
.dedupe()
|
|
108
108
|
.map(n => `animation-frame-${n}.png`);
|
|
109
109
|
|
|
110
|
-
// filenameEmitter will emit filenames as the Timeline passes through
|
|
111
|
-
// it can be listened directly or further transformed
|
|
110
|
+
// filenameEmitter will emit filenames as the Timeline passes through
|
|
111
|
+
// 'range'. it can be listened directly or further transformed
|
|
112
112
|
const urlEmitter = filenameEmitter
|
|
113
113
|
.map(filename => `http://www.example.com/${filename}`);
|
|
114
114
|
|
package/internal/emitters.d.ts
CHANGED
|
@@ -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
|
package/internal/emitters.js
CHANGED
|
@@ -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 {
|
|
@@ -236,6 +237,29 @@ export class RangeProgression extends Emitter {
|
|
|
236
237
|
}
|
|
237
238
|
return this._dedupe;
|
|
238
239
|
}
|
|
240
|
+
path(segments) {
|
|
241
|
+
const pathEvaluator = createPathEmitter(segments);
|
|
242
|
+
let parentUnsubscribe = null;
|
|
243
|
+
let pathUnsubscribe = null;
|
|
244
|
+
const { listen, emit } = createListenable(() => {
|
|
245
|
+
// onAddFirst - when first listener subscribes
|
|
246
|
+
pathUnsubscribe = pathEvaluator.listen(emit);
|
|
247
|
+
parentUnsubscribe = this.listen((timeValue) => {
|
|
248
|
+
pathEvaluator.seek(timeValue);
|
|
249
|
+
});
|
|
250
|
+
}, () => {
|
|
251
|
+
// onRemoveLast - when last listener unsubscribes
|
|
252
|
+
if (pathUnsubscribe) {
|
|
253
|
+
pathUnsubscribe();
|
|
254
|
+
pathUnsubscribe = null;
|
|
255
|
+
}
|
|
256
|
+
if (parentUnsubscribe) {
|
|
257
|
+
parentUnsubscribe();
|
|
258
|
+
parentUnsubscribe = null;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
return new Emitter(listen);
|
|
262
|
+
}
|
|
239
263
|
/**
|
|
240
264
|
* Creates a chainable progress emitter that offsets its parent's values by the given delta, wrapping at 1
|
|
241
265
|
*
|
|
@@ -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 {};
|
package/internal/path.js
ADDED
|
@@ -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
|
@@ -134,7 +134,19 @@ function parseColour(code) {
|
|
|
134
134
|
}
|
|
135
135
|
function blendColours(from, to, bias) {
|
|
136
136
|
const blended = from.map((val, i) => clamp(blendNumbers(val, to[i], bias), 0, 255));
|
|
137
|
-
|
|
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
|
+
}
|
|
138
150
|
}
|
|
139
151
|
const tweenableTokenRegex = /(#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b|[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)/g;
|
|
140
152
|
function tokenise(s) {
|