如何在循环中呈现HTML5画布

时间:2016-05-27 06:36:48

标签: javascript html5 html5-canvas

我正在做以下事情:

我正在运行一个for循环,每次我获得一个新信息并希望在画布上绘制它。 问题是画布是在循环结束后绘制的,我看不到没有动画。 如果我介入调试或使用警报消息,它将立即绘制到画布。我该怎么办?

这是我的代码,你可以看到我已经尝试过多种方式的setTimeout但是没有用。

for(var i=0;i<1000; i ++)
    {
        addLines(ga.getBestCreature());   // This method draw on the canvas 

        setTimeout(function() {
        ga.startEvolution(1);       // This method change the best creature
        }, 10);   
    }

淹没方法:

function addLines(best) {

    if(!best) {
        return;
    }
    var global_path = best.data;

    redraw(); // Clear off the canvas

    var cityA;
    var cityB;
    for (var i = 0; i < (global_path.length - 1); i++) {

        cityA = ga.cities[global_path[i]];
        cityB = ga.cities[global_path[i+1]];

        ctx.moveTo(cityA.x,cityA.y);
        ctx.lineTo(cityB.x,cityB.y);
        ctx.stroke();
    }
}

任何想法?

更新:在被建议使用requestAnimationFrame之后,我发现了类似的东西。这是最好的approuch吗? 我正在更新一个名为'run'的点击事件

的全局变量
var run = 0;
function animate() {

    if(run > 0) {
        ga.startEvolution(1);
        run--;
    }

addLines(ga.getBestCreature());
    requestAnimationFrame(animate);
}

animate();

2 个答案:

答案 0 :(得分:1)

您可以查看requestAnimationFramehttps://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

我们就像这样:

function loop() {
    addLines(ga.getBestCreature());

    // execute loop function over and over
    requestAnimationFrame(loop);
}

答案 1 :(得分:1)

最好的方法是分开关注点:让动画循环持续运行,并且每次[你]获得新信息&#34;,更新你的场景描述,它会在显示时立即绘制准备好了。

这是第一个非常简单的演示:这里有新的信息&#39;是用户点击。

&#13;
&#13;
var cv = document.getElementById('cv');
var ctx = cv.getContext('2d');
var cvWidth = cv.width;
var cvHeight = cv.height;

// Simple scene description : an array of colored rects
var everyObject = [
  [60, 70, 30, 30, 'yellow']
];

// animation : always running loop.

function animate() {
  // call again next time we can draw
  requestAnimationFrame(animate);
  // clear canvas
  ctx.clearRect(0, 0, cvWidth, cvHeight);
  // draw everything
  everyObject.forEach(function(o) {
    ctx.fillStyle = o[4];
    ctx.fillRect(o[0], o[1], o[2], o[3]);
  });
  // 
  ctx.fillStyle = '#000';
  ctx.fillText('click to add random rects', 10, 10);
}

animate();


// click handler to add random rects
window.addEventListener('click', function() {
  addRandRect();
});

function addRandRect() {
  var randColor = Math.random() > 0.5 ? 'blue' : 'red';
  everyObject.push([Math.random() * cvWidth, Math.random() * cvHeight, 10 + Math.random() * 40, 10 + Math.random() * 40, randColor]);
}
&#13;
<canvas id='cv' width=400 height=200></canvas>
&#13;
&#13;
&#13;

现在,如果你想要某种动画,或者使用setTimeOut的简单版本是:

&#13;
&#13;
var cv = document.getElementById('cv');
var ctx = cv.getContext('2d');
var cvWidth = cv.width;
var cvHeight = cv.height;

// Simple scene description : an array of colored rects
var everyObject = [
  [60, 70, 30, 30, 'yellow']
];

// animation : always running loop.

function animate() {
  // call again next time we can draw
  requestAnimationFrame(animate);
  // clear canvas
  ctx.clearRect(0, 0, cvWidth, cvHeight);
  // draw everything
  everyObject.forEach(function(o) {
    ctx.fillStyle = o[4];
    ctx.fillRect(o[0], o[1], o[2], o[3]);
  });
  // 
  ctx.fillStyle = '#000';
  ctx.fillText('click to add 4 random rects with a delay', 10, 10);
}

animate();


// click handler to add random rects
window.addEventListener('click', function() {
  addRandRect();
  setTimeout(addRandRect, 300);
  setTimeout(addRandRect, 600);
  setTimeout(addRandRect, 900);
});

function addRandRect() {
  var randColor = Math.random() > 0.5 ? 'blue' : 'red';
  everyObject.push([Math.random() * cvWidth, Math.random() * cvHeight, 10 + Math.random() * 40, 10 + Math.random() * 40, randColor]);
}
&#13;
  <canvas id='cv' width=400 height=200></canvas>
&#13;
&#13;
&#13;

(顺便提一下,如果我感兴趣的话,我会在这里写下动画:http://codepen.io/gamealchemist/post/animationcanvas1