It's written as a jQuery plugin, and relies on jQuery 1.3.x or jQuery 1.4.x (tested on 1.3.2 and 1.4.2), and also jQuery UI 1.7.2 or 1.8.0 (if you want to support dragging). For Canvas support in IE you also need to include Google's ExplorerCanvas script.
jsPlumb has been tested on the following browsers:
<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.4.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script> <script type="text/javascript" src="PATH_TO/jquery.jsPlumb-1.0.4-min.js "></script>
<body onunload="jsPlumb.unload();"> ... </body>
$("#window1").plumb({target:'window2'});
$("#window1").plumb({
target:'window2',
paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'},
endpointStyle:{fillStyle:'rgb(243,229,0)'}
});
$("#window3").plumb({
target:'window4',
paintStyle:{lineWidth:10, strokeStyle:'rgba(0, 0, 200, 0.5)'},
anchors:[jsPlumb.Anchors.RightMiddle, jsPlumb.Anchors.LeftMiddle],
endpointStyle:{radius:25}
});
$("#window2").plumb({
target:'window3',
paintStyle:{lineWidth:8, strokeStyle:'rgb(189,11,11)'},
anchors:[jsPlumb.Anchors.BottomCenter, jsPlumb.Anchors.TopCenter],
endpointType:new jsPlumb.Endpoints.Rectangle()
});
$("#window1").plumb({
target:'window2',
anchors:[jsPlumb.Anchors.BottomCenter, jsPlumb.makeAnchor(0.75,0,0,-1)],
paintStyle:{lineWidth:15,strokeStyle:'rgb(243,230,18)'},
endpointStyle:{fillStyle:'rgb(243,229,0)'}
});
var w34Stroke = 'rgba(50, 50, 200, 1)';
var w34HlStroke = 'rgba(180, 180, 200, 1)';
$("#window3").plumb(
{target:'window4',
paintStyle:{lineWidth:10, strokeStyle:w34Stroke},
anchors:[jsPlumb.Anchors.RightMiddle, jsPlumb.Anchors.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
}
);
var w23Stroke = 'rgb(189,11,11)';
$("#window2").plumb({
target:'window3',
paintStyle:{lineWidth:8,strokeStyle:w23Stroke},
anchors:[jsPlumb.makeAnchor(0.3,1,0,1), jsPlumb.Anchors.TopCenter],
endpoint:new jsPlumb.Endpoints.Rectangle(),
endpointStyles:[{ gradient : {stops:[[0, w23Stroke], [1, '#558822']] }},
{ gradient : {stops:[[0, w23Stroke], [1, '#882255']] }}]
});
$("#window5").plumb({
target:'window6',
anchors:[jsPlumb.Anchors.Center, jsPlumb.Anchors.Center],
paintStyle:{lineWidth:5,strokeStyle:'rgba(0,255,0,0.5)'},
endpointsOnTop:false,
endpointStyle:{radius:125}
});
$("#window4").plumb({
target:'window5',
anchors:[jsPlumb.Anchors.BottomRight,jsPlumb.Anchors.TopLeft],
paintStyle:{lineWidth:7,strokeStyle:'rgb(131,8,135)'},
endpoint:new jsPlumb.Endpoints.Image({url:"http://morrisonpitt.com/jsPlumb/img/endpointTest1.png"}),
connector:new jsPlumb.Connectors.Straight(),
endpointsOnTop:true
});
$("#window5").plumb({
target:'window6',
anchors:[jsPlumb.Anchors.Center, jsPlumb.Anchors.Center],
paintStyle:{lineWidth:5,strokeStyle:'rgba(0,255,0,0.5)'},
endpointsOnTop:false,
endpointStyle:{radius:125}
});
$("#window7").plumb({
target:'window8',
paintStyle:{lineWidth:10, strokeStyle:'blue'},
anchors:[jsPlumb.Anchors.TopLeft, jsPlumb.Anchors.BottomRight]
});
$("#window4").plumb({
target:'window5',
anchors:[jsPlumb.Anchors.BottomRight,jsPlumb.Anchors.TopLeft],
paintStyle:{lineWidth:7,strokeStyle:'rgb(131,8,135)'},
endpointsOnTop:false,
endpointStyle:{width:40, height:40},
endpointType:new jsPlumb.Endpoints.Rectangle(),
connector:new jsPlumb.Connectors.Straight()
});
$("#window1").plumb({target:'window2', dragOptions:{cursor:'crosshair'}});
$("#window2").detach("window1");
$("#window2").detach(["window1", "window3"]);
$("#window5").detachAll();
jsPlumb.hide("window5");
jsPlumb.show("window5");
jsPlumb.toggle("window5");
jsPlumb.repaint("window5");
jsPlumb.repaint( [ "window5", "window6", "window11" ] );
jsPlumb.repaintEverything();
jsPlumb.detachEverything();
jsPlumb.setDraggable("window1", false);
jsPlumb.setDraggable(["window1","window2"], false);
jsPlumb.setDraggableByDefault(false);
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);
These are the options you can specify on a call to the plumb method:
This is a required argument. It identifies the target element for the plumb.
Optional; if not supplied jsPlumb uses the values defined in jsPlumb.DEFAULT_PAINT_STYLE (see defaults). This object allows you to specify five attributes of the connector:
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
Optional; if not supplied jsPlumb uses a Bezier connector (see defaults)
Valid values for this are:
You can also supply your own Connector implementation; for details on how to write a Connector see the Connectors section below.
Optional; if not supplied jsPlumb uses [ jsPlumb.Anchors.BottomCenter, jsPlumb.Anchors.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 (the one on which you are calling the 'plumb' method), and the second is the anchor type for the target element.
Valid values for this are any Anchor you have created yourself (see the anchors section below), or one of these default anchors provided by jsPlumb:
The locations of these are hopefully self-explanatory. You can supply your own Anchor implementations if you need to - see the section on Anchors
Optional; if not supplied jsPlumb uses jsPlumb.Endpoints.Dot, with the default size of 10 (see defaults).
Valid values for this are:
Similar to Connectors and Anchors, you can provide your own Endpoint implementation; see the Endpoints section.
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.. } ]
Optional; if not supplied jsPlumb uses the values defined in jsPlumb.DEFAULT_ENDPOINT_STYLE (see defaults).
This Javascript object allows you to specify the following arguments for the 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.. } ]
This is a boolean value that defaults to true. When true, it forces jsPlumb to set the z-index of endpoints to be 1 more than that of the element to which they belong. This is perhaps not the most awesome thing. Something tells me a better way to manage all of this is in CSS (see the connectors, endpoints and anchors sections for information on what CSS classes jsPlumb uses).
This is a boolean value that defaults to true.
You can provide your own set of dragOptions to pass through to the call to jQuery's draggable(..) function if you need to. jsPlumb will wrap any drag method 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:
$("#someWindow").plumb({target:"otherWindow", dragOptions:{cursor: 'crosshair'}});
jsPlumb.Defaults.DragOptions = { .. your drag options here. };
DEFAULT_PAINT_STYLE : {
lineWidth : 10,
strokeStyle : "red"
}
DEFAULT_DRAG_OPTIONS : { }
DEFAULT_ENDPOINT_STYLE : { fillStyle : null; }
DEFAULT_ENDPOINT_STYLES : [ null, null ]
DEFAULT_ANCHORS : [ jsPlumb.Anchors.BottomCenter, jsPlumb.Anchors.TopCenter ]
DEFAULT_CONNECTOR : jsPlumb.Connectors.Bezier;
DEFAULT_ENDPOINT : jsPlumb.Endpoints.Dot;
DEFAULT_ENDPOINTS : [jsPlumb.Endpoints.Dot, jsPlumb.Endpoints.Dot];
Note that in DEFAULT_ENDPOINT_STYLE, 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 'DEFAULT_ENDPOINT_STYLE' and 'DEFAULT_ENDPOINT_STYLES'. This allows you to specify a different end point for each end of a connection. 'DEFAULT_ENDPOINT' and 'DEFAULT_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.DEFAULT_PAINT_STYLE = {
lineWidth:13,
strokeStyle: 'rgba(200,0,0,100)'
}
jsPlumb.Defaults.DragOptions = { cursor: 'crosshair' };
jsPlumb.DEFAULT_ENDPOINTS = [ new jsPlumb.Endpoints.Dot(7), new jsPlumb.Endpoints.Dot(11) ]
jsPlumb.DEFAULT_ENDPOINT_STYLES = [{ fillStyle:'#225588' }, { fillStyle:'#558822' }];
An Anchor models the notion of where on an element a plumb line should connect. jsPlumb has nine default anchor locations you can use to specify where the plumb lines 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.
You can provide your own anchor locations if you need to. jsPlumb supports two ways of doing this:
This function creates a basic Anchor of the type that jsPlumb uses internally, which is a statically located anchor whose position is specified by providing a fractional distance for both x and y. For instance, the TopCenter anchor in jsPlumb is situated at [0, 0.5].
A call to jsPlumb.makeAnchor should look something like this:var myAnchor = jsPlumb.makeAnchor( 0.333, 0.5, 0, 1 );
Here we have specified an anchor that will be located at a point which is one-third of the width of the element, and half its height. The orientation array indicates that the natural flow of connectors from this element is unspecified for the x direction, but straight down the page in the y direction.
The arguments are:For most cases, the jsPlumb.makeAnchor function will probably suffice. But the method signature of the compute function of an anchor takes the location of both elements, meaning it is possible to write an anchor whose position takes into account where the current element is in relation to the one it is plumbed to. jsPlumb does not natively do this but it seems like something that might be useful.
An anchors function consists of two parts - a function that computes the location of the anchor, given the current xy and width/height of the source element and target element, plus an 'orientation' array, which gives jsPlumb hints about which way plumb lines should flow into and out of the anchor point. Not every Connector implementation will use this orientation array - the Straight Connector, for instance, ignores it completely, because it just directly connects the two anchor points.
To provide your own anchor location you can either set it in amongst the defaults:
jsPlumb.Anchors.MyAnchor = {
compute : function(xy, wh, txy, twh) { // do some maths and return an [x,y] location }
orientation : [ox, oy] // each value can be one of -1, 0 or 1. 0 means dont care,
// 1 or -1 means go in this direction in this plane.
};
var myAnchor = {
compute : function(xy, wh, txy, twh) { // do some maths and return an [x,y] location }
orientation : [ox, oy] // each value can be one of -1, 0 or 1. 0 means dont care,
// 1 or -1 means go in this direction in this plane.
};
$("someWindow").plumb({target:'otherWindow', anchors:[myAnchor, myAnchor]});
The compute function should return an array of two elements - the [x,y] on screen where the anchor is located.
Examples of the orientation array can be found in the Javascript source. It is simply a hint to jsPlumb of what direction a plumb line leaving the given anchor should _initially_ head in. So, for example, the TopCenter anchor point defines its orientation as [0,-1], meaning "i don't care about x, but i want you to head towards the top of the screen as you leave this point". Take another example - BottomRight. This is the bottom right corner of an element. It declares its orientation to be [1,1], meaning go down and to the right as you leave this element.
jsPlumb attaches the CSS class _jsPlumb_connector to Connectors that it generates.
The Bezier Connector provides a Bezier path between the two endpoints. You construct one like this:
var myConnector = new jsPlumb.Connectors.Bezier(curviness);
curviness, which is optional (and defaults to 150), 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 very complex topic,
we refer you to Wikipedia.
The Straight Connector draws a straight line between the two endpoints. You construct one like this:
var myConnector = new jsPlumb.Connectors.Straight();
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) { .. }
$("#someWindow").plumb({target:'otherWindow', connector:new jsPlumb.Connectors.Straight()});
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 four Endpoint implementations - Blank, Dot, Rectangle and Image.
jsPlumb.DEFAULT_ENDPOINT = jsPlumb.Endpoints.Blank;
This draws a dot. Example constructor:
var myDot = new jsPlumb.Endpoints.Dot({radius:34});
In the endpointStyle option of a plumb call, you can set two values that this will pick up:
Draws a rectangle. Example constructor:
var myRect = new jsPlumb.Endpoints.Rectangle({width:34, height:10});
In the endpointStyle you can set the following for this:
Draws an image from a given URL. Example constructor:
var myImage = new jsPlumb.Endpoints.Image({url:"http://myserver.com/images/endpoint.png"});
To create your own Endpoint implementation, you need to implement a single method:
paint : function(anchorPoint, orientation, canvas, endpointStyle, connectorPaintStyle) { ... }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);
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.
$("#window2").plumb({
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:
$("#window2").plumb({
target : 'window3',
paintStyle : {
gradient:{
stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']]
},
lineWidth : 15
}
});
$("#window2").plumb({
target:'window3',
paintStyle:{
strokeStyle:'red',
gradient:{
stops:[[0,'red'], [0.33,'blue'], [0.66,'green'], [0.33,'blue'], [1,'red']]
},
lineWidth:15
}
});
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)';
$("#window2").plumb({
target:'window3',
paintStyle:{
lineWidth:8,
strokeStyle:w23Stroke
},
anchors:[ jsPlumb.makeAnchor(0.3,1,0,1), jsPlumb.Anchors.TopCenter ],
endpoint:new jsPlumb.Endpoints.Rectangle(),
endpointStyles:[
{ gradient : {stops:[[0, w23Stroke], [1, '#558822']] } },
{ gradient : {stops:[[0, w23Stroke], [1, '#882255']] } }
]
});
The type of gradient you will see depends on the Endpoint type:
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)';
$("#window3").plumb({
target:'window4',
paintStyle:{
lineWidth:10,
strokeStyle:w34Stroke
},
anchors:[ jsPlumb.Anchors.RightMiddle, jsPlumb.Anchors.LeftMiddle ],
endpointStyle:{
gradient : {
stops:[ [0, w34Stroke], [1, w34HlStroke] ],
offset:37.5,
innerRadius:40
},
radius:55
},
endpointsOnTop:false
});
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)';
$("#window3").plumb({
target:'window4',
paintStyle:{
lineWidth:10,
strokeStyle:w34Stroke
},
anchors:[ jsPlumb.Anchors.RightMiddle, jsPlumb.Anchors.LeftMiddle ],
endpointStyle:{
gradient : {
stops:[ [0, w34Stroke], [1, w34HlStroke] ],
offset:'68%',
innerRadius:'73%'
},
radius:25
},
endpointsOnTop:false
});
| component | css class |
|---|---|
| connector | _jsPlumb_connector |
| endpoint | _jsPlumb_endpoint |
It is possible to stroke a line in the Canvas element with both gradients and patterns. Currently jsPlumb supports only gradients.
This work is not currently targetted for any release.