Nothing Insightful

  • Archive
  • RSS

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.

    • #javascript
    • #jquery
    • #jquery ui
    • #raphael
    • #raphael js
    • #canvas
    • #html5
  • 3 years ago
  • 28
  • Comments
  • Permalink
  • Share

28 Notes/ Hide

  1. trampoline4w reblogged this from taitems
  2. tmblrmailfor likes this
  3. last--minute likes this
  4. fertilizerspreader likes this
  5. konzertekarten likes this
  6. bon-de-reduction likes this
  7. taitems posted this

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

My name is Tait Brown, and I'm a Melbourne-based UI designer and a front end developer. I like to make stuff.

taitbrown.com

taitbrown@gmail.com

Me, Elsewhere

  • taitems on Dribbble
  • taitems on Forrst
  • @taitems on Twitter
  • taita_cakes on Last.fm
  • Linkedin Profile
  • taitems on github

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr