如何实现HTML5 Canvas绘画应用的拖放?
基于 创建 HTML 5 画布绘画应用程序 我创建了一个 HTML5 画布绘画应用。它工作正常,但创建每个对象后我只需要拖动对象。 工作演示
如何实现图形的拖放?
Based on Creating an HTML 5 canvas painting application I created a HTML5 canvas painting application. It works fine, but after creating each object I just need to drag the objects.
Working demo
How to implement drag and drop of the figures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当用户单击画布时,您必须检查坐标(将其与对象的坐标进行比较),并查看它是否在对象上。例如,您可以使用以下方法测试一个点(例如,甚至鼠标按下的坐标)是否在圆内:
如果鼠标按下在对象上,则必须根据鼠标的移动方式更改对象坐标。
下面是一个示例,您可以在其中拖动圆圈:
在 jsFiddle 上尝试一下
When the user clicks on the canvas, you have to check the coordinates (compare it to the coordinates for the objects), and see if it's on an object. E.g. You can test if a point (e.g. the coordinates for the mousedown even) is within a circle with this method:
If the mousedown is on the object, you have to change the objects coordinates according to how the mouse is moved.
Here is an example, where you can drag a circle:
Try it on jsFiddle
使用 Raphael.js (http://raphaeljs.com/ )与 Joint.jS (http://www.jointjs.com/) 。
使用 Raphael 创建的形状可以像任何 DOM 元素一样进行访问,并且可以通过属性进行操作。这是一个很棒的框架。
Joint.js 有助于连接形状。他们还有一个图表库,可以帮助创建 ERD、状态机和一些常见图表。最好的部分是您可以扩展他们的图表元素并创建您自己的自定义元素。其酷得令人瞠目结舌。
在 http://www.jointjs.com/demos 查看他们的演示及其源代码
The same effect can be accomplished using Raphael.js (http://raphaeljs.com/) with Joint.jS (http://www.jointjs.com/).
Shapes created with Raphael can be accessed like any DOM element and can be manipulated via attributes. It is an awesome framework.
Joint.js helps in connecting the shapes. They also have a diagramming library and can help create ERD, Statemachine and several common diagrams. The best part is that you can extend their diagram element and create your own custom elements. Its jaw-dropingly cool.
Checkout their demos with source code at http://www.jointjs.com/demos
如果您使用 raphael 作为“原始”库,您必须自己处理撤消/重做。
graphiti 库内部确实有撤消/重做堆栈,并支持导出 SVG、PNG、JSON...
此外,您还有某种 Viso,如连接器和端口。
http://www.draw2d.org/graphiti/jsdoc/#!/example
问候
If you are using the raphael as "raw" lib you must handle the undo/redo by yourself.
The graphiti lib did have Undo/Redo Stack inside and supports the export for SVG, PNG, JSON,...
Additional you have some kind of Viso like connectors and ports.
http://www.draw2d.org/graphiti/jsdoc/#!/example
Greetings
我认为没有一个简单的方法可以做到这一点。
如果您只是处理线条,我的方法是跟踪创建的所有线条,包括起始坐标、结束坐标和某种 z 索引。当用户开始拖动操作(onmousedown)时,您必须检查该点是否位于该线附近,然后在鼠标移动时更新对象并重新绘制画布。
如何判断一个点是否属于到某一行?
但是,如果您正在处理复杂的对象,这会变得更加复杂。您可能需要找到一个解决方案来检查某个点是否位于路径内。
I don't think there's an easy way to do this.
If you're just dealing with lines, my approach would be to keep track of all lines created, with starting coordinates, ending coordinates and some kind of z-index. When the user starts a dragging action (onmousedown), you have to check if the point is near the line, and then update the object and redraw the canvas when the mouse is moved.
How can I tell if a point belongs to a certain line?
This gets a lot more complicated if you're dealing with complex objects though. You'll probably have to find a solution to check if a point is inside a path.
绘制到 HTML5 Canvas 中的对象会变成像素,然后被遗忘。您无法调整它们的属性并更新画布以查看效果。您可以自己记住它们,但画布仍然会设置这些像素,因此当您调整属性时,您必须基本上重新绘制整个画布(或至少其中一部分)。
您可能想在该应用程序中考虑使用 SVG,SVG 元素会被记住在 DOM 中,当它们的属性更新时,浏览器将更新图形以反映更改。
如果您必须使用画布,那么您将需要编写大量代码来处理鼠标点击、对象属性和重绘。
Objects drawn into HTML5 Canvas are turned into pixels and then forgotten. You can't adjust properties on them and have the canvas update to see the effects. You can remember them yourself, but the canvas will still have those pixels set, so you'd have to basically redraw the whole canvas (or at least some of it) when you adjust a property.
You might want to consider SVG for this application instead, SVG elements are remembered in the DOM and when their properties are updated the browser will update the graphic to reflect the changes.
If you must use canvas, then you're going to need to write quite a bit of code to handle mouse-hits, object properties, and repaints.