@makechart/bubble.gl 0.0.1 → 0.0.3

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/index.html +40 -20
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change Logs
2
2
 
3
+ ## v0.0.3
4
+
5
+ - add `desc` ( en / zh-TW ) to the config items, shown under the label in the makechart style panel
6
+
7
+ ## v0.0.2
8
+
9
+ - fix tooltip hover offset: map mouse into world coordinate by canvas rect ratio
10
+ ( root padding / css transform make display size != world size ); position tooltip by root rect
11
+ - transparent background support: context alpha + premultiplied clear;
12
+ parseColor accepts #rrggbbaa / rgba() / transparent
13
+ - preserveDrawingBuffer: true so export ( e.g. png ) can read canvas anytime
14
+ - default background changed to white ( #ffffff )
15
+
3
16
  ## v0.0.1
4
17
 
5
18
  - initial release: GPGPU force layout bubble chart
package/index.html CHANGED
@@ -7,7 +7,7 @@
7
7
  var mod;
8
8
  module.exports = {
9
9
  pkg: {
10
- name: '@makechart/bubble.gl', version: '0.0.1',
10
+ name: '@makechart/bubble.gl', version: '0.0.2',
11
11
  syncInit: true,
12
12
  extend: {ns: 'local', name: 'echarts', path: 'common', version: 'main'},
13
13
  dependencies: [
@@ -177,21 +177,35 @@ mod = function(o) {
177
177
  return [f(0), f(8), f(4)];
178
178
  }
179
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]; }
180
+ // 回傳 [r,g,b,a] ( 0~1 ); 支援 #rrggbb / #rrggbbaa / rgb() / rgba() / transparent
181
+ function parseWeb(c) {
182
+ if(typeof(c) != 'string') { return null; }
183
+ if(c == 'transparent') { return [0, 0, 0, 0]; }
184
+ var re = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?/i.exec(c);
185
+ if(re) {
186
+ return [
187
+ parseInt(re[1], 16) / 255, parseInt(re[2], 16) / 255, parseInt(re[3], 16) / 255,
188
+ re[4] != null ? parseInt(re[4], 16) / 255 : 1
189
+ ];
190
+ }
191
+ re = /^rgba?\((\d+)[,\s]+(\d+)[,\s]+(\d+)([,\s/]+([\d.]+%?))?/.exec(c);
192
+ if(re) {
193
+ var a = 1;
194
+ if(re[5] != null) { a = /%$/.test(re[5]) ? parseFloat(re[5]) / 100 : parseFloat(re[5]); }
195
+ return [+re[1] / 255, +re[2] / 255, +re[3] / 255, a];
188
196
  }
189
197
  return null;
190
198
  }
199
+ function parseColor(c) {
200
+ if(!c) { return null; }
201
+ var p = parseWeb(c);
202
+ if(!p && typeof(ldcolor) != 'undefined' && ldcolor.web) { p = parseWeb(ldcolor.web(c)); }
203
+ return p;
204
+ }
191
205
 
192
206
  // 內部狀態 ( closure, 不放 chart context )
193
207
  var dpr = window.devicePixelRatio || 1;
194
- var bg = [0.063, 0.078, 0.102]; // 背景色 ( config: background )
208
+ var bg = [1, 1, 1, 1]; // 背景色 rgba ( config: background, a=0 為透明 )
195
209
  var pad = 8; // bubble 間距 ( config: bubble.padding )
196
210
  var anchorMode = 'orbit'; // 群心佈局 ( config: dynamics.anchor )
197
211
  var canvas = null, tip = null, gl = null;
@@ -388,7 +402,8 @@ mod = function(o) {
388
402
 
389
403
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
390
404
  gl.viewport(0, 0, canvas.width, canvas.height);
391
- gl.clearColor(bg[0], bg[1], bg[2], 1);
405
+ // canvas 為 premultiplied alpha, clear color 也要 premultiply
406
+ gl.clearColor(bg[0] * bg[3], bg[1] * bg[3], bg[2] * bg[3], bg[3]);
392
407
  gl.clear(gl.COLOR_BUFFER_BIT);
393
408
  gl.enable(gl.BLEND);
394
409
  gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
@@ -420,7 +435,7 @@ mod = function(o) {
420
435
  var px8 = new Uint8Array(4);
421
436
  gl.readPixels(
422
437
  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))),
438
+ Math.min(canvas.height - 1, Math.max(0, Math.round(canvas.height - 1 - my * dpr))),
424
439
  1, 1, gl.RGBA, gl.UNSIGNED_BYTE, px8
425
440
  );
426
441
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
@@ -428,9 +443,11 @@ mod = function(o) {
428
443
  }
429
444
 
430
445
  function onMove(e) {
446
+ // 世界座標 != canvas 顯示座標 ( root 可能有 padding, 或整體被 CSS transform 縮放 ),
447
+ // 以 rect 比例換算; tooltip 定位在 root 座標系, 用 root rect 換算
431
448
  var rect = canvas.getBoundingClientRect();
432
- mouse.x = e.clientX - rect.left;
433
- mouse.y = e.clientY - rect.top;
449
+ mouse.x = (e.clientX - rect.left) * (Wc / (rect.width || Wc));
450
+ mouse.y = (e.clientY - rect.top) * (Hc / (rect.height || Hc));
434
451
  mouse.over = true;
435
452
  if(mouse.down || !parsed) { tip.style.display = 'none'; return; }
436
453
  var now = performance.now();
@@ -438,10 +455,11 @@ mod = function(o) {
438
455
  lastPick = now;
439
456
  var id = doPick(mouse.x, mouse.y);
440
457
  if(id >= 0 && id < parsed.names.length) {
458
+ var rrect = root.getBoundingClientRect();
441
459
  tip.textContent = parsed.names[id] + '\ncategory: ' +
442
460
  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';
461
+ tip.style.left = (e.clientX - rrect.left + 14) + 'px';
462
+ tip.style.top = (e.clientY - rrect.top + 14) + 'px';
445
463
  tip.style.display = 'block';
446
464
  } else {
447
465
  tip.style.display = 'none';
@@ -469,14 +487,15 @@ mod = function(o) {
469
487
  };
470
488
  },
471
489
  config: {
472
- palette: {type: 'palette'},
473
- background: {type: 'color', default: '#10141a'},
490
+ palette: {type: 'palette', desc: {en: "colors for the categories, assigned in the order they first appear in the data", "zh-TW": "各類別的配色,依類別在資料中第一次出現的順序套用"}},
491
+ background: {type: 'color', default: '#ffffff'},
474
492
  bubble: {
475
- padding: {name: "bubble padding", type: 'number', default: 8, min: 0, max: 40, step: 1}
493
+ padding: {name: "bubble padding", type: 'number', default: 8, min: 0, max: 40, step: 1, desc: {en: "extra room each bubble keeps from its neighbors when they push apart, in px", "zh-TW": "泡泡互相推開時,每顆周圍多保留的空間 ( px )"}}
476
494
  },
477
495
  dynamics: {
478
496
  anchor: {
479
497
  name: "Anchor Layout", type: 'choice', default: 'orbit',
498
+ desc: {en: "where each category gathers: orbit on a slowly turning ring, circular on a still ring, array in a grid, center all in the middle", "zh-TW": "各類別聚集的位置:orbit 在緩慢旋轉的圓環上,circular 在固定的圓環上,array 排成格狀,center 全部集中在中間"},
480
499
  values: [
481
500
  {name: "Orbit", value: 'orbit'},
482
501
  {name: "Circular", value: 'circular'},
@@ -505,7 +524,8 @@ mod = function(o) {
505
524
  'font:12px/1.5 monospace', 'color:#dfe8f2', 'white-space:pre', 'z-index:10'
506
525
  ].join(';');
507
526
  root.appendChild(tip);
508
- gl = canvas.getContext('webgl2', {antialias: false, alpha: false});
527
+ // alpha: 支援透明背景; preserveDrawingBuffer: 讓 toDataURL/toBlob 匯出可隨時讀取
528
+ gl = canvas.getContext('webgl2', {antialias: false, alpha: true, preserveDrawingBuffer: true});
509
529
  if(!gl) { throw new Error("bubble.gl: WebGL2 not supported"); }
510
530
  if(!gl.getExtension('EXT_color_buffer_float')) {
511
531
  throw new Error("bubble.gl: EXT_color_buffer_float not supported");
package/package.json CHANGED
@@ -1 +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"}}
1
+ {"name":"@makechart/bubble.gl","version":"0.0.3","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"}}