-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandle.html
209 lines (188 loc) · 5.68 KB
/
candle.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
canvas {
width: 50px;
height: 50px;
border-radius: 25px;
}
#sourceMarker {
border: 1px dotted blue;
position: absolute;
}
.wrapper {
max-width: 400px;
}
</style>
</head>
<body>
<div class="wrapper" style="position: relative; background-color: #333; padding: 100px">
<canvas id="c" width=250 height=250></canvas>
</div>
<hr style="clear: both"/>
<div class="wrapper" style="float: left; position: relative">
<video id="v" loop controls src="./sources/candle-clip.ogv"></video>
<div id="sourceMarker"></div>
</div>
<div class="wrapper" style="float: left; position: relative">
<textarea id="report" style="width: 400px; height: 280px;"></textarea>
</div>
<script>
// globals for debug/console convenience
var imageValues = new Array();
var sampleCount = 0;
var sampler = {
fps: 1000/60, // or 1000/30
handleEvent: function(evt) {
switch (evt.type) {
case 'DOMContentLoaded' :
this.init();
break;
case 'playing':
document.getElementById('report').textContent = '';
break;
case 'play' :
this.takeSample();
break;
case 'ended' :
case 'pause' :
this.stop();
this.reportSingleChannel();
this.playbackSingleChannel();
break;
}
}
};
sampler.init = function(){
var video = this.video = document.getElementById('v');
var canvas = this.canvas = document.getElementById('c');
var context = this.context = this.canvas.getContext('2d');
var cw = Math.floor(1);
var ch = Math.floor(1);
canvas.width = cw;
canvas.height = ch;
var sampleArea = this.sampleArea = {
x: 186,
y: 85
};
sampleArea.width = 60;
sampleArea.height = 80;
sampleArea.left = sampleArea.x - (sampleArea.width/2);
sampleArea.top = sampleArea.y - (sampleArea.height/2);
var marker = this.marker = document.querySelector('#sourceMarker');
marker.style.top = sampleArea.top + 'px';
marker.style.left = sampleArea.left + 'px';
marker.style.width = sampleArea.width + 'px';
marker.style.height = sampleArea.height + 'px';
console.log('sampleArea: ', sampleArea);
this.video.addEventListener('play', this, false);
this.video.addEventListener('playing', this, false);
this.video.addEventListener('pause', this, false);
this.video.addEventListener('ended', this, false);
};
sampler.takeSample = function() {
var video = this.video;
var ctx = this.context;
var sampleArea = this.sampleArea;
if(video.paused || video.ended) return false;
ctx.drawImage(video,
sampleArea.left, sampleArea.top,
sampleArea.width, sampleArea.height,
0, 0, 2, 2);
// only capture image data from the first 30 seconds
if (sampleCount < (30 * 30)) {
var imageData = ctx.getImageData(0, 0, 1, 1);
imageValues[sampleCount] = imageData.data.slice(0, 3);
}
sampleCount++;
setTimeout(sampler.takeSample, sampler.fps);
}.bind(sampler);
sampler.report = function() {
var byteCount = imageValues.reduce(function(previousValue, currentValue, index, array) {
return previousValue + (currentValue.byteLength);
}, 0);
console.log('samples: ', imageValues.length, 'bytes: ' + byteCount);
var lines = imageValues.map((rgb) => {
return rgb.map((c) => c.toString(2)).join(':');
}).join('\n');
document.getElementById('report').textContent = lines.trim();
};
sampler.reportSingleChannel = function() {
var byteCount = imageValues.reduce(function(previousValue, currentValue, index, array) {
return previousValue + (currentValue.byteLength);
}, 0);
console.log('samples: ', imageValues.length, 'bytes: ' + byteCount);
var lines = imageValues.map((rgb) => {
var x = Math.floor((rgb[0] + rgb[1] + rgb[2]) / 3);
return x;
}).join(', ');
document.getElementById('report').textContent = lines.trim();
};
sampler.playback = function(data) {
data = data || imageValues;
console.log('playback with data: ', data);
var ctx = this.context;
var idx = 0;
var direction = 1;
var timer = this._playbackTimer = setInterval(function() {
var rgb = data[idx];
if (direction > 0 && idx >= data.length -1) {
direction = -1;
} else if(direction < 0 && idx <= 0) {
direction = 1;
}
idx += direction;
if (rgb) {
ctx.clearRect(0, 0, 1, 1);
ctx.fillStyle = 'rgb(' + rgb.join(',') + ')';
ctx.fillRect(0, 0, 1, 1)
}
}, sampler.fps);
};
sampler.playbackSingleChannel = function(data) {
var dataRows = (data || imageValues).map(function(rgb) {
return Math.floor((rgb[0] + rgb[1] + rgb[2]) / 3);
});
console.log('playback with data: ', dataRows);
var ctx = this.context;
var rowIndex = 0;
var direction = 1;
var lastRow = -1 + dataRows.length;
// get next value to output
function loop() {
var value = dataRows[rowIndex];
// console.log(value +
// ',' +
// rowIndex +
// '/' +
// lastRow);
ctx.clearRect(0, 0, 1, 1);
ctx.fillStyle = 'rgb(' + [value, value, value].join(',') + ')';
ctx.fillRect(0, 0, 1, 1)
// advance to the next position in the data array:
rowIndex += direction;
// if we're at the end of the array...
if (rowIndex >= lastRow) {
direction = -1;
rowIndex = lastRow;
} else if(rowIndex <= 0) {
direction = 1;
rowIndex = 0;
}
}
setInterval(loop, sampler.fps);
}
sampler.stop = function() {
if (this._playbackTimer) {
clearTimeout(this._playbackTimer);
this._playbackTimer = null;
}
this.video.pause();
this.video.currentTime = 0;
}
document.addEventListener('DOMContentLoaded', sampler);
</script>
</body>
</html>