Skip to content

Commit 2f2b300

Browse files
authored
Merge pull request #2041 from plotly/normalize-zoom-speed
Normalize zoom speed and wheel behavior across trace types
2 parents e7c7149 + 4265a38 commit 2f2b300

File tree

7 files changed

+30
-33
lines changed

7 files changed

+30
-33
lines changed

src/plots/cartesian/dragbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
380380
return;
381381
}
382382

383-
var zoom = Math.exp(-Math.min(Math.max(wheelDelta, -20), 20) / 100),
383+
var zoom = Math.exp(-Math.min(Math.max(wheelDelta, -20), 20) / 200),
384384
gbb = mainplot.draglayer.select('.nsewdrag')
385385
.node().getBoundingClientRect(),
386386
xfrac = (e.clientX - gbb.left) / gbb.width,

src/plots/gl2d/camera.js

+19-24
Original file line numberDiff line numberDiff line change
@@ -256,40 +256,35 @@ function createCamera(scene) {
256256
}
257257

258258
result.wheelListener = mouseWheel(element, function(dx, dy) {
259+
if(!scene.scrollZoom) return false;
260+
259261
var dataBox = scene.calcDataBox(),
260262
viewBox = plot.viewBox;
261263

262264
var lastX = result.lastPos[0],
263265
lastY = result.lastPos[1];
264266

265-
switch(scene.fullLayout.dragmode) {
266-
case 'zoom':
267-
break;
267+
var scale = Math.exp(5.0 * dy / (viewBox[3] - viewBox[1]));
268268

269-
case 'pan':
270-
var scale = Math.exp(0.1 * dy / (viewBox[3] - viewBox[1]));
269+
var cx = lastX /
270+
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
271+
dataBox[0];
272+
var cy = lastY /
273+
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
274+
dataBox[1];
271275

272-
var cx = lastX /
273-
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
274-
dataBox[0];
275-
var cy = lastY /
276-
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
277-
dataBox[1];
276+
dataBox[0] = (dataBox[0] - cx) * scale + cx;
277+
dataBox[2] = (dataBox[2] - cx) * scale + cx;
278+
dataBox[1] = (dataBox[1] - cy) * scale + cy;
279+
dataBox[3] = (dataBox[3] - cy) * scale + cy;
278280

279-
dataBox[0] = (dataBox[0] - cx) * scale + cx;
280-
dataBox[2] = (dataBox[2] - cx) * scale + cx;
281-
dataBox[1] = (dataBox[1] - cy) * scale + cy;
282-
dataBox[3] = (dataBox[3] - cy) * scale + cy;
281+
scene.setRanges(dataBox);
283282

284-
scene.setRanges(dataBox);
285-
286-
result.lastInputTime = Date.now();
287-
unSetAutoRange();
288-
scene.cameraChanged();
289-
scene.handleAnnotations();
290-
scene.relayoutCallback();
291-
break;
292-
}
283+
result.lastInputTime = Date.now();
284+
unSetAutoRange();
285+
scene.cameraChanged();
286+
scene.handleAnnotations();
287+
scene.relayoutCallback();
293288

294289
return true;
295290
});

src/plots/gl2d/scene2d.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function Scene2D(options, fullLayout) {
3636
this.pixelRatio = options.plotGlPixelRatio || window.devicePixelRatio;
3737
this.id = options.id;
3838
this.staticPlot = !!options.staticPlot;
39+
this.scrollZoom = this.graphDiv._context.scrollZoom;
3940

4041
this.fullData = null;
4142
this.updateRefs(fullLayout);

src/plots/gl3d/camera.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function createCamera(element, options) {
256256
if(Math.abs(dx) > Math.abs(dy)) {
257257
view.rotate(t, 0, 0, -dx * flipX * Math.PI * camera.rotateSpeed / window.innerWidth);
258258
} else {
259-
var kzoom = -camera.zoomSpeed * flipY * dy / window.innerHeight * (t - view.lastT()) / 100.0;
259+
var kzoom = -camera.zoomSpeed * flipY * dy / window.innerHeight * (t - view.lastT()) / 20.0;
260260
view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1));
261261
}
262262
}, true);

src/plots/plots.js

+1
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ plots.purge = function(gd) {
13301330
delete gd._transitionData;
13311331
delete gd._transitioning;
13321332
delete gd._initialAutoSize;
1333+
delete gd._transitioningWithDuration;
13331334

13341335
// remove all event listeners
13351336
if(gd.removeAllListeners) gd.removeAllListeners();

test/jasmine/tests/cartesian_interact_test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,21 @@ describe('axis zoom/pan and main plot zoom', function() {
367367
mouseEvent('scroll', mainDragCoords.x, mainDragCoords.y, {deltaY: 20, element: mainDrag});
368368
})
369369
.then(delay(constants.REDRAWDELAY + 10))
370-
.then(checkRanges({xaxis: [-0.4428, 2], yaxis: [0, 2.4428]}, 'xy main scroll'))
370+
.then(checkRanges({xaxis: [-0.2103, 2], yaxis: [0, 2.2103]}, 'xy main scroll'))
371371
.then(function() {
372372
var ewDrag = getDragger('xy', 'ew');
373373
var ewDragCoords = getNodeCoords(ewDrag);
374374
mouseEvent('scroll', ewDragCoords.x - 50, ewDragCoords.y, {deltaY: -20, element: ewDrag});
375375
})
376376
.then(delay(constants.REDRAWDELAY + 10))
377-
.then(checkRanges({xaxis: [-0.3321, 1.6679], yaxis: [0, 2.4428]}, 'x scroll'))
377+
.then(checkRanges({xaxis: [-0.1578, 1.8422], yaxis: [0, 2.2103]}, 'x scroll'))
378378
.then(function() {
379379
var nsDrag = getDragger('xy', 'ns');
380380
var nsDragCoords = getNodeCoords(nsDrag);
381381
mouseEvent('scroll', nsDragCoords.x, nsDragCoords.y - 50, {deltaY: -20, element: nsDrag});
382382
})
383383
.then(delay(constants.REDRAWDELAY + 10))
384-
.then(checkRanges({xaxis: [-0.3321, 1.6679], yaxis: [0.3321, 2.3321]}, 'y scroll'))
384+
.then(checkRanges({xaxis: [-0.1578, 1.8422], yaxis: [0.1578, 2.1578]}, 'y scroll'))
385385
.catch(failTest)
386386
.then(done);
387387
});
@@ -420,15 +420,15 @@ describe('axis zoom/pan and main plot zoom', function() {
420420
mouseEvent('scroll', mainDragCoords.x, mainDragCoords.y, {deltaY: 20, element: mainDrag});
421421
})
422422
.then(delay(constants.REDRAWDELAY + 10))
423-
.then(checkRanges({xaxis: [-0.4428, 2], yaxis: [0, 2.4428], xaxis2: [-0.2214, 2.2214], yaxis2: [-0.2214, 2.2214]},
423+
.then(checkRanges({xaxis: [-0.2103, 2], yaxis: [0, 2.2103], xaxis2: [-0.1052, 2.1052], yaxis2: [-0.1052, 2.1052]},
424424
'scroll xy'))
425425
.then(function() {
426426
var ewDrag = getDragger('xy', 'ew');
427427
var ewDragCoords = getNodeCoords(ewDrag);
428428
mouseEvent('scroll', ewDragCoords.x - 50, ewDragCoords.y, {deltaY: -20, element: ewDrag});
429429
})
430430
.then(delay(constants.REDRAWDELAY + 10))
431-
.then(checkRanges({xaxis: [-0.3321, 1.6679], yaxis: [0.2214, 2.2214]}, 'scroll x'))
431+
.then(checkRanges({xaxis: [-0.1578, 1.8422], yaxis: [0.1052, 2.1052]}, 'scroll x'))
432432
.catch(failTest)
433433
.then(done);
434434
});

test/jasmine/tests/click_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,8 @@ describe('Test click interactions:', function() {
866866
var translate = Drawing.getTranslate(mockEl),
867867
scale = Drawing.getScale(mockEl);
868868

869-
expect([translate.x, translate.y]).toBeCloseToArray([-25.941, 43.911]);
870-
expect([scale.x, scale.y]).toBeCloseToArray([1.221, 1.221]);
869+
expect([translate.x, translate.y]).toBeCloseToArray([13.93, 62.86]);
870+
expect([scale.x, scale.y]).toBeCloseToArray([1.105, 1.105]);
871871
});
872872
});
873873

0 commit comments

Comments
 (0)