从HTML5视频元素同步加载WebGL纹理

时间:2015-11-05 10:07:59

标签: html5-video webgl

如果重复从HTML5视频元素加载WebGL纹理,我该如何同步加载过程,例如。加载纹理并为每个视频帧渲染一次WebGL场景?

1 个答案:

答案 0 :(得分:1)

Chrome浏览器中现在有requestVideoFrameCallback

规范示例:

<body>
  <video controls></video>
  <canvas width="640" height="360"></canvas>
  <span id="fps_text"/>
</body>

<script>
  function startDrawing() {
    var video = document.querySelector('video');
    var canvas = document.querySelector('canvas');
    var ctx = canvas.getContext('2d');

    var paint_count = 0;
    var start_time = 0.0;

    var updateCanvas = function(now) {
      if(start_time == 0.0)
        start_time = now;

      ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

      var elapsed = (now - start_time) / 1000.0;
      var fps = (++paint_count / elapsed).toFixed(3);
      document.querySelector('#fps_text').innerText = 'video fps: ' + fps;

      video.requestVideoFrameCallback(updateCanvas);
    }

    video.requestVideoFrameCallback(updateCanvas);

    video.src = "http://example.com/foo.webm"
    video.play()
  }
</script>

用您的WebGL代码替换ctx.drawImage