このアニメーションでは、2 つのボールに対して 2 つの関数を作成しましたが、このキャンバスには 2 つ目のボールはありません。両方のボールの私のコード-
function draw() {
ctx.clearRect(0, 0, 300, 300);
//ctx.beginPath();
//ctx.arc(x, y, 10, 0, 2 * Math.PI, true);
//ctx.closePath();
ctx.drawImage(img, x, y, 20, 20);
ctx.fill();
x += dx;
y += dy;
bounce();
}
function draw2()
{
ctx.clearRect(0,0,300,300);
ctx.beginPath();
ctx.arc(x, y, 10, 0, 2 * Math.PI, true);
ctx.closePath();
ctx.fill();
x += dx;
y += dy;
bounce();
}
関数の呼び出し -
function init() {
var ctx = document.getElementById("canvas").getContext("2d");
return setInterval(draw, 10);
return setInterval(draw2,20);
//This is how i am calling both function
}
Javascriptでこれを行うことはできますか?
期待される結果-
両方のボールが同じ位置から来ています。最初のボールがキャンバス フレームでバウンドしたときに、10 ミリ秒後に別のボールdraw2 ()
がフレームに入って同じように動作する必要があります。