Skip to content

Commit 18a3a0d

Browse files
committed
add clampFn option to dragElement & use it for radial drag box
- by default, dragElement clamps small x,y displacement to 0 independently - for radial drag box, using d(x,y) to determine 'small' displacements to clamp prove more advantageous.
1 parent 816be51 commit 18a3a0d

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/components/dragelement/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ dragElement.unhoverRaw = unhover.raw;
7676
* numClicks is how many clicks we've registered within
7777
* a doubleclick time
7878
* e is the original mousedown event
79+
* clampFn (optional, function(dx, dy) return [dx2, dy2])
80+
* Provide custom clamping function for small displacements.
81+
* By default, clamping is done using `minDrag` to x and y displacements
82+
* independently.
7983
*/
8084
dragElement.init = function init(options) {
8185
var gd = options.gd;
@@ -99,6 +103,14 @@ dragElement.init = function init(options) {
99103
element.onmousedown = onStart;
100104
element.ontouchstart = onStart;
101105

106+
function _clampFn(dx, dy, minDrag) {
107+
if(Math.abs(dx) < minDrag) dx = 0;
108+
if(Math.abs(dy) < minDrag) dy = 0;
109+
return [dx, dy];
110+
}
111+
112+
var clampFn = options.clampFn || _clampFn;
113+
102114
function onStart(e) {
103115
// make dragging and dragged into properties of gd
104116
// so that others can look at and modify them
@@ -145,12 +157,11 @@ dragElement.init = function init(options) {
145157

146158
function onMove(e) {
147159
var offset = pointerOffset(e);
148-
var dx = offset[0] - startX;
149-
var dy = offset[1] - startY;
150160
var minDrag = options.minDrag || constants.MINDRAG;
161+
var dxdy = clampFn(offset[0] - startX, offset[1] - startY, minDrag);
162+
var dx = dxdy[0];
163+
var dy = dxdy[1];
151164

152-
if(Math.abs(dx) < minDrag) dx = 0;
153-
if(Math.abs(dy) < minDrag) dy = 0;
154165
if(dx || dy) {
155166
gd._dragged = true;
156167
dragElement.unhover(gd);

src/plots/polar/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ module.exports = {
3333
cornerLen: 25,
3434
cornerHalfWidth: 2,
3535

36+
// pixels to move mouse before you stop clamping to starting point
37+
MINDRAG: 8,
3638
// smallest radial distance [px] allowed for a zoombox
3739
MINZOOM: 20,
3840
// distance [px] off (r=0) or (r=radius) where we transition

src/plots/polar/polar.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,13 @@ proto.updateRadialDrag = function(fullLayout, polarLayout) {
858858
dragBox.clearSelect(fullLayout._zoomlayer);
859859
};
860860

861-
// decrease min drag value to be able to catch smaller dx/dy that
862-
// can determine rotateMove vs rerangeMove
863-
dragOpts.minDrag = 2;
861+
dragOpts.clampFn = function(dx, dy) {
862+
if(Math.sqrt(dx * dx + dy * dy) < constants.MINDRAG) {
863+
dx = 0;
864+
dy = 0;
865+
}
866+
return [dx, dy];
867+
};
864868

865869
dragElement.init(dragOpts);
866870
};

0 commit comments

Comments
 (0)