Skip to content

Commit

Permalink
modifiers/snap: add limits to snap grid function
Browse files Browse the repository at this point in the history
interact.createSnapGrid({
  x: 50,
  y: 50,
  limits: {
    left: 100,
    right: 400,
    top: Infinity,
    bottom: Infinity,
  }
});

Close #208
  • Loading branch information
taye committed Nov 20, 2015
1 parent d26858d commit d549672
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/modifiers/snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ const snap = {

interact.createSnapGrid = function (grid) {
return function (x, y) {
const limits = grid.limits || {
left : -Infinity,
right : Infinity,
top : -Infinity,
bottom: Infinity,
};
let offsetX = 0;
let offsetY = 0;

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

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

return {
x: newX,
Expand Down

0 comments on commit d549672

Please sign in to comment.