-
Notifications
You must be signed in to change notification settings - Fork 221
/
easingAnimator.ts
300 lines (282 loc) · 8.6 KB
/
easingAnimator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright 2014-present Palantir Technologies
* @license MIT
*/
import * as d3 from "d3";
import * as d3Ease from "d3-ease";
import { Animator } from "./animator";
import { AttributeToAppliedProjector, SimpleSelection } from "../core/interfaces";
import { coerceExternalD3 } from "../utils/coerceD3";
export type EaseFn = (normalizedTime: number) => number;
const EASE_NAME_MAPPING: { [name: string]: EaseFn } = {
"linear": d3Ease.easeLinear,
"quad": d3Ease.easeQuad,
"quadIn": d3Ease.easeQuadIn,
"quadOut": d3Ease.easeQuadOut,
"quadInOut": d3Ease.easeQuadInOut,
"cubic": d3Ease.easeCubic,
"cubicIn": d3Ease.easeCubicIn,
"cubicOut": d3Ease.easeCubicOut,
"cubicInOut": d3Ease.easeCubicInOut,
"poly": d3Ease.easePoly,
"polyIn": d3Ease.easePolyIn,
"polyOut": d3Ease.easePolyOut,
"polyInOut": d3Ease.easePolyInOut,
"sin": d3Ease.easeSin,
"sinIn": d3Ease.easeSinIn,
"sinOut": d3Ease.easeSinOut,
"sinInOut": d3Ease.easeSinInOut,
"exp": d3Ease.easeExp,
"expIn": d3Ease.easeExpIn,
"expOut": d3Ease.easeExpOut,
"expInOut": d3Ease.easeExpInOut,
"circle": d3Ease.easeCircle,
"circleIn": d3Ease.easeCircleIn,
"circleOut": d3Ease.easeCircleOut,
"circleInOut": d3Ease.easeCircleInOut,
"bounce": d3Ease.easeBounce,
"bounceIn": d3Ease.easeBounceIn,
"bounceOut": d3Ease.easeBounceOut,
"bounceInOut": d3Ease.easeBounceInOut,
"back": d3Ease.easeBack,
"backIn": d3Ease.easeBackIn,
"backOut": d3Ease.easeBackOut,
"backInOut": d3Ease.easeBackInOut,
"elastic": d3Ease.easeElastic,
"elasticIn": d3Ease.easeElasticIn,
"elasticOut": d3Ease.easeElasticOut,
"elasticInOut": d3Ease.easeElasticInOut,
};
/**
* Known ease types that animator's .ease() methods understand
*/
export type EaseName =
"linear" |
"quad" |
"quadIn" |
"quadOut" |
"quadInOut" |
"cubic" |
"cubicIn" |
"cubicOut" |
"cubicInOut" |
"poly" |
"polyIn" |
"polyOut" |
"polyInOut" |
"sin" |
"sinIn" |
"sinOut" |
"sinInOut" |
"exp" |
"expIn" |
"expOut" |
"expInOut" |
"circle" |
"circleIn" |
"circleOut" |
"circleInOut" |
"bounce" |
"bounceIn" |
"bounceOut" |
"bounceInOut" |
"back" |
"backIn" |
"backOut" |
"backInOut" |
"elastic" |
"elasticIn" |
"elasticOut" |
"elasticInOut";
/**
* An Animator with easing and configurable durations and delays.
*/
export class Easing implements Animator {
/**
* The default starting delay of the animation in milliseconds
*/
private static _DEFAULT_START_DELAY_MILLISECONDS = 0;
/**
* The default duration of one animation step in milliseconds
*/
private static _DEFAULT_STEP_DURATION_MILLISECONDS = 300;
/**
* The default maximum start delay between each step of an animation
*/
private static _DEFAULT_ITERATIVE_DELAY_MILLISECONDS = 15;
/**
* The default maximum total animation duration
*/
private static _DEFAULT_MAX_TOTAL_DURATION_MILLISECONDS = Infinity;
/**
* The default easing of the animation
*/
private static _DEFAULT_EASING_MODE: EaseName = "expOut";
private _startDelay: number;
private _stepDuration: number;
private _stepDelay: number;
private _maxTotalDuration: number;
private _easingMode: EaseName | EaseFn;
/**
* Constructs the default animator
*
* @constructor
*/
constructor() {
this._startDelay = Easing._DEFAULT_START_DELAY_MILLISECONDS;
this._stepDuration = Easing._DEFAULT_STEP_DURATION_MILLISECONDS;
this._stepDelay = Easing._DEFAULT_ITERATIVE_DELAY_MILLISECONDS;
this._maxTotalDuration = Easing._DEFAULT_MAX_TOTAL_DURATION_MILLISECONDS;
this._easingMode = Easing._DEFAULT_EASING_MODE;
}
public totalTime(numberOfSteps: number) {
let adjustedIterativeDelay = this._getAdjustedIterativeDelay(numberOfSteps);
return this.startDelay() + adjustedIterativeDelay * (Math.max(numberOfSteps - 1, 0)) + this.stepDuration();
}
public animate(selection: SimpleSelection<any>, attrToAppliedProjector: AttributeToAppliedProjector): d3.Transition<any, any, any, any> {
selection = coerceExternalD3(selection);
let numberOfSteps = selection.size();
let adjustedIterativeDelay = this._getAdjustedIterativeDelay(numberOfSteps);
return selection.transition()
.ease(this._getEaseFactory())
.duration(this.stepDuration())
.delay((d: any, i: number) => this.startDelay() + adjustedIterativeDelay * i)
.attrs(attrToAppliedProjector);
}
/**
* Gets the start delay of the animation in milliseconds.
*
* @returns {number} The current start delay.
*/
public startDelay(): number;
/**
* Sets the start delay of the animation in milliseconds.
*
* @param {number} startDelay The start delay in milliseconds.
* @returns {Easing} The calling Easing Animator.
*/
public startDelay(startDelay: number): this;
public startDelay(startDelay?: number): any {
if (startDelay == null) {
return this._startDelay;
} else {
this._startDelay = startDelay;
return this;
}
}
/**
* Gets the duration of one animation step in milliseconds.
*
* @returns {number} The current duration.
*/
public stepDuration(): number;
/**
* Sets the duration of one animation step in milliseconds.
*
* @param {number} stepDuration The duration in milliseconds.
* @returns {Easing} The calling Easing Animator.
*/
public stepDuration(stepDuration: number): this;
public stepDuration(stepDuration?: number): any {
if (stepDuration == null) {
return Math.min(this._stepDuration, this._maxTotalDuration);
} else {
this._stepDuration = stepDuration;
return this;
}
}
/**
* Gets the maximum start delay between animation steps in milliseconds.
*
* @returns {number} The current maximum iterative delay.
*/
public stepDelay(): number;
/**
* Sets the maximum start delay between animation steps in milliseconds.
*
* @param {number} stepDelay The maximum iterative delay in milliseconds.
* @returns {Easing} The calling Easing Animator.
*/
public stepDelay(stepDelay: number): this;
public stepDelay(stepDelay?: number): any {
if (stepDelay == null) {
return this._stepDelay;
} else {
this._stepDelay = stepDelay;
return this;
}
}
/**
* Gets the maximum total animation duration constraint in milliseconds.
*
* If the animation time would exceed the specified time, the duration of each step
* and the delay between each step will be reduced until the animation fits within
* the specified time.
*
* @returns {number} The current maximum total animation duration.
*/
public maxTotalDuration(): number;
/**
* Sets the maximum total animation duration constraint in miliseconds.
*
* If the animation time would exceed the specified time, the duration of each step
* and the delay between each step will be reduced until the animation fits within
* the specified time.
*
* @param {number} maxTotalDuration The maximum total animation duration in milliseconds.
* @returns {Easing} The calling Easing Animator.
*/
public maxTotalDuration(maxTotalDuration: number): this;
public maxTotalDuration(maxTotalDuration?: number): any {
if (maxTotalDuration == null) {
return this._maxTotalDuration;
} else {
this._maxTotalDuration = maxTotalDuration;
return this;
}
}
/**
* Gets the current easing mode of the animation.
*
* @returns {string} the current easing mode.
*/
public easingMode(): EaseName | EaseFn;
/**
* Sets the easing mode of the animation.
*
* @param {string} easingMode The desired easing mode.
* @returns {Easing} The calling Easing Animator.
*/
public easingMode(easingMode: EaseName | EaseFn): this;
public easingMode(easingMode?: EaseName | EaseFn): any {
if (easingMode == null) {
return this._easingMode;
} else {
this._easingMode = easingMode;
return this;
}
}
protected _getEaseFactory() {
const ease = this.easingMode();
if(typeof ease === "string") {
const maybeEaseFunction = EASE_NAME_MAPPING[ease];
if (maybeEaseFunction == null) {
// oops; name is wrong - default to linear instead
return EASE_NAME_MAPPING["linear"];
} else {
return maybeEaseFunction;
}
} else {
return ease;
}
}
/**
* Adjust the iterative delay, such that it takes into account the maxTotalDuration constraint
*/
private _getAdjustedIterativeDelay(numberOfSteps: number) {
let stepStartTimeInterval = this.maxTotalDuration() - this.stepDuration();
stepStartTimeInterval = Math.max(stepStartTimeInterval, 0);
let maxPossibleIterativeDelay = stepStartTimeInterval / Math.max(numberOfSteps - 1, 1);
return Math.min(this.stepDelay(), maxPossibleIterativeDelay);
}
}