本文介绍了canvas像素点操作之视频绿幕抠图,分享给大家,具体如下:
用法:
context.putImageData(imgData, x, y, dX, dY, dWidth, dHeight);
该栗子纯属为了演示功能而做,如果只强调效果而不在乎数据的话,用CSS3的filter属性便能高效又轻松地搞定。
部分代码
import imgUrl from './component/sample.jpg'; export default { data () { return { imgUrl: imgUrl } }, methods: { onOperate1 () { this.ctx.putImageData(this.onCompute1(), 0, 0); }, onOperate2 () { this.ctx.putImageData(this.onCompute2(), 0, 0); }, ... onCancel () { this.reload(); }, onCompute1 () { let data = this.frameData.data; for (let i = 0; i上周跟同学去了一趟溧阳天目湖的南山竹海,在景区被忽悠拍了一张照片,就是这张 ——
部分代码
import videoUrl from &#39;./component/video.ogv&#39;; import imgUrl from &#39;./component/sample.jpg&#39;; const TOLERANCE = 5; export default { data () { return { videoUrl: videoUrl, imgUrl: imgUrl } }, methods: { draw () { if (this.video.paused || this.video.ended) { return; } this.ctx.drawImage(this.video, 0, 0, this.width, this.height); this.ctx.putImageData(this.cutOut(), 0, 0); }, cutOut () { let frameData = this.ctx.getImageData(0, 0, this.width, this.height), len = frameData.data.length / 4; for (let i = 0; i= TOLERANCE && g - 100 >= TOLERANCE && b - 43 <= TOLERANCE) { frameData.data[i * 4 + 3] = 0; } } return frameData; } }, mounted () { this.video = this.$refs[&#39;video&#39;]; this.canvas = this.$refs[&#39;canvas&#39;]; this.ctx = this.canvas.getContext(&#39;2d&#39;); this.timer = null; this.video.addEventListener(&#39;play&#39;, () => { this.width = this.video.videoWidth; this.height = this.video.videoHeight; this.timer && clearInterval(this.timer); this.timer = setInterval(() => { this.draw(); }, 50); }, false); } } 参考资料
Manipulating video using canvas
Pixel manipulation with canvas
Canvas and images and pixels
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问Html5视频教程!