0

I've got a canvas and a div with scaled down images. I want to drag one of the scaled down images into the canvas. When an image is clicked on, the src should be saved so that if that image is dropped into the canvas, i gets drawn in the original scale. For some reason this code doesn't work...

HTML

<div id="LeftColumn">
    <canvas id="Canvas">
    </canvas>
</div>

<div id="TextureViewInside">
    <ul class="products">
        <img onclick="getSource(this.src)" id="firstPic" src="FärgadePapper.png">
            <img src="Hajar.png">
            <img src="Labyrint.png">
            <img src="Martini.png">
            <img src="FärgadePapper.png">
    </ul>
</div>

javascript

function first(){
    canvas = document.getElementById('Canvas');
    var ctx = canvas.getContext("2d");
    canvas.addEventListener("dragenter", function(e){e.preventDefault();}, false);
    canvas.addEventListener("dragover", function(e){e.preventDefault();}, false);
    canvas.addEventListener("drop", dropped, false);
}
function getSource(scr){
    alert(src);
    a = new image();
    a.src = src;
}
function dropped(){
    canvas.drawImage(a, 0, 0);
}
window.addEventListener("load", first, false);
4

1 回答 1

1

应该是new Image(),而不是带有小写“i”的“图像”。此外,您需要使用上下文而不是直接使用画布来绘制图像。同样在您的getSource()函数中,您将参数称为“scr”,但函数中的代码将其称为“src”。

此外,您确实应该在某处明确声明“a”。

于 2013-11-10T16:36:34.217 回答