如何在fabric.js中影响视频

时间:2018-04-26 15:02:00

标签: javascript jquery html5 html5-canvas fabricjs

我希望将图片过滤器用于视频。 (http://fabricjs.com/image-filters

但它停止了视频。 如何在fabric.js中影响视频?

例如,如何更改视频的颜色?

https://codepen.io/html5andblog/pen/dmKJH

↑我不使用CSS过滤器。 有人可以解释一下吗?

感谢。

<!DOCTYPE html>
<html>
<head>
<meta charset="SHIFT-JIS">
<title>sample</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.min.js"></script>
<script>


$(document).ready(function() {

    canvas = new fabric.Canvas('c');
    canvas.setWidth(480);
    canvas.setHeight(360);

    var video1El = document.getElementById('video1');
    var video1 = new fabric.Image(video1El, {
      left: 0,
      top: 0
    });

    canvas.add(video1);
    video1.getElement().load();


    $(document.body).on('click', '#play' ,function(){
        video1.getElement().play();

        //↓It's stops video.
        /*
        var filter = new fabric.Image.filters.BlendColor({
            color:'red',
            mode: 'tint',
            alpha: 0.5
        });
            canvas.item(0).filters.push(filter);
        canvas.item(0).applyFilters();
        */


    });




    fabric.util.requestAnimFrame(function render() {
      canvas.renderAll();
      fabric.util.requestAnimFrame(render);
    });

});
</script>

</head>
<body>
<button id="play">play</button>
<canvas id="c" width="300" height="300"></canvas>

<video id="video1" style="display: none" class="canvas-img" width="480" height="360">
  <source id="video_src1" src="https://html5demos.com/assets/dizzy.mp4" type="video/mp4">
</video>

</body>
</html> 

1 个答案:

答案 0 :(得分:2)

好的,所以可以做到,问题是fabricJS要更快的缓存纹理,并避免在每个applyfilters中将它们拉到视频卡中以节省时间。

在这种情况下,我必须修改你的代码n 2个地方:

1)applyFilters需要运行每个动画帧

2)每次都必须删除纹理的缓存

最难的部分是找到一个启用了cors的视频。

$(document).ready(function() {

    canvas = new fabric.Canvas('c');
    canvas.setWidth(480);
    canvas.setHeight(360);

    var video1El = document.getElementById('video1');
    var video1 = new fabric.Image(video1El, {
      left: 0,
      top: 0
    });

    canvas.add(video1);
    video1El.load();


    $(document.body).on('click', '#play' ,function(){
        video1El.play();
        var filter = new fabric.Image.filters.BlendColor({
            color:'red',
            mode: 'tint',
            alpha: 0.5
        });
        video1.filters = [filter];
    });




    fabric.util.requestAnimFrame(function render() {
      var image = canvas.item(0);
      var backend = fabric.filterBackend;
      if (backend && backend.evictCachesForKey) {
        backend.evictCachesForKey(image.cacheKey);
        backend.evictCachesForKey(image.cacheKey + '_filtered');
      }
      canvas.item(0).applyFilters();
      canvas.renderAll();
      fabric.util.requestAnimFrame(render);
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.min.js"></script>
<button id="play">play</button>
<canvas id="c" width="300" height="300"></canvas>
<video crossorigin="anonymous" id="video1" style="display: none" class="canvas-img" width="480" height="360">
  <source id="video_src1" src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">
</video>