forked from pyalot/webgl-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathheatmap-texture.js
86 lines (76 loc) · 2.48 KB
/
heatmap-texture.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
'use strict';
function HeatmapTexture(gl, params) {
var _ref, _ref1;
this.gl = gl;
if (params == null) {
params = {};
}
this.channels = this.gl[((_ref = params.channels) != null ? _ref : 'rgba').toUpperCase()];
if (typeof params.type === 'number') {
this.type = params.type;
} else {
this.type = this.gl[((_ref1 = params.type) != null ? _ref1 : 'unsigned_byte').toUpperCase()];
}
switch (this.channels) {
case this.gl.RGBA:
this.chancount = 4;
break;
case this.gl.RGB:
this.chancount = 3;
break;
case this.gl.LUMINANCE_ALPHA:
this.chancount = 2;
break;
default:
this.chancount = 1;
}
this.target = this.gl.TEXTURE_2D;
this.handle = this.gl.createTexture();
}
HeatmapTexture.prototype.destroy = function() {
return this.gl.deleteTexture(this.handle);
};
HeatmapTexture.prototype.bind = function(unit) {
if (unit == null) {
unit = 0;
}
if (unit > 15) {
throw 'Texture unit too large: ' + unit;
}
this.gl.activeTexture(this.gl.TEXTURE0 + unit);
this.gl.bindTexture(this.target, this.handle);
return this;
};
HeatmapTexture.prototype.setSize = function(width, height) {
this.width = width;
this.height = height;
this.gl.texImage2D(this.target, 0, this.channels, this.width, this.height, 0, this.channels, this.type, null);
return this;
};
HeatmapTexture.prototype.upload = function(data) {
this.width = data.width;
this.height = data.height;
this.gl.texImage2D(this.target, 0, this.channels, this.channels, this.type, data);
return this;
};
HeatmapTexture.prototype.linear = function() {
this.gl.texParameteri(this.target, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
this.gl.texParameteri(this.target, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
return this;
};
HeatmapTexture.prototype.nearest = function() {
this.gl.texParameteri(this.target, this.gl.TEXTURE_MAG_FILTER, this.gl.NEAREST);
this.gl.texParameteri(this.target, this.gl.TEXTURE_MIN_FILTER, this.gl.NEAREST);
return this;
};
HeatmapTexture.prototype.clampToEdge = function() {
this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);
this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);
return this;
};
HeatmapTexture.prototype.repeat = function() {
this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.gl.REPEAT);
this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.gl.REPEAT);
return this;
};
module.exports = HeatmapTexture;