Skip to content

Commit

Permalink
fix: chart constantly jumping in 1-2 pixel steps
Browse files Browse the repository at this point in the history
The issue is noticeable at `millisPerPixel` being between ~ 0.5 & 2.0 the size
of `requestAnimationFrame` period. Canvas image update period would be
inconsistent - some would last longer, some less, some would get skipped
altogether.
  • Loading branch information
WofWca committed Sep 6, 2021
1 parent c849bef commit 9548410
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions smoothie.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,31 +784,31 @@
if (this.options.limitFPS > 0 && nowMillis - this.lastRenderTimeMillis < (1000/this.options.limitFPS))
return;

time = time || nowMillis - (this.delay || 0);

// Round time down to pixel granularity, so motion appears smoother.
time -= time % this.options.millisPerPixel;

if (!this.isAnimatingScale) {
// We're not animating. We can use the last render time and the scroll speed to work out whether
// we actually need to paint anything yet. If not, we can return immediately.

// Render at least every 1/6th of a second. The canvas may be resized, which there is
// no reliable way to detect.
var maxIdleMillis = Math.min(1000/6, this.options.millisPerPixel);

if (nowMillis - this.lastRenderTimeMillis < maxIdleMillis) {
return;
var sameTime = this.lastChartTimestamp === time;
if (sameTime) {
// Render at least every 1/6th of a second. The canvas may be resized, which there is
// no reliable way to detect.
var needToRenderInCaseCanvasResized = nowMillis - this.lastRenderTimeMillis > 1000/6;
if (!needToRenderInCaseCanvasResized) {
return;
}
}
}

this.resize();

this.lastRenderTimeMillis = nowMillis;

canvas = canvas || this.canvas;
time = time || nowMillis - (this.delay || 0);

// Round time down to pixel granularity, so motion appears smoother.
time -= time % this.options.millisPerPixel;

this.lastChartTimestamp = time;

this.resize();

canvas = canvas || this.canvas;
var context = canvas.getContext('2d'),
chartOptions = this.options,
dimensions = { top: 0, left: 0, width: canvas.clientWidth, height: canvas.clientHeight },
Expand Down

0 comments on commit 9548410

Please sign in to comment.