让用户在 div/img 上画一条线(箭头)

发布于 2024-09-27 23:30:27 字数 277 浏览 0 评论 0原文

使用 jQuery 插件:imgareaselect (http://odyniec.net/projects/imgareaselect/),我让用户选择图像区域来添加评论(就像 flickr)。 我的目标是让用户绘制指向特定图像区域的箭头而不是绘制方框。

知道是否(以及如何)我可以修改 imgareaselect 来绘制线条(带有箭头)而不是选择框吗?

我读到我可以使用 Canvas 或processing.js,但据我所知,这些要么不起作用,要么在 IE 上有限制。

谢谢, 亚西尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2024-10-04 23:30:27

您可以使用 CSS 绝对定位将一组箭头图像叠加在照片顶部。例如,制作 18 个箭头,每个箭头都从最后一个箭头旋转 360° / 18 = 20°。使用 CSS sprite 技术应该可以改变箭头的长度。

在下面的描述中,我将箭头的开始称为文本框附近的结束位置,将结束称为图片上指向的点。

为了计算(顺时针)箭头角度以使用给定的一对像素指向的 xy 坐标和文本框位置的坐标,我们使用:

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

然后您的脚本可以选择最接近的预制箭头:

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

加载箭头时,根据以下条件定位其左上角(相对于末端):

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

其中 l 是箭头的长度。
最后,修剪箭头:

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

并将文本框的中心放在箭头的起点上。

var textX = (startX + textWidth) / 2;
var textY = (startY + textHeight) / 2;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文