html5㧠videoã®ä»»æã®ãã¬ã¼ã ãcanvasã«æç»ããã¡ã¢
ãããµã2010ã®html5ã»ãã·ã§ã³ã§è¦ããã¢ï¼Movement trackerï¼*1 ãã
è¡æçã ã£ãã®ã§ video x canvas ãã¡ãã£ã¨åå¼·ããã
ï¼ï¼videoã®ä»»æã®ãã¬ã¼ã ããã®ã¾ã¾canvasã«æç»ããã¦ã¿ã
htmlã®bodyã«æ¸ãã«ã¯ããã ãã
<video id="v" src="video.ogv" autobuffer controls></video> <canvas id="c"></canvas> <input type="button" value="copy frame" onclick="copyFrame()" />
copy frameãã¿ã³ã§å¼ã³åºãããjavascriptã¯ãããªæãã
function copyFrame() { var cEle = document.getElementById('c'); var cCtx = cEle.getContext('2d'); var vEle = document.getElementById('v'); cEle.width = vEle.videoWidth; // canvasã®å¹ ã¨é«ãããåç»ã®å¹ ã¨é«ãã«åããã cEle.height = vEle.videoHeight; cCtx.drawImage(vEle, 0, 0); // canvasã«é¢æ°å®è¡æã®åç»ã®ãã¬ã¼ã ãæç» }
ããã§ãåç»åçãããªããcopy frameãã¿ã³ãããããããã¨ã
ãã®ã¿ã¤ãã³ã°ã®ãã¬ã¼ã ããcanvasã«æç»ãããã
ã¨ã£ã¦ãç°¡åã
ï¼ï¼videoããåå¾ãããã¬ã¼ã ã®æ å ±ãããã£ã¦ã¿ã
åç´ãªã°ã¬ã¼ã¹ã±ã¼ã«åããã¦ã¿ãã
htmlã¯ãã®ã¾ã¾ã
javascriptã«ã¡ãã£ã¨ã ãã³ã¼ãã追å ã
function copyFrame() { var cEle = document.getElementById('c'); var cCtx = cEle.getContext('2d'); var vEle = document.getElementById('v'); cEle.width = vEle.videoWidth; cEle.height = vEle.videoHeight; cCtx.drawImage(vEle, 0, 0); // ããããä¸ã追å var imgdata = cCtx.getImageData(0, 0, vEle.videoWidth, vEle.videoHeight); // pixelãã¼ã¿ãåå¾ for (var i=0; i<imgdata.data.length; i+=4) { // imgdata.data: [R,G,B,alpha,R,G,B,alpha...] var gray = (imgdata.data[i] + imgdata.data[i+1] + imgdata.data[i+2]) / 3; // ã°ã¬ã¼ã¹ã±ã¼ã«å¤è¨ç® imgdata.data[i] = imgdata.data[i+1] = imgdata.data[i+2] = gray; // RGB imgdata.data[i+3] = 255; // alpha } cCtx.putImageData(imgdata, 0, 0); // æç» }
ããã§canvasã«æç»ããããã¬ã¼ã ããã°ã¬ã¼ã¹ã±ã¼ã«ã«ã
drawImage()ããã«ãç´æ¥videoããpixelãã¼ã¿ãåå¾ãããã£ããã©ã
ããã£ã½ãã¡ã½ãããè¦ã¤ãããªãã£ãorz
åã¡ã½ãããªã©ã®è©³ç´°ã¯ãCanvasリファレンス - HTML5.JP ãåç §ã