Summary
jsPlumb allows you to connect elements on the screen with "plumbing", using a Canvas element when supported, and Google's ExplorerCanvas script to support older browsers.It can be used with jQuery, MooTools or YUI3 (or another library of your choice if you feel like implementing a plugin for it). Required versions are as follows:
jQuery
- jQuery 1.3.x or higher.
- jQuery UI 1.7.x or 1.8.x (if you wish to support drag and drop). jsPlumb has been tested on 1.7.2, 1.8.0 and 1.8.13.
MooTools
- MooTools Core 1.2.4 or higher. jsPlumb has been tested with 1.2.4 and 1.3.2.
- Drag.Move from MooTools More 1.2.4.4 or higher (if you wish to support drag and drop). jsPlumb has been tested with versions 1.2.4.4 and 1.3.2.1.
YUI3
- YUI 3.3.x. jsPlumb has been tested on 3.3.0 only; it is possible other 3.x.x versions will work.
For Canvas support in IE you also need to include Google's ExplorerCanvas script.
jsPlumb 1.2.6 has been tested on the following browsers:
- IE 7 on Windows Vista
- IE 8 on Windows Vista (we force IE7 standards compatibility mode)
- Firefox 3.5.8 on Windows Vista
- Firefox 3.6.3 on Ubuntu 10.04
- Chrome on Ubuntu 10.04
- Safari 4 on Mac Tiger
- Safari 4 on Windows Vista
- Opera 10.54 on Windows Vista
- introduced support for mouse events (click/doubleclick/enter/exit) when using Canvas
- introduced shorthand syntax for specifying Endpointds and Overlays
- added Flowchart connector type
These issues have been resolved:
- issue 54: crash on vertical straight connectors
- issue 57: wrong scope chosen for detached draggable connection
- issue 58: changing one connector's style changes others.
- issue 63: add access to the Connector in event API
- issue 65: expose Connection so it can be extended
- issue 70: right click on Connections
- issue 72: toggle visibility of endpoints
- issue 74: mootools drag error
- issue 76: arrow overlays flip and disappear with vertical straight connectors
- issue 80: support for setting an arrow overlay's direction
- issue 81: detach event fired twice
Imports
jQuery
<script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script> <script type="text/javascript" src="PATH_TO/jquery.jsPlumb-1.2.6-all-min.js "></script>
MooTools
<script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.3.2/mootools-yui-compressed.js"></script> <script type="text/javascript" src="PATH_TO_MOO_TOOLS_MORE_1_3_2_1"></script> <script type="text/javascript" src="PATH_TO/mootools.jsPlumb-1.2.6-all-min.js "></script>
YUI3
<script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/3.3.0/build/simpleyui/simpleyui-min.js"></script> <script type="text/javascript" src="PATH_TO/yui.jsPlumb-1.2.6-all-min.js "></script>
Initializing jsPlumb
You should not start making calls to jsPlumb until the DOM has been initialized - perhaps no surprises there. With YUI, though, the asynchronous nature of the script loading process means that you are not guaranteed that jsPlumb is ready as soon as the DOM is. To handle this, you should bind to the "ready" event on jsPlumb (or the instance of jsPlumb you are working with):jsPlumb.bind("ready", function() { ... // your jsPlumb related init code goes here ... });
For jQuery and MooTools you should not actually need to do this; by the time the DOM ready event is fired in each of those libraries you can be sure all the JS we need has been loaded. But in terms of explicitly calling out that you are waiting for jsPlumb, it seems like a good practice to bind to the "ready" event.
If you bind to the "ready" event after jsPlumb has already been initialized, your callback will be executed immediately.
Unloading jsPlumb
jsPlumb offers a method you can call when your page is unloading. You should do this to insure against memory leaks. You configure it like this:<body onunload="jsPlumb.unload();"> ... </body>
jsPlumb Basic Concepts
jsPlumb provides two ways of establishing connections between elements: programmatically, via Javascript, and interactively, with drag and drop. To support this, jsPlumb has these five core concepts:- Endpoint - the visual representation of one end of a Connection. You can create and attach these to elements yourself, which you are required to do to support drag and drop, or have jsPlumb create them when creating a Connection programmatically using jsPlumb.connect(...). You can also join two Endpoints programmatically, by passing them as arguments to jsPlumb.connect(...). This class, and Connection, discussed below, are the main objects you will interact with in jsPlumb.
- Anchor - a location, relative to an element's origin, at which an Endpoint can exist. You do not create these yourself; you supply hints to the various jsPlumb functions, which create them as needed. They have no visual representation; they are a logical position only. Anchors can be referenced by name, for the Anchors that jsPlumb ships with, or a four element array describing [ x, y, x orientation, y orientation ]. See the anchors section for more detail.
- Connector - the visual representation of the line connecting two elements in the page. jsPlumb has two types of these available as defaults - a Bezier curve, and a straight line. You do not interact with the Connector object.
- Overlay - a UI component that is used to decorate a Connector, such as a label, arrow, etc.
- Connection - an instance of Endpoints and a Connector with zero or more Overlays working together to join two elements. This, and Endpoint, discussed above, are the main objects you will interact with in jsPlumb.
Programmatic API
This section discusses Connections made programmatically.The most simple connection you can make looks like this:
jsPlumb.connect({source:'element1', target:'element2'});
- the Anchors that define where the connection's Endpoints appear on each element
- the type and appearance of each Endpoint in the Connection
- whether or not each Endpoint can be a source or target for new Connections
- the type and appearance of the Connection's Connector
The Basic Examples section below contains many more examples of how to create simple Connections.
Draggable Connections
To support draggable Connections, you are required to first create Endpoints, as it is Endpoints that the user drags to create new Connections. Endpoints are created by making an addEndpoint call, passing in the target element's id and an options object. There are quite a few things that can be set on the options object; all of these parameters are optional.- endpoint - the type of Endpoint, eg. Dot, Rectangle, Image.
- anchor - the Endpoint's Anchor, ie. where it will be located. this may be a string - "TopCenter", for example, or an array of coordinates - [ 0.5, 0, 0, -1 ], or an array of individual anchor specifications, in which case jsPlumb will make a 'dynamic' anchor for the Endpoint.
- paintStyle - the Endpoint's appearance
- isSource - a boolean indicating whether or not the Endpoint can be a source for new Connections. Default is false.
- isTarget - a boolean indicating whether or not the Endpoint can be a target for new Connections. Default is false.
- connector - Optional. The type of Connector to use when dragging a new connection from this Endpoint. see Connectors
- connectorStyle - the Connector's appearance (see above).
- dragOptions - options to pass to the supporting library's drag engine.
- dropOptions - options to pass to the supporting library's drop engine.
- connectorOverlays - a list of overlays that should be present on all Connections attached to this Endpoint.
var endpointOptions = { isSource:true, isTarget:true }; var endpoint = jsPlumb.addEndpoint('elementId', endpointOptions);
Tip: use the three-argument addEndpoint method for common data
One thing that happens quite often is that you have an Endpoint whose appearance and behaviour is largely the same between usages on different elements, with just a few differences.var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectorStyle : { strokeStyle:"#666" }, isTarget:true };
jsPlumb.addEndpoint('element1', { anchor:"BottomCenter" }, exampleGreyEndpointOptions)); jsPlumb.addEndpoint('element2', { anchor:"TopCenter" }, exampleGreyEndpointOptions));
Here's an example of specifying that you want an Arrow overlay halfway along any Connection dragged from this Endpoint:
var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectorStyle : { strokeStyle:"#666" }, isTarget:true, connectorOverlays: [ [ "Arrow", { location:0.5 } ] ] };
var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectorStyle : { strokeStyle:"#666" }, isTarget:true, connectorOverlays: [ [ "Arrow", { location:0.5 } ] ] anchor:[ "TopCenter","RightMiddle","BottomCenter","LeftMiddle" ] };
Drag Options
These are options that will be passed through to the supporting library's drag API. jsPlumb passes everything you supply here through, inserting wrapping functions if necessary for the various lifecycle events that jsPlumb needs to know about. So if, for example, you pass a function to be called when dragging starts, jsPlumb will wrap that function with a function that does what jsPlumb needs to do, then call yours.At the time of writing, jsPlumb supports jQuery, MooTools and YUI3, and each of those libraries uses different terminology. In addition, jQuery's API is more fully featured, providing easy support for setting the zIndex and opacity of elements being dragged, as well as the 'scope' for a drag/drop (allowing you to specify more than one type of drag/drop pair), and hover classes for when a draggable is on the move or over a droppable. If you're using jQuery you can of course just supply these values on the dragOptions; to make it easier, jsPlumb's MooTools and YUI3 adapters recognize these options and add appropriate callbacks for you.
Given that the options here are library-specific, and they are all well-documented, we're going to discuss just the three drag options that behave the same way in both (see below for hoverClass):
- opacity - the opacity of an element that is being dragged. Should be a fraction between 0 and 1 inclusive.
- zIndex - the zIndex of an element that is being dragged.
- scope - the scope of the draggable. can only be dropped on a droppable with the same scope. this is discussed below.
For more information about drag options, take a look at the jQuery, MooTools, or YUI3 docs.
NOTE: there is an issue in Chrome that affects the 'cursor' argument to drag options in jQuery. See these links:
http://forum.jquery.com/topic/draggable-cursor-option-does-not-work
http://forum.jquery.com/topic/chrome-text-select-cursor-on-drag
document.onselectstart = function () { return false; };
Drop Options
Drop options are treated by jsPlumb in the same way as drag options - they are passed through to the underlying library. MooTools does not have drop options like jQuery and YUI3 do; droppable functionality in MooTools is actually implemented by the Drag.Move class - the one used to initialise a draggable. But when you setup an Endpoint in jsPlumb you should ignore that fact, and treat droppables like you would in jQuery or YUI3. jsPlumb wires everything up for you under the hood.There are two jQuery droppable options that jsPlumb treats as shortcuts in MooTools and YUI3, for the sake of consistency:
- hoverClass - the CSS class to attach to a droppable when a draggable is hovering over it.
- scope - the scope of the draggable. The draggable can only be dropped on a droppable with the same scope. This is discussed below.
For more information about drop options when using jQuery, see here.
Scope
jsPlumb borrowed the concept of 'scope' from jQuery's drag/drop implementation: the notion of which draggables can be dropped on which droppables. In jsPlumb you can provide a 'scope' entry when creating an Endpoint. Here's the example grey Endpoint example with 'scope' added:var exampleGreyEndpointOptions = { endpoint:"Rectangle", paintStyle:{ width:25, height:21, fillStyle:"#666" }, isSource:true, connectionStyle : { strokeStyle:"#666" }, isTarget:true, scope:"exampleGreyConnection" };
jsPlumb.getDefaultScope();
jsPlumb.setDefaultScope("mySpecialDefaultScope");
var exampleGreyEndpointOptions = { endpoint:"Rectangle", painStyle:{ width:25, height:21, fillStyle:'#666' }, isSource:true, connectionStyle : { strokeStyle:"#666" }, isTarget:true, dragOptions:{ scope:"dragScope" }, dropOptions:{ scope:"dropScope" } };
Multiple jsPlumb instances
jsPlumb is registered on the browser's Window by default, providing one static instance for the whole page to use. Should you need to, though, you can instantiate independent instances of jsPlumb, using the getInstance method, for example:var firstInstance = jsPlumb.getInstance();
firstInstance.Defaults.Connector = [ "Bezier", 150 ]; firstInstance.Defaults.Container = "container1"; firstInstance.Defaults.Anchors = [ "TopCenter", "BottomCenter" ]; firstInstance.connect({source:'element1', target:'element2', scope:'someScope'});
var secondInstance = jsPlumb.getInstance({ PaintStyle:{lineWidth:3, strokeStyle:color3}, Connector:[ "Bezier", 30 ], Endpoint:[ "Dot", {radius:5} ], EndpointStyle : { fillStyle: color3 }, Anchor : [ 0.5, 0.5, 1, 1 ], Container:"container2" }); secondInstance.connect({source:'element4', target:'element3', scope:'someScope'});
Automatic Repaint
jsPlumb attaches a listener to the browser window and automatically repaints every connection when a window resize event occurs. You can disable this functionality, if you want to, with the following call:jsPlumb.setAutomaticRepaint(false);
var repaint = function() { // do some things, perhaps, and then... jsPlumb.repaintEverything(); }; jsPlumb.setRepaintFunction(repaint);
Another example:
var repaint = function() { // completely start over jsPlumb.detachEverything(); // paint all your connections }; jsPlumb.setRepaintFunction(repaint);
jsPlumb.connect Options
These are the options you can specify on a call to the connect method, to establish a Connection programmatically:
- source
This is a required argument: it identifies the source of the Connection. It may be a string representing an element's id, a selector for an element, or an Endpoint.
- target
This is a required argument: it identifies the target element for the Connection. It may be a string representing an element's id, a selector for an element, or an Endpoint.
- paintStyle
Optional; if not supplied jsPlumb uses the values defined in jsPlumb.Defaults.PaintStyle (see Defaults). This object allows you to specify five attributes of the connector (note that all of these except 'gradient' are just passed through to the Canvas element's paint context):
- strokeStyle - the color used to paint the connector. NOTE: jsPlumb does not yet support patterns.
- lineWidth - the width of the connector in pixels
- lineCap - how the end of the line will be capped.
- miterLimit - the limit on how mitery the miters can miterate.
- gradient - you can specify a set of colors to use as a gradient for the Connector. see Gradients
The arguments to the strokeStyle parameter can be anything that is a valid argument for the strokeStyle parameter of HTML Canvas element, which are CSS colors, patterns or gradients.
This is the working group's page for the Canvas element, where you can find information on painting in Canvas.
Mozilla also has some good documentation here.
- backgroundPaintStyle
Optional; if not supplied jsPlumb uses the values defined in jsPlumb.Defaults.BackgroundPaintStyle (see Defaults), which is, by default, blank.
This object allows you to specify a background for the connector, allowing you to do things such as shadows etc. It takes exactly the same arguments as paintStyle discussed above.
- hoverPaintStyle
Optional; if not supplied jsPlumb uses the values defined in jsPlumb.Defaults.HoverPaintStyle (see Defaults), which is, by default, blank.
This object allows you to specify a paint style for the connector when the mouse is hovering over it. It is only supported in browsers using Canvas. It takes exactly the same arguments as paintStyle discussed above.
- connector
Defines the appearance of the connector. Optional; if not supplied jsPlumb uses a Bezier connector (see Defaults)
Valid values for this are:
- "Straight" or ["Straight", params ] - a straight line directly connecting two anchors
- "Bezier" or ["Bezier", optional_curviness_value ] - a Bezier curve connecting two anchors.
You can also supply your own Connector implementation; for details on how to write a Connector see the Custom Connectors section below.
- anchors
Defines the location on each element to attach the Connection to. Optional; if not supplied jsPlumb uses [ "BottomCenter", "TopCenter" ] (see defaults)
If you supply this, it must be in the form of a list with two elements - the first element is the anchor type for the source element, and the second is the anchor type for the target element.
Valid values for this are either a four element array, one of the default anchors provided by jsPlumb (see the section on anchors for more information:
- "TopCenter"
- "TopRight"
- "RightMiddle"
- "BottomRight"
- "BottomCenter"
- "BottomLeft"
- "LeftMiddle"
- "TopLeft"
- "Center"
- "AutoDefault"
The locations of these are hopefully self-explanatory. These are all just shorthand for the underlying array-based notation, which you can also use, and which gives you greater control over the placement of an Anchor. This declaration, for example:
anchor:"TopLeft"
anchor:[0, 0, -1, -1]
The "AutoDefault" Anchor is a Dynamic Anchor that uses four default positions - TopCenter, RightMiddle, BottomCenter and LeftMiddle.
- dynamicAnchors
Here you can supply a list of anchors that the connection should cycle through as elements move in relation to each other.
Valid values are:
- an array of Anchor definitions:
["TopCenter", "BottomCenter"]
[ [ 0.5, 0, 0, -1], [ 0.5, 1, 0, 1] ]
- an array of Anchor definitions:
- endpoint
Defines the appearance of both Endpoints in the Connection. Optional; if not supplied jsPlumb uses "Dot", with the default size of 10 (see Defaults).
Valid values for this are:
- "Dot" or [ "Dot", params ]
- "Rectangle" or [ "Rectangle", params ]
- "Image" or [ "Image", params ]
- "Triangle" or ["Triangle", params ]
- endpoints
Defines the appearance of each Endpoint separately. Optional; this is similar to endpoint but should be used when you want to specify a different Endpoint for each end of the Connector.
This should be supplied as a list, for example:
endpoints:[ ..endpoint1.. , ..endpoint2.. ]
- endpointStyle
Defines the styles to apply to both Endpoints. Optional; if not supplied jsPlumb uses the values defined in jsPlumb.Defaults.EndpointStyle (see defaults).
This Javascript object allows you to specify the following arguments for the Endpoint:
- fillStyle - the color to fill the endpoint with.
- gradient - you can specify a set of colors to use as a gradient for the Endpoint. see Gradients
- endpointStyles
Defines the styles to apply to each Endpoint. Optional; this is similar to endpointStyle but should be used when you want to specify a different style for each of the two Endpoints.
This should be supplied as a list, for example:
endpointStyles:[ { ..style1.. }, { ..style2.. } ]
- drawEndpoints
This is a boolean value that defaults to true.
- dragOptions
You can provide your own set of dragOptions to pass through to the underlying library's drag API if you need to. jsPlumb will wrap any event methods you provide, since it needs to be aware of drag activity, but everything else is passed through as you specify it. You can do this either on each call you make:
jsPlumb.connect({source:'someWindow', target:"otherWindow", dragOptions:{cursor: 'crosshair'}});
jsPlumb.Defaults.DragOptions = { .. your drag options here. };
-
uuids
Identifies the UUIDs of the source and target Endpoints for this connection. These Endpoints may already exist, having been added with a call to jsPlumb.addEndpoint(..), in which case you do not need to provide information about the source or target elements for the connection, as the Endpoints know. However it is also possible to supply this array when you wish to make a connection and have jsPlumb create the Endpoints for you:
jsPlumb.connect({uuids:["uuid1", "uuid2"], source:"someElement", target:"someOtherElement"});
-
label
Optional label for the connection. This is a shortcut way to add a label as an Overlay, and will place the label in the middle of the connection. If you want finer-grained control you should use the 'overlays' parameter (see below, and in the Overlays section)
label may be either a String::
jsPlumb.connect({label:"Static label", source:"someElement", target:"someOtherElement"});
jsPlumb.connect({ label:function(connection) { return (new Date()).getTime(); }, source:"someElement", target:"someOtherElement" });
-
labelStyle
Optional instructions for how to paint the label, if you supplied one. Possible values for this are:
- font - a font specification that HTML Canvas can understand
- fillStyle - a fillStyle specification that HTML Canvas can understand; paints the background of the label.
- color - color for the label text, in a format that HTML Canvas can understand.
If you do not supply a labelStyle element, jsPlumb will use the values defined in jsPlumb.Defaults.LabelStyle. (see Defaults)
Example:jsPlumb.connect({ label:"Static label", labelStyle: { fillStyle:"rgba(100,100,100,80)", color:"white", font:"12px sans-serif" }, source:"someElement", target:"someOtherElement" });
-
overlays
This optional parameter is a list of objects that implement the Overlay interface, which is discussed in more detail in the Overlays section below. Here's a brief example, though, that shows the same example we just discussed for labels - but in this case there is also an arrow being painted:
jsPlumb.connect({ source:"someElement", target:"someOtherElement", overlays: [ [ "Label", { fillStyle:"rgba(100,100,100,80)", color:"white", font:"12px sans-serif", label:"Static label", borderStyle:"black", borderWidth:2 }], [ "Arrow", { location:0.2 } ] ] });
jsPlumb.addEndpoint Options
The simplest addEndpoint call looks like this:
jsPlumb.addEndpoint(someEndpoint);
jsPlumb.addEndpoint(someEndpoint, { parameters } );
These are the options you can specify on a call to the addEndpoint method, to setup a UI for drag and drop Connections:
- paintStyle
Optional.
- hoverPaintStyle
Optional.
- connectorPaintStyle
Optional.
- connectorHoverStyle
Optional.
- isSource
Optional.
- isTarget
Optional.
jsPlumb.detach Options
Both the jsPlumb object and also individual elements have a method called 'detach'. The jsPlumb.detach method is more versatile and allows you to specify elements, element ids, endpoint uuids or Endpoint objects, whereas the detach method on individual elements can only take the id of some element, or that element's selector. This documentation refers to the jsPlumb.detach method. It has several similarities with the connect method discussed above.
These are the options you can specify on a call to jsPlumb.detach. You don't need to supply them all: jsPlumb needs either [connection], [source,target] or [sourceEndpoint, targetEndpoint] or uuids.
- connection
This is a Connection object such as would have been returned from jsPlumb.connect({...})
- source
Identifies the source element to find and detach connections from. It may be a string representing the element's id, or a selector for the element.
- target
Identifies the target element to find and detach connections from. It may be a string representing the element's id, or a selector for the element.
- sourceEndpoint
Identifies the source Endpoint to find and detach connections from.
- targetEndpoint
Identifies the target Endpoint to find and detach connections from.
- uuids
An array of two Endpoint UUIDs, identifying the source and target Endpoints.
Defaults
The easiest way to set a look and feel for your plumbing is to override the defaults that jsPlumb uses. If you do not do this you are forced to provide your overridden values on every call. Every argument to the connect and addEndpoint methods has an associated default value in jsPlumb.The defaults that ship with jsPlumb are stored in jsPlumb.Defaults, which is a Javascript object. Valid entries, and their initial values, are:
Anchor : null, Anchors : [ null, null ], BackgroundPaintStyle : null, Connector : null, Container : null, DragOptions : {}, DropOptions : {}, Endpoint : null, Endpoints : [ null, null ], EndpointStyle : { fillStyle : null }, EndpointStyles : [ null, null ], EndpointHoverStyle : null, EndpointHoverStyles : [ null, null ], HoverPaintStyle : null, LabelStyle : { fillStyle : "rgba(0,0,0,0)", color : "black" }, LogEnabled : true, MaxConnections : null, PaintStyle : { lineWidth : 10, strokeStyle : 'red' }, Scope : "_jsPlumb_DefaultScope"
Note that in EndpointStyle, the default fillStyle is 'null'. This instructs jsPlumb to use the strokeStyle from the attached connector to fill the endpoint.
Note also that you can specify either or both (or neither) of 'EndpointStyle' and 'EndpointStyles'. This allows you to specify a different end point for each end of a connection. 'Endpoint' and 'Endpoints' use the same concept. jsPlumb will look first in the individual endpoint/endpoint style arrays, and then fall back to the single default version.
you can override these defaults by including this in a script somewhere:jsPlumb.Defaults.PaintStyle = { lineWidth:13, strokeStyle: 'rgba(200,0,0,100)' } jsPlumb.Defaults.DragOptions = { cursor: 'crosshair' }; jsPlumb.Default.Endpoints = [ [ "Dot", 7 ], [ "Dot", 11 ] ]; jsPlumb.Defaults.EndpointStyles = [{ fillStyle:'#225588' }, { fillStyle:'#558822' }];
- connectors are 13 pixels wide and painted with a semi-transparent red line
- when dragging an element the crosshair cursor is used
- the source endpoint is a dot of radius 7; the target endpoint is a dot of radius 11
- the source endpoint is blue; the target endpoint is green
Anchors
An Anchor models the notion of where on an element a Connector should connect. jsPlumb has nine default anchor locations you can use to specify where the Connectors connect to elements: these are the four corners of an element, the center of the element, and the midpoint of each edge of the element. Each of these string representations is just a wrapper around the underlying array-based syntax, for example:
jsPlumb.connect({...., anchor:"BottomCenter", ... });
jsPlumb.connect({...., anchor:[0.5, 1, 0, 1], ... });
Dynamic Anchors
These are Anchors that can be positioned in one of a number of locations, choosing the one that is most appropriate each time something moves or is painted in the UI.There is no special syntax to creating a DynamicAnchor; you just provide an array of individual Anchor specifications, eg:
var dynamicAnchors = [ [ 0.2, 0, 0, -1 ], [ 1, 0.2, 1, 0 ], [ 0.8, 1, 0, 1 ], [ 0, 0.8, -1, 0 ] ];
You can then use that anchor as you would any other. But note that you cannot share dynamic anchors between connections: dynamic anchors maintain state about the connection they belong to, so if you share one amongst two or more connections you will get unexpected results.
Default Dynamic Anchor
jsPlumb provides a 'default' dynamic anchor that chooses from TopCenter, RightMiddle, BottomCenter and LeftMiddle:var aDefaultDynamicAnchor = "AutoDefault";
Location Selection
The initial implementation of the algorithm that decides which location to choose just calculates which location is closest to the center of the other element in the connection. It is possible that future versions of jsPlumb could support more sophisticated choice algorithms, if the need arose.Draggable Connections
Dynamic Anchors and Draggable Connections can interoperate: jsPlumb locks the position of a dynamic anchor when you start to drag a connection from it, and unlocks it once the connection is either established or abandoned. At that point you may see the position of the dynamic anchor change, as jsPlumb optimises the connection.You can see this behaviour in the draggable connections demonstration, when you drag a connection from the blue endpoint on window 1 to the blue endpoint on window 3 - the connection is established and then window 1's blue endpoint jumps down to a location that is closer to window 3.
Connectors
Connectors are the lines that actually join elements of the UI. jsPlumb has three connector implementations - a straight line, a Bezier curve, and "flowchart" connectors. The default connector is the Bezier curve. jsPlumb attaches the CSS class _jsPlumb_connector to the canvas elements that it generates for connectors.
You optionally specify a Connector by setting the 'connector' property on a call to jsPlumb.connect or jsPlumb.addEndpoint(s). If you do not supply a value for 'connector', the default will be used (see defaults).
To specify a Connector there are two syntaxes you can use:
1. Specify the Connector by name. jsPlumb will create a Connector of that type, using its default options
jsPlumb.connect({ ... connector:"Straight", ... });
jsPlumb.connect({ ... connector:[ "Flowchart", { minStubLength:45 } ], ... });
Bezier Connector
The Bezier Connector provides a cubic Bezier path between the two endpoints. It supports a single constructor argument:
curviness - Optional; defaults to 150. This defines the distance in pixels that the Bezier's control points are situated from the anchor points. This does not mean that your connector will pass through a point at this distance from your curve. It is a hint to how you want the curve to travel. Rather than discuss Bezier curves at length here, because they are a complex topic, we refer you to Wikipedia.
Straight Connector
The Straight Connector draws a straight line between the two endpoints. No constructor arguments are supported; use the endpointStyle argument to a connect or addEndpoint call to control the appearance of one of these Connectors.
Flowchart Connector
This type of Connector draws a connection that consists of a series of vertical or horizontal segments - the classic flowchart look. A single constructor argument is supported:
minStubLength - this is the minimum length, in pixels, of the initial stub that emanates from an Endpoint. This parameter is optional, and defaults to 30 pixels.
Overlays
Overlays are UI elements that are painted onto connections, such as labels or arrows. jsPlumb comes with four defaults:
- Arrow - a configurable arrow that is painted at some point along the connector. You can control the length and width of the Arrow, the 'foldback' point - a point the tail points fold back into, and the direction (allowed values are 1 and -1; 1 is the default and means point in the direction of the connection)
- Label - a configurable label that is painted at some point along the connector
- PlainArrow - an Arrow shaped as a triangle, with no foldback.
- Diamond - as the name suggests, a diamond.
You can specify one or more overlays either when making a call to jsPlumb.connect, or when calling jsPlumb.addEndpoint. The two cases are discussed below:
1. Specifying one or more overlays on a jsPlumb.connect call. In this example we'll create an Arrow with the default options for an Arrow, and a label with the text "foo":jsPlumb.connect({ ... overlays:[ "Arrow", [ "Label", { label:"foo", location:0.25 } ] ], ... });
2. Specifying one or more overlays on a jsPlumb.addEndpoint call. Note in this example that we use the parameter 'connectorOverlays' and not 'overlays' as in the last example. This is because 'overlays' will one day be used to support Endpoint Overlays:
jsPlumb.addEndpoint("someDiv", { ... connectorOverlays:[ [ "Arrow", { width:10, length:30, location:1 } ], [ "Label", { label:"foo" } ] ], ... });
Arrow Overlay
This overlay draws an arrow, using four points: the head and two tail points, and a 'foldback' point, which permits the tail of the arrow to be indented. Available constructor arguments for this Overlay are
- width - width of the tail of the arrow
- length - distance from the tail of the arrow to the head
- location - where, as a proportional value from 0 to 1 inclusive, the Arrow should appear on the Connector
- direction - which way to point. Allowed values are 1 (the default, meaning forwards) and -1, meaning backwards
- foldback - how far along the axis of the arrow the tail points foldback in to. Default is 0.623.
- paintStyle - a style object in the form used for paintStyle values for Endpoints and Connectors
PlainArrow Overlay
This is just a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 1, meaning the tail of the Arrow is a flat edge. All of the constructor parameters from Arrow apply for PlainArrow.
Diamond Overlay
This is a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 2, meaning the Arrow turns into a Diamond. All of the constructor parameters from Arrow apply for PlainArrow.
Label Overlay
This provides a text label to decorate Connectors with. The available constructor arguments are:- label - The text to display. You can provide a function here instead of plain text: it is passed the Connection as an argument, and it should return a String.
- labelStyle - Optional arguments for the label's appearance. Valid entries in this JS object are:
- font - a font string in a format suitable for the Canvas element
- fillStyle - the color to fill the label's background with. Optional.
- color - the color of the label's text. Optional.
- padding - optional padding for the label. This is expressed as a proportion of the width of the label, not in pixels or ems.
- borderWidth - optional width in pixels for the label's border. Defaults to 0.
- borderStyle - optional. The color to paint the border, if there is one.
- location - As for Arrow Overlay. Where, proportionally from 0 to 1 inclusive, the label should appear.
Endpoints
An Endpoint is the UI component that marks the location of an Anchor, ie. the place where a Connector joins an
element. jsPlumb comes with three Endpoint implementations - Dot, Rectangle and Image. You optionally specify Endpoint properties using the
endpoint parameter in a call to either jsPlumb.connect or jsPlumb.addEndpoint. The two ways you can specify an Endpoint are similar to that used for Connectors:
1. Specify an Endpoint by name:
jsPlumb.connect({ ... endpoint:"Dot", ... });
jsPlumb.addEndpoint("someDiv", { ... endpoint:[ "Dot", { radius:67 } ], ... });
Dot Endpoint
This draws a dot. Example:
{ ... endpoint: ["Dot", {radius:34} ]; ... }
In the endpointStyle option of a connect call, you can set two values that this will pick up:
- radius - the radius of the dot (in pixels)
- fillStyle - the style to use when filling the dot. If this is blank, jsPlumb will attempt to use the strokeStyle from the associated Connector.
Rectangle Endpoint
Draws a rectangle. Example:
{ ... endpoint:[ "Rectangle", {width:34, height:10} ]; ... }
In the endpointStyle you can set the following for this:
- width - optional, defaults to 20.
- height - optional, defaults to 20.
- fillStyle - the style to use when filling the rectangle. If this is blank, jsPlumb will attempt to use the strokeStyle from the associated Connector.
Image Endpoint
Draws an image from a given URL. Example:
{ ... endpoint:[ "Image", {url:"http://myserver.com/images/endpoint.png"} ]; ... }
Endpoint Operations
These are the methods supported by the Endpoint class:removeConnection
This method is deprecated; it has been renamed to detachdetach
Removes a Connection from the Endpoint. The Connection will have been returned from a call to jsPlumb.connect. Detaching the Connection has the effect of removing the Connection from the display.var c = myEndpoint.addConnection({...}); ... time passes ... myEndpoint.detach(c);
detachAll
Removes all Connections from the Endpoint (and from the display).var c = myEndpoint.addConnection({...}); var c2 = myEndpoint.addConnection({...}); ... time passes ... myEndpoint.detachAll();
detachFrom
Detaches all Connections from this Endpoint to some other Endpoint (from the display too).var c = myEndpoint.addConnection({...}); var c2 = myOtherEndpoint.addConnection({...}); jsPlumb.connect({sourceEndpoint:c, targetEndpoint:c2}); ... time passes ... myEndpoint.detachFrom(myOtherEndpoint);
-
getElement
Returns the DOM element the Endpoint is attached to. -
isConnectedTo
Returns whether or not this Endpoint is connected to some other Endpoint.var c = myEndpoint.addConnection({...}); var c2 = myOtherEndpoint.addConnection({...}); jsPlumb.connect({sourceEndpoint:c, targetEndpoint:c2}); var isConnected = c.isConnectedTo(c2); // returns true
-
isFull
Returns whether or not the Endpoint can accept any more Connections. This is determined by the parameters passed in to the addEndpoint call, specifically the 'maxConnections' value: by default this is set to 1. You can provide any positive value for this, or -1 if you wish to have no maximum. -
getUuid
Returns the Endpoint's UUID, if one was provided when the Endpoint was created. See also jsPlumb.getEndpoint(uuid) and jsPlumb.connect. -
setDragAllowedWhenFull
Sets whether or not connections can be dragged from the Endpoint once it is full. Use this in conjunction with the 'reattach' option on a connect call - if this is true, you can specify whether or not dropped connections reattach or are removed.
Endpoint UUIDs
Each Endpoint can have a UUID associated with it; these can be used to establish Connections and also to retrieve Endpoints from jsPlumb. To assign a UUID to an Endpoint you can do one of two things:-
Nominate the UUID in a call to jsPlumb.addEndpoint (when creating an Endpoint you will use later):
jsPlumb.addEndpoint("someElement", {uuid:"abcdefg"});
- Nominate two UUIDs in a call to jsPlumb.connect (Endpoints will be created and have these UUIDs assigned):
jsPlumb.connect({uuids:["abcdefg", "hijklmn"], source:"someElement", target:"someOtherElement"});
var e = jsPlumb.getEndpoint("abcdefg");
jsPlumb.connect({uuids:["abcdefg", "hijklmn"});
Gradients
The Canvas element supports gradients, and jsPlumb can take advantage of this when painting your Connectors and/or Endpoints. Note: this does NOT WORK in IE, because we use ExplorerCanvas in IE and ExplorerCanvas does not support gradients.There are two types of gradients available in Canvas - a 'linear' gradient, which consists of colored lines all going in one direction, and a 'radial' gradient, which consists of colored circles emanating from one circle to another. Because of their basic shape, jsPlumb supports only linear gradients for Connectors. But for Endpoints, jsPlumb supports both linear and radial gradients.
Connector gradients
To specify a linear gradient to use in a Connector, you must add a gradient object to your Connector's paintStyle, for instance:jsPlumb.connect({ source : "window2", target : "window3", paintStyle:{ gradient:{ stops:[[0,"green"], [1,"red"]] }, lineWidth:15 } });
Notice the gradient object and the stops list inside it - the gradient consists of an arbitrary number of these "color stops". Each color stop is comprised of two values - [position, color]. Position must be a decimal value between 0 and 1 (inclusive), and indicates where the color stop is situated as a fraction of the length of the entire gradient. Valid values for the colors in the stops list are the same as those that are valid for strokeStyle when describing a color.
As mentioned, the stops list can hold an arbitrary number of entries. Here's an example of a gradient that goes from red to blue to green, and back again through blue to red:
jsPlumb.connect({ source : 'window2', target : 'window3', paintStyle : { gradient:{ stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']] }, lineWidth : 15 } });
jsPlumb.connect({ source : 'window2', target : 'window3', paintStyle:{ strokeStyle:'red', gradient:{ stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']] }, lineWidth:15 } });
Endpoint gradients
Endpoint gradients are specified using the same syntax as Connector gradients. You put the gradient specifier either in the endpoint member, or if you are specifying different Endpoints for each end of the Connector, in one or both of the values in the endpoints array. Also, this information applies to the case that you are creating standalone Endpoints that you will be configuring for drag and drop creation of new Connections.This is an example of an Endpoint gradient that is different for each Endpoint in the Connector. This comes from the main demo; it is the Connector joining Window 2 to Window 3:
var w23Stroke = 'rgb(189,11,11)'; jsPlumb.connect({ source : 'window2', target : 'window3', paintStyle:{ lineWidth:8, strokeStyle:w23Stroke }, anchors:[ [0.3,1,0,1], "TopCenter" ], endpoint:"Rectangle", endpointStyles:[ { gradient : {stops:[[0, w23Stroke], [1, '#558822']] } }, { gradient : {stops:[[0, w23Stroke], [1, '#882255']] } } ] });
Applying the gradient in Endpoints
Only the Dot and Rectangle endpoints honour the presence of a gradient (and, remember, not in IE). The Image endpoint of course ignores a gradient as it does no painting of its own.The type of gradient you will see depends on the Endpoint type:
- Dot - renders a radial endpoint, with color stop 0 on the outside, progressing inwards as we move through color stops.
Radial gradients actually require more data than linear gradients - in a linear gradient we just move from one point to another, whereas in a radial gradient we move from one circle to another. By default, jsPlumb will render a radial gradient using a source circle of the same radius as the Endpoint itself, and a target circle of 1/3 of the radius of the Endpoint (both circles share the same center as the Endpoint itself). This circle will be offset by radius/2 in each direction.
You can supply your own values for these inside the gradient descriptor:
var w34Stroke = 'rgba(50, 50, 200, 1)'; var w34HlStroke = 'rgba(180, 180, 200, 1)'; jsPlumb.connect({ source : 'window3', target : 'window4', paintStyle:{ lineWidth:10, strokeStyle:w34Stroke }, anchors:[ "RightMiddle", "LeftMiddle" ], endpointStyle:{ gradient : { stops:[ [0, w34Stroke], [1, w34HlStroke] ], offset:37.5, innerRadius:40 }, radius:55 } });
It is also possible to specify the offset and inner radius as percentages - enter the values as strings with a '%' symbol on the end:
var w34Stroke = 'rgba(50, 50, 200, 1)'; var w34HlStroke = 'rgba(180, 180, 200, 1)'; jsPlumb.connect({ source : 'window3', target : 'window4', paintStyle:{ lineWidth:10, strokeStyle:w34Stroke }, anchors:[ "RightMiddle", "LeftMiddle" ], endpointStyle:{ gradient : { stops:[ [0, w34Stroke], [1, w34HlStroke] ], offset:'68%', innerRadius:'73%' }, radius:25 } });
- Rectangle - renders a linear endpoint, with color stop 0 closest to the end of the Connector
Animation
jsPlumb offers an 'animate' function, which wraps the underlying animation engine for whichever library you happen to be using and inserts a callback for jsPlumb to repaint whatever it needs to at each step. You could of course do this yourself; it's a convenience method really.The method signature is:
jsPlumb.animate : function(el, properties, options)
- el - element id, or element object from the library you're using.
- properties - properties for the animation, such as duration etc.
- options - options for the animation, such as callbacks etc.
Events
jsPlumb fires an event to registered listeners whenever a connection is made (we plan to support more events in the future).Registering Event Listeners
To register an event listener you need to supply the event name and a listener object:jsPlumb.bind("jsPlumbConnection", someListener);
The listener object - in this case 'someListener' - must have a method with the same name as the event. For example, to define a listener inline, you could do a call like this:
jsPlumb.bind("jsPlumbConnection", function(data) { console.log("a connection was made from " + data.sourceId + " to " + data.targetId); }); jsPlumb.bind("jsPlumbConnectionDetached", function(data) { console.log("a connection was detached from " + data.sourceId + " and " + data.targetId); });
Event Types
event name | function arguments | explanation |
---|---|---|
jsPlumbConnection | params - JS object containing sourceId, targetId, source, target, sourceEndpoint, targetEndpoint | fired whenever a new connection is made, either programmatically via jsPlumb.connect, or drag and drop |
jsPlumbConnectionDetached | params - JS object containing sourceId, targetId, source, target, sourceEndpoint, targetEndpoint | fired whenever a connection is detached, either programmatically via one of the jsPlumb.detach methods, or drag and drop |
CSS Class Reference
jsPlumb attaches classes to each of the UI components it creates:component | css class |
---|---|
connector | _jsPlumb_connector |
endpoint | _jsPlumb_endpoint |
jsPlumb.connect Examples
This section provides examples of how to use the programmatic API to establish Connections.The basic syntax of a call is that you execute 'connect', providing a source and a target, and optionally a paintStyle and preferences for where you want the plumbing to be anchored on each element, as well as the type of connector to use.
- Connect window1 to window2 with the default settings:
jsPlumb.connect({source:"window1", target:"window2"});
- Connect window1 to window2 with a 15 pixel wide yellow Connector, and a slightly brighter endpoint (remember the default Endpoint is a Dot):
jsPlumb.connect({ source:'window1', target:'window2', paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'}, endpointStyle:{fillStyle:'rgb(243,229,0)'} });
- Connect window1 to window2 with a 15 pixel wide yellow Connector, and a slightly brighter endpoint:
jsPlumb.connect({ source:'window1', target:'window2', paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'}, endpointStyle:{fillStyle:'rgb(243,229,0)'} });
- Connect window3 to 'window4' with a 10 pixel wide, semi opaque blue Connector, anchored to the left middle of window3, and the right middle of window4, with a Rectangle endpoint of width 10 and height 8:
jsPlumb.connect({ source:'window3', target:'window4', paintStyle:{ lineWidth:10, strokeStyle:'rgba(0, 0, 200, 0.5)' }, anchors:["RightMiddle", "LeftMiddle"], endpoint:[ "Rectangle", { width:10, height:8 } ] });
- Connect window2 to window3 with a default Connector from the top center of window2 to the bottom center of window3, and rectangular endpoints:
jsPlumb.connect({ source:'window2', target:'window3', paintStyle:{lineWidth:8, strokeStyle:'rgb(189,11,11)'}, anchors:["BottomCenter", "TopCenter"], endpoint:"Rectangle" });
- Connect window1 to window2 with a 15 px wide yellow Bezier. endpoints are a slightly lighter shade of yellow.
jsPlumb.connect({ source:'window1', target:'window2', anchors:["BottomCenter", [0.75,0,0,-1]], paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'}, endpointStyle:{fillStyle:'rgb(243,229,0)'} });
- Connect window3 to window4 with a 10px wide blue-ish half transparent Bezier. put endpoints underneath the element they attach to.
the endpoints have a radial gradient. both ways of specifying gradient positioning are shown here.
var w34Stroke = 'rgba(50, 50, 200, 1)'; var w34HlStroke = 'rgba(180, 180, 200, 1)'; jsPlumb.connect( { source:'window3', target:'window4', paintStyle:{lineWidth:10, strokeStyle:w34Stroke}, anchors:["RightMiddle", "LeftMiddle"], endpointStyle:{ gradient : {stops:[[0, w34Stroke], [1, w34HlStroke]], offset:17.5, innerRadius:15 }, radius:35}, //endpointStyle:{ gradient : {stops:[[0, w34Stroke], [1, w34HlStroke]], offset:'78%', innerRadius:'73%'}, radius:35 }, endpointsOnTop:false } );
- Connect window2 to window3 with an 8px red Bezier and default rectangular endpoints. see also how the first anchor is
specified here - this is how you create anchors in locations jsPlumb does not offer shortcuts for.
the endpoints in this example have linear gradients applied.
var w23Stroke = 'rgb(189,11,11)'; jsPlumb.connect({ source:'window2', target:'window3', paintStyle:{lineWidth:8,strokeStyle:w23Stroke}, anchors:[[0.3,1,0,1], "TopCenter"], endpoint:"Rectangle", endpointStyles:[{ gradient : {stops:[[0, w23Stroke], [1, '#558822']] }}, { gradient : {stops:[[0, w23Stroke], [1, '#882255']] }}] });
- Connect window5 to window6 from center to center, 5px wide line that is green and half transparent. the endpoints are
125px in radius and spill out from underneath their elements.
jsPlumb.connect({ source:'window5', target:'window6', anchors:["Center", "Center"], paintStyle:{lineWidth:5,strokeStyle:'rgba(0,255,0,0.5)'}, endpointStyle:{radius:125} });
- Connect window4 to window5 from bottom right to top left, with a 7px straight line purple connector, and an image as the endpoint,
placed on top of the element it is connected to.
jsPlumb.connect({ source:'window4', target:'window5', anchors:["BottomRight","TopLeft"], paintStyle:{lineWidth:7,strokeStyle:'rgb(131,8,135)'}, endpoint:[ "Image", {url:"http://morrisonpitt.com/jsPlumb/img/endpointTest1.png"} ], connector:"Straight" });
- Connect window5 to window6 between their center points with a semi-opaque connector, and 125px endpoints:
jsPlumb.connect({ source:'window5', target:'window6', anchors:["Center", "Center"], paintStyle:{lineWidth:5,strokeStyle:'rgba(0,255,0,0.5)'}, endpointStyle:{radius:125} });
- Connect window7 to window8 with a 10 pixel wide blue Connector, anchored on the top left of window7 and the bottom right of window8:
jsPlumb.connect({ source:'window7', target:'window8', paintStyle:{lineWidth:10, strokeStyle:'blue'}, anchors:["TopLeft", "BottomRight"] });
- Connect the bottom right corner of window4 to the top left corner of window5, with rectangular endpoints of size 40x40:
jsPlumb.connect({ source:'window4', target:'window5', anchors:["BottomRight","TopLeft"], paintStyle:{lineWidth:7,strokeStyle:'rgb(131,8,135)'}, endpointStyle:{width:40, height:40}, endpoint:"Rectangle", connector:"Straight" });
- Connect window1 to window2 with the default paint settings but provide some drag options (which are passed through to the underlying library's draggable call):
jsPlumb.connect({source:'window1', target:'window2', dragOptions:{cursor:'crosshair'}});
Utility Functions
jsPlumb.detachAll("window5");
jsPlumb.hide("window5");
jsPlumb.show("window5");
jsPlumb.toggle("window5");
jsPlumb.repaint("window5");
jsPlumb.repaint( [ "window5", "window6", "window11" ] );
jsPlumb.repaintEverything();
jsPlumb.detachEverything();
jsPlumb.removeEndpoint("window1", someEndpoint);
jsPlumb.removeAllEndpoints("window1");
jsPlumb.removeEveryEndpoint();
jsPlumb.deleteEndpoint(endpoint);
jsPlumb.reset();
jsPlumb.setDraggable("window1", false);
jsPlumb.setDraggable(["window1","window2"], false);
jsPlumb.setDraggableByDefault(false);
jsPlumb.draggable("window1");
jsPlumb.draggable(["window1","window2"]);
jsPlumb.draggable("window1");
jsPlumb.draggable($(".window"));
jsPlumb.draggable($$(".window"));
jsPlumb.draggable(Y.all(".window"));
jsPlumb.draggable($("#window1"));
jsPlumb.draggable($("window1"));
jsPlumb.draggable(Y.one("window1"));
Draggable Connections Examples
This is a list of examples of how to use jsPlumb to create Connections using drag and drop.The basic procedure is:
- Create Endpoints and register them on elements in your UI
- Drag and Drop
- Define an Endpoint with default appearance, that is both a source and target of new Connections:
var endpointOptions = { isSource:true, isTarget:true };
- Register that Endpoint on window3, specifying that it should be located in the top center of the element:
var window3Endpoint = jsPlumb.addEndpoint('window3', { anchor:"TopCenter" }, endpointOptions );
- Now register that Endpoint on window4, specifying that it should be located in the bottom center of the element:
var window4Endpoint = jsPlumb.addEndpoint('window4', { anchor:"BottomCenter" }, endpointOptions );
- Connect window3 to window4 with a 25px wide yellow Bezier that has a 'curviness' of 175:
jsPlumb.connect({ source:window3Endpoint, target:window4Endpoint, connector: [ "Bezier", 175 ], paintStyle:{ lineWidth:25, strokeStyle:'yellow' } });
- Define an Endpoint that creates Connections that are 20px wide straight lines, that is both a source and target of new Connections,
and that has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can
no longer be dragged from it (even if 'reattach' is specified on a Connection):
var endpointOptions = { isSource:true, isTarget:true, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dragAllowedWhenFull:false };
- Define an Endpoint that will be anchored to "TopCenter". It creates Connections that are 20px wide straight lines, that is both a source and target of new Connections,
and that has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can
no longer be dragged from it (even if 'reattach' is specified on a Connection):
var endpointOptions = { anchor:"TopCenter", isSource:true, isTarget:true, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dragAllowedWhenFull:false };
- Define an Endpoint that will create a dynamic anchor which can be positioned at "TopCenter" or "BottomCenter". It creates Connections that are 20px wide straight lines, it is both a source and target of new Connections,
and it has a 'scope' of 'blueline'. Also, this Endpoint mandates that once it is full, Connections can
no longer be dragged from it (even if 'reattach' is specified on a Connection):
var endpointOptions = { anchor:[ "TopCenter", "BottomCenter" ], isSource:true, isTarget:true, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dragAllowedWhenFull:false };
- Exactly the same as before, but shows how you can use "anchors" instead of "anchor", if that makes you feel happier:
var endpointOptions = { anchors:[ "TopCenter", "BottomCenter" ], isSource:true, isTarget:true, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dragAllowedWhenFull:false };
- Define an Endpoint that is a 30px blue dot, creates Connections that are 20px wide straight lines, is both a source and target of new Connections,
has a 'scope' of 'blueline', and has an event handler that pops up an alert (note: the event handler name means this example is jQuery - MooTools
and YUI3 use different event handler names):
var endpointOptions = { isSource:true, isTarget:true, endpoint: [ "Dot", { radius:30 } ], style:{fillStyle:'blue'}, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dropOptions:{ drop:function(e, ui) { alert('drop!'); } } };
- Same example as before, but this is for MooTools, and the Endpoint can support up to 5 connections (the default is 1):
var endpointOptions = { isSource:true, isTarget:true, endpoint: [ "Dot", { radius:30 } ], style:{ fillStyle:'blue' }, maxConnections:5, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dropOptions:{ onDrop:function(e, ui) { alert('drop!'); } } };
- Same example again, but maxConnections being set to -1 means that the Endpoint has no maximum limit of Connections:
var endpointOptions = { isSource:true, isTarget:true, endpoint: [ "Dot", {radius:30} ], style:{ fillStyle:'blue' }, maxConnections:-1, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dropOptions:{ onDrop:function(e, ui) { alert('drop!'); } } };
- Same example again, but for YUI3. Note the drop callback is "drop:hit":
var endpointOptions = { isSource:true, isTarget:true, endpoint: [ "Dot", { radius:30 } ], style:{fillStyle:'blue'}, maxConnections:-1, connector : "Straight", connectorStyle: { lineWidth:20, strokeStyle:'blue' }, scope:"blueline", dropOptions:{ "drop:hit":function(e, ui) { alert('drop!'); } } };
- Assign a UUID to the endpoint options created above, and add as Endpoints to "window1" and "window2":
jsPlumb.addEndpoint("window1", { uuid:"abcdefg" }, endpointOptions ); jsPlumb.addEndpoint("window2", { uuid:"hijklmn" }, endpointOptions );
- Connect the two Endpoints we just registered on "window1" and "window2":
jsPlumb.connect({uuids:["abcdefg", "hijklmn"]});
Retrieving Connection Information
jsPlumb offers one fairly versatile method - getConnections - to retrieve information about the currently managed connections.Before you use this method you should understand jsPlumb's notion of 'scope' - documentation is here
getConnections optionally takes an object specifying filter parameters, of which there are three:
- scope - the scope of the connection type(s) you wish to retrieve
- source - limits the returned connections to those that have this source id
- target - limits the returned connections to those that have this target id
The return value of a call to getConnection is a dictionary whose keys are scope names, and whose values are lists of sourceId/targetId pairs, for example:
{ "_jsPlumb_DefaultScope" : [ {sourceId:"window1", targetId:"window2", source:<sourceElement>, target:<targetElement>, sourceEndpoint:<sourceEndpoint>, targetEndpoint:<targetEndpoint>, connection:<connection> }, {sourceId:"window5", targetId:"window3", source:<sourceElement>, target:<targetElement>, sourceEndpoint:<sourceEndpoint>, targetEndpoint:<targetEndpoint>, connection:<connection> } ], "someCustomScope": [ {sourceId:"window6", targetId:"window2", source:<sourceElement>, target:<targetElement>, sourceEndpoint:<sourceEndpoint>, targetEndpoint:<targetEndpoint>, connection:<connection> }, {sourceId:"window4", targetId:"window13", source:<sourceElement>, target:<targetElement>, sourceEndpoint:<sourceEndpoint>, targetEndpoint:<targetEndpoint>, connection:<connection> }, {sourceId:"window2", targetId:"window10", source:<sourceElement>, target:<targetElement>, sourceEndpoint:<sourceEndpoint>, targetEndpoint:<targetEndpoint>, connection:<connection> } ] }
- Get all connections:
var c = jsPlumb.getConnections();
- Get all connections for the default scope only:
var c = jsPlumb.getConnections(jsPlumb.getDefaultScope());
- Get all connections for the given scope:
var c = jsPlumb.getConnections({scope:"myTestScope"});
- Get all connections for the given scopes:
var c = jsPlumb.getConnections({scope:["myTestScope", "yourTestScope"]});
- Get all connections for the given source:
var c = jsPlumb.getConnections({source:"mySourceElement"});
- Get all connections for the given sources:
var c = jsPlumb.getConnections({source:["mySourceElement", "yourSourceElement"]});
- Get all connections for the given target:
var c = jsPlumb.getConnections({target:"myTargetElement"});
- Get all connections for the given source and targets:
var c = jsPlumb.getConnections({source:"mySourceElement", target:["target1", "target2"]});
- Get all connections for the given scope, with the given source and target:
var c = jsPlumb.getConnections({scope:'myScope", source:"mySourceElement", target:"myTargetElement"});
var c = jsPlumb.getConnections({scope:"myScope", source:"mySourceElement"}); var conns = c["myScope"];
Advanced Topics
Which files are which?
In development, jsPlumb is broken up into four main scripts:- jsPlumb-X.X.js
This is the main jsPlumb engine. It calls out to the underlying library implementation, and it has no Anchors, Endpoints or Connections specified.
- jsPlumb-defaults-x.x.js
This contains the default Anchor, Endpoints and Connections implementations
- <LIBRARY_PREFIX>.jsPlumb-X.X.js
This contains library-specific helper methods. jsPlumb ships with three of these - one each for jQuery, MooTools and YUI3. See below for information on how to create a new library implementation.
- <jsBezier-0.2-min.js>
These are the Bezier curve functions; they are maintained in a separate project called jsBezier
- jquery.jsPlumb-1.2.6-all.js
Contains jsPlumb-1.2.6.js, jsPlumb-defaults-1.2.6.js, jquery.jsPlumb-1.2.6.js and jsBezier-0.2-min.js
- jquery.jsPlumb-1.2.6-all-min.js
A minified version of the script above (minified using the YUI Compressor)
Pluggable Library Support
Out of the box, jsPlumb can be run on top of jQuery, MooTools or YUI3. This is achieved by delegating several core methods - tasks such as finding an element by id, finding an element's position or dimensions, initialising a draggable, etc - to the library in question.To develop one of these, your test page should include the first two scripts discussed above, and then your own script containing your library specific functionality. The two existing implementations should be documented well enough for you to create your own. If you do this, it would be great to share it with everyone...
Custom Connectors
You can provide your own connectors if you need to. A Connector consists of two functions, which work as a pair. First a call is made to the compute function:this.compute = function(sourcePos, targetPos, sourceAnchor, targetAnchor, lineWidth) { ... return dimensions; }
The next four elements must be the coordinates of the two endpoints of the line you are going to draw.
The remainder of the items in the returned list are arbitrary, and will vary between Connector implementations; this list is passed in to a Connector's paint function, so each implementation will put into the list whatever it needs to paint itself. For instance, the straight line connector only needs the [x,y] location of each end of the line it will paint, and that is one of the required entries, so it does not have to do anything extra, whereas the Bezier connector adds the location of the two control points. Other types of Connectors will do whatever is appropriate for their particular situation.
This is the method signature for the paint function:this.paint = function(dimensions, ctx) { .. }
jsPlumb.connect({source:'someWindow', target:'otherWindow', connector:new jsPlumb.Connectors.Straight()});
jsPlumb.connect({source:'someWindow', target:'otherWindow', connector:"Straight"});
The section below this discusses Overlays, which allow you to decorate Connectors with things such as labels or arrows or whatever else you like. Overlays can only work with Connectors that implement a few helper functions.
The concept of location
Before discussing the helper functions you first must be across the concept of 'location' as used by this mechanism. The location on a connector can be a decimal value between 0 and 1 inclusive, which marks a proportionate amount of travel along the path inscribed by the Connector. For a straight line connector this is a simple piece of maths, but for Bezier connectors it's a little bit more involved.Required Helper Methods
- pointOnPath(location) - returns an [x,y] point corresponding to the given location
- pointAlongPathFrom(location, distance) - returns an [x,y] point corresponding to travelling 'distance' pixels along the connector from 'location'.
- gradientAtPoint(location) - returns the connector's gradient at the given location. For linear connectors such as Straight this is constant, but for Bezier connectors the gradient changes continually.
- perpendicularToPathAt(location, distance, length) - returns a line that is perpendicular to (and centered on) the connector at 'distance' from the given location, with length 'length'.
Custom Endpoints
To create your own Endpoint implementation, you need to implement a single method:
paint : function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) { ... }
- anchorPoint - [x,y] location of the anchor point on screen
- orientation - [x,y] hints for the general direction the anchor points to
- canvas - the canvas to draw into
- endpointStyle - Javascript object containing style directives as discussed above. The contents of this are arbitrary, so if you write a new Endpoint that needs some extra settings, you can add them no hassle.
- connectorPaintStyle - the style being used to paint the associated Connector.
It is your responsibility to size and locate the canvas to suit your needs. jsPlumb provides the following helper method to assist you:
jsPlumb.sizeCanvas(canvas, x, y, width, height);
Custom Overlays
Overlays can only work with Connectors that implement the methods pointOnPath, pointAlongPathFrom, gradientAtPoint, and perpendicularToPathAt. These are discussed in more detail in the Custom Connectors section above. Both of the Connectors that come with jsPlumb - Straight and Bezier -implement these methods; if you write a custom connector, or have written a custom connector, you will need to supply them.Overlay Interface
An Overlay is required to implement two methods in order to be usable by jsPlumb:- computeMaxSize(connector, context) - returns an integer value indicating the larger of this overlay's width and height. It is used by jsPlumb to ensure that the canvas is large enough to accomodate the overlay. The examples below should help to clarify this.
- draw(connector, ctx) - draws the overlay. What happens in this method is up to the given implementation.
computeMaxSize methods
This is the Arrow overlay's computeMaxSize method:this.computeMaxSize = function() { return width; }
this.computeMaxSize = function(connector, ctx) { if (labelText) { ctx.save(); if (self.labelStyle.font) ctx.font = self.labelStyle.font; var t = ctx.measureText(labelText).width; // a fake text height measurement: use the width of upper case M var h = ctx.measureText("M").width; labelPadding = self.labelStyle.padding || 0.25; labelWidth = t + (2 * t * labelPadding); labelHeight = h + (2 * h * labelPadding); ctx.restore(); return Math.max(labelWidth, labelHeight); } return 0; };
draw methods
To give you a taste for how you can interact with a connector, consider the first few lines of the Arrow overlay's draw method:this.draw = function(connector, ctx) { // this is the arrow head position var hxy = connector.pointAlongPathFrom(self.loc, length / 2); // this is the center of the tail var txy = connector.pointAlongPathFrom(self.loc, -length / 2), tx = txy[0], ty = txy[1]; // this is the tail vector var tail = connector.perpendicularToPathAt(self.loc, -length / 2, width); ...
The first two calls to the connector use the pointAlongPathFrom method, which passes self.loc as the location of the point on the path, and length / 2 as the number of pixels along the path to traverse.
self.loc is Arrow's internal member describing the location of the overlay, and it is a decimal (between 0 and 1 inclusive) that points to a location at some distance along the path inscribed by the connector. So these first two calls get us [x,y] locations of points on the connector that mark the head and tail points for the arrow.
The connector.perpendicularToPathAt(self.loc, -length / 2, width) call returns a line description of a line that is perpendicular to, and centered on, the connector at "-length / 2" pixels from the given point, with the given width (width is an internal member of Arrow). At this point, the Arrow object has the three main points it needs in order to draw itself.