-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (63 loc) · 2.11 KB
/
app.js
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
// TODO: refactor more and make it configurable and
// remove the hard codes.
(function(){
function canvasMouseDown(event) {
this.style.zIndex += 20;
}
function generateBlocks(cols, rows) {
var blocks = new Array();
for (var col = 0; col < cols; col++) {
blocks[col] = new Array();
for (var row = 0; row < rows; row++) {
var canvas = document.createElement("canvas");
canvas.addEventListener("mousedown", canvasMouseDown, false);
canvas.width = width;
canvas.height = height;
blocks[col][row] = canvas;
}
}
return blocks;
}
function drawBlockElements(container) {
for (var col = 0; col < cols; col++) {
for (var row = 0; row < rows; row++) {
var canvas = blocks[col][row];
var randomLeft = Math.floor(Math.random() * (container.offsetWidth - width))
var randomTop = Math.floor(Math.random() * (container.offsetHeight - height))
canvas.style.left = randomLeft + "px";
canvas.style.top = randomTop + "px";
container.appendChild(canvas);
}
}
}
function updateBlocks() {
for (var col = 0; col < cols; col++) {
for (var row = 0; row < rows; row++) {
var canvas = blocks[col][row];
var context = canvas.getContext('2d');
context.drawImage(video, col * width, row * height, width, height, 0, 0, width, height)
}
}
}
var video = document.querySelector('#video');
var x = 0, y = 0, width = 160, height = 120, cols = 4, rows = 4;
var container = document.querySelector("#container");
var blocks = generateBlocks(cols, rows);
drawBlockElements(container);
function gameLoop() {
window.setTimeout(gameLoop, 20);
updateBlocks();
}
gameLoop();
jQuery('canvas').drag(function( ev, dd ){
jQuery( this ).css({
top: Math.round( dd.offsetY / 20 ) * 20,
left: Math.round( dd.offsetX / 20 ) * 20
});
});
navigator.webkitGetUserMedia({audio: false, video: true}, function(stream) {
video.src = webkitURL.createObjectURL(stream);
}, function(e) {
console.error(e);
});
})();