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.
28 Notes/ Hide
-
trampoline4w reblogged this from taitems
-
tmblrmailfor likes this
-
last--minute likes this
-
fertilizerspreader likes this
-
konzertekarten likes this
-
bon-de-reduction likes this
-
taitems posted this