This repository was archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathvictory-animation.js
183 lines (170 loc) · 5.13 KB
/
victory-animation.js
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
/*global setTimeout:false */
import React from "react";
import PropTypes from "prop-types";
import * as d3Ease from "d3-ease";
import { victoryInterpolator } from "./util";
import Timer from "../victory-util/timer";
export default class VictoryAnimation extends React.Component {
static displayName = "VictoryAnimation";
static propTypes = {
children: PropTypes.func,
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array
]),
delay: PropTypes.number,
duration: PropTypes.number,
easing: PropTypes.oneOf([
"back", "backIn", "backOut", "backInOut",
"bounce", "bounceIn", "bounceOut", "bounceInOut",
"circle", "circleIn", "circleOut", "circleInOut",
"linear", "linearIn", "linearOut", "linearInOut",
"cubic", "cubicIn", "cubicOut", "cubicInOut",
"elastic", "elasticIn", "elasticOut", "elasticInOut",
"exp", "expIn", "expOut", "expInOut",
"poly", "polyIn", "polyOut", "polyInOut",
"quad", "quadIn", "quadOut", "quadInOut",
"sin", "sinIn", "sinOut", "sinInOut"
]),
onEnd: PropTypes.func
};
static defaultProps = {
data: {},
delay: 0,
duration: 1000,
easing: "quadInOut"
};
static contextTypes = {
getTimer: PropTypes.func
};
constructor(props) {
super(props);
/* defaults */
this.state = {
data: Array.isArray(this.props.data) ?
this.props.data[0] : this.props.data,
animationInfo: {
progress: 0,
animating: false
}
};
this.interpolator = null;
this.queue = Array.isArray(this.props.data) ?
this.props.data.slice(1) : [];
/* build easing function */
this.ease = d3Ease[this.toNewName(this.props.easing)];
/*
There is no autobinding of this in ES6 classes
so we bind functionToBeRunEachFrame to current instance of victory animation class
*/
this.functionToBeRunEachFrame = this.functionToBeRunEachFrame.bind(this);
this.getTimer = this.getTimer.bind(this);
}
componentDidMount() {
// Length check prevents us from triggering `onEnd` in `traverseQueue`.
if (this.queue.length) {
this.traverseQueue();
}
}
/* lifecycle */
componentWillReceiveProps(nextProps) {
/* cancel existing loop if it exists */
this.getTimer().unsubscribe(this.loopID);
/* If an object was supplied */
if (!Array.isArray(nextProps.data)) {
// Replace the tween queue. Could set `this.queue = [nextProps.data]`,
// but let's reuse the same array.
this.queue.length = 0;
this.queue.push(nextProps.data);
/* If an array was supplied */
} else {
/* Extend the tween queue */
this.queue.push(...nextProps.data);
}
/* Start traversing the tween queue */
this.traverseQueue();
}
componentWillUnmount() {
if (this.loopID) {
this.getTimer().unsubscribe(this.loopID);
} else {
this.getTimer().stop();
}
}
getTimer() {
if (this.context.getTimer) {
return this.context.getTimer();
}
if (!this.timer) {
this.timer = new Timer();
}
return this.timer;
}
toNewName(ease) {
// d3-ease changed the naming scheme for ease from "linear" -> "easeLinear" etc.
const capitalize = (s) => s && s[0].toUpperCase() + s.slice(1);
return `ease${capitalize(ease)}`;
}
/* Traverse the tween queue */
traverseQueue() {
if (this.queue.length) {
/* Get the next index */
const data = this.queue[0];
/* compare cached version to next props */
this.interpolator = victoryInterpolator(this.state.data, data);
/* reset step to zero */
if (this.props.delay) {
setTimeout(() => {
this.loopID = this.getTimer().subscribe(
this.functionToBeRunEachFrame, this.props.duration
);
}, this.props.delay);
} else {
this.loopID = this.getTimer().subscribe(
this.functionToBeRunEachFrame, this.props.duration
);
}
} else if (this.props.onEnd) {
this.props.onEnd();
}
}
/* every frame we... */
functionToBeRunEachFrame(elapsed, duration) {
/*
step can generate imprecise values, sometimes greater than 1
if this happens set the state to 1 and return, cancelling the timer
*/
duration = duration !== undefined ? duration : this.props.duration;
const step = duration ? elapsed / duration : 1;
if (step >= 1) {
this.setState({
data: this.interpolator(1),
animationInfo: {
progress: 1,
animating: false
}
});
if (this.loopID) {
this.getTimer().unsubscribe(this.loopID);
}
this.queue.shift();
this.traverseQueue();
return;
}
/*
if we're not at the end of the timer, set the state by passing
current step value that's transformed by the ease function to the
interpolator, which is cached for performance whenever props are received
*/
this.setState({
data: this.interpolator(this.ease(step)),
animationInfo: {
progress: step,
animating: step < 1
}
});
}
render() {
return this.props.children(this.state.data, this.state.animationInfo);
}
}