-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
46 lines (36 loc) · 1.07 KB
/
index.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
var React = require('react');
var raf = require('raf');
var ease = require('ease-component');
var Counter = React.createClass({
getInitialState: function() {
return { value: this.props.begin };
},
componentDidMount: function() {
this.start = Date.now();
raf(this.animate);
},
animate: function() {
if (this.stop) return;
raf(this.animate);
this.draw()
},
draw: function() {
if (!this.isMounted()) return;
var time = this.props.time;
var begin = this.props.begin;
var end = this.props.end;
var easing = this.props.easing;
easing = easing && easing in ease ? easing : 'outCube';
var now = Date.now()
if (now - this.start >= time) this.stop = true;
var percentage = (now - this.start) / time;
percentage = percentage > 1 ? 1 : percentage;
var easeVal = ease[easing](percentage);
var val = begin + (end - begin) * easeVal;
this.setState({ value: val });
},
render: function() {
return React.DOM.span({ className: 'counter' }, Math.round(this.state.value));
}
});
module.exports = Counter;