使用Canvas实时处理Video

HTML5:使用Canvas实时处理Video

2015/09/11 · HTML5 ·
Canvas

本文由 伯乐在线 –
cucr
翻译,使用Canvas实时处理Video。唐尤华亚洲城ca88网页版,
校稿。未经许可,禁止转发!
英文出处:mozilla。欢迎加入翻译组。

结合HTML5下的videocanvas的意义,你可以实时处理摄像数据,为正值播放的视频添加各样种种的视觉效果。本课程演示怎么着行使JavaScript代码完结chroma-keying特效(也被喻为“灰色屏幕效应”)。

请看那一个实例.

HTML 5 Video, Canvas, ECMAScript 6

Canvas ,HTML 5中引入它,可以做过多作业:画图、动画、游戏支付等等。

Canvas ,HTML 5中引入它,可以做过多工作:画图、动画、游戏开发等等。

文档内容

本文使用的XHTML文档如下所示。

XHTML

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“; <html
xmlns=”; <head> <style> body
{ background: black; color:#CCCCCC; } #c2 { background-image:
url(foo.png); background-repeat: no-repeat; } div { float: left; border
:1px solid #444444; padding:10px; margin: 10px; background:#3B3B3B; }
</style> <script type=”text/javascript;version=1.8″
src=”main.js”></script> </head> <body
onload=”processor.doLoad()”> <div> <video id=”video”
src=”video.ogv” controls=”true”/> </div> <div> <canvas
id=”c1″ width=”160″ height=”96″/> <canvas id=”c2″ width=”160″
height=”96″/> </div> </body> </html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <style>
      body {
        background: black;
        color:#CCCCCC;
      }
      #c2 {
        background-image: url(foo.png);
        background-repeat: no-repeat;
      }
      div {
        float: left;
        border :1px solid #444444;
        padding:10px;
        margin: 10px;
        background:#3B3B3B;
      }
    </style>
    <script type="text/javascript;version=1.8" src="main.js"></script>
  </head>
 
  <body onload="processor.doLoad()">
    <div>
      <video id="video" src="video.ogv" controls="true"/>
    </div>
    <div>
      <canvas id="c1" width="160" height="96"/>
      <canvas id="c2" width="160" height="96"/>
    </div>
  </body>
</html>

以上代码关键部分如下:

1.创办了五个canvas元素,ID分别为c1和c2。c1用以显示当前帧的本来面目视频,c2是用来显示执行chroma-keying特效后的视频;c2预加载了一张静态图片,将用来代表摄像中的背景象部分。
2.JavaScript代码从main.js文件导入;那段脚本使用JavaScript
1.8的性状,所以在导入脚本时,第22行中指定了版本。
3.当网页加载时,main.js中的processor.doLoad()方法会运行。

<html>
<head>
<title>HTML5 Video+Canvas with ECMAScript 6</title>
<script>
class Position{
    static caculate(obj){
        let x = 0, y = 0;
        if(obj.offsetLeft && obj.offsetTop){
            x = obj.offsetLeft, y = obj.offsetTop;
        }
        let p;
        if(obj.parentNode){
            p = Position.caculate(obj.parentNode);
            x = x + p.x, y = y + p.y;
        }
        return {'x': x, 'y': y};
    }
}
class Player{
    constructor(w = 640, h = 360){
        this.w = w, this.h = h;
        this.video  = document.createElement('video');
        this.canvas = document.createElement('canvas');
        //document.body.appendChild(this.player);
        document.body.appendChild(this.canvas);
        this.video.id       = 'objPlayer'
        this.video.width    = this.w;
        this.video.height   = this.h;
        this.canvas.id      = 'objCanvas'
        this.canvas.style.position  = 'absolute';
        this.canvas.width   = this.w;
        this.canvas.height  = this.h;
        let p = Position.caculate(this.video);
        this.canvas.style.left  = p.x;
        this.canvas.style.top   = p.y;
        this.canvas.style.zIndex    = 1000;
    }
    load(url){
        this.video.src = url;
    }
    play(){
        this.video.play();
    }
    capture(){
        this.canvas.getContext('2d').drawImage(this.video, 0, 0, this.w, this.h);
    }
    record(speed){
        this.timer = window.setInterval(() => {
            this.capture();
        }, 1000/speed);
    }
}

var player;
document.onreadystatechange = function(e){
    if(document.readyState == 'complete'){
        //console.log('ready');
        player = new Player(640, 360);
        player.load('/Users/Hao/Downloads/ReshmaSaujani_2016-480p.mp4');
        player.play();
        player.record(15);
    }
}
</script>
</head>
<body>
</body>
</html>

 

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website