简体   繁体   English

可以拖动HTML dom元素并放到SVG dom元素上吗?

[英]Possible to drag an HTML dom element & drop onto an SVG dom element?

I'm currently using Raphaël JS (open to switching to jQuery SVG) and jQuery UI to try and make a prototype of a board game. 我目前正在使用RaphaëlJS(开放转换为jQuery SVG)和jQuery UI来尝试制作棋盘游戏的原型。 It's somewhat similar to Risk in that the board is a map and you can (hopefully soon) drag pieces from one zone on the map to another (say from A to B) and drop them there. 它有点类似于Risk,因为棋盘是一张地图,你可以(希望很快)将棋子从地图上的一个区域拖到另一个区域(例如从A到B)并将它们放在那里。 Once dropped, it would trigger a callback to do some work. 一旦被删除,它将触发回调以完成一些工作。

Right now I'm stuck trying to get the drag/drop functionality. 现在我不得不尝试获得拖放功能。 I want to drag an html element (div) and drop it onto an SVG element. 我想拖动一个html元素(div)并将其放到SVG元素上。 I'm not very familiar with SVG, but from what I understand there's issues to overcome in getting the HTML and SVG DOMs to work together. 我对SVG不太熟悉,但据我所知,在使HTML和SVG DOM协同工作时需要克服一些问题。

I've got two versions and I'm trying to get jQuery UI to drag/drop to with either of them. 我有两个版本,我试图让jQuery UI拖放到其中任何一个。 One is using jQuery SVG with the svgdom plugin + jQuery UI and the other is just Raphaël + jQuery UI. 一个是使用jQuery SVG和svgdom插件+ jQuery UI,另一个是Raphaël+ jQuery UI。 Neither of them seems to be firing the drop event. 他们似乎都没有解雇掉落事件。 As far as I know, the Raphaël version doesn't work because jQuery's dom manipulation can't interact with the SVG dom. 据我所知,Raphaël版本不起作用,因为jQuery的dom操作无法与SVG dom交互。

JS Fiddle jQuery SVG: http://jsfiddle.net/cqMUL/5/ (you'll have to scroll far down to see the SVG elements, my apologies). JS Fiddle jQuery SVG: http//jsfiddle.net/cqMUL/5/ (你必须向下滚动才能看到SVG元素,我的道歉)。

The other possible solutions seem to be: 其他可能的解决方案似乎是:
a) Using Raphaël with it's builtin drag and onDragOver functions, although then I'm using all SVG and I'm trying to use the HTML dom for the moving pieces because they will be images (div with background image). a)使用Raphaël的内置dragonDragOver函数,虽然我正在使用所有SVG,我正在尝试使用HTML dom来移动棋子,因为它们将是图像(div与背景图像)。 In addition, I'd probably have to implement drop myself. 另外,我可能不得不自己实施drop。
b) Trying to use HTML5 drag & drop, although I'm not sure this would work either. b)尝试使用HTML5拖放,虽然我不确定这也会有用。

This is an old question, but I'll give a shot at answering it anyway. 这是一个老问题,但无论如何我都会尽力回答它。 The solution I came up with worked like this: 我提出的解决方案就是这样的:

  1. Use JQueryUI to create the "draggable" functionality. 使用JQueryUI创建“可拖动”功能。 It has a lot of convenient hooks. 它有很多方便的钩子。
  2. Attach mouseover and mouseout events to the SVG elements. 将mouseover和mouseout事件附加到SVG元素。
  3. Create a custom drag/drop manager. 创建自定义拖放管理器。
  4. When you start a drag, register the element (or associated data) with the dragdrop manager. 开始拖动时,使用dragdrop管理器注册元素(或关联数据)。
  5. When you hover over a drop target, register that target with the dragdrop manager. 将鼠标悬停在放置目标上时,使用dragdrop管理器注册该目标。
  6. Check your "drop" criteria on JQueryUI.draggable's "stop" event, and do the processing required there. 检查JQueryUI.draggable的“stop”事件的“drop”条件,然后在那里进行处理。

You can see an example of this in action (using D3 to generate the SVG) here: http://bl.ocks.org/thudfactor/6611441 您可以在此处查看此操作的示例(使用D3生成SVG): http//bl.ocks.org/thudfactor/6611441

you can use Rapheal.js It could be done by applying simple jquery drag and drop, Make our html element draggable and your rapheal paper div droppable. 你可以使用Rapheal.js它可以通过应用简单的jquery拖放来完成,使我们的html元素可拖动,你的rapheal文件div可以删除。 Here is code 这是代码

HTML Code: HTML代码:

<table style = "width:600px; border:1 px solid black;">
       <tr>
         <td>
           <div id="divDragable" class="ui-widget-content">
    <p>Drag me around</p>
           </div>
         </td>

         <td>
         <div id="divDroppable">
           </div>
         </td>

       </tr>
    </table>

Jquery Code: Jquery代码:

$(function () {

    //--- Draggable

    $("#divDragable").draggable();

    //-------------------- Code for Raphael---------------------
    var paper = Raphael("divDroppable", 200, 200);

    // Making SVG droppable
    $("#divDroppable").droppable({
        drop: function (event, ui) {
            // get targeted SVG elemnet by
            // event.originalEvent.target
        }

    });

    var recatngleObj1 = paper.rect(10, 15, 50, 30, 2);
    recatngleObj1.attr("id", "emptyRect");
    recatngleObj1.dropShadow(2, 3, 3, 1);
    recatngleObj1.attr("fill", "#B3E6FF");

});

Indeed, SVG is difficult to manipulate. 实际上,SVG很难操纵。 Here is an example you could base your programming on ; 这是一个你可以编程的例子 ; just look at the source. 只看源头。

Alternatively, if ever you decide to abandon SVG, here are a couple of other ideas. 或者,如果你决定放弃SVG,这里还有其他一些想法。

  1. You could make the pieces absolutely positioned <div> s with different z-indexes, then use jquery ui to move them. 您可以使用不同的z索引使碎片绝对定位<div> ,然后使用jquery ui移动它们。
  2. You could use Canvas and HTML5 . 您可以使用Canvas和HTML5 Using this, you wouldn't need JQuery UI either. 使用它,您也不需要JQuery UI。

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

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