summaryrefslogtreecommitdiff
path: root/cam.html
blob: 25eb177b6a4b8fe0079aeeaf9d4c5a5efc7d05f3 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!doctype html>
<html>
<body>
<div id="state"></div><br>
frames: <input type="text" id="frameCount">
<div id="buttons"></div>
<video id="vid" style="display: none"></video>
<button id="camera" style="display:none">camera</button>
</body>
<script src="/js/jquery.js"></script>
<script src="canvasquery.js"></script>
<script src="http://asdf.us/dither/js/camera.js"></script>
<script>
var images = "gradient_top.png gradient_bot.png gradient_left.png gradient_right.png gradient_radial.png gradient_bands.png gradient_radial_bands.png swoosh.png swoosh_blur.png louisvuitton.jpg".split(" ")
var state = document.getElementById('state')
for (var i in images) {
  button(images[i]);
}
function button (src) {
  var el = document.createElement("button");
  el.innerHTML = src;
  el.onclick = function(){ loadImage(src) }
  document.getElementById("buttons").appendChild( el );
}
var frameCountEl = document.getElementById("frameCount");
var frameCount = frameCountEl.value = 255
frameCountEl.addEventListener("keyup", function(){
  var _this = this;
  setTimeout(function(){
    frameCount = parseInt( _this.value.replace(/[^0-9]/g,"") )
  });
}, false)

var cc = cq(1,1)
min = Math.min
$("#camera").trigger("click")
var checker = setInterval(function(){
  if (window.cam) {
    clearInterval(checker)
    video = cam
    loaded()
  }
}, 200)
function loaded () {
  src = cq(w, h)
  dest = cq(w, h).appendTo("body");
  loadImage( images[0] )
  video.play()
}

state.innerHTML = "loading";

function loadImage(src){
  var image = new Image ();
  image.addEventListener("load", function(){
    var iq = cq(w, h)
              .drawImage( image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0, w, h )
    imageData = iq.getImageData( 0, 0, w, h )
    mask = iq.grayscaleToMask()
    console.log('got it')
  }, false);
  image.src = src;
}

var src, dest, mask, fill, imageData;
var frames = [];
var ready = false;
(function animate(){
  requestAnimationFrame(animate);
  if (! src || ! mask) return;
  storeFrame();
  if (ready) render();
})();
function storeFrame(){
  src.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, w, h)
  var data = src.getImageData(0, 0, w, h)
  if (frames.length > frameCount) {
    frames.shift()
    if (! ready) {
      ready = true;
      state.innerHTML = "playing"
    }
  }
  else {
    state.innerHTML = "buffering " + frames.length;
  }
  frames.push(data.data)
}
function render(){
  data = imageData.data
  buffered = frames.length - 1
  for (var i = 0; i < mask.length; i++) {
    var j = i * 4;
    var n = Math.min(buffered, Math.floor( frameCount * mask[i] / 255 ))
    data[j] = frames[n][j]
    data[j+1] = frames[n][j+1]
    data[j+2] = frames[n][j+2]
  }
  dest.putImageData(imageData, 0, 0)
}

</script>
</html>