@yanqirenshi/d3.wireframe 0.2.0 → 0.2.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/src/Drawer.js CHANGED
@@ -1,270 +1,270 @@
1
- // https://yanqirenshi.github.io/D3.Sketch/dist/edge/Drawer.js
2
- /**
3
- * 幾何学計算するための Drawer です。
4
- *
5
- * @example
6
- * let drawer = new DrawerHierarchy();
7
- */
8
- export class DrawerGeometry {
9
- ///// ////////////////////////////////////////////////////////////////
10
- ///// Utilities
11
- ///// ////////////////////////////////////////////////////////////////
12
- deg2rad (degree) {
13
- return degree * ( Math.PI / 180 );
14
- }
15
- ///// ////////////////////////////////////////////////////////////////
16
- ///// Cross Point of two lines
17
- ///// ////////////////////////////////////////////////////////////////
18
- isCorss(A, B, C, D) {
19
- // 二つの線分の交差チェック
20
- // https://www.hiramine.com/programming/graphics/2d_segmentintersection.html
21
- let ACx = C.x - A.x;
22
- let ACy = C.y - A.y;
23
- let BUNBO = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);
24
-
25
- if (BUNBO===0)
26
- return false;
27
-
28
- let r = ((D.y - C.y) * ACx - (D.x - C.x) * ACy) / BUNBO;
29
- let s = ((B.y - A.y) * ACx - (B.x - A.x) * ACy) / BUNBO;
30
-
31
- return ((0 <= r && r <= 1) && (0 <= s && s <= 1));
32
- }
33
- // 2直線の交点を求める。(具)
34
- getCrossPointCore (line, line_port) {
35
- let out = { x:0, y:0 };
36
-
37
- let A = line.from;
38
- let B = line.to;
39
- let C = line_port.from;
40
- let D = line_port.to;
41
-
42
- let bunbo = (B.y - A.y) * (D.x - C.x) - (B.x - A.x) * (D.y - C.y);
43
-
44
- if (!this.isCorss(A, B, C, D))
45
- return null;
46
-
47
- // 二つの線分の交点を算出する。
48
- // http://mf-atelier.sakura.ne.jp/mf-atelier/modules/tips/program/algorithm/a1.html
49
- let d1, d2;
50
-
51
- d1 = (C.y * D.x) - (C.x * D.y);
52
- d2 = (A.y * B.x) - (A.x * B.y);
53
-
54
- out.x = (d1 * (B.x - A.x) - d2 * (D.x - C.x)) / bunbo;
55
- out.y = (d1 * (B.y - A.y) - d2 * (D.y - C.y)) / bunbo;
56
-
57
- return out;
58
- }
59
- // 2直線の交点を求める。
60
- getCrossPoint (lines, line_port) {
61
- for (let line of lines) {
62
- let point = this.getCrossPointCore(line, line_port);
63
-
64
- if (point)
65
- return point;
66
- }
67
-
68
- return null;
69
- }
70
- }
71
-
72
- /**
73
- * Drawer の共通要素とての Core です。
74
- * 各 Drawer の要素を整えるメッソッドをまとめているクラスです。
75
- * いろいろと共通なものがあると思うので。
76
- * また見通しも良くなるかと。
77
- * 作る必要があるのかはアレですが。。。
78
- *
79
- * @example
80
- * let drawer = new DrawerHierarchy();
81
- */
82
- export class Drawer {
83
- ///// ////////////////////////////////////////////////////////////////
84
- ///// normalize
85
- ///// ////////////////////////////////////////////////////////////////
86
- normalizeFont (font_core) {
87
- let template = { size: 16, color: '#333333' };
88
-
89
- if (!font_core)
90
- return template;
91
-
92
- return font_core;
93
- }
94
- normalizeLabel (label_core) {
95
- let label = {
96
- contents: '',
97
- position: { x: 20, y: 20 },
98
- font: { size: 16, color: '#333333' },
99
- };
100
-
101
- if (!label_core)
102
- return label;
103
-
104
- if (label_core.contents)
105
- label.contents = label_core.contents;
106
-
107
- let label_tmp = label_core.position ? label_core.position : label.position;
108
- label.position = this.normalizePosition(label_tmp);
109
-
110
- label.font = this.normalizeFont(label_core.font);
111
-
112
- return label;
113
- }
114
- normalizeSize (size_core) {
115
- let size = { w: 0, h: 0 };
116
-
117
- if (!size_core)
118
- return size;
119
-
120
- if (size_core.w || size_core.w===0)
121
- size.w = size_core.w;
122
-
123
- if (size_core.h || size_core.h===0)
124
- size.h = size_core.h;
125
-
126
- return size;
127
- }
128
- normalizePosition (position_core) {
129
- let position = { x: 0, y: 0 };
130
-
131
- if (!position_core)
132
- return position;
133
-
134
- if (position_core.x || position_core.x===0)
135
- position.x = position_core.x;
136
-
137
- if (position_core.y || position_core.y===0)
138
- position.y = position_core.y;
139
-
140
- return position;
141
- }
142
- normalizeBackground (background_core) {
143
- let background = {
144
- color: '#ffffff'
145
- };
146
-
147
- if (!background_core)
148
- return background;
149
-
150
- if (background_core.color)
151
- background.color = background_core.color;
152
-
153
- return background;
154
- }
155
- normalizeBorder (border_core) {
156
- let border = {
157
- width: 1,
158
- type: 'solid',
159
- color: '#666666'
160
- };
161
-
162
- if (!border_core)
163
- return border;
164
-
165
- if (border_core.width || border_core.width===0)
166
- border.width = border_core.width;
167
-
168
- if (border_core.color)
169
- border.color = border_core.color;
170
-
171
- if (border_core.type)
172
- border.type = border_core.type;
173
-
174
- return border;
175
- }
176
- normalizePadding (padding_core) {
177
- let template = { top: 0, left: 0, bottom: 0, right: 0 };
178
-
179
- if (!padding_core)
180
- return template;
181
-
182
- return padding_core;
183
- }
184
- }
185
-
186
- /**
187
- * 階層構造のデータの Drawer です。
188
- * Data の 位置、サイズ を整えます。
189
- *
190
- * @example
191
- * let drawer = new DrawerHierarchy();
192
- */
193
- export class DrawerHierarchy extends Drawer {
194
- ///// ////////////////////////////////////////////////////////////////
195
- ///// Utilities
196
- ///// ////////////////////////////////////////////////////////////////
197
- data2rect (data) {
198
- return {
199
- from: {
200
- x: data.position.x,
201
- y: data.position.y,
202
- },
203
- to: {
204
- x: data.position.x + data.rectangle.w,
205
- y: data.position.y + data.rectangle.h,
206
- }
207
- };
208
- }
209
- ///// ////////////////////////////////////////////////////////////////
210
- ///// Fitting
211
- ///// ////////////////////////////////////////////////////////////////
212
- fittingCalSizeCore (rect_a, rect_b) {
213
- if (!rect_a.from) {
214
- rect_a.from = { x: rect_b.from.x, y: rect_b.from.y };
215
- } else {
216
- if (rect_a.from.x > rect_b.from.x)
217
- rect_a.from.x = rect_b.from.x;
218
-
219
- if (rect_a.from.y > rect_b.from.y)
220
- rect_a.from.y = rect_b.from.y;
221
- }
222
-
223
- if (!rect_a.to) {
224
- rect_a.to = {
225
- x: rect_b.to.x,
226
- y: rect_b.to.y,
227
- };
228
- } else {
229
- if (rect_a.to.x < rect_b.to.x)
230
- rect_a.to.x = rect_b.to.x;
231
-
232
- if (rect_a.to.y < rect_b.to.y)
233
- rect_a.to.y = rect_b.to.y;
234
- }
235
- }
236
- fittingCalSize (rect, child) {
237
- let rect_a = rect;
238
- let rect_b = this.data2rect(child);
239
-
240
- this.fittingCalSizeCore(rect_a, rect_b);
241
- }
242
- fitting (data, parent) {
243
- // parent からの相対位置で補正
244
- if (parent) {
245
- data.position.x = parent.position.x + data.position.x;
246
- data.position.y = parent.position.y + data.position.y;
247
- }
248
-
249
- // children も同様に。
250
- let rect = {
251
- from: null,
252
- to: null,
253
- };
254
-
255
- // children の fitting 合せて data のサイズも計測。
256
- let children = data.children;
257
- if (children && children.length > 0) {
258
- for (let child of data.children) {
259
- this.fitting(child, data);
260
-
261
- this.fittingCalSize(rect, child); // rect は破壊的
262
- }
263
-
264
- // children 内容で data のサイズを補正
265
- this.fittingCalSizeCore(rect, this.data2rect(data));
266
- data.rectangle.w = rect.to.x - rect.from.x;
267
- data.rectangle.h = rect.to.y - rect.from.y;
268
- }
269
- }
270
- }
1
+ // https://yanqirenshi.github.io/D3.Sketch/dist/edge/Drawer.js
2
+ /**
3
+ * 幾何学計算するための Drawer です。
4
+ *
5
+ * @example
6
+ * let drawer = new DrawerHierarchy();
7
+ */
8
+ export class DrawerGeometry {
9
+ ///// ////////////////////////////////////////////////////////////////
10
+ ///// Utilities
11
+ ///// ////////////////////////////////////////////////////////////////
12
+ deg2rad (degree) {
13
+ return degree * ( Math.PI / 180 );
14
+ }
15
+ ///// ////////////////////////////////////////////////////////////////
16
+ ///// Cross Point of two lines
17
+ ///// ////////////////////////////////////////////////////////////////
18
+ isCorss(A, B, C, D) {
19
+ // 二つの線分の交差チェック
20
+ // https://www.hiramine.com/programming/graphics/2d_segmentintersection.html
21
+ let ACx = C.x - A.x;
22
+ let ACy = C.y - A.y;
23
+ let BUNBO = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);
24
+
25
+ if (BUNBO===0)
26
+ return false;
27
+
28
+ let r = ((D.y - C.y) * ACx - (D.x - C.x) * ACy) / BUNBO;
29
+ let s = ((B.y - A.y) * ACx - (B.x - A.x) * ACy) / BUNBO;
30
+
31
+ return ((0 <= r && r <= 1) && (0 <= s && s <= 1));
32
+ }
33
+ // 2直線の交点を求める。(具)
34
+ getCrossPointCore (line, line_port) {
35
+ let out = { x:0, y:0 };
36
+
37
+ let A = line.from;
38
+ let B = line.to;
39
+ let C = line_port.from;
40
+ let D = line_port.to;
41
+
42
+ let bunbo = (B.y - A.y) * (D.x - C.x) - (B.x - A.x) * (D.y - C.y);
43
+
44
+ if (!this.isCorss(A, B, C, D))
45
+ return null;
46
+
47
+ // 二つの線分の交点を算出する。
48
+ // http://mf-atelier.sakura.ne.jp/mf-atelier/modules/tips/program/algorithm/a1.html
49
+ let d1, d2;
50
+
51
+ d1 = (C.y * D.x) - (C.x * D.y);
52
+ d2 = (A.y * B.x) - (A.x * B.y);
53
+
54
+ out.x = (d1 * (B.x - A.x) - d2 * (D.x - C.x)) / bunbo;
55
+ out.y = (d1 * (B.y - A.y) - d2 * (D.y - C.y)) / bunbo;
56
+
57
+ return out;
58
+ }
59
+ // 2直線の交点を求める。
60
+ getCrossPoint (lines, line_port) {
61
+ for (let line of lines) {
62
+ let point = this.getCrossPointCore(line, line_port);
63
+
64
+ if (point)
65
+ return point;
66
+ }
67
+
68
+ return null;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Drawer の共通要素とての Core です。
74
+ * 各 Drawer の要素を整えるメッソッドをまとめているクラスです。
75
+ * いろいろと共通なものがあると思うので。
76
+ * また見通しも良くなるかと。
77
+ * 作る必要があるのかはアレですが。。。
78
+ *
79
+ * @example
80
+ * let drawer = new DrawerHierarchy();
81
+ */
82
+ export class Drawer {
83
+ ///// ////////////////////////////////////////////////////////////////
84
+ ///// normalize
85
+ ///// ////////////////////////////////////////////////////////////////
86
+ normalizeFont (font_core) {
87
+ let template = { size: 16, color: '#333333' };
88
+
89
+ if (!font_core)
90
+ return template;
91
+
92
+ return font_core;
93
+ }
94
+ normalizeLabel (label_core) {
95
+ let label = {
96
+ contents: '',
97
+ position: { x: 20, y: 20 },
98
+ font: { size: 16, color: '#333333' },
99
+ };
100
+
101
+ if (!label_core)
102
+ return label;
103
+
104
+ if (label_core.contents)
105
+ label.contents = label_core.contents;
106
+
107
+ let label_tmp = label_core.position ? label_core.position : label.position;
108
+ label.position = this.normalizePosition(label_tmp);
109
+
110
+ label.font = this.normalizeFont(label_core.font);
111
+
112
+ return label;
113
+ }
114
+ normalizeSize (size_core) {
115
+ let size = { w: 0, h: 0 };
116
+
117
+ if (!size_core)
118
+ return size;
119
+
120
+ if (size_core.w || size_core.w===0)
121
+ size.w = size_core.w;
122
+
123
+ if (size_core.h || size_core.h===0)
124
+ size.h = size_core.h;
125
+
126
+ return size;
127
+ }
128
+ normalizePosition (position_core) {
129
+ let position = { x: 0, y: 0 };
130
+
131
+ if (!position_core)
132
+ return position;
133
+
134
+ if (position_core.x || position_core.x===0)
135
+ position.x = position_core.x;
136
+
137
+ if (position_core.y || position_core.y===0)
138
+ position.y = position_core.y;
139
+
140
+ return position;
141
+ }
142
+ normalizeBackground (background_core) {
143
+ let background = {
144
+ color: '#ffffff'
145
+ };
146
+
147
+ if (!background_core)
148
+ return background;
149
+
150
+ if (background_core.color)
151
+ background.color = background_core.color;
152
+
153
+ return background;
154
+ }
155
+ normalizeBorder (border_core) {
156
+ let border = {
157
+ width: 1,
158
+ type: 'solid',
159
+ color: '#666666'
160
+ };
161
+
162
+ if (!border_core)
163
+ return border;
164
+
165
+ if (border_core.width || border_core.width===0)
166
+ border.width = border_core.width;
167
+
168
+ if (border_core.color)
169
+ border.color = border_core.color;
170
+
171
+ if (border_core.type)
172
+ border.type = border_core.type;
173
+
174
+ return border;
175
+ }
176
+ normalizePadding (padding_core) {
177
+ let template = { top: 0, left: 0, bottom: 0, right: 0 };
178
+
179
+ if (!padding_core)
180
+ return template;
181
+
182
+ return padding_core;
183
+ }
184
+ }
185
+
186
+ /**
187
+ * 階層構造のデータの Drawer です。
188
+ * Data の 位置、サイズ を整えます。
189
+ *
190
+ * @example
191
+ * let drawer = new DrawerHierarchy();
192
+ */
193
+ export class DrawerHierarchy extends Drawer {
194
+ ///// ////////////////////////////////////////////////////////////////
195
+ ///// Utilities
196
+ ///// ////////////////////////////////////////////////////////////////
197
+ data2rect (data) {
198
+ return {
199
+ from: {
200
+ x: data.position.x,
201
+ y: data.position.y,
202
+ },
203
+ to: {
204
+ x: data.position.x + data.rectangle.w,
205
+ y: data.position.y + data.rectangle.h,
206
+ }
207
+ };
208
+ }
209
+ ///// ////////////////////////////////////////////////////////////////
210
+ ///// Fitting
211
+ ///// ////////////////////////////////////////////////////////////////
212
+ fittingCalSizeCore (rect_a, rect_b) {
213
+ if (!rect_a.from) {
214
+ rect_a.from = { x: rect_b.from.x, y: rect_b.from.y };
215
+ } else {
216
+ if (rect_a.from.x > rect_b.from.x)
217
+ rect_a.from.x = rect_b.from.x;
218
+
219
+ if (rect_a.from.y > rect_b.from.y)
220
+ rect_a.from.y = rect_b.from.y;
221
+ }
222
+
223
+ if (!rect_a.to) {
224
+ rect_a.to = {
225
+ x: rect_b.to.x,
226
+ y: rect_b.to.y,
227
+ };
228
+ } else {
229
+ if (rect_a.to.x < rect_b.to.x)
230
+ rect_a.to.x = rect_b.to.x;
231
+
232
+ if (rect_a.to.y < rect_b.to.y)
233
+ rect_a.to.y = rect_b.to.y;
234
+ }
235
+ }
236
+ fittingCalSize (rect, child) {
237
+ let rect_a = rect;
238
+ let rect_b = this.data2rect(child);
239
+
240
+ this.fittingCalSizeCore(rect_a, rect_b);
241
+ }
242
+ fitting (data, parent) {
243
+ // parent からの相対位置で補正
244
+ if (parent) {
245
+ data.position.x = parent.position.x + data.position.x;
246
+ data.position.y = parent.position.y + data.position.y;
247
+ }
248
+
249
+ // children も同様に。
250
+ let rect = {
251
+ from: null,
252
+ to: null,
253
+ };
254
+
255
+ // children の fitting 合せて data のサイズも計測。
256
+ let children = data.children;
257
+ if (children && children.length > 0) {
258
+ for (let child of data.children) {
259
+ this.fitting(child, data);
260
+
261
+ this.fittingCalSize(rect, child); // rect は破壊的
262
+ }
263
+
264
+ // children 内容で data のサイズを補正
265
+ this.fittingCalSizeCore(rect, this.data2rect(data));
266
+ data.rectangle.w = rect.to.x - rect.from.x;
267
+ data.rectangle.h = rect.to.y - rect.from.y;
268
+ }
269
+ }
270
+ }