forked from ShinNoNoir/commonhit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llheatmap.js
329 lines (268 loc) · 8.43 KB
/
llheatmap.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Heat-map for LikeLines "Light"; Must be included after lllight.js */
LLL.HEATMAP = {
DEFAULT_WIDTH: LLL.DEFAULT_WIDTH,
DEFAULT_HEIGHT: 20,
CSS_CLASSES: ["lll-heatmap"],
};
(function (LLL, HEATMAP, $) {
HEATMAP.DummyPlayer = function(duration) {
this.duration = duration;
};
HEATMAP.DummyPlayer.prototype.getDuration = function() {
return this.duration;
};
HEATMAP.DummyPlayer.prototype.seek = function() {};
HEATMAP.DummyPlayer.prototype.getViewHistogram = function() {
return [0];
};
HEATMAP.injectHeatmapsIntoDOM = function () {
var exec = function () {
$('.lll-heatmap:not(canvas)').each(function() {
HEATMAP._setupHeatmapDiv(this);
});
};
// NOTE: An lllplayer is created *after* the YouTube API has loaded.
// Since heat-maps depend on an lllplayer, execution needs to be deferred as well.
if (LLL._ytReady) {
exec();
}
else {
LLL._creationQueue.push(exec);
}
};
// (May only be called after YouTube API is ready)
HEATMAP.createHeatmapAndDiv = function(lllplayer, map, width, height) {
var heatmap_div = $('<div class="lll-heatmap">');
return new HEATMAP.Heatmap(heatmap_div, lllplayer, map, width, height);
};
HEATMAP._setupHeatmapDiv = function(id_or_node) {
var exec = function () {
var heatmap_div = ('string' == typeof id_or_node) ? document.getElementById(id_or_node) : id_or_node;
var $heatmap_div = $(heatmap_div);
var lllplayer_exposed_name = $heatmap_div.data('for');
var map = [];
var width = $heatmap_div.data('width');
var height = $heatmap_div.data('height');
var live_map;
var data_map = $heatmap_div.data('map') || '';
if (!(live_map = (data_map.toUpperCase() === 'LIVE'))) {
map = HEATMAP._parseFloatList(data_map);
}
var lllplayer = window[lllplayer_exposed_name];
var exposed_name = $heatmap_div.data('name');
var heatmap = new HEATMAP.Heatmap(heatmap_div, lllplayer, map, width, height);
heatmap.showLiveHeatmap(live_map);
if (exposed_name !== undefined) {
window[exposed_name] = heatmap;
}
// experimental feature:
var oncreated = $heatmap_div.data('oncreated');
if (oncreated !== undefined) {
window[oncreated](heatmap);
}
};
// NOTE: An lllplayer is created *after* the YouTube API has loaded.
// Since heat-maps depend on an lllplayer, execution needs to be deferred as well.
if (LLL._ytReady) {
exec();
}
else {
LLL._creationQueue.push(exec);
}
};
HEATMAP._parseFloatList = function(s) {
var parts = s.split(',');
var fs = [];
for(var i=0, n=parts.length; i < n; i++){
if (!/^\s*$/.test(parts[i])) {
fs.push( parseFloat(parts[i]) );
}
}
return fs;
};
HEATMAP.Heatmap = function(node, lllplayer, map, width, height) {
this.node = node;
this.canvas = document.createElement('canvas');
this.lllplayer = lllplayer;
this.timecode = document.createElement('span');
this.map = map || [];
this.live_map_timer = undefined;
this.width = width || HEATMAP.DEFAULT_WIDTH;
this.height = height || HEATMAP.DEFAULT_HEIGHT;
this._clickEventListeners = [];
$(this.canvas).prop({
width: this.width,
height: this.height
});
$(node).append(this.canvas)
.append($(this.timecode).addClass('lll-timecode').html('00:00')
);
this._bindHandlers();
this.paintHeatmap(map);
};
HEATMAP.Heatmap.prototype._eventToTimepoint = function (e, domNode) {
var $node = $(domNode);
var x = e.clientX +
((window.pageXOffset !== undefined) ? window.pageXOffset
: (document.documentElement || document.body).scrollLeft) -
$node.offset().left;
var w = $node.outerWidth();
var d = this.lllplayer.getDuration();
if (x < 0) {
x = 0;
}
else if (x >= w) {
x = w-1;
}
return (d !== -1) ? x*d/w : -1;
};
HEATMAP.Heatmap.prototype._bindHandlers = function() {
var self = this;
var $canvas = $(this.canvas);
var canvasOnClick = function(e) {
var t = self._eventToTimepoint(e, this);
self.lllplayer.seek(t);
var handlers = self._clickEventListeners;
for(var i=0, n=handlers.length; i<n; i++) {
handlers[i].call(self, t);
}
};
$(this.canvas).click(canvasOnClick);
var timecode = this.timecode;
var updateTimeCode = function(e) {
var x = (e.clientX + 20) + 'px';
var y = (e.clientY + 0) + 'px';
timecode.style.top = y;
timecode.style.left = x;
var t = self._eventToTimepoint(e, self.canvas);
$(timecode).html(HEATMAP.timecodeToHHMMSS(t));
};
$(this.node).mousemove(updateTimeCode);
};
HEATMAP.Heatmap.prototype.addClickListener = function(handler) {
var handlers = this._clickEventListeners;
handlers.push(handler);
var self = this;
var disposed = false;
var subscription = {
dispose: function() {
if (!disposed) {
var pos = $.inArray(handler, handlers);
if (pos > -1) {
handlers.splice(pos, 1);
}
disposed = true;
}
}
}
return subscription;
};
HEATMAP.Heatmap.prototype.paintHeatmap = function(heatmap, dontScale) {
var ctx = this.canvas.getContext('2d');
var w = this.width;
var elWidth = 1;
var elHeight = this.height;
var palette = HEATMAP.DEFAULT_PALETTE;
if (!dontScale) {
heatmap = HEATMAP._scaleArray(heatmap, w);
}
for(var i = 0; i < w; i++) {
ctx.beginPath();
var val = heatmap[i];
var color = palette(val);
ctx.fillStyle = 'rgb(' + color[0] + ',' + color[1] + ',' + color[2] + ')';
ctx.fillRect(i, 0, elWidth, elHeight);
}
};
HEATMAP.Heatmap.prototype.heatmapFromViewHistogram = function(bins) {
var heatmap = LLL.Util.zeros(bins.length);
var max;
max = bins[0];
for (var i = 1, n = bins.length; i < n; i++) {
max = Math.max(max, bins[i]);
}
var scale = (max > 0) ? max : 1;
for (var i = 0, n = heatmap.length; i < n; i++) {
heatmap[i] = bins[i] / scale;
}
return heatmap;
};
HEATMAP.Heatmap.prototype.showHeatmapOf = function(lllplayer) {
this.map = this.heatmapFromViewHistogram(lllplayer.getViewHistogram());
this.paintHeatmap(this.map);
};
HEATMAP.Heatmap.prototype.showLiveHeatmap = function(show) {
show = (show === undefined) || show;
if (show && this.live_map_timer === undefined) {
var self = this;
this.live_map_timer = window.setInterval(function () {
if (!self.lllplayer.ready)
return;
self.showHeatmapOf(self.lllplayer);
}, 500);
}
else if (!show && this.live_map_timer !== undefined) {
window.clearInterval(this.live_map_timer);
this.live_map_timer = undefined;
}
};
HEATMAP.timecodeToHHMMSS = function(t, forceHours) {
var hours = Math.floor(t / 3600);
t -= hours*3600;
var minutes = Math.floor(t / 60);
t -= minutes*60;
var seconds = Math.floor(t);
var hh = (hours < 10) ? ('0'+hours) : hours;
var mm = (minutes < 10) ? ('0'+minutes) : minutes;
var ss = (seconds < 10) ? ('0'+seconds) : seconds;
return ((hours > 0 || forceHours) ? (hh+':') : '') + mm+':'+ss;
};
HEATMAP._scaleArray = function (data, newSize) {
var n = (data || []).length;
var scaledArray;
if (n == 0 || newSize == 0) {
return LLL.Util.zeros(newSize);
}
else if (n <= 2) {
return LLL.Util.linspace(data[0], data[n-1], newSize);
}
// interpolate
var step = (n-1)/(newSize-1);
scaledArray = [];
for (var j = 0; j < newSize-1; j++) {
var x = j*step;
var i = Math.floor(x);
scaledArray[j] = data[i] + (x-i) * (data[i+1] - data[i]);
}
scaledArray[newSize-1] = data[n-1];
return scaledArray;
}
HEATMAP.createPalette = function (/*colorstop, ...*/) {
var colorstops = Array.prototype.slice.call(arguments);
var n = colorstops.length;
return function (x) {
for (var i = 0; i < n; i++) {
if (x == colorstops[i][0]) {
return colorstops[i][1];
} else if (x < colorstops[i][0]) {
var a = i-1;
var b = i;
var c_a = colorstops[a][1];
var c_b = colorstops[b][1];
var w_a = (colorstops[b][0] - x) / (colorstops[b][0] - colorstops[a][0]);
var w_b = 1 - w_a;
var res = [0,0,0];
for (var k=0; k<3; k++) {
res[k] = Math.round(w_a*c_a[k] + w_b*c_b[k]);
}
return res;
}
};
return [0,0,0];
};
};
HEATMAP.DEFAULT_PALETTE = HEATMAP.createPalette([0.0, [255, 255, 255]],
[0.2, [255, 255, 0]],
[1.0, [255, 0, 0]]);
})(LLL, LLL.HEATMAP, jQuery);
jQuery(document).ready(LLL.HEATMAP.injectHeatmapsIntoDOM);