是否可以使 HTML5 Canvas 元素中的元素可拖动和调整大小。有什么方法可以使以下代码中的动态图像可移动:
var ctx, imgArray;
function initAll() {
ctx = document.getElementById("canvas").getContext("2d");
imgArray = ["http://i46.tinypic.com/nqe1dt.jpg",
"http://i50.tinypic.com/i21phi.jpg",
"http://i49.tinypic.com/1zmfcrq.jpg",
"http://i50.tinypic.com/v8g6mo.jpg",
"http://i49.tinypic.com/21kh7ah.jpg"];
drawCanvas();
document.getElementById("checkgroup").onchange = drawCanvas;
}
function drawCanvas() {
ctx.clearRect(0, 0, 600, 400);
for(var i=0; i < imgArray.length; ++i) {
var img = new Image();
img.posX = (i < 3) ? i*200 : (i-2.5)*200;
img.posY = (i < 3) ? 0 : 200;
img.src = imgArray[i];
img.onload = function() {
ctx.drawImage(this, this.posX, this.posY, 150, 150);
}
}
}
window.onload = initAll;
该项目位于 JSBin:http: //jsbin.com/welcome/7209/edit