簡體   English   中英

將控件從容器中拖放到畫布上

[英]Drag controls from container and drop/draw them on canvas

我想允許用戶從一個div容器中拖動圖標,並將其拖放到畫布上,以保持原始圖標完整。 圖標將動態添加到表中。 當我拖動圖標時,我也想使圖標帶有光標的效果。當我使用“ helper:'clone'”屬性時,我會收到效果,但是放置的位置不正確,當我刪除該屬性時,原始圖標也將被刪除。

我的圖標的父容器

     <div id="dvEquipmentTool" style="removed: absolute; border: 1px solid rgb(173, 201, 247);
    display: block; background-color: white; height: 400px; z-index: 50000; padding: 3px;
    removed 100px; removed 50px; width: 200px !important;" class="ui-draggable">
    <table id="tbEquipmentTool" border="0" style="background-color: White; padding-left: 10px;
        max-height: 400px !important;">
        <tbody>
            <tr id="tRow1">
                <td>
                    <img src="" id="imgEquipIcon1" class="ui-draggable" style="position: relative; left: 473px;
                        top: 183px;">
                </td>
                <td>
                    <span id="lblImgEquipName1" class="label">10-FL-105-A20</span><span id="lblImgEquipID1"
                        class="label" style="display: none;">100200</span>
                </td>
                <td width="10px">
                </td>
            </tr>
            <tr id="tRow2">
                <td>
                    <img src="" id="imgEquipIcon2" class="ui-draggable" style="position: relative;">
                </td>
                <td>
                    <span id="lblImgEquipName2" class="label">10-FL-3111-A20</span><span id="lblImgEquipID2"
                        class="label" style="display: none;">100199</span>
                </td>
                <td width="10px">
                </td>
            </tr>
        </tbody>
    </table>
</div>

Javascript功能使圖像可拖動

    $("img[id^=imgEquipIcon]").each(function () {


    $(this).draggable({ containment: "#dvContainer", scroll: false,//helper: 'clone',
        stop: function (event, ui) {
            var stoppos = $(this).position();
            alert(stoppos.left+","+ stoppos.top);

            var img = new Image();
            img.src = this.src;
            createEquipIcon(img, stoppos.left, stoppos.top);

        }

    });

});

function createEquipIcon(img, X, Y) {
    var dv = document.createElement('div');

    $(dv).css('top', Y);
    $(dv).css('left', X);
    $(dv).css('cursor', 'move');
    $(dv).css('position', 'absolute');
    $(dv).css('background-color', 'red');

    dv.appendChild(img);
    var index = img.id.replace('imgEquipIcon', '');

    var container = document.getElementById('dvContainer');
    container.appendChild(dv);
   //code for drawing on canvas goes here

}

用於繪制圖像的畫布

    <div id="dvContainer" runat="server" style="overflow: visible; background-repeat: no-repeat">
            <canvas id="myCanvas" width="1000px" height="600px">
          <b> *Your browser doesn't support canvas. Please switch to different browser.</b>
        </canvas>

演示: http : //jsfiddle.net/m1erickson/cyur7/

拖動之前和之后:

在此處輸入圖片說明在此處輸入圖片說明

制作一個HTML工具欄

  • 創建一個div來保存所有工具圖標。
  • 為每個工具創建img元素,並將其放入工具欄div中
  • 給所有工具img的class =“ tool”

帶工具img的HTML工具欄div:

<div id="toolbar">
    <img class="tool" width=32 height=32 src="equipment1.jpg">
    <img class="tool" width=32 height=32 src="equipment1.jpg">
    <img class="tool" width=32 height=32 src="equipment1.jpg">
</div>

使用jQuery使所有.tools可拖動

  • 選擇jquery中的所有.tools($ tools)
  • 使所有.tools可拖動
  • 為所有.tools提供數據有效負載,並在$ tools中添加其索引。

使所有.tools可拖動:

    // select all .tool's

    var $tools=$(".tool");

    // make all .tool's draggable

    $tools.draggable({ helper:'clone' });


    // assign each .tool its index in $tools

    $tools.each(function(index,element){
        $(this).data("toolsIndex",index);
    });

使畫布成為放置目標:

    // make the canvas a dropzone
    $canvas.droppable({
        drop:dragDrop,
    });

放下后,在畫布上繪制img

  • 獲取可放置對象的放置點
  • 獲取可放置對象的數據有效載荷($ tools中被放置項目的索引)
  • 使用context.drawImage在畫布上繪制拖放的圖像

下降處理程序

    // handle a drop into the canvas
    function dragDrop(e,ui){

        // get the drop point (be sure to adjust for border)
        var x=parseInt(ui.offset.left-offsetX)-1;
        var y=parseInt(ui.offset.top-offsetY);

        // get the drop payload (here the payload is the $tools index)
        var theIndex=ui.draggable.data("toolsIndex");

        // drawImage at the drop point using the dropped image 
        ctx.drawImage($tools[theIndex],x,y,32,32);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM