Skip to content

Commit f65ace8

Browse files
committed
Fix sluggish grid snapping.
Previously the grid snapping could only move one unit per mousemove event.
1 parent 975e716 commit f65ace8

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

lib/draggable.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,10 @@ function removeEvent(el, event, handler) {
125125
}
126126
}
127127

128-
function snapToGrid(draggable, clientX, clientY) {
129-
var stateX = parseInt(draggable.state.clientX, 10);
130-
var stateY = parseInt(draggable.state.clientY, 10);
131-
var directionX = clientX < stateX ? -1 : 1;
132-
var directionY = clientY < stateY ? -1 : 1;
133-
134-
clientX = Math.abs(clientX - stateX) >= draggable.props.grid[0] ?
135-
(stateX + (draggable.props.grid[0] * directionX)) :
136-
stateX;
137-
138-
clientY = Math.abs(clientY - stateY) >= draggable.props.grid[1] ?
139-
(stateY + (draggable.props.grid[1] * directionY)) :
140-
stateY;
141-
142-
return [clientX, clientY];
128+
function snapToGrid(grid, pendingX, pendingY) {
129+
var x = Math.round(pendingX / grid[0]) * grid[0];
130+
var y = Math.round(pendingY / grid[1]) * grid[1];
131+
return [x, y];
143132
}
144133

145134
// Useful for preventing blue highlights all over everything when dragging.
@@ -435,7 +424,7 @@ module.exports = React.createClass({
435424

436425
// Snap to grid if prop has been provided
437426
if (Array.isArray(this.props.grid)) {
438-
var coords = snapToGrid(this, clientX, clientY);
427+
var coords = snapToGrid(this.props.grid, clientX, clientY);
439428
clientX = coords[0], clientY = coords[1];
440429
}
441430

0 commit comments

Comments
 (0)