🎉 JointJS has new documentation! ðŸ¥³

JointJS+ Snaplines

ui.Snaplines

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.

Installation

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>

Usage

Enabling snaplines is as easy as creating a joint.ui.Snaplines object:

const snaplines = new joint.ui.Snaplines({ paper });

Demo

Configuration

The joint.ui.Snaplines constructor function takes the following parameters:

paperThe JointJS paper (joint.dia.Paper) object.
distanceThe distance in pixels between edges (center) of other elements for snapping applies. The default is 10.
filterFilter 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.
usePaperGridWhen 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.

API

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.

Styling Snaplines

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;
}