forked from mistic100/Photo-Sphere-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.js
More file actions
183 lines (159 loc) · 4.68 KB
/
Copy pathAnimation.js
File metadata and controls
183 lines (159 loc) · 4.68 KB
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
import { EASINGS } from './data/constants';
import { each } from './utils';
/**
* @callback OnTick
* @summary Function called for each animation frame with computed properties
* @memberOf PSV.Animation
* @param {Object.<string, number>} properties - current values
* @param {float} progress - 0 to 1
*/
/**
* @summary Interpolation helper for animations
* @memberOf PSV
* @description
* Implements the Promise API with an additional "cancel" and "finally" methods.
* The promise is resolved when the animation is complete and rejected if the animation is cancelled.
* @example
* new Animation({
* properties: {
* width: {start: 100, end: 200}
* },
* duration: 5000,
* onTick: (properties) => element.style.width = `${properties.width}px`;
* })
*/
export class Animation {
/**
* @param {Object} options
* @param {Object.<string, Object>} options.properties
* @param {number} options.properties[].start
* @param {number} options.properties[].end
* @param {number} options.duration
* @param {number} [options.delay=0]
* @param {string} [options.easing='linear']
* @param {PSV.Animation.OnTick} options.onTick - called on each frame
*/
constructor(options) {
this.__cancelled = false;
this.__resolved = false;
this.__promise = new Promise((resolve, reject) => {
this.__resolve = resolve;
this.__reject = reject;
});
if (options) {
if (!options.easing || typeof options.easing === 'string') {
options.easing = EASINGS[options.easing || 'linear'];
}
this.__start = null;
this.options = options;
if (options.delay) {
this.__delayTimeout = setTimeout(() => {
this.__delayTimeout = null;
window.requestAnimationFrame(t => this.__run(t));
}, options.delay);
}
else {
window.requestAnimationFrame(t => this.__run(t));
}
}
}
/**
* @summary Main loop for the animation
* @param {number} timestamp
* @private
*/
__run(timestamp) {
// the animation has been cancelled
if (this.__cancelled) {
return;
}
// first iteration
if (this.__start === null) {
this.__start = timestamp;
}
// compute progress
const progress = (timestamp - this.__start) / this.options.duration;
const current = {};
if (progress < 1.0) {
// interpolate properties
each(this.options.properties, (prop, name) => {
if (prop) {
current[name] = prop.start + (prop.end - prop.start) * this.options.easing(progress);
}
});
this.options.onTick(current, progress);
window.requestAnimationFrame(t => this.__run(t));
}
else {
// call onTick one last time with final values
each(this.options.properties, (prop, name) => {
if (prop) {
current[name] = prop.end;
}
});
this.options.onTick(current, 1.0);
window.requestAnimationFrame(() => {
this.__resolved = true;
this.__resolve();
});
}
}
/**
* @summary Animation chaining
* @param {Function} [onFulfilled] - Called when the animation is complete, can return a new animation
* @param {Function} [onRejected] - Called when the animation is cancelled
* @returns {PSV.Animation}
*/
then(onFulfilled = null, onRejected = null) {
const p = new Animation();
// Allow cancellation to climb up the promise chain
p.__promise.then(null, () => this.cancel());
this.__promise.then(
() => p.__resolve(onFulfilled ? onFulfilled() : undefined),
() => p.__reject(onRejected ? onRejected() : undefined)
);
return p;
}
/**
* @summary Alias to `.then(null, onRejected)`
* @param {Function} onRejected - Called when the animation has been cancelled
* @returns {PSV.Animation}
*/
catch(onRejected) {
return this.then(undefined, onRejected);
}
/**
* @summary Alias to `.then(onFinally, onFinally)`
* @param {Function} onFinally - Called when the animation is either complete or cancelled
* @returns {PSV.Animation}
*/
finally(onFinally) {
return this.then(onFinally, onFinally);
}
/**
* @summary Cancels the animation
*/
cancel() {
if (!this.__cancelled && !this.__resolved) {
this.__cancelled = true;
this.__reject();
if (this.__delayTimeout) {
window.cancelAnimationFrame(this.__delayTimeout);
this.__delayTimeout = null;
}
}
}
/**
* @summary Returns a resolved animation promise
* @returns {PSV.Animation}
*/
static resolve() {
const p = Promise.resolve();
p.cancel = () => {
};
p.finally = (onFinally) => {
return p.then(onFinally, onFinally);
};
return p;
}
}