Skip to content

Commit

Permalink
Revert "Updated reference to camera.js"
Browse files Browse the repository at this point in the history
This reverts commit 92ae7ba.
  • Loading branch information
idevelop committed Feb 27, 2013
1 parent 92ae7ba commit d0e3e9b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>Your browser does not support the Camera API.</h1>

<a href="https://github.com/idevelop/ascii-camera" id="fork" target="_blank">View on GitHub</a>

<script src="script/camerajs/camera.js"></script>
<script src="script/camera.js"></script>
<script src="script/ascii.js"></script>
<script src="script/app.js"></script>
</body>
Expand Down
85 changes: 85 additions & 0 deletions script/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Author: Andrei Gheorghe (http://github.com/idevelop)

var camera = (function() {
function initCamera(options) {
var video = document.createElement("video");
video.setAttribute('width', options.width);
video.setAttribute('height', options.height);

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;

if (navigator.getUserMedia) {
navigator.getUserMedia({
video: true
}, function(stream) {
options.onSuccess();

if (video.mozSrcObject !== undefined) { // hack for Mozilla
video.mozSrcObject = stream;
} else {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
}

startVideoStream(video, options);
}, options.onError);
} else {
options.onNotSupported();
}
}

function startVideoStream(video, options) {
var canvas = options.targetCanvas || document.createElement("canvas");
canvas.setAttribute('width', options.width);
canvas.setAttribute('height', options.height);

var context = canvas.getContext('2d');

// mirror video
if (options.mirror) {
context.translate(canvas.width, 0);
context.scale(-1, 1);
}

video.play();

setInterval(function() {
try {
context.drawImage(video, 0, 0, video.width, video.height);
options.onFrame(canvas);
} catch (e) {
// TODO
}
}, Math.round(1000 / options.fps));
}

return {
init: function(options) {
var doNothing = function(){};

options = options || {};
options.fps = options.fps || 30;
options.width = options.width || 640;
options.height = options.height || 480;
options.mirror = options.mirror || false;
options.targetCanvas = options.targetCanvas || null;

options.onSuccess = options.onSuccess || doNothing;
options.onError = options.onError || doNothing;
options.onNotSupported = options.onNotSupported || doNothing;
options.onFrame = options.onFrame || doNothing;

initCamera(options);
},

stop: function() {
video.pause();

if (video.mozSrcObject !== undefined) {
video.mozSrcObject = null;
} else {
video.src = "";
}
}
};
})();

0 comments on commit d0e3e9b

Please sign in to comment.