Skip to content

Instantly share code, notes, and snippets.

@guibot17
Forked from caleb531/snap-to-grid.js
Created December 7, 2017 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guibot17/5486cca6c081100e1782549dd7ec4c95 to your computer and use it in GitHub Desktop.
Save guibot17/5486cca6c081100e1782549dd7ec4c95 to your computer and use it in GitHub Desktop.
Makes a jCanvas layer draggable along a defined grid
// Available in jCanvas v20.1.0
// The pixel multiple to snap to
var snapToAmount = 40;
// Round the given value to the nearest multiple of n
function nearest(value, n) {
return Math.round(value / n) * n;
}
$('canvas').drawArc({
layer: true,
draggable: true,
fillStyle: '#36c',
x: 160, y: 120,
radius: 50,
updateDragX: function (layer, x) {
return nearest(x, snapToAmount);
},
updateDragY: function (layer, y) {
return nearest(y, snapToAmount);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment