three-base 2.0.87 → 2.0.88

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.
@@ -1,160 +0,0 @@
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
-
8
-
9
-
10
-
11
-
12
- import { Sprite, CanvasTexture, Vector2, Raycaster, Intersection } from "three";
13
- import spriteModal1PNG from "./spriteModal1.png";
14
- import threeLoader from "../threeLoader";
15
-
16
- export type SpritModal1Config = {
17
- size?: number,
18
- title?: string,
19
- titleColor?: string,
20
- texts?: Array<{
21
- text?: string,
22
- color?: string,
23
- size?: number
24
- }>
25
- }
26
-
27
- // sprit弹窗1
28
- export default class SpritModal1 extends Sprite {
29
-
30
- constructor(config: SpritModal1Config = {}) {
31
- super();
32
- this.updata(config);
33
- this.material.map = this.texture;
34
- this.material.transparent = true;
35
- this.material.alphaTest = 0.01;
36
- }
37
-
38
- // 背景图
39
- static img = (() => {
40
- const img = new Image();
41
- img.src = spriteModal1PNG;
42
- return img;
43
- })();
44
-
45
- canvas = (() => {
46
- const canvas = document.createElement("canvas");
47
- canvas.width = 1024;
48
- canvas.height = 1024;
49
- return canvas;
50
- })();
51
-
52
- context = this.canvas.getContext("2d")!;
53
-
54
- texture = threeLoader.getTexture(this.canvas);
55
-
56
- // 配置
57
- _config: {
58
- size: number,
59
- title: string,
60
- titleColor: string,
61
- texts: Array<{
62
- text: string,
63
- color: string,
64
- size: number
65
- }>
66
- } = {
67
- size: 10,
68
- title: "SpritModal1",
69
- titleColor: "#ffffff",
70
- texts: []
71
- // [
72
- // {
73
- // text: "1",
74
- // color: "#ffff00",
75
- // size: 40
76
- // },
77
- // {
78
- // text: "2",
79
- // color: "#ff00ff",
80
- // size: 40
81
- // },
82
- // ]
83
- }
84
-
85
- async updata(config: SpritModal1Config) {
86
-
87
- Object.assign(this._config, config)
88
-
89
- //绘制背景
90
- this.context.clearRect(0, 0, 1024, 1024);
91
-
92
- while (!SpritModal1.img.complete) {
93
- await new Promise(resolve => setTimeout(resolve, 100));
94
- }
95
-
96
- // 设置大小
97
- this.scale.set(this._config.size, this._config.size, this._config.size);
98
-
99
- // this.context.fillStyle = "#00000088";
100
- // this.context.fillRect(0, 0, 1024, 1024);
101
-
102
- //绘制背景
103
- this.context.drawImage(SpritModal1.img, 186, 0);
104
-
105
- // 绘制标题
106
- this.context.fillStyle = this._config.titleColor;
107
- this.context.font = '40px serif';// 设置字体大小
108
- this.context.textAlign = "left";
109
- this.context.textBaseline = "top";
110
- this.context.fillText(this._config.title, 250, 72);
111
-
112
- let textPos = new Vector2(250, 150);
113
- let maxWidth = 530;
114
- // 绘制内容
115
- this._config.texts.map(({ text = "text", color = "#ffffff", size = 40 }) => {
116
- this.context.fillStyle = color;
117
- this.context.font = `${size}px serif`;// 设置字体大小
118
- this.context.textAlign = "left";
119
- this.context.textBaseline = "top";
120
-
121
- // 字符分隔为数组
122
- var arrText = text.split('');
123
- var line = '';
124
-
125
- // 文字自动换行计算
126
- for (var n = 0; n < arrText.length; n++) {
127
- var metrics = this.context.measureText(line + arrText[n]);
128
- var testWidth = metrics.width;
129
- if ((testWidth > maxWidth && n > 0) || arrText[n] === "\n") {
130
- this.context.fillText(line, textPos.x, textPos.y);
131
- line = "";
132
- textPos.y += size;
133
- } else {
134
- line += arrText[n];
135
- }
136
- }
137
-
138
- this.context.fillText(line, textPos.x, textPos.y);
139
- textPos.y += size;
140
- });
141
-
142
- this.texture.needsUpdate = true;
143
-
144
- this.dispatchEvent({ type: "loaded" } as never);
145
- }
146
-
147
- //鼠标拾取计算
148
- raycast(raycaster: Raycaster, intersects: Intersection[]) {
149
-
150
- let intersect: Intersection[] = [];
151
- super.raycast(raycaster, intersect);
152
- let inte = intersect[0];
153
- if (inte && (inte.uv!.x > 0.22 && inte.uv!.x < 0.78) && (inte.uv!.y > 0.54 && inte.uv!.y < 0.94)) {
154
- intersects.push(inte);
155
- }
156
-
157
- }
158
-
159
- }
160
-
@@ -1 +0,0 @@
1
- export { default } from "./SpriteModal1";
package/SpriteText.ts DELETED
@@ -1,190 +0,0 @@
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
-
8
- import { Sprite, SpriteMaterial, Texture, LinearFilter } from "three";
9
-
10
- export default class SpriteText extends Sprite {
11
-
12
- constructor(text = '', textHeight = 10, color: `#${string}` | `rgba(${string})` = 'rgba(255, 255, 255, 1)') {
13
- super(new SpriteMaterial());
14
-
15
- this.material.alphaTest = 0.1;
16
- this._text = `${text}`;
17
- this._textHeight = textHeight;
18
- this._color = color;
19
-
20
- this._genCanvas();
21
- }
22
-
23
- _text: string = "";
24
- _textHeight: number = 10;
25
- _color: `#${string}` | `rgba(${string})` = "#fffff";
26
- _backgroundColor: boolean | `#${string}` | `rgba(${string})` = false; // no background color
27
-
28
- _padding: number = 0;
29
- _borderWidth: number = 0;
30
- _borderRadius: number = 0;
31
- _borderColor: `#${string}` | `rgba(${string})` = "#fffff";
32
-
33
- _strokeWidth: number = 0;
34
- _strokeColor: `#${string}` | `rgba(${string})` = "#fffff";
35
-
36
- _fontFace: string = 'Arial';
37
- _fontSize: number = 90; // defines text resolution
38
- _fontWeight: string = 'normal';
39
-
40
- _canvas = document.createElement('canvas');
41
-
42
- get text() { return this._text; }
43
- set text(text) { this._text = text; this._genCanvas(); }
44
- get textHeight() { return this._textHeight; }
45
- set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
46
- get color() { return this._color; }
47
- set color(color) { this._color = color; this._genCanvas(); }
48
- get backgroundColor() { return this._backgroundColor; }
49
- set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
50
- get padding() { return this._padding; }
51
- set padding(padding) { this._padding = padding; this._genCanvas(); }
52
- get borderWidth() { return this._borderWidth; }
53
- set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
54
- get borderRadius() { return this._borderRadius; }
55
- set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
56
- get borderColor() { return this._borderColor; }
57
- set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
58
- get fontFace() { return this._fontFace; }
59
- set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
60
- get fontSize() { return this._fontSize; }
61
- set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
62
- get fontWeight() { return this._fontWeight; }
63
- set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
64
- get strokeWidth() { return this._strokeWidth; }
65
- set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
66
- get strokeColor() { return this._strokeColor; }
67
- set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
68
-
69
- _genCanvas() {
70
- const canvas = this._canvas;
71
- const ctx = canvas.getContext('2d')!;
72
-
73
- const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
74
- const relBorder = border.map(b => b * this.fontSize * 0.1) as [number, number]; // border in canvas units
75
-
76
- const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
77
- const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
78
-
79
- const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
80
- const relPadding = padding.map(p => p * this.fontSize * 0.1) as [number, number];; // padding in canvas units
81
-
82
- const lines = this.text.split('\n');
83
- const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
84
-
85
- ctx.font = font; // measure canvas with appropriate font
86
- const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
87
- const innerHeight = this.fontSize * lines.length;
88
- canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
89
- canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
90
-
91
- // paint border
92
- if (this.borderWidth) {
93
- ctx.strokeStyle = this.borderColor;
94
-
95
- if (relBorder[0]) { // left + right borders
96
- const hb = relBorder[0] / 2;
97
- ctx.lineWidth = relBorder[0];
98
- ctx.beginPath();
99
- ctx.moveTo(hb, relBorderRadius[0]);
100
- ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
101
- ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
102
- ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
103
- ctx.stroke();
104
- }
105
-
106
- if (relBorder[1]) { // top + bottom borders
107
- const hb = relBorder[1] / 2;
108
- ctx.lineWidth = relBorder[1];
109
- ctx.beginPath();
110
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
111
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
112
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
113
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
114
- ctx.stroke();
115
- }
116
-
117
- if (this.borderRadius) { // strike rounded corners
118
- const cornerWidth = Math.max(...relBorder);
119
- const hb = cornerWidth / 2;
120
- ctx.lineWidth = cornerWidth;
121
- ctx.beginPath();
122
- ([
123
- !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
124
- !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
125
- !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
126
- !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
127
- ].filter(d => d) as [number, number, number, number][]).forEach(([x0, x1, y0, y1]) => {
128
- ctx.moveTo(x0, y0);
129
- ctx.quadraticCurveTo(x1, y0, x1, y1);
130
- });
131
- ctx.stroke();
132
- }
133
- }
134
-
135
- // paint background
136
- if (this.backgroundColor) {
137
- //@ts-ignore
138
- ctx.fillStyle = this.backgroundColor;
139
- if (!this.borderRadius) {
140
- ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
141
- } else { // fill with rounded corners
142
- ctx.beginPath();
143
- ctx.moveTo(relBorder[0], relBorderRadius[0]);
144
- [
145
- [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]], // t
146
- [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]], // r
147
- [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]], // b
148
- [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
149
- ].forEach(([x0, x1, x2, y0, y1, y2]) => {
150
- ctx.quadraticCurveTo(x0, y0, x1, y1);
151
- ctx.lineTo(x2, y2);
152
- });
153
- ctx.closePath();
154
- ctx.fill();
155
- }
156
- }
157
-
158
- ctx.translate(...relBorder);
159
- ctx.translate(...relPadding);
160
-
161
- // paint text
162
- ctx.font = font; // Set font again after canvas is resized, as context properties are reset
163
- ctx.fillStyle = this.color;
164
- ctx.textBaseline = 'bottom';
165
-
166
- const drawTextStroke = this.strokeWidth > 0;
167
- if (drawTextStroke) {
168
- ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
169
- ctx.strokeStyle = this.strokeColor;
170
- }
171
-
172
- lines.forEach((line, index) => {
173
- const lineX = (innerWidth - ctx.measureText(line).width) / 2;
174
- const lineY = (index + 1) * this.fontSize;
175
-
176
- drawTextStroke && ctx.strokeText(line, lineX, lineY);
177
- ctx.fillText(line, lineX, lineY);
178
- });
179
-
180
- // Inject canvas into sprite
181
- if (this.material.map) this.material.map.dispose(); // gc previous texture
182
- const texture = this.material.map = new Texture(canvas);
183
- texture.minFilter = LinearFilter;
184
- texture.needsUpdate = true;
185
-
186
- const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
187
- this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
188
- }
189
-
190
- }
package/SpriteText2.ts DELETED
@@ -1,237 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-04-24 15:31:02
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-24 16:18:47
6
- */
7
-
8
-
9
- import { Intersection, LinearFilter, Raycaster, Sprite, SpriteMaterial, Texture } from 'three';
10
-
11
- // 带箭头的精灵文字
12
- export default class SpriteText2 extends Sprite {
13
- constructor(text = '', textHeight = 10, color: `rgb(${string})` | `rgba(${string})` | `#${string}` = 'rgba(255, 255, 255, 1)') {
14
- super();
15
-
16
- this.material.alphaTest = 0.1;
17
- this._text = `${text}`;
18
- this._textHeight = textHeight;
19
- this._color = color;
20
- this._backgroundColor = false; // no background color
21
-
22
- this._padding = 0;
23
- this._borderWidth = 0;
24
- this._borderRadius = 0;
25
- this._borderColor = 'white';
26
-
27
- this._strokeWidth = 0;
28
- this._strokeColor = 'white';
29
-
30
- this._fontFace = 'Arial';
31
- this._fontSize = 90; // defines text resolution
32
- this._fontWeight = 'normal';
33
-
34
- this._genCanvas();
35
- }
36
-
37
- _text = "";
38
- _textHeight = 0;
39
- _color: `rgb(${string})` | `rgba(${string})` | `#${string}` = "#ffffff";
40
- _backgroundColor: `rgb(${string})` | `rgba(${string})` | `#${string}` | false = false; // no background color
41
-
42
- _padding = 0;
43
- _borderWidth = 0;
44
- _borderRadius = 0;
45
- _borderColor = 'white';
46
-
47
- _strokeWidth = 0;
48
- _strokeColor = 'white';
49
-
50
- _fontFace = 'Arial';
51
- _fontSize = 90; // defines text resolution
52
- _fontWeight = 'normal';
53
- _canvas = document.createElement('canvas');
54
-
55
- get text() { return this._text; }
56
- set text(text) { this._text = text; this._genCanvas(); }
57
- get textHeight() { return this._textHeight; }
58
- set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
59
- get color() { return this._color; }
60
- set color(color) { this._color = color; this._genCanvas(); }
61
- get backgroundColor() { return this._backgroundColor; }
62
- set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
63
- get padding() { return this._padding; }
64
- set padding(padding) { this._padding = padding; this._genCanvas(); }
65
- get borderWidth() { return this._borderWidth; }
66
- set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
67
- get borderRadius() { return this._borderRadius; }
68
- set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
69
- get borderColor() { return this._borderColor; }
70
- set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
71
- get fontFace() { return this._fontFace; }
72
- set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
73
- get fontSize() { return this._fontSize; }
74
- set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
75
- get fontWeight() { return this._fontWeight; }
76
- set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
77
- get strokeWidth() { return this._strokeWidth; }
78
- set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
79
- get strokeColor() { return this._strokeColor; }
80
- set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
81
-
82
- _genCanvas() {
83
- const canvas = this._canvas;
84
- const ctx = canvas.getContext('2d')!;
85
-
86
- const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
87
- const relBorder: number[] = border.map(b => b * this.fontSize * 0.1); // border in canvas units
88
-
89
- const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
90
- const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
91
-
92
- const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
93
- const relPadding = padding.map(p => p * this.fontSize * 0.1); // padding in canvas units
94
-
95
- const lines = this.text.split('\n');
96
- const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
97
-
98
- ctx.font = font; // measure canvas with appropriate font
99
- const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
100
- const innerHeight = this.fontSize * lines.length;
101
- canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
102
- canvas.height = (innerHeight + relBorder[1] * 2 + relPadding[1] * 2) * 2 + 40;
103
-
104
- // paint border
105
- if (this.borderWidth) {
106
- ctx.strokeStyle = this.borderColor;
107
-
108
- if (relBorder[0]) { // left + right borders
109
- const hb = relBorder[0] / 2;
110
- ctx.lineWidth = relBorder[0];
111
- ctx.beginPath();
112
- ctx.moveTo(hb, relBorderRadius[0]);
113
- ctx.lineTo(hb, ((canvas.height - 40) / 2) - relBorderRadius[3]);
114
- ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
115
- ctx.lineTo(canvas.width - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]);
116
- ctx.stroke();
117
- }
118
-
119
- if (relBorder[1]) { // top + bottom borders
120
- const hb = relBorder[1] / 2;
121
- ctx.lineWidth = relBorder[1];
122
- ctx.beginPath();
123
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
124
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
125
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), ((canvas.height - 40) / 2) - hb);
126
-
127
- ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - hb);
128
- ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - hb);
129
- ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - hb);
130
-
131
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), ((canvas.height - 40) / 2) - hb);
132
- ctx.stroke();
133
- }
134
-
135
- if (this.borderRadius) { // strike rounded corners
136
- const cornerWidth = Math.max(...relBorder);
137
- const hb = cornerWidth / 2;
138
- ctx.lineWidth = cornerWidth;
139
- ctx.beginPath();
140
- ([
141
- !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
142
- !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
143
- !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]],
144
- !!relBorderRadius[3] && [relBorderRadius[3], hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[3]]
145
- ].filter(d => d) as [number, number, number, number][]).forEach(([x0, x1, y0, y1]) => {
146
- ctx.moveTo(x0, y0);
147
- ctx.quadraticCurveTo(x1, y0, x1, y1);
148
- });
149
- ctx.stroke();
150
- }
151
- }
152
-
153
- // paint background
154
- if (this.backgroundColor) {
155
- ctx.fillStyle = this.backgroundColor;
156
- if (!this.borderRadius) {
157
- ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, ((canvas.height - 40) / 2) - relBorder[1] * 2);
158
- } else { // fill with rounded corners
159
- ctx.beginPath();
160
- ctx.moveTo(relBorder[0], relBorderRadius[0]);
161
-
162
- ctx.quadraticCurveTo(relBorder[0], relBorder[1], relBorderRadius[0], relBorder[1]);
163
- ctx.lineTo(canvas.width - relBorderRadius[1], relBorder[1]);
164
- ctx.quadraticCurveTo(canvas.width - relBorder[0], relBorder[1], canvas.width - relBorder[0], relBorderRadius[1]);
165
- ctx.lineTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[2]);
166
- ctx.quadraticCurveTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], canvas.width - relBorderRadius[2], ((canvas.height - 40) / 2) - relBorder[1]);
167
-
168
- ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - relBorder[1]);
169
- ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - relBorder[1]);
170
- ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - relBorder[1]);
171
-
172
- ctx.lineTo(relBorderRadius[3], ((canvas.height - 40) / 2) - relBorder[1]);
173
- ctx.quadraticCurveTo(relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[3]);
174
-
175
- ctx.closePath();
176
- ctx.fill();
177
- }
178
- }
179
-
180
- ctx.translate(relBorder[0], relBorder[1]);
181
- ctx.translate(relPadding[0], relPadding[0]);
182
-
183
- // paint text
184
- ctx.font = font; // Set font again after canvas is resized, as context properties are reset
185
- ctx.fillStyle = this.color;
186
- ctx.textBaseline = 'bottom';
187
-
188
- const drawTextStroke = this.strokeWidth > 0;
189
- if (drawTextStroke) {
190
- ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
191
- ctx.strokeStyle = this.strokeColor;
192
- }
193
-
194
- lines.forEach((line, index) => {
195
- const lineX = (innerWidth - ctx.measureText(line).width) / 2;
196
- const lineY = (index + 1) * this.fontSize;
197
-
198
- drawTextStroke && ctx.strokeText(line, lineX, lineY);
199
- ctx.fillText(line, lineX, lineY);
200
- });
201
-
202
- // Inject canvas into sprite
203
- if (this.material.map) this.material.map.dispose(); // gc previous texture
204
- const texture = this.material.map = new Texture(canvas);
205
- texture.minFilter = LinearFilter;
206
- texture.needsUpdate = true;
207
-
208
- const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
209
- this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
210
- }
211
-
212
- //鼠标拾取计算
213
- raycast(raycaster: Raycaster, intersects: Intersection[]) {
214
-
215
- let intersect: Intersection[] = [];
216
- super.raycast(raycaster, intersect);
217
- let inte = intersect[0];
218
- if (inte && (inte.uv!.y > 0.55)) {
219
- intersects.push(inte);
220
- }
221
-
222
- }
223
-
224
- copy(source: SpriteText2) {
225
- this.color = source.color;
226
- this.backgroundColor = source.backgroundColor;
227
- this.padding = source.padding;
228
- this.borderWidth = source.borderWidth;
229
- this.borderColor = source.borderColor;
230
- this.fontFace = source.fontFace;
231
- this.fontSize = source.fontSize;
232
- this.fontWeight = source.fontWeight;
233
- this.strokeWidth = source.strokeWidth;
234
- this.strokeColor = source.strokeColor;
235
- return this;
236
- }
237
- }
package/SpriteWithHtml.ts DELETED
@@ -1,62 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-04-15 11:26:32
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-15 12:38:09
6
- */
7
-
8
- import html2canvas from "html2canvas";
9
- import { CanvasTexture, Sprite } from "three";
10
- import threeLoader from "./threeLoader";
11
-
12
-
13
- // html 文本面板
14
- export default class SpriteWithHtml extends Sprite {
15
-
16
- constructor(html: string = "", backgroundColor: `#${string}` = "#00000000") {
17
- super();
18
- this.setHtml(html, backgroundColor);
19
- }
20
-
21
- visible = false;
22
-
23
- div = (() => {
24
- const div = document.createElement("div");
25
- div.style.position = "fixed";
26
- div.style.top = "100%";
27
- div.style.left = "100%";
28
- div.style.pointerEvents = "none";
29
- div.style.userSelect = "none";
30
- div.style.lineHeight = "1";
31
- div.style.whiteSpace = "nowrap";
32
- return div;
33
- })();
34
-
35
- async setHtml(html: string = "", backgroundColor: `#${string}` = "#00000000") {
36
- this.div.innerHTML = html;
37
- document.body.appendChild(this.div);
38
-
39
- await new Promise<void>((resolve, reject) => {
40
-
41
- html2canvas(this.div, {
42
- backgroundColor,
43
- removeContainer: true
44
- }).then(async (canvas) => {
45
- this.scale.set(canvas.width, canvas.height, 1);
46
- this.material.map = new CanvasTexture(canvas);
47
- this.material.map.anisotropy = threeLoader.anisotropy;
48
- this.material.alphaTest = 0.1;
49
- this.material.needsUpdate = true;
50
- await new Promise(resolve => requestAnimationFrame(resolve));
51
- this.visible = true;
52
- this.div.remove();
53
- resolve();
54
- });
55
-
56
- });
57
-
58
- this.dispatchEvent({ type: "loaded" } as never);
59
- }
60
-
61
-
62
- }