-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (104 loc) · 2.57 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(function(win, doc){
"use strict";
var
canvas,
video,
ctx,
localMediaStream = null,
width,
height;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
function snapshot() {
var
snapshot = document.getElementsByTagName("img")[0];
if(snapshot) {
drawImg(snapshot.src);
return true;
} else if(localMediaStream) {
ctx.drawImage(video, 0, 0, width, height);
return false;
}
}
function drawImg(src) {
var img = new Image();
logln(src);
setTimeout(function() {
width = img.width;
height = img.height;
if(width > height && width > 400) {
height = 400 * (height / width);
width = 400;
} else if (height > width) {
width = 400 * (width / height);
height = 400;
}
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
ctx.drawImage(img, 0, 0, width, height);
calcColor();
}, 300);
img.src = src;
}
function initialize() {
width = video.width;
height = video.height;
video.removeEventListener('playing', initialize);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
requestAnimationFrame();
}
function requestAnimationFrame() {
var bowser = snapshot();
if(!bowser) {
calcColor();
}
}
function calcColor() {
var
color;
color = getCanvasColor();
doc.body.style.background = 'rgb(' + color.r + ',' + color.g + ',' + color.b + ')';
win.requestAnimationFrame(requestAnimationFrame);
}
function getCanvasColor () {
var
blockSize = 5,
data = ctx.getImageData(0, 0, width, height),
color = {r: 0, g: 0, b: 0},
len = data.data.length,
count = 0,
i;
for(i = 0; i < len; i += blockSize * 4) {
count++;
color.r += data.data[i];
color.g += data.data[i + 1];
color.b += data.data[i + 2];
}
color.r = ~~(color.r / count);
color.g = ~~(color.g / count);
color.b = ~~(color.b / count);
return color;
}
window.onload = function() {
canvas = doc.getElementById('canvas');
video = doc.getElementById('video');
ctx = canvas.getContext('2d');
//video.addEventListener('playing', initialize);
navigator.getUserMedia({video: true}, function(stream) {
video.src = win.URL.createObjectURL(stream);
localMediaStream = stream;
setTimeout(function() {
initialize();
}, 1000);
}, function(err) {
if(err) throw err;
});
};
function log(str) {
doc.body.appendChild(doc.createTextNode(str));
}
function logln(str) {
log(str);
doc.body.appendChild(doc.createElement('br'));
}
})(this, this.document);