I've got two visual objects that I can drag. I want to "glue" a line between them so that when I drag one object the line adjusts and stays stuck to the same relative point on the dragged object (think node-red, jointjs, cad/cam).
The visual objects are created using groups of elements (at 0,0). Dragging is implemented by translating the matrix for the group objects. (I want to use the matrix, as this will facilitate future zoom-in and zoom-out).
I think that I want to create a new kind of "line" which contains two elements - a start point and an end-point. The startpoint is grouped with visual object 1, the endpoint is grouped with object 2. If object 2 is dragged, the line's endpoint will be included in the matrix transformation for group 2 and will stay stuck with that object. (And similarly with object 1 and the startpoint).
I'm new to snap and js. I'm stuck on how to create such a new "line" element which contains startpoint and endpoint elements (e.g. how to extend the line prototype or "subclass" it).
Advice needed. Thanks.
I presume this is what you're looking for. It attaches a line between two squares on screen which you can drag.
Create a snap plugin for draggable rectangles.
Get the current transformation.
Update the current transformation.
Save the current transformation.
Go through each saved path and update it's path attribute. Call prependTo - to make sure it is behind the rectangles.
Get the coordinates of each element.
Get the path string using both elements coordinates.
Add a path to two elements. Use their IDs as reference. Snap automatically creates a random ID for the elements so we don't need to.
Create your draggableRectangle protoype and attach all functionality.
Create the paper.
Create the draggable rectangles.
Attach them to each other.
Here is a link to the JSFiddle