🎉 JointJS has new documentation! 🥳
Snaplines (a.k.a Guidelines) are great for guiding the user in aligning elements in your application. Snaplines are vertical and horizontal lines that appear when the user is dragging an element that is vertically or horizontally close enough to some other element.
Include joint.ui.snaplines.css
and joint.ui.snaplines.js
files to your HTML:
<link rel="stylesheet" type="text/css" href="joint.ui.snaplines.css">
<script src="joint.ui.snaplines.js"></script>
Enabling snaplines is as easy as creating a joint.ui.Snaplines
object:
const snaplines = new joint.ui.Snaplines({ paper });
The joint.ui.Snaplines
constructor function takes the following parameters:
paper | The JointJS paper (joint.dia.Paper ) object. |
---|---|
distance | The distance in pixels between edges (center) of other elements for snapping applies. The default is 10. |
filter | Filter allows you to filter out elements taken into account for snapping. It can either be an array
in which case the array is considered to contain cell types (or elements directly, they can even be intermixed) that should be filtered out from snapping.
Or it can be a function with the following signature: function(element) in which case the function should return true
if the element should not be taken into account for snapping. |
usePaperGrid | When an element is snapped to other element, adjust its top-left corner to the paper's grid. Useful when the elements in the graph (or their centers) are not aligned with the grid. The default is false . |
canSnap(elementView) | A function to determine whether an element will use snaplines while moving. All elements use snaplines by default. |
additionalSnapPoints(elementView, options) |
A function that allows the user to specify additional snap points.
The first parameter of the callback is the elementView of the dragged/changed element.
The second parameter is the options object.
The options object contains a single type property that describes what kind of snaplines were invoked
(it should snap because the element was moved 'move' or resized 'resize' ).
The return value represents an array of points (objects with properties x and y ) to which the dragged element should snap.
The returned points take precedence over the default snap points generated from the elements.
|
enable() | void | Enable the snaplines. The plugin will start listening to the paper and align elements as they move. |
---|---|---|
disable() | void | Disable the snaplines. |
isDisabled() | boolean | Return true if the snaplines are disabled. Otherwise, return false . The plugin is enabled by default. |
Snaplines can be customized in CSS. Snapline is a <div> HTML element that has the
snapline
CSS class set. Moreover, you can customize horizontal and vertical
snaplines differently by using the horizontal
and vertical
CSS class:
.snapline.horizontal {
border-bottom: 2px dashed red;
box-shadow: 0 0 2px black;
}
.snapline.vertical {
border-right: 2px dotted blue;
box-shadow: 0 0 2px black;
}