Skip to content

Commit d549672

Browse files
committed
modifiers/snap: add limits to snap grid function
interact.createSnapGrid({ x: 50, y: 50, limits: { left: 100, right: 400, top: Infinity, bottom: Infinity, } }); Close #208
1 parent d26858d commit d549672

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/modifiers/snap.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ const snap = {
217217

218218
interact.createSnapGrid = function (grid) {
219219
return function (x, y) {
220+
const limits = grid.limits || {
221+
left : -Infinity,
222+
right : Infinity,
223+
top : -Infinity,
224+
bottom: Infinity,
225+
};
220226
let offsetX = 0;
221227
let offsetY = 0;
222228

@@ -228,8 +234,8 @@ interact.createSnapGrid = function (grid) {
228234
const gridx = Math.round((x - offsetX) / grid.x);
229235
const gridy = Math.round((y - offsetY) / grid.y);
230236

231-
const newX = gridx * grid.x + offsetX;
232-
const newY = gridy * grid.y + offsetY;
237+
const newX = Math.max(limits.left, Math.min(limits.right , gridx * grid.x + offsetX));
238+
const newY = Math.max(limits.top , Math.min(limits.bottom, gridy * grid.y + offsetY));
233239

234240
return {
235241
x: newX,

0 commit comments

Comments
 (0)