Skip to content

Commit 5733adb

Browse files
committed
Changed tick to use animationFrame timestamp
1 parent 3c60ddb commit 5733adb

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

src/Spinner.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import TruncatingText from './TruncatingText.js';
66
const FRICTION = .0001;
77
const SPIN_MIN = 5;
88
const SPIN_MAX = 10;
9-
//const FRAME_RATE = 100;
109
const FONT_SIZE = 22;
1110

1211
class Spinner extends React.PureComponent {
@@ -16,16 +15,13 @@ class Spinner extends React.PureComponent {
1615
spinAngle: 360,
1716
};
1817
this.sliceAngle = [];
19-
this.animationTime = 0;
20-
this.previousFrameTime = 0;
18+
this.animationStartTime = null;
2119

2220
this.colors = [];
2321
}
2422

2523
handleClick() {
2624
if(!this.animationID) {
27-
this.animationTime = 0;
28-
this.previousFrameTime = performance.now()
2925

3026
// calculate index and degrees of item to land on
3127
// TODO: make theta generation more dramatic (i.e. more likely to land near edge of a wedge)
@@ -46,7 +42,7 @@ class Spinner extends React.PureComponent {
4642
1000/FRAME_RATE
4743
);
4844
*/
49-
this.animationID = window.requestAnimationFrame(() => this.tick(initialV, initialSpin));
45+
this.animationID = window.requestAnimationFrame((ts) => this.tick(ts, initialV, initialSpin));
5046
}
5147
}
5248

@@ -55,23 +51,25 @@ class Spinner extends React.PureComponent {
5551
window.cancelAnimationFrame(this.animationID);
5652
}
5753

58-
tick(initialV, initialSpin) {
59-
this.animationTime += performance.now() - this.previousFrameTime;
60-
const velocity = initialV - this.animationTime*FRICTION;
54+
tick(timestamp, initialV, initialSpin) {
55+
if(this.animationStartTime === null)
56+
this.animationStartTime = timestamp
57+
const animationTime = timestamp - this.animationStartTime;
58+
const velocity = initialV - animationTime*FRICTION;
6159

6260
this.previousFrameTime = performance.now();
6361

6462
// this creates problems at high FRICTION values on the last frame
6563
// TODO: think of some way to avoid that, maybe special case for last frame here?
6664
if(velocity <= 0) {
67-
window.cancelAnimationFrame(this.animationID);
6865
this.animationID = false;
66+
this.animationStartTime = null;
6967
}
7068
else
71-
window.requestAnimationFrame(() => this.tick(initialV, initialSpin));
69+
window.requestAnimationFrame((ts) => this.tick(ts, initialV, initialSpin));
7270

7371
this.setState({
74-
spinAngle: (initialSpin + initialV*this.animationTime - .5*FRICTION*this.animationTime*this.animationTime) % 360
72+
spinAngle: (initialSpin + initialV*animationTime - .5*FRICTION*animationTime*animationTime) % 360
7573
});
7674
}
7775

0 commit comments

Comments
 (0)