簡體   English   中英

如何移動畫布元素?

[英]How to move canvas elements?

如何將黑色填充的第一個元素移動到鼠標光標? 或者只是如何將其移動到我在事件處理程序中的位置?

Javascript:

var drawing = document.getElementById('canvas');
var ctx = drawing.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(188, 50, 200, 100);

ctx.fillStyle = 'yellow';
ctx.fillRect(0, 0, 200, 100);

document.onmousemove = function(e) {
    /* How to move rectangle here? */
}

http://jsfiddle.net/9545qbo4/1/

提前致謝。

我認為這在畫布上是不可能的,但是您的函數可以這樣做:

ctx.clearRect(/* Old rect position */); 
ctx.fillRect(/* New rect position*/);

編輯:同樣的問題在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM