画布Javascript循环

时间:2017-12-29 00:26:26

标签: javascript html animation canvas

我试图循环播放动画,但无论我做什么,它都不会循环播放。我对画布,javascript和代码都很陌生。

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

如果我做错了,请告诉我,我真的想了解我在这里做的事情。球应该从画布上移开,然后循环回到自身上,但它会从画布上移开并结束。

提前致谢!

2 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改。

首先我使用requestAnimationFrame代替setIntervalhttp://www.javascriptkit.com/javatutors/requestanimationframe.shtml

其次我没有使用图像,因为我不想遇到CORS问题。但是你可以把背景图片放回来。

我简化了您的posi数组,以使用索引01代替12来清理您创建数组的方式。

我将你的return从两个if之前移动到了之后,所以当球从侧面移开时,球会向左移动或向上移动。 我认为这是你看到的真正问题



var canvas = document.getElementById("fabrication");
var ctx = canvas.getContext("2d");

//Loading all of my canvas

var posi =[20,20];
var dx=10;
var dy=10;
var ballRadius = 4;

//Variables for drawing a ball and it's movement

function drawballleft(){
  posi = xy(posi[0],posi[1])
  requestAnimationFrame(drawballleft);
}

function xy(x,y){
  ctx.fillStyle = '#FFF';
  ctx.fillRect(0,0,400,300);
  ctx.beginPath();
  ctx.arc(x, y, ballRadius, 0, Math.PI*2);
  ctx.fillStyle = "#000";
  ctx.fill();
  ctx.closePath();
  var newpos=[x+dx,y+dy];

    //Drawing the ball, making it move off canvas.

  if (newpos[0] > canvas.width) {
    newpos[0] = 20;
  }
  if (newpos[1] > canvas.height) {
    newpos[1] = 20;
  }
  //If statement to detect if the ball moves off the canvas, to make it return to original spot

  return newpos;
}

requestAnimationFrame(drawballleft);

canvas {
  outline: 1px solid red;
}

<canvas width="400" height="300" id="fabrication"></canvas>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

让一切变得更加简单...... 使用外部脚本来处理画布。

一个非常好的;): https://github.com/GustavGenberg/handy-front-end#canvasjs

将其包含在

<script type="text/javascript" src="https://gustavgenberg.github.io/handy-front-end/Canvas.js"></script>

然后就这么简单了:

// Setup canvas
const canvas = new Canvas('my-canvas', 400, 300).start(function (ctx, handyObject, now) {

    // init
    handyObject.Ball = {};

    handyObject.Ball.position = { x: 20, y: 20 };
    handyObject.Ball.dx = 10;
    handyObject.Ball.dy = 10;
    handyObject.Ball.ballRadius = 4;

  });

// Update loop, runs before draw loop
canvas.on('update', function (handyObject, delta, now) {

    handyObject.Ball.position.x += handyObject.Ball.dx;
    handyObject.Ball.position.y += handyObject.Ball.dy;

    if(handyObject.Ball.position.x > canvas.width)
      handyObject.Ball.position.x = 20;

    if(handyObject.Ball.position.y > canvas.height)
      handyObject.Ball.position.y = 20;

});

// Draw loop
canvas.on('draw', function (ctx, handyObject, delta, now) {

    ctx.clear();

    ctx.beginPath();

    ctx.arc(handyObject.Ball.position.x, handyObject.Ball.position.y, handyObject.Ball.ballRadius, 0, Math.PI * 2);

    ctx.fillStyle = '#000';
    ctx.fill();

    ctx.closePath();

});

我重新组织了您的代码并使用了外部脚本,现在它看起来更清晰,更容易阅读和填充!

JSFiddle:https://jsfiddle.net/n7osvt7y/