@xtia/timeline 1.0.6 → 1.0.8

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.
@@ -128,9 +128,9 @@ export declare class Timeline {
128
128
  * @deprecated Use timeline.position += n
129
129
  */
130
130
  step(delta: number): void;
131
- tween<T extends Tweenable>(start: number | TimelinePoint, duration: number, apply: (v: Widen<T>) => void, from: T, to: T, easer?: Easer): ChainingInterface;
131
+ tween<T extends Tweenable>(start: number | TimelinePoint, duration: number, apply: (v: Widen<T>) => void, from: T, to: T, easer?: Easer | keyof typeof easers): ChainingInterface;
132
132
  tween<T extends Tweenable>(start: number | TimelinePoint, end: TimelinePoint, // ease migration for tl.tween(0, tl.end, ...)
133
- apply: (v: Widen<T>) => void, from: T, to: T, easer?: Easer): ChainingInterface;
133
+ apply: (v: Widen<T>) => void, from: T, to: T, easer?: Easer | keyof typeof easers): ChainingInterface;
134
134
  at(position: number | TimelinePoint, action?: () => void, reverse?: boolean | (() => void)): ChainingInterface;
135
135
  private createChainingInterface;
136
136
  /**
@@ -139,7 +139,7 @@ export declare class Timeline {
139
139
  get position(): number;
140
140
  }
141
141
  export interface ChainingInterface {
142
- thenTween(duration: number, apply: (v: number) => void, from?: number, to?: number, easer?: Easer): ChainingInterface;
142
+ thenTween<T extends Tweenable>(duration: number, apply: (v: Widen<T>) => void, from: T, to: T, easer: Easer): ChainingInterface;
143
143
  then(action: () => void): ChainingInterface;
144
144
  thenWait(duration: number): ChainingInterface;
145
145
  readonly end: TimelinePoint;
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Timeline = void 0;
4
- exports.animate = animate;
5
- const emitters_1 = require("./emitters");
6
- const utils_1 = require("./utils");
1
+ import { TimelinePoint } from "./point";
2
+ import { TimelineRange } from "./range";
3
+ import { clamp } from "./utils";
7
4
  const default_fps = 60;
8
5
  const EndAction = {
9
6
  pause: 0,
@@ -16,10 +13,10 @@ const EndAction = {
16
13
  * @param duration
17
14
  * @returns Object representing a range on a single-use, autoplaying Timeline
18
15
  */
19
- function animate(duration) {
16
+ export function animate(duration) {
20
17
  return new Timeline(true).range(0, duration);
21
18
  }
22
- class Timeline {
19
+ export class Timeline {
23
20
  get currentTime() { return this._currentTime; }
24
21
  set currentTime(v) {
25
22
  this.seek(v);
@@ -88,7 +85,7 @@ class Timeline {
88
85
  handlers,
89
86
  position,
90
87
  };
91
- return (0, emitters_1.createEmitter)(handler => {
88
+ const addHandler = (handler) => {
92
89
  if (this.seeking)
93
90
  throw new Error("Can't add a listener while seeking");
94
91
  // we're adding and removing points and ranges to the internal registry according to whether any subscriptions are active, to allow obsolete points and ranges to be garbage-collected
@@ -107,17 +104,8 @@ class Timeline {
107
104
  this.points.splice(idx, 1);
108
105
  }
109
106
  };
110
- }, {
111
- delta: t => this.point(position + t),
112
- range: duration => this.range(position, duration),
113
- to: target => {
114
- const targetPosition = typeof target == "number"
115
- ? target
116
- : target.position;
117
- return this.range(position, targetPosition - position);
118
- },
119
- position,
120
- });
107
+ };
108
+ return new TimelinePoint(addHandler, this, position);
121
109
  }
122
110
  range(start = 0, optionalDuration) {
123
111
  const startPoint = typeof start == "number"
@@ -153,58 +141,7 @@ class Timeline {
153
141
  }
154
142
  };
155
143
  };
156
- return (0, emitters_1.createProgressEmitter)(addHandler, {
157
- duration,
158
- start: this.point(startPosition),
159
- end: this.point(startPosition + duration),
160
- bisect: (position = duration / 2) => {
161
- return [
162
- this.range(startPosition, position),
163
- this.range(startPosition + position, duration - position),
164
- ];
165
- },
166
- spread: (count) => {
167
- const delta = duration / (count + 1);
168
- return [
169
- ...Array(count).fill(0).map((_, idx) => this.point(idx * delta + startPosition + delta))
170
- ];
171
- },
172
- play: (easer) => {
173
- this.pause();
174
- this.currentTime = startPosition;
175
- return this.seek(startPosition + duration, duration, easer);
176
- },
177
- grow: (delta, anchor = 0) => {
178
- const clampedAnchor = (0, utils_1.clamp)(anchor, 0, 1);
179
- const leftDelta = -delta * (1 - clampedAnchor);
180
- const rightDelta = delta * clampedAnchor;
181
- const newStart = startPosition + leftDelta;
182
- const newEnd = endPosition + rightDelta;
183
- if (newEnd < newStart) {
184
- const mid = (newStart + newEnd) / 2;
185
- return this.range(mid, 0);
186
- }
187
- return this.range(newStart, newEnd - newStart);
188
- },
189
- scale: (factor, anchor = 0.5) => {
190
- if (factor <= 0) {
191
- throw new RangeError('scale factor must be > 0');
192
- }
193
- const clampedAnchor = (0, utils_1.clamp)(anchor, 0, 1);
194
- const oldLen = endPosition - startPosition;
195
- const pivot = startPosition + oldLen * clampedAnchor;
196
- const newStart = pivot - (pivot - startPosition) * factor;
197
- const newEnd = pivot + (endPosition - pivot) * factor;
198
- if (newEnd < newStart) {
199
- const mid = (newStart + newEnd) / 2;
200
- return this.range(mid, 0);
201
- }
202
- return this.range(newStart, newEnd - newStart);
203
- },
204
- contains: point => {
205
- return point.position >= startPosition && point.position < endPosition;
206
- }
207
- });
144
+ return new TimelineRange(addHandler, this, startPosition, duration);
208
145
  }
209
146
  getWrappedPosition(n) {
210
147
  if (this.endAction.type !== EndAction.wrap)
@@ -298,7 +235,7 @@ class Timeline {
298
235
  // filter ranges that overlap seeked range
299
236
  if (Math.min(position, end) <= Math.max(to, fromTime)
300
237
  && Math.min(to, fromTime) <= Math.max(position, end)) {
301
- let progress = (0, utils_1.clamp)((to - range.position) / range.duration, 0, 1);
238
+ let progress = clamp((to - range.position) / range.duration, 0, 1);
302
239
  range.handlers.slice().forEach(h => h(progress));
303
240
  }
304
241
  });
@@ -385,7 +322,7 @@ class Timeline {
385
322
  }
386
323
  createChainingInterface(position) {
387
324
  return {
388
- thenTween: (duration, apply, from = 0, to = 1, easer) => {
325
+ thenTween: (duration, apply, from, to, easer) => {
389
326
  return this.tween(position, duration, apply, from, to, easer);
390
327
  },
391
328
  then: (action) => this.at(position, action),
@@ -403,7 +340,6 @@ class Timeline {
403
340
  return this._currentTime;
404
341
  }
405
342
  }
406
- exports.Timeline = Timeline;
407
343
  const sortEvents = (a, b) => {
408
344
  return a.position - b.position;
409
345
  };
@@ -4,5 +4,8 @@ export type Tweenable = number | number[] | string | Blendable;
4
4
  export interface Blendable {
5
5
  blend(target: this, progress: number): this;
6
6
  }
7
+ export interface BlendableWith<T, R> {
8
+ blend(target: R, progress: number): T;
9
+ }
7
10
  /** @internal */
8
11
  export declare function tweenValue<T extends Tweenable>(from: T, to: T, progress: number): T;
package/internal/tween.js CHANGED
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tweenValue = tweenValue;
4
- const utils_1 = require("./utils");
1
+ import { clamp } from "./utils";
5
2
  /** @internal */
6
- function tweenValue(from, to, progress) {
3
+ export function tweenValue(from, to, progress) {
7
4
  if (Array.isArray(from)) {
8
5
  const toArr = to;
9
6
  if (from.length != toArr.length)
@@ -89,7 +86,7 @@ function parseColour(code) {
89
86
  function blendColours(from, to, bias) {
90
87
  const fromColour = parseColour(from);
91
88
  const toColour = parseColour(to);
92
- const blended = fromColour.map((val, i) => (0, utils_1.clamp)(blendNumbers(val, toColour[i], bias), 0, 255));
89
+ const blended = fromColour.map((val, i) => clamp(blendNumbers(val, toColour[i], bias), 0, 255));
93
90
  return ("#" + blended.map(n => Math.round(n).toString(16).padStart(2, "0")).join("")).replace(/ff$/, "");
94
91
  }
95
92
  const tweenableTokenRegex = /(#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b|[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)/g;
@@ -2,5 +2,3 @@
2
2
  export declare const clamp: (value: number, min: number, max: number) => number;
3
3
  /** @internal */
4
4
  export type Widen<T> = T extends number ? number : T extends string ? string : T;
5
- export type OptionalIfKeyIn<T, U> = Omit<T, keyof U> & Partial<Pick<T, Extract<keyof T, keyof U>>>;
6
- export declare function prototypify<Prototype extends object, Members extends object>(proto: Prototype, members: Members): Prototype & Members;
package/internal/utils.js CHANGED
@@ -1,14 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.clamp = void 0;
4
- exports.prototypify = prototypify;
5
1
  /** @internal */
6
- const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
7
- exports.clamp = clamp;
8
- function prototypify(proto, members) {
9
- const propertyDescriptor = Object.fromEntries(Object.entries(members).map(([key, value]) => [
10
- key,
11
- { value }
12
- ]));
13
- return Object.create(proto, propertyDescriptor);
14
- }
2
+ export const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtia/timeline",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "repository": {
5
5
  "url": "https://github.com/tiadrop/timeline",
6
6
  "type": "github"