Glitchcam
Playing with glitching up webcam input
by mich
HTML
<video id="recording" autoplay></video>
<canvas id="output" width="640" height="480"></canvas>
<div id="controls">
<button id="screenshot">Snapshot</button>
</div>
<div id="screenshots">
</div>
CSS
body {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
canvas, #controls {
display: block;
margin: 20px auto;
}
canvas {
width: 640px;
height: 480px;
border: solid 2px #AAA;
}
video {
position: fixed;
width: 160px;
height: 120px;
top: 0;
right: 0;
opacity: 0.4;
box-shadow: 0 0 4px 1px #000;
transition: all 500ms ease;
}
video:hover {
opacity: 1;
width: 320px;
height: 240px;
}
#controls {
padding: 20px;
background-color: #DDD;
width: 350px;
text-align: center;
}
#screenshots {
width: 640px;
margin: 20px auto;
}
#screenshots img {
margin 20px auto;
}
JavaScript
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
(function () { // SET UP CAMERA, not part of fun effects
var getUserMedia;
function hasGetUserMedia() {
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
getUserMedia = (function () {
var vendorFunc = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
return function () {
return Function.prototype.apply.apply(vendorFunc, [navigator, arguments]);
};
}());
if (!hasGetUserMedia()) {
return alert('Yo, get a modern browser.');
}
getUserMedia({
video: true
}, function (localMediaStream) {
document.getElementById('recording').src = window.URL.createObjectURL(localMediaStream);
}, function () {
alert('REJECTED.');
});
}());
(function () { // FUN BEGINS HERE
var
vid = document.getElementById('recording'),
cv = document.getElementById('output'),
ctx = cv.getContext('2d'),
bgCv = document.createElement('canvas'),
bgCtx = bgCv.getContext('2d'),
buffer = null,
linePointer = 0,
lines = [],
thickness =1,
uiControls = {
},
settings = {
};
bgCv.width = cv.width;
bgCv.height = cv.height;
function drawPortion(img, sourceCoords, sourceDim, destCoords, destDim) {
var imgData;
ctx.drawImage(img, sourceCoords.x, sourceCoords.y, sourceDim.w, sourceDim.h, destCoords.x, destCoords.y, destDim.w, destDim.h);
imgData =...