LoginSignup
5
1

More than 5 years have passed since last update.

HTML,Webカメラで残像パンチを打ってみた

Last updated at Posted at 2018-07-22

パンチ.gif

やり方

Webカメラをブラウザ上に表示

CanvasにWebカメラの画像を書き出す
書き出す時の透明度を0.5に設定

setInterval で繰り返し

<body>
残像
<canvas id="canvas" width="400" height="300" style="border: solid 1px"></canvas>
元動画
<video id="camera" width="400" height="300" autoplay="1" class="camera" style="border: solid 1px"></video>


<script type="text/javascript">
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || window.navigator.mozGetUserMedia;
window.URL = window.URL || window.webkitURL;

var video = document.getElementById('camera');
var localStream = null;
navigator.getUserMedia({video: true, audio: false}, 
    function(stream) { // for success case
    console.log(stream);
    video.src = window.URL.createObjectURL(stream);
    localStream = stream;
    },
    function(err) { // for error case
    console.log(err);
    }
);

//canvasに描画権利を与える
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var img = document.getElementById('img');

//videoの縦幅横幅を取得
var w = video.offsetWidth;
var h = video.offsetHeight;

//同じサイズをcanvasに指定
canvas.setAttribute("width", w);
canvas.setAttribute("height", h);

var record = function(){
    //canvasにvideoタグ画像をコピー
    ctx.globalAlpha = 0.5;
    ctx.drawImage(video, 0, 0, w, h);
}

setInterval(function(){
    record();
},100);
</script>
 </body>


参考URL
カメラを使ってみよう ーWebRTC入門2016
https://html5experts.jp/mganeko/19728/

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1