@yanqirenshi/d3.wireframe 0.1.1 → 0.2.0

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.
@@ -0,0 +1,100 @@
1
+ /**
2
+ * 回転ビュー用の透視投影(データクラス — DOM/d3 非依存)。
3
+ *
4
+ * レイアウト平面(z=0)上の点を、origin を中心に Y軸 → X軸の順で回転し、
5
+ * 視点距離 perspective から透視投影して 2D 座標に落とす。
6
+ * SVG の transform は affine のみで透視変換を持たないため、矩形は
7
+ * 4頂点をこのクラスで射影して polygon として描く(ペインタ側)。
8
+ * 文字は SVG では遠近で歪ませられないので、射影後の位置 + 距離に応じた
9
+ * スケール(k)で近似する。
10
+ *
11
+ * config:
12
+ * y: Y軸(縦軸)回りの回転角(度)。正で右側が奥に倒れる
13
+ * x: X軸(横軸)回りの回転角(度)。正で下側が奥に倒れる
14
+ * perspective: 視点距離(px、既定 1600)。小さいほどパースが強い
15
+ * origin: 回転の中心 { x, y }(レイアウト座標。既定 {0,0})
16
+ * lift: 入れ子1階層ごとに子を手前(視点側)へ浮かせる距離(px、
17
+ * 既定 0)。親平面と子パネルの間に奥行きの隙間ができる
18
+ */
19
+ export default class Projector {
20
+ constructor (config) {
21
+ const cfg = config || {};
22
+
23
+ this._ry = this.deg2rad(cfg.y || 0);
24
+ this._rx = this.deg2rad(cfg.x || 0);
25
+ this._f = cfg.perspective || 1600;
26
+ this._lift = cfg.lift || 0;
27
+
28
+ this._origin = {
29
+ x: (cfg.origin && (cfg.origin.x || cfg.origin.x===0)) ? cfg.origin.x : 0,
30
+ y: (cfg.origin && (cfg.origin.y || cfg.origin.y===0)) ? cfg.origin.y : 0,
31
+ };
32
+ }
33
+ deg2rad (degree) {
34
+ return degree * (Math.PI / 180);
35
+ }
36
+ /**
37
+ * 入れ子の深さ(ルート=0)を z 座標に変換する。
38
+ * 負の z = 視点側(手前)。lift × 深さ だけ浮く。
39
+ */
40
+ depthZ (depth) {
41
+ return -((depth || 0) * this._lift);
42
+ }
43
+ /**
44
+ * レイアウト座標 (x, y)(+任意の z。負=手前)を射影する。
45
+ * @returns { x, y, k } k は距離に応じたスケール(手前 >1 / 奥 <1)。
46
+ * 文字サイズの近似スケールに使う。
47
+ */
48
+ point (x, y, z) {
49
+ const px = x - this._origin.x;
50
+ const py = y - this._origin.y;
51
+ const pz = z || 0;
52
+
53
+ // Y軸回転: x1 = px cosθ - pz sinθ, z1 = px sinθ + pz cosθ
54
+ const x1 = px * Math.cos(this._ry) - pz * Math.sin(this._ry);
55
+ const z1 = px * Math.sin(this._ry) + pz * Math.cos(this._ry);
56
+
57
+ // X軸回転: y2 = py cosφ - z1 sinφ, z2 = py sinφ + z1 cosφ
58
+ const y2 = py * Math.cos(this._rx) - z1 * Math.sin(this._rx);
59
+ const z2 = py * Math.sin(this._rx) + z1 * Math.cos(this._rx);
60
+
61
+ // 透視投影(奥 = z が大きいほど縮む)
62
+ const k = this._f / (this._f + z2);
63
+
64
+ return {
65
+ x: this._origin.x + x1 * k,
66
+ y: this._origin.y + y2 * k,
67
+ k: k,
68
+ };
69
+ }
70
+ /**
71
+ * 矩形(x, y, w, h)の4頂点を射影し、SVG polygon の points 文字列にする。
72
+ */
73
+ rectPoints (x, y, w, h, z) {
74
+ const corners = [
75
+ this.point(x, y, z),
76
+ this.point(x + w, y, z),
77
+ this.point(x + w, y + h, z),
78
+ this.point(x, y + h, z),
79
+ ];
80
+
81
+ return corners.map((p) => p.x + ',' + p.y).join(' ');
82
+ }
83
+ /**
84
+ * polygon 化できない要素(画像アイコンなど)向けの近似 affine 行列。
85
+ * 要素の局所座標 (0,0), (w,0), (0,h) を射影先へ正確に写す
86
+ * matrix(a,b,c,d,e,f) を返す(4隅のうち残り1隅は近似 — 小さい要素用)。
87
+ */
88
+ affine (x, y, w, h, z) {
89
+ const p0 = this.point(x, y, z);
90
+ const p1 = this.point(x + w, y, z);
91
+ const p3 = this.point(x, y + h, z);
92
+
93
+ const a = (p1.x - p0.x) / w;
94
+ const b = (p1.y - p0.y) / w;
95
+ const c = (p3.x - p0.x) / h;
96
+ const d = (p3.y - p0.y) / h;
97
+
98
+ return `matrix(${a},${b},${c},${d},${p0.x},${p0.y})`;
99
+ }
100
+ }
package/src/Rectum.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Colon } from '@yanqirenshi/assh0le';
2
2
 
3
3
  import Element from './Element';
4
+ import Projector from './Projector';
4
5
 
5
6
  import { DrawerHierarchy } from './Drawer.js';
6
7
 
@@ -17,6 +18,117 @@ export default class Rectum extends Colon {
17
18
 
18
19
  this.element = new Element();
19
20
  this._elements = { ht: {}, list: [] };
21
+
22
+ // 回転ビュー(任意)。{ y, x, perspective, origin } を渡すと
23
+ // レイアウト平面を透視投影して描く(Projector 参照)。
24
+ this._rotation = (params && params.rotation) || null;
25
+ }
26
+ /* **************************************************************** *
27
+ * Rotation (回転ビュー)
28
+ * **************************************************************** */
29
+ selector_setAfter () {
30
+ this.attachRotateDrag();
31
+ }
32
+ /**
33
+ * Ctrl + グラブ(ドラッグ)でレイアウトを回転させる対話操作。
34
+ * パン/ズーム(d3.zoom)は Ctrl 押下の mousedown を既定 filter で
35
+ * 無視するため、通常のグラブ移動とは競合しない。
36
+ * 横移動 → y(縦軸回り)、縦移動 → x(横軸回り)を 0.25度/px で加減し、
37
+ * ±80度でクランプする(平面が裏返って表示が壊れないように)。
38
+ * 平面表示(rotation 未指定)から始めた場合は、回転中心に要素全体の
39
+ * バウンディング中心を使う。
40
+ */
41
+ attachRotateDrag () {
42
+ const svg = this.d3Element();
43
+ if (!svg)
44
+ return;
45
+
46
+ const DEG_PER_PX = 0.25;
47
+
48
+ svg.on('mousedown.wf-rotate', (event) => {
49
+ if (!event.ctrlKey || event.button!==0)
50
+ return;
51
+
52
+ event.preventDefault();
53
+
54
+ const base = this._rotation || {};
55
+ const start = {
56
+ mx: event.clientX,
57
+ my: event.clientY,
58
+ y: base.y || 0,
59
+ x: base.x || 0,
60
+ };
61
+ const rest = {
62
+ perspective: base.perspective,
63
+ origin: base.origin || this.defaultOrigin(),
64
+ lift: base.lift,
65
+ };
66
+
67
+ const move = (e) => {
68
+ this.rotation({
69
+ ...rest,
70
+ y: this.clampAngle(start.y + (e.clientX - start.mx) * DEG_PER_PX),
71
+ x: this.clampAngle(start.x + (e.clientY - start.my) * DEG_PER_PX),
72
+ });
73
+ };
74
+ const up = () => {
75
+ window.removeEventListener('mousemove', move);
76
+ window.removeEventListener('mouseup', up);
77
+ };
78
+
79
+ window.addEventListener('mousemove', move);
80
+ window.addEventListener('mouseup', up);
81
+ });
82
+ }
83
+ /** 回転角のクランプ(±80度。±90度で平面が真横になり、越えると裏返る) */
84
+ clampAngle (v) {
85
+ const LIMIT = 80;
86
+
87
+ return Math.max(-LIMIT, Math.min(LIMIT, v));
88
+ }
89
+ /** 回転中心の既定値: ルート要素群のバウンディング中心 */
90
+ defaultOrigin () {
91
+ const list = this._elements.list;
92
+
93
+ if (!list || list.length===0)
94
+ return { x: 0, y: 0 };
95
+
96
+ let min = { x: Infinity, y: Infinity };
97
+ let max = { x: -Infinity, y: -Infinity };
98
+
99
+ for (const d of list) {
100
+ min.x = Math.min(min.x, d.position.x);
101
+ min.y = Math.min(min.y, d.position.y);
102
+ max.x = Math.max(max.x, d.position.x + d.rectangle.w);
103
+ max.y = Math.max(max.y, d.position.y + d.rectangle.h);
104
+ }
105
+
106
+ return { x: (min.x + max.x) / 2, y: (min.y + max.y) / 2 };
107
+ }
108
+ projector () {
109
+ if (!this._rotation)
110
+ return null;
111
+
112
+ return new Projector(this._rotation);
113
+ }
114
+ /**
115
+ * 回転設定の取得/変更。変更時は描画済みのレイヤを空にして描き直す
116
+ * (要素ペインタは enter join のため、その場更新ではなく再描画する)。
117
+ * null を渡すと通常の平面表示に戻る。
118
+ */
119
+ rotation (v) {
120
+ if (arguments.length===0)
121
+ return this._rotation;
122
+
123
+ this._rotation = v;
124
+
125
+ const place = this.layer('foreground');
126
+ if (place) {
127
+ place.selectAll('*').remove();
128
+ this.draw();
129
+ }
130
+
131
+ return this;
20
132
  }
21
133
  /* **************************************************************** *
22
134
  * Make Elements
@@ -78,16 +190,23 @@ export default class Rectum extends Colon {
78
190
  /* **************************************************************** *
79
191
  * Draw
80
192
  * **************************************************************** */
81
- flatten (data) {
193
+ flatten (data, level) {
82
194
  let out = [];
83
195
 
196
+ const depth = level || 0;
197
+
84
198
  for (let d of data) {
199
+ // 入れ子の深さ(ルート=0)。回転ビューの lift(階層ごとに手前へ
200
+ // 浮かせる)が参照する。描画順は親→子なので、浮いた子は常に
201
+ // 親の上に描かれる。
202
+ d._depth = depth;
203
+
85
204
  out.push(d);
86
205
 
87
206
  if (d.children.length===0)
88
207
  continue;
89
208
 
90
- out = out.concat(this.flatten(d.children));
209
+ out = out.concat(this.flatten(d.children, depth + 1));
91
210
  }
92
211
 
93
212
  return out;
@@ -99,7 +218,7 @@ export default class Rectum extends Colon {
99
218
  if (!place)
100
219
  return;
101
220
 
102
- this.element.draw(place, data);
221
+ this.element.draw(place, data, this.projector());
103
222
  }
104
223
  /* **************************************************************** *
105
224
  * Data
@@ -5,5 +5,9 @@ import Asshole from '@yanqirenshi/assh0le';
5
5
  export default function D3Wireframe (props) {
6
6
  const rectum = props.rectum;
7
7
 
8
- return <Asshole id="d3-wireframe-graph" rectum={rectum}/>;
8
+ // 同一ページに複数インスタンスを置く場合は id を指定して
9
+ // DOM id の衝突を避ける(既定 "d3-wireframe-graph")。
10
+ const id = props.id || 'd3-wireframe-graph';
11
+
12
+ return <Asshole id={id} rectum={rectum}/>;
9
13
  }
package/src/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import D3Wireframe from './components/D3Wireframe.js';
2
2
 
3
3
  import Rectum from './Rectum.js';
4
+ import Projector from './Projector.js';
4
5
 
5
6
  export default D3Wireframe;
6
7
 
7
8
  export {
8
9
  Rectum,
10
+ Projector,
9
11
  }