0

我有一些代码没有给出任何错误,但是当我将图像放入画布时,而不是将图像应用到画布上,什么也没有发生。我怀疑它与javascript中的handledrop函数有关,但我不确定。

附言。我正在使用 Fabricjs

在此先感谢您的帮助 :)

HTML:

<!doctype html>
<html lang="en";>
<head>
    <meta charset="utf-8" />
    <title>CustomCase</title>
    <link rel="stylesheet" href="SkapaDesign.css">
    <script src="Jquery.js"></script>
    <script src="Modernizr.js"></script>
    <script src="Fabric.js"></script>
    <script src="Canvas.js"></script>
</head>
<body>  
    <div id="Wrapper">  
        <section id="Body">
                <div id="CanvasDiv">
                    <canvas id="Canvas" width="270px" height="519px"></canvas>
                </div>

                <div id="TextureView">
                    <div id="TextureViewInside">
                        <div id="images">
                            <img draggable="true" src="FärgadePapper.png">
                            <img draggable="true" src="Hajar.png">
                            <img draggable="true" src="Labyrint.png">
                            <img draggable="true" src="Martini.png">
                        </div>
                    </div>
                </div>  
        </section>
    </div>
</body>
</html>

CSS:

#Body{
    height: 675px;
}
#Canvas, #TextureView{
    float: left;
}
#LeftColumn{
    float: left;
    width: 355px;
    margin: 20px 30px 0px 0px;
}
#RightColumn{
    float: left;
    width: 600px;
    margin-top: 20px;
}
#CanvasDiv{
    width: 270px;
    height: 519px;
    margin-left: 15px;
}
#Canvas{
    overflow: hidden;
}
#TextureView{
    width: 470px;
    height: 559px;
    overflow: auto;
}
#TextureViewInside{
    padding: 10px;
}
#images img{
    width: 97px;
    display: inline-block;
    vertical-align: top;
    padding: 4px;
}

Javascript:

var canvas = new fabric.Canvas('Canvas');

function handleDragStart(e) {
    [].forEach.call(images, function (img) {
        img.classList.remove('img_dragging');
    });
    this.classList.add('img_dragging');
}

function handleDragOver(e) {
    if (e.preventDefault) {
        e.preventDefault(); 
    }
    e.dataTransfer.dropEffect = 'copy';
    return false;
}

function handleDragEnter(e) {
    this.classList.add('over');
}

function handleDragLeave(e) {
    this.classList.remove('over');
}

function handleDrop(e) {
    if (e.stopPropagation) {
        e.stopPropagation();
    }

    var img = document.querySelector('#images img.img_dragging');

    console.log('event: ', e);

    var newImage = new fabric.Image(img, {
        width: img.width,
        height: img.height,
        left: e.layerX,
        top: e.layerY
    });
    canvas.add(newImage);

    return false;
}

function handleDragEnd(e) {
    [].forEach.call(images, function (img) {
        img.classList.remove('img_dragging');
    });
}


$(function() {
     if (Modernizr.draganddrop) {
         var images = document.querySelectorAll('#images img');
         [].forEach.call(images, function (img) {
             img.addEventListener('dragstart', handleDragStart, false);
             img.addEventListener('dragend', handleDragEnd, false);
         });
         var canvasContainer = document.getElementById('CanvasDiv');
         canvasContainer.addEventListener('dragenter', handleDragEnter, false);
         canvasContainer.addEventListener('dragover', handleDragOver, false);
         canvasContainer.addEventListener('dragleave', handleDragLeave, false);
         canvasContainer.addEventListener('drop', handleDrop, false);
     } else {
         alert("This browser doesn't support the HTML5 Drag and Drop API.");
     }
});
4

1 回答 1

0

检查类 img_dragging 是否正确应用于元素。我有一个类似的问题

var images = document.querySelectorAll('#images img');

没有在 Firefox 中收集所有图像,但在 Chrome 中收集了。

查看这个 JSFiddle 的解决方案:http: //jsfiddle.net/DanBrown180/USQ7w/

我通过对每个可以抓取的图像应用降序 ID 来修复它。特别是这段代码:

 function handleDragStart(e) {
    $('img').removeAttr('class').attr('class', 'img');
    console.log("Drag Started");
    console.log(e)
    var imageid = e.target.id;
    if (imageid == "") {
        var imageid = e.target.firstChild.id
    }
    console.log(imageid);
    $("#" + imageid).addClass('img_dragging');
    //this.classList.add('img_dragging');


 }

在我的 JS fiddle 中,它修复了错误,chrome 找到了目标:

e.target.id;

和火狐使用:

e.target.firstChild.id
于 2013-11-12T12:07:50.233 回答