Drawing Arrows in RaphaelJS


I’ve been testing out the different canvas drawing libraries, such as Processing.js and RaphaelJS, and there seems to a common missing element: drawing arrows and arrowheads. RaphaelJS even goes as far as to mention the Raphael.fn.arrow function in the documentation and not actually provide any code for it. Googling turned up nothing, no-one had really written a full one, and a few people mentioned the use of Math.atan2().

So here’s my version of Raphael.fn.arrow():

Raphael.fn.arrow = function (x1, y1, x2, y2, size) {
var angle = Math.atan2(x1-x2,y2-y1);
angle = (angle / (2 * Math.PI)) * 360;
var arrowPath = this.path(“M” + x2 + ” ” + y2 + ” L” + (x2 – size) + ” ” + (y2 – size) + ” L” + (x2 – size) + ” ” + (y2 + size) + ” L” + x2 + ” ” + y2 ).attr(“fill”,”black”).rotate((90+angle),x2,y2);
var linePath = this.path(“M” + x1 + ” ” + y1 + ” L” + x2 + ” ” + y2);
return [linePath,arrowPath];
}

The demonstration below is not perfect, as dragging is handled via jQuery UI. Ideally, dragging should be handled natively within RaphaelJS, but this function is being utilised in my hybrid project of jQuery UI and RaphaelJS.

Copy from http://taitems.tumblr.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s