Плъзнете контролите от контейнера и ги пуснете/начертайте върху платно

Искам да позволя на потребителя да плъзга икони от един div контейнер и да ги пуска и рисува върху платно, запазвайки оригиналната икона непокътната. Иконите се добавят динамично на масата. Също така искам да дам ефект на плъзгане на икона с курсора, когато плъзгам иконата. Когато използвам свойството "помощник: 'клонинг'", получавам ефекта, но изпуснатата позиция е неправилна и когато премахна това свойство, оригиналната икона се премахва.

Моят родителски контейнер за икони

     <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>

person hampi2017    schedule 28.02.2014    source източник


Отговори (1)


Демо: http://jsfiddle.net/m1erickson/cyur7/

Преди плъзгане и след плъзгане:

въведете описание на изображението туквъведете описание на изображението тук

Създайте html лента с инструменти

  • създайте div, който да съдържа всички икони на инструменти.
  • създайте img елементи за всеки от вашите инструменти и ги поставете в div на лентата с инструменти
  • дайте целия инструмент img на class="tool"

Html toolbar-div с tool-imgs:

<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>

Направете всички .tools възможност за плъзгане с jQuery

  • изберете всички .tools в jquery ($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,
    });

При изпускане нарисувайте изображението върху платното

  • вземете точката на изпускане на dropable
  • вземете полезния товар на данните на 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);

    }
person markE    schedule 28.02.2014