Skip to content

Instantly share code, notes, and snippets.

@caleb531
Last active May 21, 2018 08:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save caleb531/5886072 to your computer and use it in GitHub Desktop.
Save caleb531/5886072 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);
}
})
@joshuapinter
Copy link

I swear, you've thought of everything... 👍

@joshuapinter
Copy link

FYI, this seems to do strange things when using dragGroups as well.

@MattyGT
Copy link

MattyGT commented May 21, 2018

So easy to integrate thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment