Created
March 8, 2013 14:28
-
-
Save dvberkel/5116777 to your computer and use it in GitHub Desktop.
getUserMedia demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var app = express(); | |
app.use("/static", express.static(__dirname + '/public')); | |
var port = process.env.PORT || 3000; | |
app.listen(port); | |
console.log("Listening on port " + port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var streamVideo = function(){ | |
requestAnimationFrame(streamVideo); | |
var context = canvas.getContext("2d"); | |
context.drawImage(video, 0, 0, canvas.width, canvas.height); | |
drawGrid(context,3); | |
}; | |
var drawGrid = function(context, divisions){ | |
drawHorizontalLines(context, divisions); | |
drawVerticalLines(context, divisions); | |
}; | |
var drawHorizontalLines = function(context, divisions){ | |
var width = context.canvas.width; | |
var dy = context.canvas.height / divisions; | |
for (var index = 0; index < divisions; index++){ | |
var y = dy * index; | |
context.beginPath(); | |
context.moveTo(0,y); | |
context.lineTo(width, y); | |
context.stroke(); | |
} | |
}; | |
var drawVerticalLines = function(context, divisions){ | |
var dx = context.canvas.width / divisions; | |
var height = context.canvas.height; | |
for (var index = 0; index < divisions; index++){ | |
var x = dx * index; | |
context.beginPath(); | |
context.moveTo(x,0); | |
context.lineTo(x, height); | |
context.stroke(); | |
} | |
}; | |
var video = document.querySelector("video"); | |
var canvas = document.querySelector("canvas"); | |
canvas.width = 640; | |
canvas.height = 480; | |
navigator.getUserMedia = navigator.webkitGetUserMedia; | |
navigator.getUserMedia({ "video" : true }, function(localMediaStream){ | |
video.src = window.URL.createObjectURL(localMediaStream); | |
streamVideo(); | |
}, function(e){ | |
console.log("Failed", e); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<video autoplay></video> | |
<canvas></canvas> | |
<script src="js/camera.js" type="text/javascript"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment