Skip to content

Commit 979920e

Browse files
committed
Spinner animation now uses setState callback for synchronicity
1 parent 5733adb commit 979920e

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

src/Spinner.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,25 @@ class Spinner extends React.PureComponent {
5151
window.cancelAnimationFrame(this.animationID);
5252
}
5353

54-
tick(timestamp, initialV, initialSpin) {
54+
tick(timestamp, initialV, initialSpin, lastVelocity) {
55+
// this creates problems at high FRICTION values on the last frame
56+
// TODO: think of some way to avoid that, maybe special case for last frame here?
57+
if(lastVelocity <= 0) {
58+
this.animationID = false;
59+
this.animationStartTime = null;
60+
return;
61+
}
62+
5563
if(this.animationStartTime === null)
5664
this.animationStartTime = timestamp
5765
const animationTime = timestamp - this.animationStartTime;
5866
const velocity = initialV - animationTime*FRICTION;
5967

6068
this.previousFrameTime = performance.now();
6169

62-
// this creates problems at high FRICTION values on the last frame
63-
// TODO: think of some way to avoid that, maybe special case for last frame here?
64-
if(velocity <= 0) {
65-
this.animationID = false;
66-
this.animationStartTime = null;
67-
}
68-
else
69-
window.requestAnimationFrame((ts) => this.tick(ts, initialV, initialSpin));
70-
7170
this.setState({
7271
spinAngle: (initialSpin + initialV*animationTime - .5*FRICTION*animationTime*animationTime) % 360
73-
});
72+
}, () => window.requestAnimationFrame((ts) => this.tick(ts, initialV, initialSpin, velocity)));
7473
}
7574

7675
render() {

0 commit comments

Comments
 (0)