【问题标题】:How do I Improve my canvas animation speed?如何提高画布动画速度?
【发布时间】:2014-02-16 03:15:47
【问题描述】:

基本上我有一些代码可以生成图像数据并将其放入画布中。

但是,当我放大窗口时,渲染速度会大大减慢。

有没有办法优化它以在全屏下流畅运行? (天真我知道)

优化它的最佳方法是什么?

这是运行的代码:http://jsfiddle.net/AMwNc/3/

这是我的代码:

HTML:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>_#Example</title>
  <meta name="description" content="_#Example">
  <meta name="author" content="SitePoint">
  <link rel="stylesheet" href="css/main.css?v=1.0">

  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>

<body>
  <script src="js/main.js"></script>
  <div id="containingDiv">
    <canvas class = "board" id = "mainBoard">Your browser version is not supported</canvas>
  </div>
</body>
</html>

CSS:

#containingDiv { 
  overflow: hidden;
}
#mainBoard {
  position: absolute; 
  top: 0px;
  left: 0px;
} 

JS:

var canvas, ctx, frame;

function initAll(){
    canvas = document.getElementById('mainBoard');
    buffer = document.createElement('canvas');
    ctx = canvas.getContext('2d');
    frame = ctx.createImageData(ctx.canvas.height,ctx.canvas.width);
};

function resizeCanvas() {
    ctx.canvas.width  = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
};

function draw(){
    frame = ctx.createImageData(ctx.canvas.width, ctx.canvas.height);
    for(var i=0;i<ctx.canvas.height; i++){
        for(var j=0; j<ctx.canvas.width; j++){
          var index  = ((ctx.canvas.width * i) + j) * 4;
          frame.data[index]= 255*Math.random();
          frame.data[index + 1]=255*Math.random();
          frame.data[index + 2]=255*Math.random();
          frame.data[index + 3]=255;
        }
    }
    ctx.putImageData(frame, 0, 0 );
};

window.requestAnimFrame = (function(callback) {
        return window.requestAnimationFrame ||
         window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame ||
           window.oRequestAnimationFrame || 
           window.msRequestAnimationFrame ||
        function(callback) {
          window.setTimeout(callback, 2000);
        };
      })();

function animate() {
    draw();
    requestAnimFrame(function() {
        animate();
    });
};

function viewDidLoad(){
    initAll();
    resizeCanvas();
    animate();
};


window.onload = viewDidLoad;
window.onresize = resizeCanvas;

【问题讨论】:

    标签: javascript performance animation html5-canvas doublebuffered


    【解决方案1】:

    大多数 HTML5 画布实现确实从使用 GPU 硬件加速进行高级操作功能(如 drawImage 或描边路径)中受益匪浅。不幸的是,对原始图像数据的任何操作都不是硬件加速的。您生成随机像素数据的算法是在 CPU 上执行的,而不是在 GPU 上执行的,所以速度很慢。

    如果您想要进行大规模像素级操作并仍然保持其性能,您可能需要了解 WebGL 和像素着色器。这将允许您在 GPU 上实现像素级算法,速度可以提高几个数量级。

    【讨论】:

    • 我喜欢这个主意。我会看看它。谢谢。
    【解决方案2】:

    我看到你打电话了

     frame = ctx.createImageData(ctx.canvas.height,ctx.canvas.width);
    

    initAll() 和 draw() 中的 2 次。在调整画布大小之前创建 imageData。 而且我不知道在 animate() 中你处理什么来绘制画布。

    我的建议:您应该在 initAll() 中删除该调用。希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 2020-09-23
      • 2011-09-20
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      相关资源
      最近更新 更多