three-base 1.0.81 → 1.0.82

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.
@@ -4,87 +4,160 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2023-04-24 16:18:47
6
6
  */
7
+ var __extends = (this && this.__extends) || (function () {
8
+ var extendStatics = function (d, b) {
9
+ extendStatics = Object.setPrototypeOf ||
10
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12
+ return extendStatics(d, b);
13
+ };
14
+ return function (d, b) {
15
+ if (typeof b !== "function" && b !== null)
16
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17
+ extendStatics(d, b);
18
+ function __() { this.constructor = d; }
19
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
+ };
21
+ })();
7
22
  import { LinearFilter, Sprite, Texture } from 'three';
8
23
  // 带箭头的精灵文字
9
- export default class SpriteText2 extends Sprite {
10
- constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
11
- super();
12
- this._text = `${text}`;
13
- this._textHeight = textHeight;
14
- this._color = color;
15
- this._backgroundColor = false; // no background color
16
- this._padding = 0;
17
- this._borderWidth = 0;
18
- this._borderRadius = 0;
19
- this._borderColor = 'white';
20
- this._strokeWidth = 0;
21
- this._strokeColor = 'white';
22
- this._fontFace = 'Arial';
23
- this._fontSize = 90; // defines text resolution
24
- this._fontWeight = 'normal';
25
- this._genCanvas();
24
+ var SpriteText2 = /** @class */ (function (_super) {
25
+ __extends(SpriteText2, _super);
26
+ function SpriteText2(text, textHeight, color) {
27
+ if (text === void 0) { text = ''; }
28
+ if (textHeight === void 0) { textHeight = 10; }
29
+ if (color === void 0) { color = 'rgba(255, 255, 255, 1)'; }
30
+ var _this = _super.call(this) || this;
31
+ _this._text = "";
32
+ _this._textHeight = 0;
33
+ _this._color = "#ffffff";
34
+ _this._backgroundColor = false; // no background color
35
+ _this._padding = 0;
36
+ _this._borderWidth = 0;
37
+ _this._borderRadius = 0;
38
+ _this._borderColor = 'white';
39
+ _this._strokeWidth = 0;
40
+ _this._strokeColor = 'white';
41
+ _this._fontFace = 'Arial';
42
+ _this._fontSize = 90; // defines text resolution
43
+ _this._fontWeight = 'normal';
44
+ _this._canvas = document.createElement('canvas');
45
+ _this._text = "".concat(text);
46
+ _this._textHeight = textHeight;
47
+ _this._color = color;
48
+ _this._backgroundColor = false; // no background color
49
+ _this._padding = 0;
50
+ _this._borderWidth = 0;
51
+ _this._borderRadius = 0;
52
+ _this._borderColor = 'white';
53
+ _this._strokeWidth = 0;
54
+ _this._strokeColor = 'white';
55
+ _this._fontFace = 'Arial';
56
+ _this._fontSize = 90; // defines text resolution
57
+ _this._fontWeight = 'normal';
58
+ _this._genCanvas();
59
+ return _this;
26
60
  }
27
- _text = "";
28
- _textHeight = 0;
29
- _color = "#ffffff";
30
- _backgroundColor = false; // no background color
31
- _padding = 0;
32
- _borderWidth = 0;
33
- _borderRadius = 0;
34
- _borderColor = 'white';
35
- _strokeWidth = 0;
36
- _strokeColor = 'white';
37
- _fontFace = 'Arial';
38
- _fontSize = 90; // defines text resolution
39
- _fontWeight = 'normal';
40
- _canvas = document.createElement('canvas');
41
- get text() { return this._text; }
42
- set text(text) { this._text = text; this._genCanvas(); }
43
- get textHeight() { return this._textHeight; }
44
- set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
45
- get color() { return this._color; }
46
- set color(color) { this._color = color; this._genCanvas(); }
47
- get backgroundColor() { return this._backgroundColor; }
48
- set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
49
- get padding() { return this._padding; }
50
- set padding(padding) { this._padding = padding; this._genCanvas(); }
51
- get borderWidth() { return this._borderWidth; }
52
- set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
53
- get borderRadius() { return this._borderRadius; }
54
- set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
55
- get borderColor() { return this._borderColor; }
56
- set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
57
- get fontFace() { return this._fontFace; }
58
- set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
59
- get fontSize() { return this._fontSize; }
60
- set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
61
- get fontWeight() { return this._fontWeight; }
62
- set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
63
- get strokeWidth() { return this._strokeWidth; }
64
- set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
65
- get strokeColor() { return this._strokeColor; }
66
- set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
67
- _genCanvas() {
68
- const canvas = this._canvas;
69
- const ctx = canvas.getContext('2d');
70
- const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
71
- const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
72
- const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
73
- const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
74
- const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
75
- const relPadding = padding.map(p => p * this.fontSize * 0.1); // padding in canvas units
76
- const lines = this.text.split('\n');
77
- const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
61
+ Object.defineProperty(SpriteText2.prototype, "text", {
62
+ get: function () { return this._text; },
63
+ set: function (text) { this._text = text; this._genCanvas(); },
64
+ enumerable: false,
65
+ configurable: true
66
+ });
67
+ Object.defineProperty(SpriteText2.prototype, "textHeight", {
68
+ get: function () { return this._textHeight; },
69
+ set: function (textHeight) { this._textHeight = textHeight; this._genCanvas(); },
70
+ enumerable: false,
71
+ configurable: true
72
+ });
73
+ Object.defineProperty(SpriteText2.prototype, "color", {
74
+ get: function () { return this._color; },
75
+ set: function (color) { this._color = color; this._genCanvas(); },
76
+ enumerable: false,
77
+ configurable: true
78
+ });
79
+ Object.defineProperty(SpriteText2.prototype, "backgroundColor", {
80
+ get: function () { return this._backgroundColor; },
81
+ set: function (color) { this._backgroundColor = color; this._genCanvas(); },
82
+ enumerable: false,
83
+ configurable: true
84
+ });
85
+ Object.defineProperty(SpriteText2.prototype, "padding", {
86
+ get: function () { return this._padding; },
87
+ set: function (padding) { this._padding = padding; this._genCanvas(); },
88
+ enumerable: false,
89
+ configurable: true
90
+ });
91
+ Object.defineProperty(SpriteText2.prototype, "borderWidth", {
92
+ get: function () { return this._borderWidth; },
93
+ set: function (borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); },
94
+ enumerable: false,
95
+ configurable: true
96
+ });
97
+ Object.defineProperty(SpriteText2.prototype, "borderRadius", {
98
+ get: function () { return this._borderRadius; },
99
+ set: function (borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); },
100
+ enumerable: false,
101
+ configurable: true
102
+ });
103
+ Object.defineProperty(SpriteText2.prototype, "borderColor", {
104
+ get: function () { return this._borderColor; },
105
+ set: function (borderColor) { this._borderColor = borderColor; this._genCanvas(); },
106
+ enumerable: false,
107
+ configurable: true
108
+ });
109
+ Object.defineProperty(SpriteText2.prototype, "fontFace", {
110
+ get: function () { return this._fontFace; },
111
+ set: function (fontFace) { this._fontFace = fontFace; this._genCanvas(); },
112
+ enumerable: false,
113
+ configurable: true
114
+ });
115
+ Object.defineProperty(SpriteText2.prototype, "fontSize", {
116
+ get: function () { return this._fontSize; },
117
+ set: function (fontSize) { this._fontSize = fontSize; this._genCanvas(); },
118
+ enumerable: false,
119
+ configurable: true
120
+ });
121
+ Object.defineProperty(SpriteText2.prototype, "fontWeight", {
122
+ get: function () { return this._fontWeight; },
123
+ set: function (fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); },
124
+ enumerable: false,
125
+ configurable: true
126
+ });
127
+ Object.defineProperty(SpriteText2.prototype, "strokeWidth", {
128
+ get: function () { return this._strokeWidth; },
129
+ set: function (strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); },
130
+ enumerable: false,
131
+ configurable: true
132
+ });
133
+ Object.defineProperty(SpriteText2.prototype, "strokeColor", {
134
+ get: function () { return this._strokeColor; },
135
+ set: function (strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); },
136
+ enumerable: false,
137
+ configurable: true
138
+ });
139
+ SpriteText2.prototype._genCanvas = function () {
140
+ var _this = this;
141
+ var canvas = this._canvas;
142
+ var ctx = canvas.getContext('2d');
143
+ var border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
144
+ var relBorder = border.map(function (b) { return b * _this.fontSize * 0.1; }); // border in canvas units
145
+ var borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
146
+ var relBorderRadius = borderRadius.map(function (b) { return b * _this.fontSize * 0.1; }); // border radius in canvas units
147
+ var padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
148
+ var relPadding = padding.map(function (p) { return p * _this.fontSize * 0.1; }); // padding in canvas units
149
+ var lines = this.text.split('\n');
150
+ var font = "".concat(this.fontWeight, " ").concat(this.fontSize, "px ").concat(this.fontFace);
78
151
  ctx.font = font; // measure canvas with appropriate font
79
- const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
80
- const innerHeight = this.fontSize * lines.length;
152
+ var innerWidth = Math.max.apply(Math, lines.map(function (line) { return ctx.measureText(line).width; }));
153
+ var innerHeight = this.fontSize * lines.length;
81
154
  canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
82
155
  canvas.height = (innerHeight + relBorder[1] * 2 + relPadding[1] * 2) * 2 + 40;
83
156
  // paint border
84
157
  if (this.borderWidth) {
85
158
  ctx.strokeStyle = this.borderColor;
86
159
  if (relBorder[0]) { // left + right borders
87
- const hb = relBorder[0] / 2;
160
+ var hb = relBorder[0] / 2;
88
161
  ctx.lineWidth = relBorder[0];
89
162
  ctx.beginPath();
90
163
  ctx.moveTo(hb, relBorderRadius[0]);
@@ -94,7 +167,7 @@ export default class SpriteText2 extends Sprite {
94
167
  ctx.stroke();
95
168
  }
96
169
  if (relBorder[1]) { // top + bottom borders
97
- const hb = relBorder[1] / 2;
170
+ var hb = relBorder[1] / 2;
98
171
  ctx.lineWidth = relBorder[1];
99
172
  ctx.beginPath();
100
173
  ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
@@ -107,8 +180,8 @@ export default class SpriteText2 extends Sprite {
107
180
  ctx.stroke();
108
181
  }
109
182
  if (this.borderRadius) { // strike rounded corners
110
- const cornerWidth = Math.max(...relBorder);
111
- const hb = cornerWidth / 2;
183
+ var cornerWidth = Math.max.apply(Math, relBorder);
184
+ var hb = cornerWidth / 2;
112
185
  ctx.lineWidth = cornerWidth;
113
186
  ctx.beginPath();
114
187
  [
@@ -116,7 +189,8 @@ export default class SpriteText2 extends Sprite {
116
189
  !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
117
190
  !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]],
118
191
  !!relBorderRadius[3] && [relBorderRadius[3], hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[3]]
119
- ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
192
+ ].filter(function (d) { return d; }).forEach(function (_a) {
193
+ var x0 = _a[0], x1 = _a[1], y0 = _a[2], y1 = _a[3];
120
194
  ctx.moveTo(x0, y0);
121
195
  ctx.quadraticCurveTo(x1, y0, x1, y1);
122
196
  });
@@ -152,36 +226,36 @@ export default class SpriteText2 extends Sprite {
152
226
  ctx.font = font; // Set font again after canvas is resized, as context properties are reset
153
227
  ctx.fillStyle = this.color;
154
228
  ctx.textBaseline = 'bottom';
155
- const drawTextStroke = this.strokeWidth > 0;
229
+ var drawTextStroke = this.strokeWidth > 0;
156
230
  if (drawTextStroke) {
157
231
  ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
158
232
  ctx.strokeStyle = this.strokeColor;
159
233
  }
160
- lines.forEach((line, index) => {
161
- const lineX = (innerWidth - ctx.measureText(line).width) / 2;
162
- const lineY = (index + 1) * this.fontSize;
234
+ lines.forEach(function (line, index) {
235
+ var lineX = (innerWidth - ctx.measureText(line).width) / 2;
236
+ var lineY = (index + 1) * _this.fontSize;
163
237
  drawTextStroke && ctx.strokeText(line, lineX, lineY);
164
238
  ctx.fillText(line, lineX, lineY);
165
239
  });
166
240
  // Inject canvas into sprite
167
241
  if (this.material.map)
168
242
  this.material.map.dispose(); // gc previous texture
169
- const texture = this.material.map = new Texture(canvas);
243
+ var texture = this.material.map = new Texture(canvas);
170
244
  texture.minFilter = LinearFilter;
171
245
  texture.needsUpdate = true;
172
- const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
246
+ var yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
173
247
  this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
174
- }
248
+ };
175
249
  //鼠标拾取计算
176
- raycast(raycaster, intersects) {
177
- let intersect = [];
178
- super.raycast(raycaster, intersect);
179
- let inte = intersect[0];
250
+ SpriteText2.prototype.raycast = function (raycaster, intersects) {
251
+ var intersect = [];
252
+ _super.prototype.raycast.call(this, raycaster, intersect);
253
+ var inte = intersect[0];
180
254
  if (inte && (inte.uv.y > 0.55)) {
181
255
  intersects.push(inte);
182
256
  }
183
- }
184
- copy(source) {
257
+ };
258
+ SpriteText2.prototype.copy = function (source) {
185
259
  this.color = source.color;
186
260
  this.backgroundColor = source.backgroundColor;
187
261
  this.padding = source.padding;
@@ -193,5 +267,7 @@ export default class SpriteText2 extends Sprite {
193
267
  this.strokeWidth = source.strokeWidth;
194
268
  this.strokeColor = source.strokeColor;
195
269
  return this;
196
- }
197
- }
270
+ };
271
+ return SpriteText2;
272
+ }(Sprite));
273
+ export default SpriteText2;
@@ -4,59 +4,80 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2023-04-07 21:52:03
6
6
  */
7
+ var __extends = (this && this.__extends) || (function () {
8
+ var extendStatics = function (d, b) {
9
+ extendStatics = Object.setPrototypeOf ||
10
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12
+ return extendStatics(d, b);
13
+ };
14
+ return function (d, b) {
15
+ if (typeof b !== "function" && b !== null)
16
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17
+ extendStatics(d, b);
18
+ function __() { this.constructor = d; }
19
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
+ };
21
+ })();
7
22
  import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry.js';
8
23
  import { Mesh, MeshStandardMaterial } from "three";
9
24
  import fontContorller from "./fontController";
10
25
  // 3D文字模型
11
- export default class Text3D extends Mesh {
12
- constructor(text = "new Text3D()") {
13
- super();
14
- this.setText({ text });
26
+ var Text3D = /** @class */ (function (_super) {
27
+ __extends(Text3D, _super);
28
+ function Text3D(text) {
29
+ if (text === void 0) { text = "new Text3D()"; }
30
+ var _this = _super.call(this) || this;
31
+ _this.material = new MeshStandardMaterial({
32
+ metalness: 0.8,
33
+ roughness: 0.5, // 粗糙度,值为0到1
34
+ });
35
+ // 配置
36
+ _this._config = {
37
+ text: "",
38
+ fontType: "simhei",
39
+ size: 1,
40
+ curveSegments: 10,
41
+ height: 0.1,
42
+ bevelEnabled: true,
43
+ bevelThickness: 0.02,
44
+ bevelSize: 0.015,
45
+ bevelOffset: 0,
46
+ bevelSegments: 3, //斜角分段
47
+ };
48
+ _this.setText({ text: text });
49
+ return _this;
15
50
  }
16
- // 字体的存储路径 例如 : "http://drm.gitee.io/three-base-font/simhei/字.js"
17
- static path = "http://drm.gitee.io/three-base-font/";
18
- material = new MeshStandardMaterial({
19
- metalness: 0.8,
20
- roughness: 0.5, // 粗糙度,值为0到1
21
- });
22
- // 配置
23
- _config = {
24
- text: "",
25
- fontType: "simhei",
26
- size: 1,
27
- curveSegments: 10,
28
- height: 0.1,
29
- bevelEnabled: true,
30
- bevelThickness: 0.02,
31
- bevelSize: 0.015,
32
- bevelOffset: 0,
33
- bevelSegments: 3, //斜角分段
34
- };
35
51
  // 设置文字
36
- setText(text3DProps) {
37
- if (Object.keys(text3DProps).some(key => text3DProps[key] !== this._config[key])) {
52
+ Text3D.prototype.setText = function (text3DProps) {
53
+ var _this = this;
54
+ if (Object.keys(text3DProps).some(function (key) { return text3DProps[key] !== _this._config[key]; })) {
38
55
  Object.assign(this._config, text3DProps);
39
56
  fontContorller.loadText({
40
57
  fontBasePath: Text3D.path,
41
58
  text: this._config.text,
42
59
  fontType: this._config.fontType,
43
- callback: THREEFont => {
44
- this.geometry.dispose();
45
- this.geometry = new TextGeometry(this._config.text || "", {
60
+ callback: function (THREEFont) {
61
+ _this.geometry.dispose();
62
+ _this.geometry = new TextGeometry(_this._config.text || "", {
46
63
  font: THREEFont,
47
- size: this._config.size,
48
- curveSegments: this._config.curveSegments,
49
- height: this._config.height,
50
- bevelEnabled: this._config.bevelEnabled,
51
- bevelThickness: this._config.bevelThickness,
52
- bevelSize: this._config.bevelSize,
53
- bevelOffset: this._config.bevelOffset,
54
- bevelSegments: this._config.bevelSegments, //斜角分段
64
+ size: _this._config.size,
65
+ curveSegments: _this._config.curveSegments,
66
+ height: _this._config.height,
67
+ bevelEnabled: _this._config.bevelEnabled,
68
+ bevelThickness: _this._config.bevelThickness,
69
+ bevelSize: _this._config.bevelSize,
70
+ bevelOffset: _this._config.bevelOffset,
71
+ bevelSegments: _this._config.bevelSegments, //斜角分段
55
72
  });
56
- this.geometry.center();
57
- this.dispatchEvent({ type: "loaded" });
73
+ _this.geometry.center();
74
+ _this.dispatchEvent({ type: "loaded" });
58
75
  }
59
76
  });
60
77
  }
61
- }
62
- }
78
+ };
79
+ // 字体的存储路径 例如 : "http://drm.gitee.io/three-base-font/simhei/字.js"
80
+ Text3D.path = "http://drm.gitee.io/three-base-font/";
81
+ return Text3D;
82
+ }(Mesh));
83
+ export default Text3D;