私はアニメーションをループしようとしていますが、私が何をしていてもループしません。私はキャンバス、javascript、そして一般的なコードにはかなり新しいです。キャンバスJavascript Looping
var canvas = document.getElementById("fabrication");
var ctx = canvas.getContext("2d");
var background = new Image();
background.src =
"C:/Users/dylan/Desktop/ProjectTwo/Images/fabricationbackground.jpg";
background.onload = function(){
}
//Loading all of my canvas
var posi =[];
posi[1] = 20;
posi[2] = 20;
var dx=10;
var dy=10;
var ballRadius = 4;
//Variables for drawing a ball and it's movement
function drawballleft(){
posi =xy(posi[1],posi[2])
}
function xy(x,y){
ctx.drawImage(background,0,0);
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#FFFFFFF";
ctx.fill();
ctx.closePath();
var newpos=[];
newpos[1]= x +dx;
newpos[2]= y +dy;
return newpos;
//Drawing the ball, making it move off canvas.
if (newpos[1] > canvas.width) {
newpos[1] = 20;
}
if (newpos[2] > canvas.height) {
newpos[2] = 20;
}
//If statement to detect if the ball moves off the canvas, to make it return to original spot
}
setInterval(drawballleft, 20);
//Looping the function
私が間違ったことをした場合は、私が実際にここでやっていることを学びたいと思います。ボールはキャンバスから外れて、それ自体の上にループバックするはずですが、キャンバスから外れて終了します。
ありがとうございます!
は(.pushを使用してnewpos値を設定してみてください)(20)そうnewpos.push様およびその配列のインデックスは 'ファイルを使用しないでください0ない1 – luckyape
から始まり覚え://'を。実際の静的サーバーを使用します。ほとんどのIDEにはこの機能が組み込まれています。 'http-server'モジュールを使うこともできます。 – zero298