Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@minowa_keita

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

パンチ.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/

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?