从 DOM 拖放到 Canvas 或 SVG 中
我即将构建一个工具,允许用户从列表中拖动项目并将其放入画布中以构建树/组织结构图。
想知道是否可以将常规 li-node 拖到画布/svg 上并检测画布上的位置以及哪个元素被删除?
也许您有比使用 canvas/svg 更好的建议?我唯一的要求是用户应该能够直观地从列表中拖动项目来构建图形/组织结构图。
I am about to build a tool that allows users to drag items from a list and drop them in a canvas to build a tree/organization chart.
Was wondering if it is even possible to drag a regular li-node onto a canvas/svg and detect where on the canvas, and what element, is being dropped?
Maybe you have a better suggestion than to use canvas/svg? My only requirement is that the user should be able to intuitively drag items from the list to build a graph/organization chart.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
和拉斐尔一起你会发现一切都很容易
加快开发速度,创建您的 SVG 并将其放入
转换器工具位于
http://irunmywebsite.com/raphael/svgsource.php
拖放示例
http://irunmywebsite.com/raphael/additionalhelp.php?q=bearbones< /a>
You will find it easy with Raphael
Speed development, create your SVG and drop it into the
Converter tool at
http://irunmywebsite.com/raphael/svgsource.php
Drag drop example
http://irunmywebsite.com/raphael/additionalhelp.php?q=bearbones
是的,可以拖动到另一个元素上/上方并确定您在该元素上的位置。对于 SVG,您需要使用如下代码将鼠标坐标转换为画布本地空间:
对于 Canvas,您可能需要确保画布的
.style.offsetWidth
相同作为其宽度
,或者在它们之间进行转换以进入本地空间。 (仅当您调整画布的显示大小时。)然而,与 Canvas 相比,SVG 的最大优点之一是它是一个“保留图形模式”绘图 API。除此之外,这允许您将事件处理程序放在 SVG 元素本身上并检测它们上的鼠标悬停事件,而不是尝试根据坐标确定您位于哪个对象上。 (后者是您需要对 Canvas 执行的操作,因为画布上没有“对象”,只有油漆像素,一旦您绘制它们,它们就会立即干燥。)
我个人建议将您的 SVG 直接嵌入 XHTML 中,而不是通过
Yes, it's possible to drag onto/over another element and determine where on that element you are. For SVG, you'll want to use code like the following to transform from mouse coordinates to canvas local space:
For Canvas, you might want to ensure that the
.style.offsetWidth
of the canvas is the same as itswidth
, or else transform between them to get into local space. (Only if you are resizing the display of your canvas.)One of the best things about SVG compared to Canvas, however, is that it is a "retained graphics mode" drawing API. Among other things, this allows you to put event handlers on the SVG elements themselves and detect
mouseover
events on them, instead of attempting to determine from coordinates which object you are over. (The latter is what you'll need to do with a Canvas, as there are no 'objects' on the canvas, only pixels of paint that instantly dried once you painted them.)I personally suggest embedding your SVG directly in XHTML, instead of via
<object>
or<img>
, so that you have a singledocument
to deal with. Here's an example page of mine that embeds multiple SVG's into a single XHTML page: http://phrogz.net/SVG/convert_path_to_polygon.xhtml