2016-04-20 11 views
-1

これは私のコードです。私はなぜ円が動いていないのか分かりません。それはちょうど私がxとyの座標をどこに記述したかのままです。canvas htmlでサークルを移動できませんか?

class Circle { 
constructor(x, y, r, clr) { 
this.radius = r; 
this.x = x; 
this.y = y; 
this.clr = clr; 
ctx.beginPath(); 
ctx.arc(this.x, this.y, this.radius, 0, 2*Math.PI); 
ctx.fillStyle = this.clr; 
ctx.fill(); 
ctx.closePath(); 
} 

move(finalx, finaly) { 
    this.finalx = finalx; 
    this.finaly = finaly; 

    while (this.finalx != this.x && this.finaly != this.y) { 
    this.x += 2; 
    this.y += 2; 
} 

} 
} 

var x = new Circle(150, 225, 50, black); 
x.move(400, 25); 
+0

HTML5キャンバスはFlashのようには機能しません。 Canvasのアニメーションでは、すべてのフレームでCanvasを描画してクリアする必要があります。それについてはWeb上のチュートリアルをチェックしてください。 [例](https://www.kirupa.com/html5/creating_simple_html5_canvas_animation.htm) – michaPau

答えて

1

あなたはそれぞれの更新後にキャンバスを再描画する必要があります。

class Circle { 
    constructor(x, y, r, clr) { 
     this.radius = r; 
     this.x = x; 
     this.y = y; 
     this.clr = clr; 
     draw(); 
    } 

    move(finalx, finaly) { 
     this.finalx = finalx; 
     this.finaly = finaly; 

     while (this.finalx != this.x && this.finaly != this.y) { 
     this.x += 2; 
     this.y += 2; 
     draw(); 
     } 
    } 

    draw() { 
     ctx.clearRect(0, 0, 500, 500); // Enter your specific dimensions 
     ctx.beginPath(); 
     ctx.arc(this.x, this.y, this.radius, 0, 2*Math.PI); 
     ctx.fillStyle = this.clr; 
     ctx.fill(); 
     ctx.closePath(); 
    } 
} 
+1

かなり良いですが、描画メソッドの呼び出しの間の遅延はさらに良いでしょう! –

関連する問題