简体   繁体   English

使用 Draw2D.js 拖放菜单

[英]Drag & Drop menu with Draw2D.js

How would I create a Drag & Drop menu with Draw2D.js?如何使用 Draw2D.js 创建拖放菜单? I am making an application using React and can't find how to do it in the docs.我正在使用 React 制作应用程序,但在文档中找不到如何操作。 There is something similar in this example , but I don't understand how to I would make my own.这个例子中有类似的东西,但我不明白如何自己制作。

In the example, the author uses methods that aren't in the docs .在示例中,作者使用了docs中没有的方法。

示例中的代码

And when I try to copy the code from that example, eslint says that it is wrong.当我尝试从该示例中复制代码时,eslint 说这是错误的。

在此处输入图像描述

You can find the code of this example here:您可以在此处找到此示例的代码:

shape db example 形状数据库示例

The idea is to add a ghost img to the dom following the mouse moove and to create your new element at the drop event.这个想法是在鼠标移动之后向 dom 添加一个幽灵 img 并在 drop 事件中创建新元素。

The following extract of the code from this example do exactly what it is needed to perform the drag and drop: 此示例中的以下代码摘录正是执行拖放操作所需的:

onMouseDrag:function(canvas, dx, dy, dx2, dy2)
{
    var _this = this;

    if( !((this.mouseDraggingElement instanceof draw2d.ResizeHandle) || (this.mouseDraggingElement instanceof draw2d.Port))){
        if(this.cloneOnDrag ===true && this.mouseDraggingElement !==null){
            var tmp = this.mouseDraggingElement;

            // get the current position of the selected shape
            // cancel the current drag&drop operation
            var pos = this.mouseDraggingElement.getPosition();
            this.mouseDraggingElement.onDragEnd(pos.x, pos.y, false,false);
            this.mouseDraggingElement = null;

            // PNG snapshot's didn'T work with all kind of shapes. You can use
            // a DIV ghost as workaround if this didn't work for you
            //
            var usePNGSnapshot = true;

            // create an base64 coded image snapshoot from the figure
            // (not from the complete canvas)
            //
            if(usePNGSnapshot===true) {
                new draw2d.io.png.Writer().marshal(tmp, function (base64Image) {
                    // add the image to the DOM tree
                    //
                    var ghost = $("<img  style='position:absolute'>");
                    ghost.attr("src", base64Image);

                    _this.setupDragDropGhost(tmp, ghost);

                });
            }
            else{
                var ghost = $("<div>");
                ghost.css({
                    position: "absolute",
                    width: tmp.getWidth(),
                    height: tmp.getHeight(),
                    border:"1px dotted black",
                    borderRadius:"3",
                    backgroundColor:"rgba(128,128,200,0.3)"
                });

                _this.setupDragDropGhost(tmp, ghost);
            }
        }
    }
    this.cloneOnDrag=false;
    this._super(canvas, dx,dy,dx2,dy2);
},
setupDragDropGhost: function(figure, ghost){
    var _this = this;

    $("body").append(ghost);

    // and track mouseMove events to move the IMG element around
    //
    var offset = _this.mouseDownPos.subtract(figure.getPosition());
    var mousemoveHandler = function (e) {
        var diffX = e.pageX - offset.x;
        var diffY = e.pageY - offset.y;
        ghost.css('left', diffX + 'px').css('top', diffY + 'px');
    };
    $(document).bind('mousemove', mousemoveHandler);

    ghost.bind('mouseup', function (e) {
        try {
            // this is a drop event...determine the related canvas and add
            // the figure clone to the canvas
            //
            $(document).unbind('mousemove', mousemoveHandler);
            var r1 = new draw2d.geo.Rectangle($("#container1")[0].getBoundingClientRect());
            var r2 = new draw2d.geo.Rectangle($("#container2")[0].getBoundingClientRect());
            var canvas1Hit = r1.hitTest(e.pageX, e.pageY);
            var canvas2Hit = r2.hitTest(e.pageX, e.pageY);

            if (canvas1Hit) {
                var clone = figure.clone();
                var p = canvas1.fromDocumentToCanvasCoordinate(e.pageX, e.pageY);
                clone.setPosition(p.subtract(offset));
                canvas1.add(clone);
            }
            if (canvas2Hit) {
                var clone = figure.clone();
                var p = canvas2.fromDocumentToCanvasCoordinate(e.pageX, e.pageY);
                clone.setPosition(p.subtract(offset));
                canvas2.add(clone);
            }
        }
        finally{
            ghost.remove();
        }
    });
}

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

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