Draggable Layers
Basic Usage
Layers can be made draggable using the draggable
property.
By default, draggable shapes will not move to the front when dragged. To change this behavior, set the bringToFront
property to true
.
Drag Events
You can provide callbacks for when any drag event occurs by defining dragstart
, drag
, dragstop
, and dragcancel
callbacks.
dragstart
: Triggers when you start dragging a layerdrag
: Triggers as you drag a layerdragstop
: Triggers when you stop dragging a layerdragcancel
: Triggers when you drag a layer off the edge of the canvas
The dragging
property
At any time, you can determine if a layer is currently being dragged by checking its dragging
property. When the layer is being dragged, its value is true
. Otherwise, its value is false
.
Drag groups
jCanvas allows you to assign a drag group to a layer. This means that when the layer is dragged, all other layers in the same layer group
will also be dragged.
This draggable grouping is achieved when you add the dragGroups
property to any (typically all) layers in the same layer group.
Restricting dragging to an axis
You can restrict the dragging of any layer to either the x or y axis using the restrictDragToAxis
property.
Please note that if a layer in a drag group has restricted draggability, then all the draggability of all other layers in that drag group will also be restricted only when the original layer is dragged.
Snap-to-grid dragging
If you wish to have your layers snap to a grid when dragged, you can do with the
updateDragX
and updateDragY
callbacks, along with some simple math.