从容器拖动控件并在画布上放置/绘制它们

dex*_*007 2 javascript jquery html5 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>
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

画布用于绘制图像

    <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>
Run Code Online (Sandbox Code Playgroud)

mar*_*rkE 6

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

在拖动之前和拖动之后:

在此输入图像描述在此输入图像描述

制作一个html工具栏

  • 创建一个div来保存所有工具图标.
  • 为每个工具创建img元素并将它们放在工具栏div中
  • 给所有工具img的class ="工具"

带有tool-imgs的Html toolbar-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>
Run Code Online (Sandbox Code Playgroud)

使用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);
    });
Run Code Online (Sandbox Code Playgroud)

使画布成为放置目标:

    // make the canvas a dropzone
    $canvas.droppable({
        drop:dragDrop,
    });
Run Code Online (Sandbox Code Playgroud)

删除后,在画布上绘制img

  • 得到droppable的下降点
  • 获取droppable的数据有效负载($ 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);

    }
Run Code Online (Sandbox Code Playgroud)