|
| 1 | + |
| 2 | +function VideoPlayer(canvasId, video){ |
| 3 | + |
| 4 | + var canvas = document.getElementById(canvasId); |
| 5 | + var ctx = canvas.getContext('2d'); |
| 6 | + var lastFrame = [] |
| 7 | + var lastVideo = 0 |
| 8 | + |
| 9 | + canvas.setAttribute("width",video.width) |
| 10 | + canvas.setAttribute("height",video.height) |
| 11 | + |
| 12 | + |
| 13 | + this.playVideo=function(video,freq){ |
| 14 | + fps=freq |
| 15 | + if (!fps){ |
| 16 | + fps=video.freq |
| 17 | + } |
| 18 | + console.log("Playing at "+fps+"fps") |
| 19 | + for(var i=0; i<video.data.length-1; i++){ |
| 20 | + lastFrame[i]=setTimeout(this.playFrame.bind(this),fps*i,i,video.data[i],video) |
| 21 | + } |
| 22 | + lastVideo=setTimeout(this.playVideo.bind(this),fps*video.data.length-2,video,freq) |
| 23 | + } |
| 24 | + |
| 25 | + this.writeText=function(ctx,text, px){ |
| 26 | + ctx.font = '10px Arial'; |
| 27 | + ctx.fillStyle='white'; |
| 28 | + ctx.fillText(text, px+5, 10); |
| 29 | + ctx.fillText(text, px+7, 12); |
| 30 | + ctx.fillStyle='black'; |
| 31 | + ctx.fillText(text, px+6, 11); |
| 32 | + } |
| 33 | + |
| 34 | + this.playFrame=function(i,frame,video){ |
| 35 | + ctx.fillRect(0,0,video.width,video.height) |
| 36 | + var d = ctx.createImageData(video.frame_width, video.frame_height); |
| 37 | + var f=0; |
| 38 | + for(var px=0;px<frame.length; px++){ |
| 39 | + var value=parseInt(frame[px],10) |
| 40 | + d.data[f]=value*26; f++; |
| 41 | + if((px+1)%3==0){ |
| 42 | + d.data[f]=255; |
| 43 | + f++; |
| 44 | + } |
| 45 | + } |
| 46 | + var padx=(video.width-video.frame_width)/2 |
| 47 | + ctx.putImageData(d, padx, 0); |
| 48 | + this.writeText(ctx,"Frame "+i,padx) |
| 49 | + } |
| 50 | + |
| 51 | + this.stopVideo=function(){ |
| 52 | + this.pauseVideo(); |
| 53 | + window.clearTimeout(lastVideo) |
| 54 | + lastVideo=0 |
| 55 | + } |
| 56 | + |
| 57 | + this.pauseVideo=function(){ |
| 58 | + for(var f=0; f<lastFrame.length; f++){ |
| 59 | + window.clearTimeout(lastFrame[f]) |
| 60 | + } |
| 61 | + lastFrame=[] |
| 62 | + } |
| 63 | + |
| 64 | + this.startVideo=function(){ |
| 65 | + if (lastFrame.length){ |
| 66 | + return |
| 67 | + } |
| 68 | + this.playVideo(video) |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments