summaryrefslogtreecommitdiff
path: root/js/camera.js
blob: ef4898ac69f61db8cdc75bad64ea074172b5aeb2 (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
62
63
64
65
66
67
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(){
    // defer here if necessary.. firefox fires "canplay" before videoWidth is available
    if (! video.videoWidth) {
      setTimeout(ready, 50)
      return
    }
    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(e){
      if (! loaded) {
        loaded = true
        ready()
      }
    }, false);
  }
})()