我制作了一个HTML5视频播放器,将其加载到画布中进行操作,然后再将其返回到画布中进行显示。视频开始播放的速度相当缓慢,并且每次播放时帧率只会变得更差。目前,我正在视频中操作的只是颜色值,在视频暂停时进行操作,但将来将使用实时操作在未来发布的视频中进行操作。
我使用以下教程学习了这个技巧:https://www.youtube.com/watch?v=zjQzP3mOXdc 以下是相关代码,但可能会有其他地方的干扰,所以请随意查看下面链接中的源代码。
任何帮助都将不胜感激!谢谢!
我使用以下教程学习了这个技巧:https://www.youtube.com/watch?v=zjQzP3mOXdc 以下是相关代码,但可能会有其他地方的干扰,所以请随意查看下面链接中的源代码。
var v = document.getElementById('video');
var color = "#DA7AC1";
var processes={
timerCallback:function() {
if (this.v2.paused || this.v2.ended) {
return;
}
this.ctxIn.drawImage(this.v2,0,0,this.width,this.height);
this.pixelScan();
var self=this;
setTimeout(function() {
self.timerCallback();
}, 0);
},
doLoad:function(){
this.v2=document.getElementById("video");
this.cIn=document.getElementById("cIn");
this.ctxIn=this.cIn.getContext("2d");
this.cOut=document.getElementById("cOut");
this.ctxOut=this.cOut.getContext("2d");
var self=this;
this.v2.addEventListener("playing", function() {
self.width=self.v2.videoWidth;
self.height=self.v2.videoHeight;
cIn.width=self.v2.videoWidth;
cIn.height=self.v2.videoHeight;
cOut.width=self.v2.videoWidth;
cOut.height=self.v2.videoHeight;
self.timerCallback();
}, false);
},
pixelScan: function() {
var frame = this.ctxIn.getImageData(0,0,this.width,this.height);
for(var i=0; i<frame.data.length;i+=4) {
var grayscale=frame.data[i]*.3+frame.data[i+1]*.59+frame.data[i+2]*.11;
frame.data[i]=grayscale;
frame.data[i+1]=grayscale;
frame.data[i+2]=grayscale;
}
this.ctxOut.putImageData(frame,0,0);
return;
}
}
任何帮助都将不胜感激!谢谢!
requestAnimationFrame
来解决。var self=this; setTimeout(function() { self.timerCallback(); }, 0);
- Tomelower