Skip to content

Commit

Permalink
utils/snappers/grid: allow grid of other fields
Browse files Browse the repository at this point in the history
(width, height), (top, left), (bottom, right) in addition to (x, y)
  • Loading branch information
taye committed Mar 25, 2018
1 parent 8bc0f85 commit abeedb5
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions packages/utils/snappers/grid.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as is from '../is';

export default (grid) => {
const coordFields = [
['x', 'y'],
['left', 'top'],
['right', 'bottom'],
['width', 'height'],
].filter(([xField, yField]) => xField in grid || yField in grid);

return function (x, y) {
const {
x: gridX,
y: gridY,
range,
offset,
limits = {
left : -Infinity,
right : Infinity,
Expand All @@ -15,24 +17,17 @@ export default (grid) => {
},
} = grid;

let offsetX = 0;
let offsetY = 0;
const offset = offset || { x: 0, y: 0 };
const result = { range };

if (is.object(offset)) {
offsetX = offset.x;
offsetY = offset.y;
}
for (const [xField, yField] of coordFields) {
const gridx = Math.round((x - offset.x) / grid[xField]);
const gridy = Math.round((y - offset.y) / grid[yField]);

const gridx = Math.round((x - offsetX) / gridX);
const gridy = Math.round((y - offsetY) / gridY);

const newX = Math.max(limits.left, Math.min(limits.right , gridx * gridX + offsetX));
const newY = Math.max(limits.top , Math.min(limits.bottom, gridy * gridY + offsetY));
result[xField] = Math.max(limits.left, Math.min(limits.right , gridx * grid[xField] + offset.x));
result[yField] = Math.max(limits.top, Math.min(limits.bottom , gridy * grid[yField] + offset.y));
}

return {
x: newX,
y: newY,
range: range,
};
return result;
};
};

0 comments on commit abeedb5

Please sign in to comment.