2016-10-12 20 views
0

キャンバス上の図形を別の座標系のキャンバス上の特定の座標に移動することができますか?この場合、マウスクリックの位置を取得し、特定の時間にそのポイントに移動するオブジェクトを作成することは可能ですか?事前にあなたの助けをありがとう!キャンバスでXとYをクリックしてオブジェクトを移動する

+0

はい、すべて可能です。おそらく、あなたはキャンバス上のいくつかのドキュメンテーションを見るべきでしょう。 –

+0

たとえば、http://stackoverflow.com/documentation/html5-canvas/4822/animation/20153/animate-from-x0-y0-to-x1-y1 –

+0

私はこのトピックをオフトピックとして閉じようとしていますそれは広すぎるためです。私は、幅広い質問がドキュメントの例の重複として閉じられることを示唆するために、カスタムクロージャの理由を使用しています。 http://stackoverflow.com/documentation/html5-canvas/4822/animation/20153/animate-from-x0-y0-to-x1-y1 –

答えて

0
<canvas id="canvas"></canvas> 
<script> 
var canvas = document.getElementById('canvas'); 
var ctx = canvas.getContext('2d'); 

var x2=100,y2=100; 
var dx=0,dy=0; 
var counter=10; 

canvas.width = canvas.height = 256; 
var shape = {x:100, y:100, width : 50, height : 30}; 

function clear() { 
    ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; 
    ctx.fillRect(0,0,canvas.width,canvas.height); 
} 

function draw(){ 
if(counter>0){ 
    shape.x=shape.x+dx; 
    shape.y=shape.y+dy; 
    ctx.clearRect(0,0,256,256); 
    ctx.fillStyle = "red"; 
    ctx.strokeStyle = "black"; 
     ctx.fillRect(shape.x, shape.y, shape.width, shape.height); 
     ctx.strokeRect(shape.x, shape.y, shape.width,shape.height); 
    counter--; 
    setTimeout(draw,20) 
    } 
} 

canvas.addEventListener("click", function(e) { 
    clear(); 
    x2=e.clientX; 
    y2=e.clientY; 

    dx=(x2-shape.x)/10; 
    dy=(y2-shape.y)/10; 
    counter=10; 
    draw(); 
}); 

draw(); 

</script> 
+0

http://jsfiddle.net/xutawoxv/1/ –

関連する問題