Skip to content

Commit

Permalink
Fixed issue #106 - Animation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhus committed Jan 18, 2017
1 parent 3ef85de commit 3591da7
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions examples/ofthen-updates.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!doctype html>
<html>
<head>
<title>Car assist</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>body { padding: 0; margin: 0; background: #F0F0F0 }</style>
</head>

<body>
<p>
<canvas id="hpa"></canvas>
<canvas id="hpb"></canvas>
</p>

<script src="../gauge.min.js"></script>
<script>
var myHighlightsHP = [
{ from: 0, to: 40, color: 'rgba(0,0,255,.8)'},
{ from: 400, to: 450, color: 'rgba(255,0,0,.8)'}
];
var myMajorTicksHP = ['0', '100', '200', '300', '400', '450'];
var myCommonProp = {
width: 150,
height: 150,
value: 1,
colorPlate: '#F0F0F0',
colorMajorTicks: '#222',
colorMinorTicks: '#222',
colorTitle: '#222',
colorUnits: '#222',
colorNumbers: '#222',
colorNeedle: 'rgba(240, 128, 128, 1)',
colorNeedleEnd: 'rgba(255, 160, 122, .9)',
valueBox: false
};

var myAnimation = {
animationRule: 'decycle',
animationDuration: 500
};

var myCommonPropHP=Object.assign({},
myCommonProp,
myAnimation,
{
units:'BAR',
minValue:0, maxValue:450,
majorTicks:myMajorTicksHP,
highlights:myHighlightsHP
}
);

var hpaConfig = Object.assign({},
{ renderTo: 'hpa', title: 'HPA' },
myCommonPropHP
);
var hpbConfig = Object.assign({},
{ renderTo: 'hpb', title: 'HPB' },
myCommonPropHP
);
var gaugeHpa = new RadialGauge(hpaConfig);
var gaugeHpb = new RadialGauge(hpbConfig);

function onRender() {
console.log(this._value, this.value);
}

gaugeHpa.on('render', onRender);
gaugeHpb.on('render', onRender);

gaugeHpa.draw();
gaugeHpb.draw();

setInterval(function() {
gaugeHpa.value = 0;
gaugeHpb.value = 0;
}, 50);
</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.

19 changes: 17 additions & 2 deletions lib/Animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,23 @@ function step(time, draw, start, rule, duration, end, anim) {

let progress = time - start;
let percent = progress / duration;
let animationTransformed = 0;

if (percent > 1) {
percent = 1;
}

draw && draw(percent === 1 ? percent : rule(percent));
if (percent !== 1) {
animationTransformed = rule(percent);

// make sure we have correct number after applying animation
// transformation
if (isFinite(animationTransformed) && !isNaN(animationTransformed)) {
percent = animationTransformed;
}
}

draw && draw(percent);

if (progress < duration) {
anim.frame = requestAnimationFrame(time =>
Expand All @@ -131,6 +142,7 @@ function step(time, draw, start, rule, duration, end, anim) {

else {
end && end();
anim.inProgress = false;
}
}

Expand Down Expand Up @@ -253,7 +265,7 @@ export default class Animation {
* @param {EndEventCallback} [end]
*/
animate(draw, end) {
this.cancel();
this.frame && this.cancel();

// noinspection JSUnresolvedVariable
const start = window.performance && window.performance.now ?
Expand All @@ -263,6 +275,9 @@ export default class Animation {
draw = draw || this.draw;
end = end || this.end;

this.draw = draw;
this.end = end;

/**
* Current requested animation frame identifier
*
Expand Down
13 changes: 11 additions & 2 deletions lib/BaseGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,23 @@ export default class BaseGauge extends EventEmitter {

let fromValue = this.options.value;

if (value === fromValue) return;
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;
this.options.value = this._value;

// if there is no actual value change requested stop it
if (this._value === value) {
this.animation.cancel();
delete this._value;
return ;
}
}

/**
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 3591da7

Please sign in to comment.