-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheater.js
More file actions
128 lines (95 loc) · 2.25 KB
/
heater.js
File metadata and controls
128 lines (95 loc) · 2.25 KB
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
var heater;
(function(win,doc,undefined) {
var coords = {},min=0,max=0,time=300,
block_size = {
x: 10,
y: 10
},
blocks = {
x: 0,
y: 0
},
max = 1,
canvas,ctx;
heater = {};
function monitorMouse(e) {
var x=e.clientX,y=e.clientY,
block = {
x: Math.ceil(x/block_size.x),
y: Math.ceil(y/block_size.y)
};
coords[block.x] = coords[block.x] || {};
if(!coords[block.x][block.y]) {
coords[block.x][block.y] = 0;
}
coords[block.x][block.y]++;
max = Math.max(max,coords[block.x][block.y]);
}
document.addEventListener('mousemove',monitorMouse,false);
function init() {
canvas = doc.createElement('canvas');
ctx = canvas.getContext('2d');
var sizeCanvas = function() {
var x = canvas.width/block_size.x,
y = canvas.height/block_size.y;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
blocks.x = Math.ceil(x);
blocks.y = Math.ceil(y);
};
canvas.id = 'heater_canvas';
canvas.style.position = 'absolute';
canvas.style.zIndex = '9999';
canvas.style.top = 0;
canvas.style.left = 0;
sizeCanvas();
doc.body.appendChild(canvas);
window.addEventListener('resize',sizeCanvas,false);
setTimeout(function() {
drawCanvas(canvas,ctx);
},time)
}
function getRGBA(x,y) {
var block = {
x: Math.ceil(x/block_size.x),
y: Math.ceil(y/block_size.y)
},
val = (coords[block.x] && coords[block.x][block.y]) ? coords[block.x][block.y] : 0,
value = val/max;
return {
a: 0.5,
r: parseInt(255*value,10),
g: parseInt(255 * (1-value),10),
b: 0
};
}
function each(x,fun) {
var key;
for(key in x) {
fun(x[key],x,key);
}
}
function drawCanvas(can,ctx) {
var x=0,y=0,pos=0,
rgba;
ctx.clearRect(0,0,can.width,can.height);
for(x;x<can.width;x=x+block_size.x) {
for(y;y<can.height;y=y+block_size.y) {
rgba = getRGBA(x,y);
ctx.fillStyle = "rgba("+rgba.r+","+rgba.g+","+rgba.b+","+rgba.a+")";
ctx.fillRect(x,y,block_size.x,block_size.y);
}
y=0;
}
setTimeout(function() {
drawCanvas(can,ctx);
},time);
}
heater.getRGBA = getRGBA;
heater.init = init;
heater.coords = coords;
heater.drawCanvas = function() {
drawCanvas(canvas,ctx);
};
return heater;
})(window,window.document);