views:

88

answers:

1

Using the jQuery plugin: imgareaselect (http://odyniec.net/projects/imgareaselect/), I let users select areas of an image to add comments (just like flickr). I'm aiming to let users draw arrows pointing on specific image areas instead of drawing boxes.

Any idea if (and how) I can modify imgareaselect to draw lines (with an arrow head) instead of selection boxes?

I read that I could use Canvas or processing.js, but AFAIK those either don't work or have limitations on IE.

Thanks, Yasser

+1  A: 

You can make a set of arrow images to overlay, using CSS absolute positioning, on top of the photo. For example, make 18 arrows, each rotated from the last one by 360° / 18 = 20°. Using the CSS sprite technique should allow you to vary the length of the arrow.

In the description that follows, I refer to the start of the arrow as the end near the textbox, and the end as the spot that is pointed to on the picture.

To calculate the (clockwise) arrow angle to use given a pair of x-y coordinates of the pixel pointed to and those of the text box location, we use:

var radians = Math.atan2(startY - endY, startX - endX),
    degrees = radians * 180 / Math.PI;
if (degrees < 0) degrees += 360;

Then your script could choose the closest pre-made arrow:

var approxDegrees = Math.round(degrees / 20) * 20;

When the arrow is loaded, position its top-left corner (relative to the end) according to:

var approxRadians = approxDegrees / 180 * Math.PI,
    imageX = arrowLength * Math.cos(approxRadians),
    imageY = arrowLength * Math.sin(approxRadians);

where l is the length of the arrow. Finally, trim the arrow:

var width = Math.abs(endX - startX);
var height = Math.abs(endY - startY);

and put the center of the text box on the start of the arrow.

var textX = (startX + textWidth) / 2;
var textY = (startY + textHeight) / 2;
idealmachine
Thanks for your quick response! This seems like a good solution, however, I'd like to show users the arrow as they drag and drop, just like this Raphael.js solution: http://fcargoet.evolix.net/demos/extjs/Ext.ux.DDArrow/example/arrow.html. However, I want the arrows to stay, and I don't want to use the Ext JS library. Raphael.js seems like a good alternative (works on IE). I just need to find some sample code to learn how to do draw arrows (instead of lines).