是否可以使 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
最佳答案
使用为 Canvas 提供形状对象支持的 KineticJS。
KineticJS 将为其形状提供鼠标事件,因此您可以更轻松地实现类似拖动的功能:
http://www.html5canvastutorials.com/kineticjs/html5-canvas-drag-and-drop-tutorial/
关于javascript - HTML5 Canvas 中的可拖动和可调整大小的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774212/