Skip to content

Commit 902f1fc

Browse files
committed
initial commit
0 parents  commit 902f1fc

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# react-counter
2+
3+
A dead-simple incremental counter component with easing animations.
4+
5+
npm install react-counter
6+
7+
```js
8+
var React = require('react');
9+
var Counter = require('react-counter');
10+
11+
React.render(
12+
<Counter begin={0} end={1000} time={2000} easing="outCube" />,
13+
document.body
14+
);
15+
```
16+
17+
## Usage
18+
19+
```js
20+
<Counter
21+
begin={Number} // The number to count from
22+
end={Number} // The number to count to
23+
time={Number} // The time (in ms) the counting animation should take
24+
easing={String} // Which easing function to use (default="outCube")
25+
/>
26+
```
27+
28+
The `easing` property, can be a name of any easing function from
29+
[ease-component](https://www.npmjs.com/package/ease-component)

index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var React = require('react');
2+
var raf = require('raf');
3+
var ease = require('ease-component');
4+
5+
var Counter = React.createClass({
6+
getInitialState: function() {
7+
return { value: this.props.begin };
8+
},
9+
10+
componentDidMount: function() {
11+
this.start = Date.now();
12+
raf(this.animate);
13+
},
14+
15+
animate: function() {
16+
if (this.stop) return;
17+
18+
raf(this.animate);
19+
this.draw()
20+
},
21+
22+
draw: function() {
23+
if (!this.isMounted()) return;
24+
25+
var time = this.props.time;
26+
var begin = this.props.begin;
27+
var end = this.props.end;
28+
var easing = this.props.easing;
29+
30+
easing = easing && easing in ease ? easing : 'outCube';
31+
var now = Date.now()
32+
if (now - this.start >= time) this.stop = true;
33+
var percentage = (now - this.start) / time;
34+
var easeVal = ease[easing](percentage);
35+
var val = begin + (end - begin) * easeVal;
36+
37+
this.setState({ value: val });
38+
},
39+
40+
render: function() {
41+
return React.DOM.span({ className: 'counter' }, Math.round(this.state.value));
42+
}
43+
});
44+
45+
module.exports = Counter;

package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "react-counter",
3+
"version": "1.0.0",
4+
"description": "Simple incremental counter with easings",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/saebekassebil/react-counter.git"
9+
},
10+
"keywords": [
11+
"counter",
12+
"incremental",
13+
"animation",
14+
"react-component"
15+
],
16+
"author": "Jakob Miland",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/saebekassebil/react-counter/issues"
20+
},
21+
"homepage": "https://github.com/saebekassebil/react-counter",
22+
"dependencies": {
23+
"ease-component": "^1.0.0",
24+
"raf": "^2.0.4",
25+
"react": "^0.12.2"
26+
}
27+
}

0 commit comments

Comments
 (0)