2014-08-31 42 views
0

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

的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/

在此先感謝。

回答

1

我認爲這是不可能的在畫布上,但你的函數可以做到這一點是這樣的:

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

編輯:同樣的問題是here

+0

好的,謝謝! – qwerty 2014-08-31 11:07:49