JavaScriptコードに問題があります。それぞれの円が独自の属性を持つ、動く円のセットを作成しようとしています。これまでのところ、必要なすべての値を配列に入力することができましたが、キャンバスに描画するためにそれらを適切に使用する方法がわかりません。
これがjavascriptです:
var radius = 10;
var step = x = y = 0;
var r = g = b = 255;
var circleHolder = [];
var loop = setInterval(function(){update();}, 30);
function Circle(x, y, radius, r, g, b)
{
this.x = x;
this.y = y;
this.radius = radius;
this.r = r;
this.g = g;
this.b = b;
circleHolder.push(this);
}
Circle.prototype.draw = function()
{
Circle.prototype.ctx = document.getElementById("MyCanvas").getContext("2d");
Circle.prototype.ctx.clearRect(0,0,720,720); // clear canvas
Circle.prototype.ctx.beginPath();
Circle.prototype.ctx.strokeStyle = "rgb("+ this.r +", "+ this.g +", "+ this.b +")";
Circle.prototype.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
Circle.prototype.ctx.stroke();
}
Circle.prototype.update = function ()
{
step += .02;
step %= 2 * Math.PI;
this.x = parseInt((Math.sin(step)) * 150) + 360;
this.y = parseInt((Math.cos(step)) * 150) + 360;
this.radius += 16;
if (this.radius > 200)
{
for (i in circleHolder)
{
if (circleHolder[i]==this)
{
circleHolder.splice(i, 1);
}
}
}
}
function update()
{
var ci = new Circle(x, y, radius, r, g, b);
for (i in circleHolder)
{
ci = circleHolder[i];
ci.update();
ci.draw();
}
}
私の問題はupdate(){}にあると確信していますが、それを正しく行う方法がわかりません。
編集:さて、私はそれをいくつかの変更で動作させました!このフィドルをチェックしてください!ただし、コンソールで「ci not defined」エラーが発生し、奇妙なバグがあります。「if(this.radius> 128)」をより高い整数に変更すると、円の回転が速くなります。わかりません。どうして。必要に応じて、256に変更して、何が起こるかを確認できます。
for (var i=0; i < allCircles; i++)
{
ci = circleHolder[i]; <----- This is causing the error
ci.update();
ci.draw();
}