@makechart/bubble.gl 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +65 -0
- package/index.html +607 -0
- package/package.json +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Change Logs
|
|
2
|
+
|
|
3
|
+
## v0.0.1
|
|
4
|
+
|
|
5
|
+
- initial release: GPGPU force layout bubble chart
|
|
6
|
+
- fully GPU-side simulation ( density field repulsion + position/velocity texture ping-pong ),
|
|
7
|
+
zero per-frame CPU-GPU transfer; handles 100k+ points
|
|
8
|
+
- dimension: name / category / size
|
|
9
|
+
- config: palette, background, bubble padding, anchor layout ( orbit / circular / array / center )
|
|
10
|
+
- interaction: hover tooltip ( ID buffer picking ), press-and-hold mouse repulsor
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 zbryikt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @makechart/bubble.gl
|
|
2
|
+
|
|
3
|
+
GPGPU force layout bubble chart for [makechart](https://makechart.io) —
|
|
4
|
+
WebGL2-based, handles 100k+ data points at 60fps.
|
|
5
|
+
|
|
6
|
+
Part of the **chartgl** series: charts that run their layout simulation entirely
|
|
7
|
+
on the GPU, with zero per-frame CPU-GPU data transfer.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## How it works
|
|
11
|
+
|
|
12
|
+
Each frame runs 3 render passes, all on GPU:
|
|
13
|
+
|
|
14
|
+
1. **splat**: all points are rendered into a low-resolution density texture
|
|
15
|
+
with additive blending — O(N)
|
|
16
|
+
2. **update**: position / velocity are stored in a pair of RGBA32F textures
|
|
17
|
+
( ping-pong ). repulsion is the gradient of the density field ( `-∇density` ),
|
|
18
|
+
plus per-category anchor attraction — O(N)
|
|
19
|
+
3. **draw**: vertex shader fetches positions via `gl_VertexID` / `texelFetch`,
|
|
20
|
+
radius / color from static VBOs
|
|
21
|
+
|
|
22
|
+
The CPU only uploads a few uniforms per frame ( anchor centers, mouse ).
|
|
23
|
+
|
|
24
|
+
Requires WebGL2 with `EXT_color_buffer_float` ( widely available on desktop ).
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Chart interface
|
|
28
|
+
|
|
29
|
+
Implemented as a `@plotdb/block` block, extending makechart's generic
|
|
30
|
+
`@plotdb/chart` adapter.
|
|
31
|
+
|
|
32
|
+
### dimension
|
|
33
|
+
|
|
34
|
+
- `size` ( R ): bubble area. radii are auto-scaled so total area fits the canvas
|
|
35
|
+
- `category` ( C ): color / cluster grouping ( up to 16 categories )
|
|
36
|
+
- `name` ( NC ): label, shown in hover tooltip
|
|
37
|
+
|
|
38
|
+
### config
|
|
39
|
+
|
|
40
|
+
- `palette`: category colors
|
|
41
|
+
- `background`: canvas background color
|
|
42
|
+
- `bubble.padding`: spacing between bubbles ( repulsion kernel radius )
|
|
43
|
+
- `dynamics.anchor`: cluster anchor layout — `orbit` ( slowly rotating, default ),
|
|
44
|
+
`circular`, `array` ( grid ), `center`
|
|
45
|
+
|
|
46
|
+
### interaction
|
|
47
|
+
|
|
48
|
+
- hover: tooltip with name / category / size ( ID-buffer picking, works at any
|
|
49
|
+
point count )
|
|
50
|
+
- press and hold: mouse becomes a repulsor pushing bubbles away
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
npm install
|
|
56
|
+
./build # src/index.pug + src/index.js -> dist/index.html
|
|
57
|
+
|
|
58
|
+
In makechart server, link the local build for development:
|
|
59
|
+
|
|
60
|
+
npx fedep -l "@makechart/bubble.gl:<path-to-this-repo>"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/index.html
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
<div class="w-100 h-100"><script type="@plotdb/block">// @makechart/bubble.gl - GPGPU force layout bubble chart
|
|
2
|
+
// 100k+ 點的 force layout。sim 全在 GPU:
|
|
3
|
+
// pass 1 (splat): 所有點 additive blend 進低解析度 density texture
|
|
4
|
+
// pass 2 (update): position/velocity texture ping-pong, repulsion = -∇density
|
|
5
|
+
// pass 3 (draw): gl_VertexID -> texelFetch position
|
|
6
|
+
// 互動: hover tooltip ( ID buffer picking ), 按住滑鼠 = 斥力點
|
|
7
|
+
var mod;
|
|
8
|
+
module.exports = {
|
|
9
|
+
pkg: {
|
|
10
|
+
name: '@makechart/bubble.gl', version: '0.0.1',
|
|
11
|
+
syncInit: true,
|
|
12
|
+
extend: {ns: 'local', name: 'echarts', path: 'common', version: 'main'},
|
|
13
|
+
dependencies: [
|
|
14
|
+
{name: "ldcolor"}
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
init: function(o) {
|
|
18
|
+
o.pubsub.fire('init', {mod: mod({ctx: o.ctx, root: o.root, t: o.t}), prepareSvg: false});
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
mod = function(o) {
|
|
23
|
+
var ldcolor = o.ctx.ldcolor, root = o.root;
|
|
24
|
+
var TW = 512, MAXK = 16, DW = 320, DH = 180;
|
|
25
|
+
|
|
26
|
+
var VS_SPLAT = [
|
|
27
|
+
'#version 300 es',
|
|
28
|
+
'uniform sampler2D u_pos;',
|
|
29
|
+
'uniform sampler2D u_attr;',
|
|
30
|
+
'uniform vec2 u_world;',
|
|
31
|
+
'uniform float u_scale;',
|
|
32
|
+
'uniform float u_pad;',
|
|
33
|
+
'out float v_r;',
|
|
34
|
+
'void main() {',
|
|
35
|
+
' ivec2 tc = ivec2(gl_VertexID % 512, gl_VertexID / 512);',
|
|
36
|
+
' vec4 pv = texelFetch(u_pos, tc, 0);',
|
|
37
|
+
' v_r = texelFetch(u_attr, tc, 0).r;',
|
|
38
|
+
' vec2 clip = (pv.xy / u_world) * 2.0 - 1.0;',
|
|
39
|
+
' gl_Position = vec4(clip, 0.0, 1.0);',
|
|
40
|
+
' gl_PointSize = max(2.0, (v_r + u_pad) * 2.0 * u_scale);',
|
|
41
|
+
'}'
|
|
42
|
+
].join('\n');
|
|
43
|
+
|
|
44
|
+
var FS_SPLAT = [
|
|
45
|
+
'#version 300 es',
|
|
46
|
+
'precision mediump float;',
|
|
47
|
+
'in float v_r;',
|
|
48
|
+
'out vec4 o;',
|
|
49
|
+
'void main() {',
|
|
50
|
+
' vec2 uv = gl_PointCoord - 0.5;',
|
|
51
|
+
' float k = max(0.0, 1.0 - dot(uv, uv) * 4.0);',
|
|
52
|
+
' o = vec4(v_r * 0.3 * k * k, 0.0, 0.0, 1.0);',
|
|
53
|
+
'}'
|
|
54
|
+
].join('\n');
|
|
55
|
+
|
|
56
|
+
var VS_QUAD = [
|
|
57
|
+
'#version 300 es',
|
|
58
|
+
'void main() {',
|
|
59
|
+
' vec2 v = vec2(gl_VertexID == 1 ? 3.0 : -1.0, gl_VertexID == 2 ? 3.0 : -1.0);',
|
|
60
|
+
' gl_Position = vec4(v, 0.0, 1.0);',
|
|
61
|
+
'}'
|
|
62
|
+
].join('\n');
|
|
63
|
+
|
|
64
|
+
var FS_UPDATE = [
|
|
65
|
+
'#version 300 es',
|
|
66
|
+
'precision highp float;',
|
|
67
|
+
'uniform sampler2D u_pos;',
|
|
68
|
+
'uniform sampler2D u_attr;',
|
|
69
|
+
'uniform sampler2D u_density;',
|
|
70
|
+
'uniform vec2 u_world;',
|
|
71
|
+
'uniform vec2 u_centers[16];',
|
|
72
|
+
'uniform float u_repel;',
|
|
73
|
+
'uniform vec3 u_mouse;',
|
|
74
|
+
'uniform int u_n;',
|
|
75
|
+
'out vec4 outPV;',
|
|
76
|
+
'void main() {',
|
|
77
|
+
' ivec2 tc = ivec2(gl_FragCoord.xy);',
|
|
78
|
+
' int id = tc.y * 512 + tc.x;',
|
|
79
|
+
' vec4 pv = texelFetch(u_pos, tc, 0);',
|
|
80
|
+
' if(id >= u_n) { outPV = pv; return; }',
|
|
81
|
+
' vec2 p = pv.xy, v = pv.zw;',
|
|
82
|
+
' int k = int(texelFetch(u_attr, tc, 0).g);',
|
|
83
|
+
' vec2 f = (u_centers[k] - p) * 0.004;',
|
|
84
|
+
' vec2 uv = p / u_world;',
|
|
85
|
+
' vec2 e = vec2(1.5 / 320.0, 1.5 / 180.0);',
|
|
86
|
+
' float dr = texture(u_density, uv + vec2(e.x, 0.0)).r;',
|
|
87
|
+
' float dl = texture(u_density, uv - vec2(e.x, 0.0)).r;',
|
|
88
|
+
' float du = texture(u_density, uv + vec2(0.0, e.y)).r;',
|
|
89
|
+
' float dd = texture(u_density, uv - vec2(0.0, e.y)).r;',
|
|
90
|
+
' f -= vec2(dr - dl, du - dd) * u_repel;',
|
|
91
|
+
' vec2 dm = p - u_mouse.xy;',
|
|
92
|
+
' float md2 = dot(dm, dm);',
|
|
93
|
+
' if(u_mouse.z > 0.5 && md2 < 22500.0) {',
|
|
94
|
+
' float md = sqrt(md2) + 0.001;',
|
|
95
|
+
' f += dm / md * (1.0 - md / 150.0) * 3.0;',
|
|
96
|
+
' }',
|
|
97
|
+
' if(p.x < 10.0) f.x += (10.0 - p.x) * 0.02;',
|
|
98
|
+
' else if(p.x > u_world.x - 10.0) f.x -= (p.x - u_world.x + 10.0) * 0.02;',
|
|
99
|
+
' if(p.y < 10.0) f.y += (10.0 - p.y) * 0.02;',
|
|
100
|
+
' else if(p.y > u_world.y - 10.0) f.y -= (p.y - u_world.y + 10.0) * 0.02;',
|
|
101
|
+
' v = (v + f) * 0.9;',
|
|
102
|
+
' float sp2 = dot(v, v);',
|
|
103
|
+
' if(sp2 > 36.0) v *= 6.0 / sqrt(sp2);',
|
|
104
|
+
' outPV = vec4(p + v, v);',
|
|
105
|
+
'}'
|
|
106
|
+
].join('\n');
|
|
107
|
+
|
|
108
|
+
var VS_DRAW = [
|
|
109
|
+
'#version 300 es',
|
|
110
|
+
'in float a_radius;',
|
|
111
|
+
'in vec3 a_color;',
|
|
112
|
+
'uniform sampler2D u_pos;',
|
|
113
|
+
'uniform vec2 u_world;',
|
|
114
|
+
'uniform float u_dpr;',
|
|
115
|
+
'out vec3 v_color;',
|
|
116
|
+
'void main() {',
|
|
117
|
+
' ivec2 tc = ivec2(gl_VertexID % 512, gl_VertexID / 512);',
|
|
118
|
+
' vec4 pv = texelFetch(u_pos, tc, 0);',
|
|
119
|
+
' vec2 clip = (pv.xy / u_world) * 2.0 - 1.0;',
|
|
120
|
+
' gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);',
|
|
121
|
+
' gl_PointSize = a_radius * 2.0 * u_dpr;',
|
|
122
|
+
' v_color = a_color;',
|
|
123
|
+
'}'
|
|
124
|
+
].join('\n');
|
|
125
|
+
|
|
126
|
+
var FS_DRAW = [
|
|
127
|
+
'#version 300 es',
|
|
128
|
+
'precision mediump float;',
|
|
129
|
+
'in vec3 v_color;',
|
|
130
|
+
'out vec4 outColor;',
|
|
131
|
+
'void main() {',
|
|
132
|
+
' vec2 uv = gl_PointCoord - 0.5;',
|
|
133
|
+
' float d2 = dot(uv, uv);',
|
|
134
|
+
' if(d2 > 0.25) discard;',
|
|
135
|
+
' float alpha = smoothstep(0.25, 0.16, d2);',
|
|
136
|
+
' outColor = vec4(v_color * alpha, alpha);',
|
|
137
|
+
'}'
|
|
138
|
+
].join('\n');
|
|
139
|
+
|
|
140
|
+
var VS_PICK = [
|
|
141
|
+
'#version 300 es',
|
|
142
|
+
'in float a_radius;',
|
|
143
|
+
'uniform sampler2D u_pos;',
|
|
144
|
+
'uniform vec2 u_world;',
|
|
145
|
+
'uniform float u_dpr;',
|
|
146
|
+
'out vec4 v_id;',
|
|
147
|
+
'void main() {',
|
|
148
|
+
' ivec2 tc = ivec2(gl_VertexID % 512, gl_VertexID / 512);',
|
|
149
|
+
' vec4 pv = texelFetch(u_pos, tc, 0);',
|
|
150
|
+
' vec2 clip = (pv.xy / u_world) * 2.0 - 1.0;',
|
|
151
|
+
' gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);',
|
|
152
|
+
' gl_PointSize = max(4.0, a_radius * 2.0 * u_dpr);',
|
|
153
|
+
' int id = gl_VertexID + 1;',
|
|
154
|
+
' v_id = vec4(float(id % 256), float((id / 256) % 256), float(id / 65536), 255.0) / 255.0;',
|
|
155
|
+
'}'
|
|
156
|
+
].join('\n');
|
|
157
|
+
|
|
158
|
+
var FS_PICK = [
|
|
159
|
+
'#version 300 es',
|
|
160
|
+
'precision mediump float;',
|
|
161
|
+
'in vec4 v_id;',
|
|
162
|
+
'out vec4 o;',
|
|
163
|
+
'void main() {',
|
|
164
|
+
' vec2 uv = gl_PointCoord - 0.5;',
|
|
165
|
+
' if(dot(uv, uv) > 0.25) discard;',
|
|
166
|
+
' o = v_id;',
|
|
167
|
+
'}'
|
|
168
|
+
].join('\n');
|
|
169
|
+
|
|
170
|
+
function hsl2rgb(h, s, l) {
|
|
171
|
+
h = ((h % 1) + 1) % 1;
|
|
172
|
+
var f = function(n) {
|
|
173
|
+
var k = (n + h * 12) % 12;
|
|
174
|
+
var a = s * Math.min(l, 1 - l);
|
|
175
|
+
return l - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
176
|
+
};
|
|
177
|
+
return [f(0), f(8), f(4)];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function parseColor(c) {
|
|
181
|
+
if(!c) { return null; }
|
|
182
|
+
if(typeof(ldcolor) != 'undefined' && ldcolor.web) { c = ldcolor.web(c); }
|
|
183
|
+
if(typeof(c) == 'string') {
|
|
184
|
+
var re = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i.exec(c);
|
|
185
|
+
if(re) { return [parseInt(re[1], 16) / 255, parseInt(re[2], 16) / 255, parseInt(re[3], 16) / 255]; }
|
|
186
|
+
re = /^rgba?\((\d+)[,\s]+(\d+)[,\s]+(\d+)/.exec(c);
|
|
187
|
+
if(re) { return [+re[1] / 255, +re[2] / 255, +re[3] / 255]; }
|
|
188
|
+
}
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 內部狀態 ( closure, 不放 chart context )
|
|
193
|
+
var dpr = window.devicePixelRatio || 1;
|
|
194
|
+
var bg = [0.063, 0.078, 0.102]; // 背景色 ( config: background )
|
|
195
|
+
var pad = 8; // bubble 間距 ( config: bubble.padding )
|
|
196
|
+
var anchorMode = 'orbit'; // 群心佈局 ( config: dynamics.anchor )
|
|
197
|
+
var canvas = null, tip = null, gl = null;
|
|
198
|
+
var progs = {};
|
|
199
|
+
var fixed = {}; // dataset 無關資源: density texture / pick buffer / vaoEmpty
|
|
200
|
+
var ds = null; // dataset 相關資源
|
|
201
|
+
var parsed = null; // parse 後的 {names, cats, sizes, catNames}
|
|
202
|
+
var dirty = false;
|
|
203
|
+
var Wc = 0, Hc = 0, tsim = 0, rafId = null, lastPick = 0;
|
|
204
|
+
var mouse = {x: -9999, y: -9999, down: false, over: false};
|
|
205
|
+
var destroyed = false;
|
|
206
|
+
|
|
207
|
+
function compile(type, src) {
|
|
208
|
+
var sh = gl.createShader(type);
|
|
209
|
+
gl.shaderSource(sh, src);
|
|
210
|
+
gl.compileShader(sh);
|
|
211
|
+
if(!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { throw new Error(gl.getShaderInfoLog(sh)); }
|
|
212
|
+
return sh;
|
|
213
|
+
}
|
|
214
|
+
function makeProg(vs, fs) {
|
|
215
|
+
var p = gl.createProgram();
|
|
216
|
+
gl.attachShader(p, compile(gl.VERTEX_SHADER, vs));
|
|
217
|
+
gl.attachShader(p, compile(gl.FRAGMENT_SHADER, fs));
|
|
218
|
+
gl.bindAttribLocation(p, 0, 'a_radius');
|
|
219
|
+
gl.bindAttribLocation(p, 1, 'a_color');
|
|
220
|
+
gl.linkProgram(p);
|
|
221
|
+
if(!gl.getProgramParameter(p, gl.LINK_STATUS)) { throw new Error(gl.getProgramInfoLog(p)); }
|
|
222
|
+
return p;
|
|
223
|
+
}
|
|
224
|
+
function makeTex(w, h, ifmt, data, filter) {
|
|
225
|
+
var tx = gl.createTexture();
|
|
226
|
+
gl.bindTexture(gl.TEXTURE_2D, tx);
|
|
227
|
+
gl.texStorage2D(gl.TEXTURE_2D, 1, ifmt, w, h);
|
|
228
|
+
if(data) { gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, w, h, gl.RGBA, gl.FLOAT, data); }
|
|
229
|
+
var ft = filter || gl.NEAREST;
|
|
230
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, ft);
|
|
231
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, ft);
|
|
232
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
233
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
234
|
+
return tx;
|
|
235
|
+
}
|
|
236
|
+
function makeFbo(tx) {
|
|
237
|
+
var f = gl.createFramebuffer();
|
|
238
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, f);
|
|
239
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tx, 0);
|
|
240
|
+
return f;
|
|
241
|
+
}
|
|
242
|
+
function uni(p, n) { return gl.getUniformLocation(p, n); }
|
|
243
|
+
|
|
244
|
+
function disposeDs() {
|
|
245
|
+
if(!ds) { return; }
|
|
246
|
+
gl.deleteTexture(ds.texPos[0]);
|
|
247
|
+
gl.deleteTexture(ds.texPos[1]);
|
|
248
|
+
gl.deleteTexture(ds.texAttr);
|
|
249
|
+
gl.deleteFramebuffer(ds.fboPos[0]);
|
|
250
|
+
gl.deleteFramebuffer(ds.fboPos[1]);
|
|
251
|
+
gl.deleteBuffer(ds.radBuf);
|
|
252
|
+
gl.deleteBuffer(ds.colBuf);
|
|
253
|
+
gl.deleteVertexArray(ds.vao);
|
|
254
|
+
ds = null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// 由 parsed + catColors ( 每 category 一個 [r,g,b] ) 建 dataset GPU 資源
|
|
258
|
+
function build(catColors) {
|
|
259
|
+
disposeDs();
|
|
260
|
+
if(!parsed || !parsed.sizes.length) { return; }
|
|
261
|
+
var n = parsed.sizes.length;
|
|
262
|
+
var kCount = Math.min(parsed.catNames.length, MAXK);
|
|
263
|
+
var th = Math.ceil(n / TW);
|
|
264
|
+
var sum = 0, i;
|
|
265
|
+
for(i = 0; i < n; i++) { sum += parsed.sizes[i]; }
|
|
266
|
+
var kr = Math.sqrt(Wc * Hc * 0.35 / (Math.PI * (sum || 1)));
|
|
267
|
+
var posvel = new Float32Array(TW * th * 4);
|
|
268
|
+
var attr = new Float32Array(TW * th * 4);
|
|
269
|
+
var radii = new Float32Array(n);
|
|
270
|
+
var colors = new Float32Array(n * 3);
|
|
271
|
+
for(i = 0; i < n; i++) {
|
|
272
|
+
posvel[i * 4] = Math.random() * Wc;
|
|
273
|
+
posvel[i * 4 + 1] = Math.random() * Hc;
|
|
274
|
+
var r = Math.min(20, Math.max(0.7, Math.sqrt(parsed.sizes[i]) * kr));
|
|
275
|
+
var ki = parsed.cats[i] % MAXK;
|
|
276
|
+
attr[i * 4] = r;
|
|
277
|
+
attr[i * 4 + 1] = ki;
|
|
278
|
+
radii[i] = r;
|
|
279
|
+
var c = (catColors && catColors[ki]) || hsl2rgb(ki / (kCount || 1), 0.7, 0.6);
|
|
280
|
+
colors[i * 3] = c[0];
|
|
281
|
+
colors[i * 3 + 1] = c[1];
|
|
282
|
+
colors[i * 3 + 2] = c[2];
|
|
283
|
+
}
|
|
284
|
+
var texPos = [makeTex(TW, th, gl.RGBA32F, posvel), makeTex(TW, th, gl.RGBA32F, null)];
|
|
285
|
+
var vao = gl.createVertexArray();
|
|
286
|
+
gl.bindVertexArray(vao);
|
|
287
|
+
var radBuf = gl.createBuffer();
|
|
288
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, radBuf);
|
|
289
|
+
gl.bufferData(gl.ARRAY_BUFFER, radii, gl.STATIC_DRAW);
|
|
290
|
+
gl.enableVertexAttribArray(0);
|
|
291
|
+
gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 0);
|
|
292
|
+
var colBuf = gl.createBuffer();
|
|
293
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, colBuf);
|
|
294
|
+
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
|
|
295
|
+
gl.enableVertexAttribArray(1);
|
|
296
|
+
gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 0, 0);
|
|
297
|
+
ds = {
|
|
298
|
+
n: n, th: th, vao: vao, radBuf: radBuf, colBuf: colBuf, texPos: texPos,
|
|
299
|
+
texAttr: makeTex(TW, th, gl.RGBA32F, attr),
|
|
300
|
+
fboPos: [makeFbo(texPos[0]), makeFbo(texPos[1])],
|
|
301
|
+
kCount: kCount, centers: new Float32Array(MAXK * 2), cur: 0
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function setWorldUnis() {
|
|
306
|
+
var name, p, u;
|
|
307
|
+
for(name in progs) {
|
|
308
|
+
p = progs[name];
|
|
309
|
+
gl.useProgram(p);
|
|
310
|
+
u = uni(p, 'u_world');
|
|
311
|
+
if(u) { gl.uniform2f(u, Wc, Hc); }
|
|
312
|
+
}
|
|
313
|
+
gl.useProgram(progs.splat);
|
|
314
|
+
gl.uniform1f(uni(progs.splat, 'u_scale'), DW / Wc);
|
|
315
|
+
gl.useProgram(progs.draw);
|
|
316
|
+
gl.uniform1f(uni(progs.draw, 'u_dpr'), dpr);
|
|
317
|
+
gl.useProgram(progs.pick);
|
|
318
|
+
gl.uniform1f(uni(progs.pick, 'u_dpr'), dpr);
|
|
319
|
+
// pick buffer 尺寸跟 canvas 綁定, resize 時重建
|
|
320
|
+
if(fixed.texPick) { gl.deleteTexture(fixed.texPick); }
|
|
321
|
+
if(fixed.fboPick) { gl.deleteFramebuffer(fixed.fboPick); }
|
|
322
|
+
var tx = gl.createTexture();
|
|
323
|
+
gl.bindTexture(gl.TEXTURE_2D, tx);
|
|
324
|
+
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, canvas.width, canvas.height);
|
|
325
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
326
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
327
|
+
fixed.texPick = tx;
|
|
328
|
+
fixed.fboPick = makeFbo(tx);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// 依 anchor 設定計算各 category 的群心 ( array 演算法參考 @makechart/bubble )
|
|
332
|
+
function updateCenters() {
|
|
333
|
+
var K = ds.kCount, rr = Math.min(Wc, Hc) * 0.28, k, a;
|
|
334
|
+
if(anchorMode == 'center') {
|
|
335
|
+
for(k = 0; k < K; k++) {
|
|
336
|
+
ds.centers[k * 2] = Wc / 2;
|
|
337
|
+
ds.centers[k * 2 + 1] = Hc / 2;
|
|
338
|
+
}
|
|
339
|
+
} else if(anchorMode == 'array') {
|
|
340
|
+
var cols = Math.max(1, Math.ceil(Math.sqrt(K * Wc / Hc)));
|
|
341
|
+
var rows = Math.max(1, Math.ceil(K / cols));
|
|
342
|
+
var cw = Wc / cols, ch = Hc / rows;
|
|
343
|
+
for(k = 0; k < K; k++) {
|
|
344
|
+
ds.centers[k * 2] = ((k % cols) + 0.5) * cw;
|
|
345
|
+
ds.centers[k * 2 + 1] = (Math.floor(k / cols) + 0.5) * ch;
|
|
346
|
+
}
|
|
347
|
+
} else {
|
|
348
|
+
// orbit ( 緩慢繞行 ) 或 circular ( 靜態圓 )
|
|
349
|
+
var spin = (anchorMode == 'orbit') ? tsim * 0.15 : 0;
|
|
350
|
+
for(k = 0; k < K; k++) {
|
|
351
|
+
a = spin + k * Math.PI * 2 / K;
|
|
352
|
+
ds.centers[k * 2] = Wc / 2 + Math.cos(a) * rr;
|
|
353
|
+
ds.centers[k * 2 + 1] = Hc / 2 + Math.sin(a) * rr * 0.8;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function step() {
|
|
359
|
+
if(!ds) { return; }
|
|
360
|
+
tsim += 1 / 60;
|
|
361
|
+
updateCenters();
|
|
362
|
+
|
|
363
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fixed.fboDen);
|
|
364
|
+
gl.viewport(0, 0, DW, DH);
|
|
365
|
+
gl.clearColor(0, 0, 0, 0);
|
|
366
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
367
|
+
gl.enable(gl.BLEND);
|
|
368
|
+
gl.blendFunc(gl.ONE, gl.ONE);
|
|
369
|
+
gl.useProgram(progs.splat);
|
|
370
|
+
gl.bindVertexArray(fixed.vaoEmpty);
|
|
371
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
372
|
+
gl.bindTexture(gl.TEXTURE_2D, ds.texPos[ds.cur]);
|
|
373
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
374
|
+
gl.bindTexture(gl.TEXTURE_2D, ds.texAttr);
|
|
375
|
+
gl.drawArrays(gl.POINTS, 0, ds.n);
|
|
376
|
+
|
|
377
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, ds.fboPos[1 - ds.cur]);
|
|
378
|
+
gl.viewport(0, 0, TW, ds.th);
|
|
379
|
+
gl.disable(gl.BLEND);
|
|
380
|
+
gl.useProgram(progs.update);
|
|
381
|
+
gl.uniform2fv(uni(progs.update, 'u_centers'), ds.centers);
|
|
382
|
+
gl.uniform1i(uni(progs.update, 'u_n'), ds.n);
|
|
383
|
+
gl.uniform3f(uni(progs.update, 'u_mouse'), mouse.x, mouse.y, (mouse.down && mouse.over) ? 1 : 0);
|
|
384
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
385
|
+
gl.bindTexture(gl.TEXTURE_2D, fixed.texDen);
|
|
386
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
387
|
+
ds.cur = 1 - ds.cur;
|
|
388
|
+
|
|
389
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
390
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
391
|
+
gl.clearColor(bg[0], bg[1], bg[2], 1);
|
|
392
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
393
|
+
gl.enable(gl.BLEND);
|
|
394
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
395
|
+
gl.useProgram(progs.draw);
|
|
396
|
+
gl.bindVertexArray(ds.vao);
|
|
397
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
398
|
+
gl.bindTexture(gl.TEXTURE_2D, ds.texPos[ds.cur]);
|
|
399
|
+
gl.drawArrays(gl.POINTS, 0, ds.n);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function loopFn() {
|
|
403
|
+
if(destroyed) { return; }
|
|
404
|
+
step();
|
|
405
|
+
rafId = requestAnimationFrame(loopFn);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function doPick(mx, my) {
|
|
409
|
+
if(!ds) { return -1; }
|
|
410
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fixed.fboPick);
|
|
411
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
412
|
+
gl.disable(gl.BLEND);
|
|
413
|
+
gl.clearColor(0, 0, 0, 0);
|
|
414
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
415
|
+
gl.useProgram(progs.pick);
|
|
416
|
+
gl.bindVertexArray(ds.vao);
|
|
417
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
418
|
+
gl.bindTexture(gl.TEXTURE_2D, ds.texPos[ds.cur]);
|
|
419
|
+
gl.drawArrays(gl.POINTS, 0, ds.n);
|
|
420
|
+
var px8 = new Uint8Array(4);
|
|
421
|
+
gl.readPixels(
|
|
422
|
+
Math.min(canvas.width - 1, Math.max(0, Math.round(mx * dpr))),
|
|
423
|
+
Math.min(canvas.height - 1, Math.max(0, Math.round(canvas.height - my * dpr))),
|
|
424
|
+
1, 1, gl.RGBA, gl.UNSIGNED_BYTE, px8
|
|
425
|
+
);
|
|
426
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
427
|
+
return px8[0] + px8[1] * 256 + px8[2] * 65536 - 1;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function onMove(e) {
|
|
431
|
+
var rect = canvas.getBoundingClientRect();
|
|
432
|
+
mouse.x = e.clientX - rect.left;
|
|
433
|
+
mouse.y = e.clientY - rect.top;
|
|
434
|
+
mouse.over = true;
|
|
435
|
+
if(mouse.down || !parsed) { tip.style.display = 'none'; return; }
|
|
436
|
+
var now = performance.now();
|
|
437
|
+
if(now - lastPick < 50) { return; }
|
|
438
|
+
lastPick = now;
|
|
439
|
+
var id = doPick(mouse.x, mouse.y);
|
|
440
|
+
if(id >= 0 && id < parsed.names.length) {
|
|
441
|
+
tip.textContent = parsed.names[id] + '\ncategory: ' +
|
|
442
|
+
parsed.catNames[parsed.cats[id]] + '\nsize: ' + parsed.sizes[id];
|
|
443
|
+
tip.style.left = (mouse.x + 14) + 'px';
|
|
444
|
+
tip.style.top = (mouse.y + 14) + 'px';
|
|
445
|
+
tip.style.display = 'block';
|
|
446
|
+
} else {
|
|
447
|
+
tip.style.display = 'none';
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
sample: function() {
|
|
453
|
+
var cats = ['alpha', 'beta', 'gamma', 'delta', 'epsilon'];
|
|
454
|
+
var raw = [], i;
|
|
455
|
+
for(i = 0; i < 3000; i++) {
|
|
456
|
+
raw.push({
|
|
457
|
+
name: 'item-' + i,
|
|
458
|
+
category: cats[Math.floor(Math.random() * cats.length)],
|
|
459
|
+
size: +(1 + Math.random() * 99).toFixed(1)
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
raw: raw,
|
|
464
|
+
binding: {
|
|
465
|
+
name: {key: 'name'},
|
|
466
|
+
category: {key: 'category'},
|
|
467
|
+
size: {key: 'size'}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
},
|
|
471
|
+
config: {
|
|
472
|
+
palette: {type: 'palette'},
|
|
473
|
+
background: {type: 'color', default: '#10141a'},
|
|
474
|
+
bubble: {
|
|
475
|
+
padding: {name: "bubble padding", type: 'number', default: 8, min: 0, max: 40, step: 1}
|
|
476
|
+
},
|
|
477
|
+
dynamics: {
|
|
478
|
+
anchor: {
|
|
479
|
+
name: "Anchor Layout", type: 'choice', default: 'orbit',
|
|
480
|
+
values: [
|
|
481
|
+
{name: "Orbit", value: 'orbit'},
|
|
482
|
+
{name: "Circular", value: 'circular'},
|
|
483
|
+
{name: "Array", value: 'array'},
|
|
484
|
+
{name: "Center", value: 'center'}
|
|
485
|
+
]
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
dimension: {
|
|
490
|
+
size: {type: 'R', name: "size", priority: 1},
|
|
491
|
+
category: {type: 'C', name: "category", priority: 2},
|
|
492
|
+
name: {type: 'NC', name: "name", priority: 3}
|
|
493
|
+
},
|
|
494
|
+
init: function() {
|
|
495
|
+
if(getComputedStyle(root).position == 'static') { root.style.position = 'relative'; }
|
|
496
|
+
canvas = document.createElement('canvas');
|
|
497
|
+
canvas.style.display = 'block';
|
|
498
|
+
canvas.style.width = '100%';
|
|
499
|
+
canvas.style.height = '100%';
|
|
500
|
+
root.appendChild(canvas);
|
|
501
|
+
tip = document.createElement('div');
|
|
502
|
+
tip.style.cssText = [
|
|
503
|
+
'position:absolute', 'display:none', 'padding:6px 10px', 'pointer-events:none',
|
|
504
|
+
'background:rgba(24,30,40,0.92)', 'border:1px solid #3a4a5e', 'border-radius:4px',
|
|
505
|
+
'font:12px/1.5 monospace', 'color:#dfe8f2', 'white-space:pre', 'z-index:10'
|
|
506
|
+
].join(';');
|
|
507
|
+
root.appendChild(tip);
|
|
508
|
+
gl = canvas.getContext('webgl2', {antialias: false, alpha: false});
|
|
509
|
+
if(!gl) { throw new Error("bubble.gl: WebGL2 not supported"); }
|
|
510
|
+
if(!gl.getExtension('EXT_color_buffer_float')) {
|
|
511
|
+
throw new Error("bubble.gl: EXT_color_buffer_float not supported");
|
|
512
|
+
}
|
|
513
|
+
progs.splat = makeProg(VS_SPLAT, FS_SPLAT);
|
|
514
|
+
progs.update = makeProg(VS_QUAD, FS_UPDATE);
|
|
515
|
+
progs.draw = makeProg(VS_DRAW, FS_DRAW);
|
|
516
|
+
progs.pick = makeProg(VS_PICK, FS_PICK);
|
|
517
|
+
gl.useProgram(progs.splat);
|
|
518
|
+
gl.uniform1i(uni(progs.splat, 'u_pos'), 0);
|
|
519
|
+
gl.uniform1i(uni(progs.splat, 'u_attr'), 1);
|
|
520
|
+
gl.uniform1f(uni(progs.splat, 'u_pad'), pad);
|
|
521
|
+
gl.useProgram(progs.update);
|
|
522
|
+
gl.uniform1i(uni(progs.update, 'u_pos'), 0);
|
|
523
|
+
gl.uniform1i(uni(progs.update, 'u_attr'), 1);
|
|
524
|
+
gl.uniform1i(uni(progs.update, 'u_density'), 2);
|
|
525
|
+
gl.uniform1f(uni(progs.update, 'u_repel'), 0.5);
|
|
526
|
+
gl.useProgram(progs.draw);
|
|
527
|
+
gl.uniform1i(uni(progs.draw, 'u_pos'), 0);
|
|
528
|
+
gl.useProgram(progs.pick);
|
|
529
|
+
gl.uniform1i(uni(progs.pick, 'u_pos'), 0);
|
|
530
|
+
fixed.texDen = makeTex(DW, DH, gl.RGBA16F, null, gl.LINEAR);
|
|
531
|
+
fixed.fboDen = makeFbo(fixed.texDen);
|
|
532
|
+
fixed.vaoEmpty = gl.createVertexArray();
|
|
533
|
+
canvas.addEventListener('mousemove', onMove);
|
|
534
|
+
canvas.addEventListener('mousedown', function() { mouse.down = true; tip.style.display = 'none'; });
|
|
535
|
+
window.addEventListener('mouseup', this._mouseupFn = function() { mouse.down = false; });
|
|
536
|
+
canvas.addEventListener('mouseleave', function() { mouse.over = false; tip.style.display = 'none'; });
|
|
537
|
+
loopFn();
|
|
538
|
+
// 若 race 導致 resize 比 init 先被呼叫過, 這裡補跑一次
|
|
539
|
+
if(dirty && this.mod && this.mod.resize) { this.mod.resize.apply(this); }
|
|
540
|
+
},
|
|
541
|
+
parse: function() {
|
|
542
|
+
var rows = this.data || [];
|
|
543
|
+
var catIndex = new Map();
|
|
544
|
+
var names = [], cats = [], sizes = [], i, row, cat;
|
|
545
|
+
for(i = 0; i < rows.length; i++) {
|
|
546
|
+
row = rows[i];
|
|
547
|
+
cat = String(row.category != null ? row.category : '');
|
|
548
|
+
if(!catIndex.has(cat)) { catIndex.set(cat, catIndex.size); }
|
|
549
|
+
names.push(String(row.name != null ? row.name : ''));
|
|
550
|
+
cats.push(catIndex.get(cat));
|
|
551
|
+
sizes.push(Math.max(0.01, +row.size || 1));
|
|
552
|
+
}
|
|
553
|
+
parsed = {names: names, cats: cats, sizes: sizes, catNames: Array.from(catIndex.keys())};
|
|
554
|
+
dirty = true;
|
|
555
|
+
},
|
|
556
|
+
resize: function() {
|
|
557
|
+
// 防禦: mod.init 前不應被呼叫 ( @plotdb/chart 3.0.1+ 已在 chart 層 gate, 此為保險 )
|
|
558
|
+
if(!gl) { dirty = true; return; }
|
|
559
|
+
var cfg = this.cfg || {};
|
|
560
|
+
var c = cfg.background && parseColor(cfg.background);
|
|
561
|
+
if(c) { bg = c; }
|
|
562
|
+
pad = (cfg.bubble && cfg.bubble.padding != null) ? +cfg.bubble.padding : 8;
|
|
563
|
+
anchorMode = (cfg.dynamics && cfg.dynamics.anchor) || 'orbit';
|
|
564
|
+
gl.useProgram(progs.splat);
|
|
565
|
+
gl.uniform1f(uni(progs.splat, 'u_pad'), pad);
|
|
566
|
+
var w = root.clientWidth || 640;
|
|
567
|
+
var h = root.clientHeight || 480;
|
|
568
|
+
var sizeChanged = (w != Wc || h != Hc);
|
|
569
|
+
if(sizeChanged) {
|
|
570
|
+
Wc = w;
|
|
571
|
+
Hc = h;
|
|
572
|
+
canvas.width = w * dpr;
|
|
573
|
+
canvas.height = h * dpr;
|
|
574
|
+
setWorldUnis();
|
|
575
|
+
}
|
|
576
|
+
if(dirty || sizeChanged) {
|
|
577
|
+
var catColors = null;
|
|
578
|
+
if(parsed && this.cfg && this.cfg.palette && this.cfg.palette.colors) {
|
|
579
|
+
var pcs = this.cfg.palette.colors.map(parseColor).filter(function(c) { return c; });
|
|
580
|
+
if(pcs.length) {
|
|
581
|
+
catColors = parsed.catNames.map(function(cn, i) { return pcs[i % pcs.length]; });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
build(catColors);
|
|
585
|
+
dirty = false;
|
|
586
|
+
}
|
|
587
|
+
},
|
|
588
|
+
render: function() {},
|
|
589
|
+
destroy: function() {
|
|
590
|
+
var name;
|
|
591
|
+
destroyed = true;
|
|
592
|
+
if(rafId != null) { cancelAnimationFrame(rafId); }
|
|
593
|
+
if(this._mouseupFn) { window.removeEventListener('mouseup', this._mouseupFn); }
|
|
594
|
+
disposeDs();
|
|
595
|
+
if(gl) {
|
|
596
|
+
for(name in progs) { gl.deleteProgram(progs[name]); }
|
|
597
|
+
if(fixed.texDen) { gl.deleteTexture(fixed.texDen); }
|
|
598
|
+
if(fixed.texPick) { gl.deleteTexture(fixed.texPick); }
|
|
599
|
+
var lose = gl.getExtension('WEBGL_lose_context');
|
|
600
|
+
if(lose) { lose.loseContext(); }
|
|
601
|
+
}
|
|
602
|
+
if(canvas && canvas.parentNode) { canvas.parentNode.removeChild(canvas); }
|
|
603
|
+
if(tip && tip.parentNode) { tip.parentNode.removeChild(tip); }
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
};
|
|
607
|
+
</script></div>
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@makechart/bubble.gl","version":"0.0.1","description":"GPGPU force layout bubble chart - 100k+ points, WebGL2","scripts":{"build":"./build"},"keywords":[],"author":"zbryikt","license":"MIT","dependencies":{"@plotdb/srcbuild":"^0.0.70","fedep":"1.7.3","livescript":"^1.6.0"}}
|