Skip to content

Commit

Permalink
Fixed issue Mikhus#94 - clear previous animations properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhus committed Dec 25, 2016
1 parent 36b9c6b commit 1cba79b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/async.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,5 @@
</script>

<script async src="../gauge.min.js" onload="initScriptedGauges()"></script>

</body>
</html>
2 changes: 1 addition & 1 deletion examples/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@
});
});
</script>

</body>
</html>
2 changes: 1 addition & 1 deletion examples/radial-min-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@
});
}
</script>

</body>
</html>
2 changes: 1 addition & 1 deletion examples/radial.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@
});
}
</script>

</body>
</html>
4 changes: 2 additions & 2 deletions gauge.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gauge.min.js.map

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions lib/Animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ export default class Animation {
* @param {EndEventCallback} [end]
*/
animate(draw, end) {
//noinspection JSUnresolvedVariable
this.cancel();

// noinspection JSUnresolvedVariable
const start = window.performance && window.performance.now ?
window.performance.now() :
(vendorize('animationStartTime') || Date.now());
Expand All @@ -272,9 +274,9 @@ export default class Animation {
}

/**
* Destroys this object properly
* Cancels current animation if any
*/
destroy() {
cancel() {
if (this.frame) {
const cancelAnimationFrame = vendorize('cancelAnimationFrame') ||
/* istanbul ignore next */
Expand All @@ -283,7 +285,13 @@ export default class Animation {
cancelAnimationFrame(this.frame);
this.frame = null;
}
}

/**
* Destroys this object properly
*/
destroy() {
this.cancel();
this.draw = null;
this.end = null;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/BaseGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ export default class BaseGauge extends EventEmitter {
if (value === fromValue) return;

if (this.options.animation) {
if (this.animation.frame) {
// animation is already in progress -
// forget related old animation value
// @see https://github.com/Mikhus/canvas-gauges/issues/94
delete this._value;
}

/**
* @type {number}
* @access private
Expand All @@ -232,6 +239,7 @@ export default class BaseGauge extends EventEmitter {
this.draw();

this.emit('animate', percent, this.options.value);
console.log('animation progress', this.options.value, this._value);
}, () => {
if (this._value !== undefined) {
this.options.value = this._value;
Expand All @@ -240,6 +248,7 @@ export default class BaseGauge extends EventEmitter {

this.draw();
this.emit('animationEnd');
console.log('animation end');
});
}

Expand Down
2 changes: 1 addition & 1 deletion test-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1cba79b

Please sign in to comment.