diff --git a/draftlogs/8201_fix.md b/draftlogs/8201_fix.md new file mode 100644 index 00000000000..d2b6b5890db --- /dev/null +++ b/draftlogs/8201_fix.md @@ -0,0 +1 @@ +- Fix a wrong frame at the end of zero-duration axis transitions [[#8201](https://github.com/plotly/plotly.js/pull/8201)] diff --git a/src/plots/cartesian/transition_axes.js b/src/plots/cartesian/transition_axes.js index f98969de352..79f807991b8 100644 --- a/src/plots/cartesian/transition_axes.js +++ b/src/plots/cartesian/transition_axes.js @@ -181,14 +181,15 @@ module.exports = function transitionAxes(gd, edits, transitionOpts, makeOnComple function doFrame() { t2 = Date.now(); - var tInterp = Math.min(1, (t2 - t1) / transitionOpts.duration); + var duration = transitionOpts.duration; + var tInterp = duration > 0 ? Math.min(1, (t2 - t1) / duration) : 1; var progress = easeFn(tInterp); for(var i = 0; i < edits.length; i++) { updateSubplot(edits[i], progress); } - if(t2 - t1 > transitionOpts.duration) { + if(tInterp >= 1) { transitionComplete(); raf = window.cancelAnimationFrame(doFrame); } else { diff --git a/test/jasmine/tests/transition_test.js b/test/jasmine/tests/transition_test.js index ceba1cc4a66..d1d2a55a97a 100644 --- a/test/jasmine/tests/transition_test.js +++ b/test/jasmine/tests/transition_test.js @@ -30,6 +30,25 @@ describe('Plots.transition', function() { destroyGraphDiv(); }); + it('resolves a zero-duration axis transition whose first frame runs right away', function(done) { + var traces = plotApiHelpers.coerceTraceIndices(gd, null); + var resolved = false; + + // the first frame then runs in the same millisecond the transition started + spyOn(Date, 'now').and.returnValue(Date.now()); + + Plots.transition(gd, null, {'xaxis.range': [0.2, 0.3]}, traces, {redraw: true}, {duration: 0, easing: 'cubic-in-out'}) + .then(function() { resolved = true; }); + + Promise.resolve() + .then(delay(100)) + .then(function() { + expect(resolved).toBe(true, 'transition resolved'); + expect(gd._fullLayout.xaxis.range).toEqual([0.2, 0.3]); + }) + .then(done, done.fail); + }); + // Run the whole set of tests twice: once with zero duration and once with // nonzero duration since the behavior should be identical, but there's a // very real possibility of race conditions or other timing issues.