简体   繁体   English

在 canvas 上绘制 html5 视频 - 在 iPad 上

[英]drawing html5 video on a canvas - on iPad

I'm drawing a video on a canvas, this works fine with Safari / Chrome / Firefox / Opera, but on the iPad, even though the video plays, (correct codec, etc) it is never rendered on the canvas, I'm drawing a video on a canvas, this works fine with Safari / Chrome / Firefox / Opera, but on the iPad, even though the video plays, (correct codec, etc) it is never rendered on the canvas,

Basically I just call:基本上我只是打电话:

canvas.getContext("2d").drawImage(video, 0, 0);

when the video is playing, and stop doing this when the video is paused or ended.当视频正在播放时,当视频暂停或结束时停止播放。

Is there anything else I should consider?还有什么我应该考虑的吗? Like clearing the canvas?清除canvas 一样?

For now safari on iPad is not supporting this feature.目前 iPad 上的 safari 不支持此功能。 There are some limits on the attributes and events of canvas tag and video tag of HTML5 particularly on iPad. canvas 标签和 HTML5 的视频标签的属性和事件有一些限制,特别是在 iPad 上。 The attributes and events of canvas and video tags which work fine on desktop browsers wont work on iPad. canvas 的属性和事件以及在桌面浏览器上正常工作的视频标签在 iPad 上不起作用。 This is my personal experience too.这也是我的个人经历。

Have you tried wrapping it inside the requestAnimationFrame() function.您是否尝试将其包装在 requestAnimationFrame() function 中。

<video src="YourSrcFile.webm" autolay muted></video>
         // Fallback to mp4 if not supported.
<canvas></canvas>
    const video = document.querySelector("video");      // Offscreen Canvas.
    const canvas = document.querySelector("canvas");    // Onscreen Canvas.

          canvas.style.zIndex = '50';
    const ctx = canvas.getContext("2d");

    video.addEventListener('play',()=>{
      function step() {
        ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
        requestAnimationFrame(step)
      }
      requestAnimationFrame(step);
    })

Make sure to Match both the onscreen & offscreen canvas to the original videos aspect ratio otherwise the extra calculations make it laggy & poor performance.. You can use Transform Scale inside your css to resize it aslong as its proportionately.确保将屏幕上和屏幕外 canvas 与原始视频宽高比相匹配,否则额外的计算会使其滞后和性能不佳。您可以在 css 中使用 Transform Scale 来按比例调整其大小。 Which doesn't seem to make it glitchy, but I'd suggest converting the video from mp4, avi or other file type to webm..这似乎不会让它出现故障,但我建议将视频从 mp4、avi 或其他文件类型转换为 webm ..

just used this for a vjloop and its running smooth.只是将其用于 vjloop 并且运行平稳。

Try these and see if it makes any difference..试试这些,看看是否有什么不同..

<script>
document.addEventListener('DOMContentLoaded', function(){
    var v = document.getElementById('v');
    var canvas = document.getElementById('c');
    var context = canvas.getContext('2d');

    var cw = Math.floor(canvas.clientWidth / 100);
    var ch = Math.floor(canvas.clientHeight / 100);
    canvas.width = cw;
    canvas.height = ch;

    v.addEventListener('play', function(){
        draw(this,context,cw,ch);
    },false);

},false);

function draw(v,c,w,h) {
    if(v.paused || v.ended) return false;
    c.drawImage(v,0,0,w,h);
    setTimeout(draw,20,v,c,w,h);
}
</script>

See Putting Video on Canvas请参阅在 Canvas 上放视频

You basically can't pass a video object to the canvas drawImage method.您基本上不能将视频 object 传递给 canvas drawImage 方法。 Apple suggests having the video positioned behind the canvas but this won't help if you want to manipulate the video somehow. Apple 建议将视频放在 canvas 后面,但如果您想以某种方式操纵视频,这将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM