简体   繁体   English

Javascript绘制动态线

[英]Javascript draw dynamic line

I'm looking for Javascript code for letting the user draw a line (on an image). 我正在寻找允许用户绘制一条线(在图像上)的Javascript代码。

Just as the line tool in Photoshop (for example): 就像Photoshop中的线条工具一样(例如):

The user clicks on the image, drags the mouse (while the line between the start point and the mouse point is dynamically drawn on mouse drag). 用户单击图像,拖动鼠标(在鼠标拖动时动态绘制起点和鼠标点之间的线)。

I would also need a callable function to send the page the start and end coordinates. 我还需要一个可调用的函数来发送页面的开始和结束坐标。

I've found this very nice script for area selection: http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/ 我发现这个非常好的区域选择脚本: http//www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/

and I've found many script for drawing lines (and other shapes in JS), but could not find what I'm looking for. 我发现了许多绘制线条的脚本(以及JS中的其他形状),但找不到我要找的东西。

Thanks 谢谢

Since there is no standard method of drawing, if you want to support older browsers and IE, you will need to use a library. 由于没有标准的绘图方法,如果要支持旧浏览器和IE,则需要使用库。 The library will handle the cross platform issues of drawing with SVG or Microsoft's VML 该库将处理使用SVG或Microsoft的VML绘图的跨平台问题

I recommend Raphael JS 我推荐Raphael JS

raphael.js is a lightweight rendering tool for javascript (MIT licensed) which works in FF, Safari, Chrome and IE6+. raphael.js是一个用于javascript(MIT许可)的轻量级渲染工具,适用于FF,Safari,Chrome和IE6 +。

It uses SVG for the first 3 and VML for IE but the API is identical across browsers and very sweet. 它使用SVG作为前3和VML用于IE,但API在浏览器中是相同的并且非常甜。

http://raphaeljs.com/ http://raphaeljs.com/

eg 例如

// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(10, 50, 320, 200);

// Creates circle at x = 50, y = 40, with radius 10
var circle = paper.circle(50, 40, 10);
// Sets the fill attribute of the circle to red (#f00)
circle.attr("fill", "#f00");

// Sets the stroke attribute of the circle to white
circle.attr("stroke", "#fff"); 

I've used it to draw a line while dragging and can vouch for its ease of use 我用它来拖动画线,可以保证它的易用性

A cross-browser solution for drawing lines in javascript, without requiring any external libraries, can be found here: http://bytes.com/topic/javascript/answers/88771-drawing-vector-lines-javascript 可以在此处找到用于在javascript中绘制线条而不需要任何外部库的跨浏览器解决方案: http//bytes.com/topic/javascript/answers/88771-drawing-vector-lines-javascript

This works in all browsers, including IE. 这适用于所有浏览器,包括IE。

Consider using the canvas element to display the image. 考虑使用canvas元素来显示图像。 Then, drawing a line (or anything else) on it is trivial. 然后,在其上绘制一条线(或其他任何东西)是微不足道的。

If your maths is good enough, it is possible (although mad) to do it without the canvas tag for most modern browsers using one of (as appropriate): 如果你的数学足够好,可以(尽管很疯狂)在没有canvas标签的情况下使用(根据需要)使用其中一个的大多数现代浏览器:

By creating eg. 通过创建例如。 a 1px high div, with eg. 一个1px高的div,例如。 a border-top for your 'line', and using the mouse drag event to position and rotate it. 您的“线”的边框顶部,并使用鼠标拖动事件来定位和旋转它。

Madness lies this way but it would be a quite fun exercise. 疯狂就是这样,但这将是一个非常有趣的运动。 (You should use something like Raphael JS, which is cross browser and sane - see above) (你应该使用类似Raphael JS的东西,这是跨浏览器和理智的 - 见上文)

i'm working on something similar. 我正在做类似的事情。 Drawing a line on a canvas is pretty simple. 在画布上画一条线非常简单。 You can take from my code here. 您可以从我的代码处获取。

http://p-func.com/html5_test/test2.html http://p-func.com/html5_test/test2.html

Just follow the mousedown listener. 只需按照mousedown监听器。

Although I have found, when wanting to load images, that the raphael library might be better to use. 虽然我发现,当想要加载图像时,raphael库可能更好用。 I saw this because Canvas seems to act like a flat image. 我看到了这一点,因为Canvas似乎就像一个平面图像。 So if I want to add an aimge to it, then allow the user to manipulate that image, it won't let me (unless there is something that i am missing). 因此,如果我想为它添加一个目标,那么允许用户操纵该图像,它不会让我(除非有我遗漏的东西)。

Raphael allows you to draw and still use those drawings, and images, as objects. Raphael允许您绘制并仍然使用这些绘图和图像作为对象。

When supported you can use canvas, in IE you use the filters rotate function. 支持时你可以使用canvas,在IE中使用滤镜旋转功能。 As here works on both: 这里适用于:

http://www.gatekeeperel.co.uk/interactives/web.html http://www.gatekeeperel.co.uk/interactives/web.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM