summaryrefslogtreecommitdiff
path: root/js/camera.js
blob: 3a0c8b4a02a00ab77d79d08e0eb585a915aceb8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var camera = (function(){
	var loaded = false, video

	navigator.getMedia = ( navigator.getUserMedia ||
												 navigator.webkitGetUserMedia ||
												 navigator.mozGetUserMedia ||
												 navigator.msGetUserMedia);

  if (! navigator.getMedia) {
    $("#camera").hide()
    return
  }

	$("#camera").click(load)
	function load(){
		if (! loaded) {
			build()
		}
		else {
			ready()
		}
		window.gif = window.img = null
	}

	function ready(){
		cc.canvas.width = actual_w = w = min(video.videoWidth, 400)
		cc.canvas.height = actual_h = h = video.videoHeight / (video.videoWidth/w)
		video.setAttribute('width', video.videoWidth)
		video.setAttribute('height', video.videoHeight)
		window.gif = window.img = null
		window.cam = video
	}
	
	function build(){
		video = document.createElement("video")
		navigator.getMedia({
				video: true,
				audio: false
			},
			function(stream) {
				if (navigator.mozGetUserMedia) {
					video.mozSrcObject = stream;
				} else {
					var vendorURL = window.URL || window.webkitURL;
					video.src = vendorURL.createObjectURL(stream);
				}
				video.play();
			},
			function(err) {
				console.log("An error occured! " + err);
			}
		)

		video.addEventListener('canplay', function(ev){
			if (! loaded) {
				loaded = true
				ready()
			}
		}, false);
	}
})()