three-base 2.0.75 → 2.0.76

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 (82) hide show
  1. package/ApngTexture.d.ts +13 -13
  2. package/ApngTexture.js +61 -61
  3. package/ApngTextureManage.d.ts +18 -18
  4. package/ApngTextureManage.js +87 -87
  5. package/EventMesh.d.ts +18 -18
  6. package/EventMesh.js +59 -59
  7. package/ExtrudeMesh.d.ts +111 -111
  8. package/ExtrudeMesh.js +363 -363
  9. package/FPScontrol.d.ts +45 -45
  10. package/FPScontrol.js +171 -171
  11. package/Fire/Fire.d.ts +28 -28
  12. package/Fire/Fire.js +79 -79
  13. package/Fire/FireGromtry.d.ts +9 -9
  14. package/Fire/FireGromtry.js +40 -40
  15. package/Fire/FireMaterial.d.ts +28 -28
  16. package/Fire/FireMaterial.js +32 -32
  17. package/InstancedMeshBase.d.ts +32 -32
  18. package/InstancedMeshBase.js +234 -236
  19. package/InstancedMeshBase.ts +1 -4
  20. package/KeyFrameAnimation.d.ts +25 -25
  21. package/KeyFrameAnimation.js +78 -78
  22. package/MeshBase.d.ts +34 -34
  23. package/MeshBase.js +187 -187
  24. package/Mirror.d.ts +25 -25
  25. package/Mirror.js +139 -139
  26. package/PlaneModal1/PlaneModal1.d.ts +31 -31
  27. package/PlaneModal1/PlaneModal1.js +105 -105
  28. package/PlaneModal1/index.d.ts +1 -1
  29. package/PlaneModal1/index.js +1 -1
  30. package/PlaneText.d.ts +44 -44
  31. package/PlaneText.js +30 -30
  32. package/PlaneWithHtml.d.ts +11 -11
  33. package/PlaneWithHtml.js +68 -68
  34. package/PointLight/PointLight.d.ts +13 -13
  35. package/PointLight/PointLight.js +64 -64
  36. package/PointLight/index.d.ts +1 -1
  37. package/PointLight/index.js +1 -1
  38. package/Sky/Sky.d.ts +13 -13
  39. package/Sky/Sky.js +28 -28
  40. package/Sky/index.d.ts +1 -1
  41. package/Sky/index.js +1 -1
  42. package/SpriteModal1/SpriteModal1.d.ts +30 -30
  43. package/SpriteModal1/SpriteModal1.js +110 -110
  44. package/SpriteModal1/index.d.ts +1 -1
  45. package/SpriteModal1/index.js +1 -1
  46. package/SpriteText.d.ts +45 -45
  47. package/SpriteText.js +163 -163
  48. package/SpriteText2.d.ts +47 -47
  49. package/SpriteText2.js +198 -198
  50. package/SpriteWithHtml.d.ts +7 -7
  51. package/SpriteWithHtml.js +49 -49
  52. package/Text3D/Text3D.d.ts +20 -20
  53. package/Text3D/Text3D.js +62 -62
  54. package/Text3D/fontController.d.ts +59 -59
  55. package/Text3D/fontController.js +200 -200
  56. package/Text3D/index.d.ts +1 -1
  57. package/Text3D/index.js +1 -1
  58. package/ThreeBase.d.ts +92 -92
  59. package/ThreeBase.js +619 -619
  60. package/ThreeMouse.d.ts +39 -39
  61. package/ThreeMouse.js +359 -359
  62. package/Transition.d.ts +40 -40
  63. package/Transition.js +207 -207
  64. package/index.d.ts +1 -1
  65. package/index.js +2 -2
  66. package/keyContorls.d.ts +25 -25
  67. package/keyContorls.js +98 -98
  68. package/package.json +1 -1
  69. package/pass/FullScreenQuad.d.ts +7 -7
  70. package/pass/FullScreenQuad.js +25 -25
  71. package/pass/glow_pass.d.ts +2 -2
  72. package/pass/glow_pass.js +236 -236
  73. package/pass/stroke_pass.d.ts +2 -2
  74. package/pass/stroke_pass.js +202 -202
  75. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  76. package/shaders/FXAAShaderMaterial.js +21 -21
  77. package/shaders.d.ts +17 -17
  78. package/shaders.js +99 -99
  79. package/threeLoader.d.ts +26 -26
  80. package/threeLoader.js +189 -189
  81. package/threeUtils.d.ts +47 -47
  82. package/threeUtils.js +266 -266
@@ -1,110 +1,110 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-03-08 00:01:34
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-03-08 02:05:25
6
- */
7
- import { Sprite, Vector2 } from "three";
8
- import spriteModal1PNG from "./spriteModal1.png";
9
- import threeLoader from "../threeLoader";
10
- // sprit弹窗1
11
- export default class SpritModal1 extends Sprite {
12
- constructor(config = {}) {
13
- super();
14
- this.updata(config);
15
- this.material.map = this.texture;
16
- this.material.transparent = true;
17
- this.material.alphaTest = 0.01;
18
- }
19
- // 背景图
20
- static img = (() => {
21
- const img = new Image();
22
- img.src = spriteModal1PNG;
23
- return img;
24
- })();
25
- canvas = (() => {
26
- const canvas = document.createElement("canvas");
27
- canvas.width = 1024;
28
- canvas.height = 1024;
29
- return canvas;
30
- })();
31
- context = this.canvas.getContext("2d");
32
- texture = threeLoader.getTexture(this.canvas);
33
- // 配置
34
- _config = {
35
- size: 10,
36
- title: "SpritModal1",
37
- titleColor: "#ffffff",
38
- texts: []
39
- // [
40
- // {
41
- // text: "1",
42
- // color: "#ffff00",
43
- // size: 40
44
- // },
45
- // {
46
- // text: "2",
47
- // color: "#ff00ff",
48
- // size: 40
49
- // },
50
- // ]
51
- };
52
- async updata(config) {
53
- Object.assign(this._config, config);
54
- //绘制背景
55
- this.context.clearRect(0, 0, 1024, 1024);
56
- while (!SpritModal1.img.complete) {
57
- await new Promise(resolve => setTimeout(resolve, 100));
58
- }
59
- // 设置大小
60
- this.scale.set(this._config.size, this._config.size, this._config.size);
61
- // this.context.fillStyle = "#00000088";
62
- // this.context.fillRect(0, 0, 1024, 1024);
63
- //绘制背景
64
- this.context.drawImage(SpritModal1.img, 186, 0);
65
- // 绘制标题
66
- this.context.fillStyle = this._config.titleColor;
67
- this.context.font = '40px serif'; // 设置字体大小
68
- this.context.textAlign = "left";
69
- this.context.textBaseline = "top";
70
- this.context.fillText(this._config.title, 250, 72);
71
- let textPos = new Vector2(250, 150);
72
- let maxWidth = 530;
73
- // 绘制内容
74
- this._config.texts.map(({ text = "text", color = "#ffffff", size = 40 }) => {
75
- this.context.fillStyle = color;
76
- this.context.font = `${size}px serif`; // 设置字体大小
77
- this.context.textAlign = "left";
78
- this.context.textBaseline = "top";
79
- // 字符分隔为数组
80
- var arrText = text.split('');
81
- var line = '';
82
- // 文字自动换行计算
83
- for (var n = 0; n < arrText.length; n++) {
84
- var metrics = this.context.measureText(line + arrText[n]);
85
- var testWidth = metrics.width;
86
- if ((testWidth > maxWidth && n > 0) || arrText[n] === "\n") {
87
- this.context.fillText(line, textPos.x, textPos.y);
88
- line = "";
89
- textPos.y += size;
90
- }
91
- else {
92
- line += arrText[n];
93
- }
94
- }
95
- this.context.fillText(line, textPos.x, textPos.y);
96
- textPos.y += size;
97
- });
98
- this.texture.needsUpdate = true;
99
- this.dispatchEvent({ type: "loaded" });
100
- }
101
- //鼠标拾取计算
102
- raycast(raycaster, intersects) {
103
- let intersect = [];
104
- super.raycast(raycaster, intersect);
105
- let inte = intersect[0];
106
- if (inte && (inte.uv.x > 0.22 && inte.uv.x < 0.78) && (inte.uv.y > 0.54 && inte.uv.y < 0.94)) {
107
- intersects.push(inte);
108
- }
109
- }
110
- }
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-03-08 00:01:34
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-03-08 02:05:25
6
+ */
7
+ import { Sprite, Vector2 } from "three";
8
+ import spriteModal1PNG from "./spriteModal1.png";
9
+ import threeLoader from "../threeLoader";
10
+ // sprit弹窗1
11
+ export default class SpritModal1 extends Sprite {
12
+ constructor(config = {}) {
13
+ super();
14
+ this.updata(config);
15
+ this.material.map = this.texture;
16
+ this.material.transparent = true;
17
+ this.material.alphaTest = 0.01;
18
+ }
19
+ // 背景图
20
+ static img = (() => {
21
+ const img = new Image();
22
+ img.src = spriteModal1PNG;
23
+ return img;
24
+ })();
25
+ canvas = (() => {
26
+ const canvas = document.createElement("canvas");
27
+ canvas.width = 1024;
28
+ canvas.height = 1024;
29
+ return canvas;
30
+ })();
31
+ context = this.canvas.getContext("2d");
32
+ texture = threeLoader.getTexture(this.canvas);
33
+ // 配置
34
+ _config = {
35
+ size: 10,
36
+ title: "SpritModal1",
37
+ titleColor: "#ffffff",
38
+ texts: []
39
+ // [
40
+ // {
41
+ // text: "1",
42
+ // color: "#ffff00",
43
+ // size: 40
44
+ // },
45
+ // {
46
+ // text: "2",
47
+ // color: "#ff00ff",
48
+ // size: 40
49
+ // },
50
+ // ]
51
+ };
52
+ async updata(config) {
53
+ Object.assign(this._config, config);
54
+ //绘制背景
55
+ this.context.clearRect(0, 0, 1024, 1024);
56
+ while (!SpritModal1.img.complete) {
57
+ await new Promise(resolve => setTimeout(resolve, 100));
58
+ }
59
+ // 设置大小
60
+ this.scale.set(this._config.size, this._config.size, this._config.size);
61
+ // this.context.fillStyle = "#00000088";
62
+ // this.context.fillRect(0, 0, 1024, 1024);
63
+ //绘制背景
64
+ this.context.drawImage(SpritModal1.img, 186, 0);
65
+ // 绘制标题
66
+ this.context.fillStyle = this._config.titleColor;
67
+ this.context.font = '40px serif'; // 设置字体大小
68
+ this.context.textAlign = "left";
69
+ this.context.textBaseline = "top";
70
+ this.context.fillText(this._config.title, 250, 72);
71
+ let textPos = new Vector2(250, 150);
72
+ let maxWidth = 530;
73
+ // 绘制内容
74
+ this._config.texts.map(({ text = "text", color = "#ffffff", size = 40 }) => {
75
+ this.context.fillStyle = color;
76
+ this.context.font = `${size}px serif`; // 设置字体大小
77
+ this.context.textAlign = "left";
78
+ this.context.textBaseline = "top";
79
+ // 字符分隔为数组
80
+ var arrText = text.split('');
81
+ var line = '';
82
+ // 文字自动换行计算
83
+ for (var n = 0; n < arrText.length; n++) {
84
+ var metrics = this.context.measureText(line + arrText[n]);
85
+ var testWidth = metrics.width;
86
+ if ((testWidth > maxWidth && n > 0) || arrText[n] === "\n") {
87
+ this.context.fillText(line, textPos.x, textPos.y);
88
+ line = "";
89
+ textPos.y += size;
90
+ }
91
+ else {
92
+ line += arrText[n];
93
+ }
94
+ }
95
+ this.context.fillText(line, textPos.x, textPos.y);
96
+ textPos.y += size;
97
+ });
98
+ this.texture.needsUpdate = true;
99
+ this.dispatchEvent({ type: "loaded" });
100
+ }
101
+ //鼠标拾取计算
102
+ raycast(raycaster, intersects) {
103
+ let intersect = [];
104
+ super.raycast(raycaster, intersect);
105
+ let inte = intersect[0];
106
+ if (inte && (inte.uv.x > 0.22 && inte.uv.x < 0.78) && (inte.uv.y > 0.54 && inte.uv.y < 0.94)) {
107
+ intersects.push(inte);
108
+ }
109
+ }
110
+ }
@@ -1 +1 @@
1
- export { default } from "./SpriteModal1";
1
+ export { default } from "./SpriteModal1";
@@ -1 +1 @@
1
- export { default } from "./SpriteModal1";
1
+ export { default } from "./SpriteModal1";
package/SpriteText.d.ts CHANGED
@@ -1,45 +1,45 @@
1
- import { Sprite } from "three";
2
- export default class SpriteText extends Sprite {
3
- constructor(text?: string, textHeight?: number, color?: `#${string}` | `rgba(${string})`);
4
- _text: string;
5
- _textHeight: number;
6
- _color: `#${string}` | `rgba(${string})`;
7
- _backgroundColor: boolean | `#${string}` | `rgba(${string})`;
8
- _padding: number;
9
- _borderWidth: number;
10
- _borderRadius: number;
11
- _borderColor: `#${string}` | `rgba(${string})`;
12
- _strokeWidth: number;
13
- _strokeColor: `#${string}` | `rgba(${string})`;
14
- _fontFace: string;
15
- _fontSize: number;
16
- _fontWeight: string;
17
- _canvas: HTMLCanvasElement;
18
- get text(): string;
19
- set text(text: string);
20
- get textHeight(): number;
21
- set textHeight(textHeight: number);
22
- get color(): `#${string}` | `rgba(${string})`;
23
- set color(color: `#${string}` | `rgba(${string})`);
24
- get backgroundColor(): boolean | `#${string}` | `rgba(${string})`;
25
- set backgroundColor(color: boolean | `#${string}` | `rgba(${string})`);
26
- get padding(): number;
27
- set padding(padding: number);
28
- get borderWidth(): number;
29
- set borderWidth(borderWidth: number);
30
- get borderRadius(): number;
31
- set borderRadius(borderRadius: number);
32
- get borderColor(): `#${string}` | `rgba(${string})`;
33
- set borderColor(borderColor: `#${string}` | `rgba(${string})`);
34
- get fontFace(): string;
35
- set fontFace(fontFace: string);
36
- get fontSize(): number;
37
- set fontSize(fontSize: number);
38
- get fontWeight(): string;
39
- set fontWeight(fontWeight: string);
40
- get strokeWidth(): number;
41
- set strokeWidth(strokeWidth: number);
42
- get strokeColor(): `#${string}` | `rgba(${string})`;
43
- set strokeColor(strokeColor: `#${string}` | `rgba(${string})`);
44
- _genCanvas(): void;
45
- }
1
+ import { Sprite } from "three";
2
+ export default class SpriteText extends Sprite {
3
+ constructor(text?: string, textHeight?: number, color?: `#${string}` | `rgba(${string})`);
4
+ _text: string;
5
+ _textHeight: number;
6
+ _color: `#${string}` | `rgba(${string})`;
7
+ _backgroundColor: boolean | `#${string}` | `rgba(${string})`;
8
+ _padding: number;
9
+ _borderWidth: number;
10
+ _borderRadius: number;
11
+ _borderColor: `#${string}` | `rgba(${string})`;
12
+ _strokeWidth: number;
13
+ _strokeColor: `#${string}` | `rgba(${string})`;
14
+ _fontFace: string;
15
+ _fontSize: number;
16
+ _fontWeight: string;
17
+ _canvas: HTMLCanvasElement;
18
+ get text(): string;
19
+ set text(text: string);
20
+ get textHeight(): number;
21
+ set textHeight(textHeight: number);
22
+ get color(): `#${string}` | `rgba(${string})`;
23
+ set color(color: `#${string}` | `rgba(${string})`);
24
+ get backgroundColor(): boolean | `#${string}` | `rgba(${string})`;
25
+ set backgroundColor(color: boolean | `#${string}` | `rgba(${string})`);
26
+ get padding(): number;
27
+ set padding(padding: number);
28
+ get borderWidth(): number;
29
+ set borderWidth(borderWidth: number);
30
+ get borderRadius(): number;
31
+ set borderRadius(borderRadius: number);
32
+ get borderColor(): `#${string}` | `rgba(${string})`;
33
+ set borderColor(borderColor: `#${string}` | `rgba(${string})`);
34
+ get fontFace(): string;
35
+ set fontFace(fontFace: string);
36
+ get fontSize(): number;
37
+ set fontSize(fontSize: number);
38
+ get fontWeight(): string;
39
+ set fontWeight(fontWeight: string);
40
+ get strokeWidth(): number;
41
+ set strokeWidth(strokeWidth: number);
42
+ get strokeColor(): `#${string}` | `rgba(${string})`;
43
+ set strokeColor(strokeColor: `#${string}` | `rgba(${string})`);
44
+ _genCanvas(): void;
45
+ }
package/SpriteText.js CHANGED
@@ -1,163 +1,163 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-05-03 02:42:07
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-05-03 02:53:00
6
- */
7
- import { Sprite, SpriteMaterial, Texture, LinearFilter } from "three";
8
- export default class SpriteText extends Sprite {
9
- constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
10
- super(new SpriteMaterial());
11
- this.material.alphaTest = 0.1;
12
- this._text = `${text}`;
13
- this._textHeight = textHeight;
14
- this._color = color;
15
- this._genCanvas();
16
- }
17
- _text = "";
18
- _textHeight = 10;
19
- _color = "#fffff";
20
- _backgroundColor = false; // no background color
21
- _padding = 0;
22
- _borderWidth = 0;
23
- _borderRadius = 0;
24
- _borderColor = "#fffff";
25
- _strokeWidth = 0;
26
- _strokeColor = "#fffff";
27
- _fontFace = 'Arial';
28
- _fontSize = 90; // defines text resolution
29
- _fontWeight = 'normal';
30
- _canvas = document.createElement('canvas');
31
- get text() { return this._text; }
32
- set text(text) { this._text = text; this._genCanvas(); }
33
- get textHeight() { return this._textHeight; }
34
- set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
35
- get color() { return this._color; }
36
- set color(color) { this._color = color; this._genCanvas(); }
37
- get backgroundColor() { return this._backgroundColor; }
38
- set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
39
- get padding() { return this._padding; }
40
- set padding(padding) { this._padding = padding; this._genCanvas(); }
41
- get borderWidth() { return this._borderWidth; }
42
- set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
43
- get borderRadius() { return this._borderRadius; }
44
- set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
45
- get borderColor() { return this._borderColor; }
46
- set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
47
- get fontFace() { return this._fontFace; }
48
- set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
49
- get fontSize() { return this._fontSize; }
50
- set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
51
- get fontWeight() { return this._fontWeight; }
52
- set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
53
- get strokeWidth() { return this._strokeWidth; }
54
- set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
55
- get strokeColor() { return this._strokeColor; }
56
- set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
57
- _genCanvas() {
58
- const canvas = this._canvas;
59
- const ctx = canvas.getContext('2d');
60
- const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
61
- const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
62
- const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
63
- const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
64
- const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
65
- const relPadding = padding.map(p => p * this.fontSize * 0.1);
66
- ; // padding in canvas units
67
- const lines = this.text.split('\n');
68
- const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
69
- ctx.font = font; // measure canvas with appropriate font
70
- const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
71
- const innerHeight = this.fontSize * lines.length;
72
- canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
73
- canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
74
- // paint border
75
- if (this.borderWidth) {
76
- ctx.strokeStyle = this.borderColor;
77
- if (relBorder[0]) { // left + right borders
78
- const hb = relBorder[0] / 2;
79
- ctx.lineWidth = relBorder[0];
80
- ctx.beginPath();
81
- ctx.moveTo(hb, relBorderRadius[0]);
82
- ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
83
- ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
84
- ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
85
- ctx.stroke();
86
- }
87
- if (relBorder[1]) { // top + bottom borders
88
- const hb = relBorder[1] / 2;
89
- ctx.lineWidth = relBorder[1];
90
- ctx.beginPath();
91
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
92
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
93
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
94
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
95
- ctx.stroke();
96
- }
97
- if (this.borderRadius) { // strike rounded corners
98
- const cornerWidth = Math.max(...relBorder);
99
- const hb = cornerWidth / 2;
100
- ctx.lineWidth = cornerWidth;
101
- ctx.beginPath();
102
- [
103
- !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
104
- !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
105
- !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
106
- !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
107
- ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
108
- ctx.moveTo(x0, y0);
109
- ctx.quadraticCurveTo(x1, y0, x1, y1);
110
- });
111
- ctx.stroke();
112
- }
113
- }
114
- // paint background
115
- if (this.backgroundColor) {
116
- //@ts-ignore
117
- ctx.fillStyle = this.backgroundColor;
118
- if (!this.borderRadius) {
119
- ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
120
- }
121
- else { // fill with rounded corners
122
- ctx.beginPath();
123
- ctx.moveTo(relBorder[0], relBorderRadius[0]);
124
- [
125
- [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]], // t
126
- [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]], // r
127
- [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]], // b
128
- [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
129
- ].forEach(([x0, x1, x2, y0, y1, y2]) => {
130
- ctx.quadraticCurveTo(x0, y0, x1, y1);
131
- ctx.lineTo(x2, y2);
132
- });
133
- ctx.closePath();
134
- ctx.fill();
135
- }
136
- }
137
- ctx.translate(...relBorder);
138
- ctx.translate(...relPadding);
139
- // paint text
140
- ctx.font = font; // Set font again after canvas is resized, as context properties are reset
141
- ctx.fillStyle = this.color;
142
- ctx.textBaseline = 'bottom';
143
- const drawTextStroke = this.strokeWidth > 0;
144
- if (drawTextStroke) {
145
- ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
146
- ctx.strokeStyle = this.strokeColor;
147
- }
148
- lines.forEach((line, index) => {
149
- const lineX = (innerWidth - ctx.measureText(line).width) / 2;
150
- const lineY = (index + 1) * this.fontSize;
151
- drawTextStroke && ctx.strokeText(line, lineX, lineY);
152
- ctx.fillText(line, lineX, lineY);
153
- });
154
- // Inject canvas into sprite
155
- if (this.material.map)
156
- this.material.map.dispose(); // gc previous texture
157
- const texture = this.material.map = new Texture(canvas);
158
- texture.minFilter = LinearFilter;
159
- texture.needsUpdate = true;
160
- const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
161
- this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
162
- }
163
- }
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-05-03 02:42:07
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-05-03 02:53:00
6
+ */
7
+ import { Sprite, SpriteMaterial, Texture, LinearFilter } from "three";
8
+ export default class SpriteText extends Sprite {
9
+ constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
10
+ super(new SpriteMaterial());
11
+ this.material.alphaTest = 0.1;
12
+ this._text = `${text}`;
13
+ this._textHeight = textHeight;
14
+ this._color = color;
15
+ this._genCanvas();
16
+ }
17
+ _text = "";
18
+ _textHeight = 10;
19
+ _color = "#fffff";
20
+ _backgroundColor = false; // no background color
21
+ _padding = 0;
22
+ _borderWidth = 0;
23
+ _borderRadius = 0;
24
+ _borderColor = "#fffff";
25
+ _strokeWidth = 0;
26
+ _strokeColor = "#fffff";
27
+ _fontFace = 'Arial';
28
+ _fontSize = 90; // defines text resolution
29
+ _fontWeight = 'normal';
30
+ _canvas = document.createElement('canvas');
31
+ get text() { return this._text; }
32
+ set text(text) { this._text = text; this._genCanvas(); }
33
+ get textHeight() { return this._textHeight; }
34
+ set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
35
+ get color() { return this._color; }
36
+ set color(color) { this._color = color; this._genCanvas(); }
37
+ get backgroundColor() { return this._backgroundColor; }
38
+ set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
39
+ get padding() { return this._padding; }
40
+ set padding(padding) { this._padding = padding; this._genCanvas(); }
41
+ get borderWidth() { return this._borderWidth; }
42
+ set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
43
+ get borderRadius() { return this._borderRadius; }
44
+ set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
45
+ get borderColor() { return this._borderColor; }
46
+ set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
47
+ get fontFace() { return this._fontFace; }
48
+ set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
49
+ get fontSize() { return this._fontSize; }
50
+ set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
51
+ get fontWeight() { return this._fontWeight; }
52
+ set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
53
+ get strokeWidth() { return this._strokeWidth; }
54
+ set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
55
+ get strokeColor() { return this._strokeColor; }
56
+ set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
57
+ _genCanvas() {
58
+ const canvas = this._canvas;
59
+ const ctx = canvas.getContext('2d');
60
+ const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
61
+ const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
62
+ const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
63
+ const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
64
+ const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
65
+ const relPadding = padding.map(p => p * this.fontSize * 0.1);
66
+ ; // padding in canvas units
67
+ const lines = this.text.split('\n');
68
+ const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
69
+ ctx.font = font; // measure canvas with appropriate font
70
+ const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
71
+ const innerHeight = this.fontSize * lines.length;
72
+ canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
73
+ canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
74
+ // paint border
75
+ if (this.borderWidth) {
76
+ ctx.strokeStyle = this.borderColor;
77
+ if (relBorder[0]) { // left + right borders
78
+ const hb = relBorder[0] / 2;
79
+ ctx.lineWidth = relBorder[0];
80
+ ctx.beginPath();
81
+ ctx.moveTo(hb, relBorderRadius[0]);
82
+ ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
83
+ ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
84
+ ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
85
+ ctx.stroke();
86
+ }
87
+ if (relBorder[1]) { // top + bottom borders
88
+ const hb = relBorder[1] / 2;
89
+ ctx.lineWidth = relBorder[1];
90
+ ctx.beginPath();
91
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
92
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
93
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
94
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
95
+ ctx.stroke();
96
+ }
97
+ if (this.borderRadius) { // strike rounded corners
98
+ const cornerWidth = Math.max(...relBorder);
99
+ const hb = cornerWidth / 2;
100
+ ctx.lineWidth = cornerWidth;
101
+ ctx.beginPath();
102
+ [
103
+ !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
104
+ !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
105
+ !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
106
+ !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
107
+ ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
108
+ ctx.moveTo(x0, y0);
109
+ ctx.quadraticCurveTo(x1, y0, x1, y1);
110
+ });
111
+ ctx.stroke();
112
+ }
113
+ }
114
+ // paint background
115
+ if (this.backgroundColor) {
116
+ //@ts-ignore
117
+ ctx.fillStyle = this.backgroundColor;
118
+ if (!this.borderRadius) {
119
+ ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
120
+ }
121
+ else { // fill with rounded corners
122
+ ctx.beginPath();
123
+ ctx.moveTo(relBorder[0], relBorderRadius[0]);
124
+ [
125
+ [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]],
126
+ [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]],
127
+ [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]],
128
+ [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
129
+ ].forEach(([x0, x1, x2, y0, y1, y2]) => {
130
+ ctx.quadraticCurveTo(x0, y0, x1, y1);
131
+ ctx.lineTo(x2, y2);
132
+ });
133
+ ctx.closePath();
134
+ ctx.fill();
135
+ }
136
+ }
137
+ ctx.translate(...relBorder);
138
+ ctx.translate(...relPadding);
139
+ // paint text
140
+ ctx.font = font; // Set font again after canvas is resized, as context properties are reset
141
+ ctx.fillStyle = this.color;
142
+ ctx.textBaseline = 'bottom';
143
+ const drawTextStroke = this.strokeWidth > 0;
144
+ if (drawTextStroke) {
145
+ ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
146
+ ctx.strokeStyle = this.strokeColor;
147
+ }
148
+ lines.forEach((line, index) => {
149
+ const lineX = (innerWidth - ctx.measureText(line).width) / 2;
150
+ const lineY = (index + 1) * this.fontSize;
151
+ drawTextStroke && ctx.strokeText(line, lineX, lineY);
152
+ ctx.fillText(line, lineX, lineY);
153
+ });
154
+ // Inject canvas into sprite
155
+ if (this.material.map)
156
+ this.material.map.dispose(); // gc previous texture
157
+ const texture = this.material.map = new Texture(canvas);
158
+ texture.minFilter = LinearFilter;
159
+ texture.needsUpdate = true;
160
+ const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
161
+ this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
162
+ }
163
+ }