three-base 1.0.81 → 1.0.83
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/index.js +1 -1
- package/index.ts +1 -1
- package/lib/PlaneText.d.ts +2 -2
- package/lib/PlaneText.js +2 -2
- package/lib/PlaneText.ts +2 -3
- package/lib/PointLight/PointLight.d.ts +2 -2
- package/lib/PointLight/PointLight.js +3 -3
- package/lib/PointLight/PointLight.ts +3 -3
- package/lib/SpriteText.d.ts +45 -0
- package/lib/SpriteText.js +162 -0
- package/lib/SpriteText.ts +189 -0
- package/lib/SpriteText2.d.ts +4 -4
- package/lib/threeUtils.js +2 -1
- package/lib/threeUtils.ts +2 -1
- package/package.json +2 -4
package/index.js
CHANGED
|
@@ -25,7 +25,7 @@ export { default as FPScontrol } from "./lib/FPScontrol";
|
|
|
25
25
|
// 平面文字
|
|
26
26
|
export { default as PlaneText } from "./lib/PlaneText";
|
|
27
27
|
// 精灵文字
|
|
28
|
-
export { default as SpriteText } from "
|
|
28
|
+
export { default as SpriteText } from "./lib/SpriteText";
|
|
29
29
|
// 精灵文字样式2
|
|
30
30
|
export { default as SpriteText2 } from "./lib/SpriteText2";
|
|
31
31
|
//精灵文本框
|
package/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ export { default as FPScontrol } from "./lib/FPScontrol";
|
|
|
43
43
|
export { default as PlaneText } from "./lib/PlaneText";
|
|
44
44
|
|
|
45
45
|
// 精灵文字
|
|
46
|
-
export { default as SpriteText } from "
|
|
46
|
+
export { default as SpriteText } from "./lib/SpriteText";
|
|
47
47
|
|
|
48
48
|
// 精灵文字样式2
|
|
49
49
|
export { default as SpriteText2 } from "./lib/SpriteText2";
|
package/lib/PlaneText.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from "three";
|
|
2
|
-
import SpriteText from "
|
|
2
|
+
import SpriteText from "./SpriteText";
|
|
3
3
|
type Props = {
|
|
4
4
|
text?: string;
|
|
5
5
|
textHeight?: number;
|
|
@@ -32,6 +32,6 @@ export default class PlaneText extends Mesh {
|
|
|
32
32
|
materials: any;
|
|
33
33
|
textures: any;
|
|
34
34
|
images: any;
|
|
35
|
-
}) => any) | ((event: import("three").Event) => void) | import("three").Vector2 | import("three").SpriteMaterial | ((raycaster: import("three").Raycaster, intersects: import("three").Intersection<import("three").Object3D<import("three").Event>>[]) => void) | ((quaternion: import("three").Quaternion) => SpriteText) | ((axis: Vector3, angle: number) => SpriteText) | ((axis: Vector3, angle: number) => SpriteText) | ((angle: number) => SpriteText) | ((angle: number) => SpriteText) | ((angle: number) => SpriteText) | ((axis: Vector3, distance: number) => SpriteText) | ((distance: number) => SpriteText) | ((distance: number) => SpriteText) | ((distance: number) => SpriteText) | ((...object: import("three").Object3D<import("three").Event>[]) => SpriteText) | ((...object: import("three").Object3D<import("three").Event>[]) => SpriteText) | (() => SpriteText) | (() => SpriteText) | ((object: import("three").Object3D<import("three").Event>) => SpriteText) | ((recursive?: boolean) => SpriteText) | ((source: SpriteText) => SpriteText) | (<T extends string>(type: T, listener: import("three").EventListener<import("three").Event, T, SpriteText>) => void) | (<T_1 extends string>(type: T_1, listener: import("three").EventListener<import("three").Event, T_1, SpriteText>) => boolean) | (<T_2 extends string>(type: T_2, listener: import("three").EventListener<import("three").Event, T_2, SpriteText>) => void);
|
|
35
|
+
}) => any) | ((event: import("three").Event) => void) | import("three").Vector2 | HTMLCanvasElement | import("three").SpriteMaterial | ((raycaster: import("three").Raycaster, intersects: import("three").Intersection<import("three").Object3D<import("three").Event>>[]) => void) | ((quaternion: import("three").Quaternion) => SpriteText) | ((axis: Vector3, angle: number) => SpriteText) | ((axis: Vector3, angle: number) => SpriteText) | ((angle: number) => SpriteText) | ((angle: number) => SpriteText) | ((angle: number) => SpriteText) | ((axis: Vector3, distance: number) => SpriteText) | ((distance: number) => SpriteText) | ((distance: number) => SpriteText) | ((distance: number) => SpriteText) | ((...object: import("three").Object3D<import("three").Event>[]) => SpriteText) | ((...object: import("three").Object3D<import("three").Event>[]) => SpriteText) | (() => SpriteText) | (() => SpriteText) | ((object: import("three").Object3D<import("three").Event>) => SpriteText) | ((recursive?: boolean) => SpriteText) | ((source: SpriteText) => SpriteText) | (<T extends string>(type: T, listener: import("three").EventListener<import("three").Event, T, SpriteText>) => void) | (<T_1 extends string>(type: T_1, listener: import("three").EventListener<import("three").Event, T_1, SpriteText>) => boolean) | (<T_2 extends string>(type: T_2, listener: import("three").EventListener<import("three").Event, T_2, SpriteText>) => void) | (() => void);
|
|
36
36
|
}
|
|
37
37
|
export {};
|
package/lib/PlaneText.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-05-23 13:53:06
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2023-05-03 02:54:52
|
|
6
6
|
*/
|
|
7
7
|
import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from "three";
|
|
8
|
-
import SpriteText from "
|
|
8
|
+
import SpriteText from "./SpriteText";
|
|
9
9
|
export default class PlaneText extends Mesh {
|
|
10
10
|
constructor(text = "planeText") {
|
|
11
11
|
super(PlaneText.geometry);
|
package/lib/PlaneText.ts
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-05-23 13:53:06
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2023-05-03 02:54:52
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from "three";
|
|
9
|
-
import SpriteText from "
|
|
10
|
-
|
|
9
|
+
import SpriteText from "./SpriteText";
|
|
11
10
|
|
|
12
11
|
|
|
13
12
|
type Props = {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Sprite, PointLight as THREEPointLight } from "three";
|
|
2
|
-
import
|
|
2
|
+
import SpriteText from "../SpriteText";
|
|
3
3
|
/**
|
|
4
4
|
* 点光源
|
|
5
5
|
* @export
|
|
@@ -9,5 +9,5 @@ import threeSpritetext from "three-spritetext";
|
|
|
9
9
|
export default class PointLight extends THREEPointLight {
|
|
10
10
|
constructor(color?: string, intensity?: number, distance?: number, decay?: number);
|
|
11
11
|
pointLinghtHelper: Sprite;
|
|
12
|
-
label:
|
|
12
|
+
label: SpriteText;
|
|
13
13
|
}
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-05-18 16:02:55
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2023-
|
|
5
|
+
* @Last Modified time: 2023-05-03 02:55:28
|
|
6
6
|
*/
|
|
7
7
|
import { Sprite, PointLight as THREEPointLight } from "three";
|
|
8
|
-
import threeSpritetext from "three-spritetext";
|
|
9
8
|
import threeLoader from "../threeLoader";
|
|
10
9
|
import threeUtils from "../threeUtils";
|
|
11
10
|
import pointLight_mapPNG from "./pointLight_map.png";
|
|
11
|
+
import SpriteText from "../SpriteText";
|
|
12
12
|
/**
|
|
13
13
|
* 点光源
|
|
14
14
|
* @export
|
|
@@ -43,7 +43,7 @@ export default class PointLight extends THREEPointLight {
|
|
|
43
43
|
return pointLinghtHelper;
|
|
44
44
|
})();
|
|
45
45
|
label = threeUtils.isDev && (() => {
|
|
46
|
-
const label = new
|
|
46
|
+
const label = new SpriteText("点光源\n\n\n");
|
|
47
47
|
label.renderOrder = Infinity;
|
|
48
48
|
// label.backgroundColor = "rgba(0, 0, 0, 0.5)";
|
|
49
49
|
label.padding = 4;
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-05-18 16:02:55
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2023-
|
|
5
|
+
* @Last Modified time: 2023-05-03 02:55:28
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { Sprite, PointLight as THREEPointLight } from "three";
|
|
9
|
-
import threeSpritetext from "three-spritetext";
|
|
10
9
|
import threeLoader from "../threeLoader";
|
|
11
10
|
import threeUtils from "../threeUtils";
|
|
12
11
|
import pointLight_mapPNG from "./pointLight_map.png";
|
|
12
|
+
import SpriteText from "../SpriteText";
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -53,7 +53,7 @@ export default class PointLight extends THREEPointLight {
|
|
|
53
53
|
})();
|
|
54
54
|
|
|
55
55
|
label = threeUtils.isDev && (() => {
|
|
56
|
-
const label = new
|
|
56
|
+
const label = new SpriteText("点光源\n\n\n");
|
|
57
57
|
label.renderOrder = Infinity;
|
|
58
58
|
// label.backgroundColor = "rgba(0, 0, 0, 0.5)";
|
|
59
59
|
label.padding = 4;
|
|
@@ -0,0 +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;
|
|
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;
|
|
25
|
+
set backgroundColor(color: boolean);
|
|
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
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
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._text = `${text}`;
|
|
12
|
+
this._textHeight = textHeight;
|
|
13
|
+
this._color = color;
|
|
14
|
+
this._genCanvas();
|
|
15
|
+
}
|
|
16
|
+
_text = "";
|
|
17
|
+
_textHeight = 10;
|
|
18
|
+
_color = "#fffff";
|
|
19
|
+
_backgroundColor = false; // no background color
|
|
20
|
+
_padding = 0;
|
|
21
|
+
_borderWidth = 0;
|
|
22
|
+
_borderRadius = 0;
|
|
23
|
+
_borderColor = "#fffff";
|
|
24
|
+
_strokeWidth = 0;
|
|
25
|
+
_strokeColor = "#fffff";
|
|
26
|
+
_fontFace = 'Arial';
|
|
27
|
+
_fontSize = 90; // defines text resolution
|
|
28
|
+
_fontWeight = 'normal';
|
|
29
|
+
_canvas = document.createElement('canvas');
|
|
30
|
+
get text() { return this._text; }
|
|
31
|
+
set text(text) { this._text = text; this._genCanvas(); }
|
|
32
|
+
get textHeight() { return this._textHeight; }
|
|
33
|
+
set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
|
|
34
|
+
get color() { return this._color; }
|
|
35
|
+
set color(color) { this._color = color; this._genCanvas(); }
|
|
36
|
+
get backgroundColor() { return this._backgroundColor; }
|
|
37
|
+
set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
|
|
38
|
+
get padding() { return this._padding; }
|
|
39
|
+
set padding(padding) { this._padding = padding; this._genCanvas(); }
|
|
40
|
+
get borderWidth() { return this._borderWidth; }
|
|
41
|
+
set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
|
|
42
|
+
get borderRadius() { return this._borderRadius; }
|
|
43
|
+
set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
|
|
44
|
+
get borderColor() { return this._borderColor; }
|
|
45
|
+
set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
|
|
46
|
+
get fontFace() { return this._fontFace; }
|
|
47
|
+
set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
|
|
48
|
+
get fontSize() { return this._fontSize; }
|
|
49
|
+
set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
|
|
50
|
+
get fontWeight() { return this._fontWeight; }
|
|
51
|
+
set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
|
|
52
|
+
get strokeWidth() { return this._strokeWidth; }
|
|
53
|
+
set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
|
|
54
|
+
get strokeColor() { return this._strokeColor; }
|
|
55
|
+
set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
|
|
56
|
+
_genCanvas() {
|
|
57
|
+
const canvas = this._canvas;
|
|
58
|
+
const ctx = canvas.getContext('2d');
|
|
59
|
+
const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
|
|
60
|
+
const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
|
|
61
|
+
const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
|
|
62
|
+
const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
|
|
63
|
+
const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
|
|
64
|
+
const relPadding = padding.map(p => p * this.fontSize * 0.1);
|
|
65
|
+
; // padding in canvas units
|
|
66
|
+
const lines = this.text.split('\n');
|
|
67
|
+
const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
|
|
68
|
+
ctx.font = font; // measure canvas with appropriate font
|
|
69
|
+
const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
|
|
70
|
+
const innerHeight = this.fontSize * lines.length;
|
|
71
|
+
canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
|
|
72
|
+
canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
|
|
73
|
+
// paint border
|
|
74
|
+
if (this.borderWidth) {
|
|
75
|
+
ctx.strokeStyle = this.borderColor;
|
|
76
|
+
if (relBorder[0]) { // left + right borders
|
|
77
|
+
const hb = relBorder[0] / 2;
|
|
78
|
+
ctx.lineWidth = relBorder[0];
|
|
79
|
+
ctx.beginPath();
|
|
80
|
+
ctx.moveTo(hb, relBorderRadius[0]);
|
|
81
|
+
ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
|
|
82
|
+
ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
|
|
83
|
+
ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
|
|
84
|
+
ctx.stroke();
|
|
85
|
+
}
|
|
86
|
+
if (relBorder[1]) { // top + bottom borders
|
|
87
|
+
const hb = relBorder[1] / 2;
|
|
88
|
+
ctx.lineWidth = relBorder[1];
|
|
89
|
+
ctx.beginPath();
|
|
90
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
|
|
91
|
+
ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
|
|
92
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
|
|
93
|
+
ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
|
|
94
|
+
ctx.stroke();
|
|
95
|
+
}
|
|
96
|
+
if (this.borderRadius) { // strike rounded corners
|
|
97
|
+
const cornerWidth = Math.max(...relBorder);
|
|
98
|
+
const hb = cornerWidth / 2;
|
|
99
|
+
ctx.lineWidth = cornerWidth;
|
|
100
|
+
ctx.beginPath();
|
|
101
|
+
[
|
|
102
|
+
!!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
|
|
103
|
+
!!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
|
|
104
|
+
!!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
|
|
105
|
+
!!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
|
|
106
|
+
].filter(d => d).forEach(([x0, x1, y0, y1]) => {
|
|
107
|
+
ctx.moveTo(x0, y0);
|
|
108
|
+
ctx.quadraticCurveTo(x1, y0, x1, y1);
|
|
109
|
+
});
|
|
110
|
+
ctx.stroke();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// paint background
|
|
114
|
+
if (this.backgroundColor) {
|
|
115
|
+
//@ts-ignore
|
|
116
|
+
ctx.fillStyle = this.backgroundColor;
|
|
117
|
+
if (!this.borderRadius) {
|
|
118
|
+
ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
|
|
119
|
+
}
|
|
120
|
+
else { // fill with rounded corners
|
|
121
|
+
ctx.beginPath();
|
|
122
|
+
ctx.moveTo(relBorder[0], relBorderRadius[0]);
|
|
123
|
+
[
|
|
124
|
+
[relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]],
|
|
125
|
+
[canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]],
|
|
126
|
+
[canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]],
|
|
127
|
+
[relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
|
|
128
|
+
].forEach(([x0, x1, x2, y0, y1, y2]) => {
|
|
129
|
+
ctx.quadraticCurveTo(x0, y0, x1, y1);
|
|
130
|
+
ctx.lineTo(x2, y2);
|
|
131
|
+
});
|
|
132
|
+
ctx.closePath();
|
|
133
|
+
ctx.fill();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
ctx.translate(...relBorder);
|
|
137
|
+
ctx.translate(...relPadding);
|
|
138
|
+
// paint text
|
|
139
|
+
ctx.font = font; // Set font again after canvas is resized, as context properties are reset
|
|
140
|
+
ctx.fillStyle = this.color;
|
|
141
|
+
ctx.textBaseline = 'bottom';
|
|
142
|
+
const drawTextStroke = this.strokeWidth > 0;
|
|
143
|
+
if (drawTextStroke) {
|
|
144
|
+
ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
|
|
145
|
+
ctx.strokeStyle = this.strokeColor;
|
|
146
|
+
}
|
|
147
|
+
lines.forEach((line, index) => {
|
|
148
|
+
const lineX = (innerWidth - ctx.measureText(line).width) / 2;
|
|
149
|
+
const lineY = (index + 1) * this.fontSize;
|
|
150
|
+
drawTextStroke && ctx.strokeText(line, lineX, lineY);
|
|
151
|
+
ctx.fillText(line, lineX, lineY);
|
|
152
|
+
});
|
|
153
|
+
// Inject canvas into sprite
|
|
154
|
+
if (this.material.map)
|
|
155
|
+
this.material.map.dispose(); // gc previous texture
|
|
156
|
+
const texture = this.material.map = new Texture(canvas);
|
|
157
|
+
texture.minFilter = LinearFilter;
|
|
158
|
+
texture.needsUpdate = true;
|
|
159
|
+
const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
|
|
160
|
+
this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
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._text = `${text}`;
|
|
16
|
+
this._textHeight = textHeight;
|
|
17
|
+
this._color = color;
|
|
18
|
+
|
|
19
|
+
this._genCanvas();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
_text: string = "";
|
|
23
|
+
_textHeight: number = 10;
|
|
24
|
+
_color: `#${string}` | `rgba(${string})` = "#fffff";
|
|
25
|
+
_backgroundColor = false; // no background color
|
|
26
|
+
|
|
27
|
+
_padding: number = 0;
|
|
28
|
+
_borderWidth: number = 0;
|
|
29
|
+
_borderRadius: number = 0;
|
|
30
|
+
_borderColor: `#${string}` | `rgba(${string})` = "#fffff";
|
|
31
|
+
|
|
32
|
+
_strokeWidth: number = 0;
|
|
33
|
+
_strokeColor: `#${string}` | `rgba(${string})` = "#fffff";
|
|
34
|
+
|
|
35
|
+
_fontFace: string = 'Arial';
|
|
36
|
+
_fontSize: number = 90; // defines text resolution
|
|
37
|
+
_fontWeight: string = 'normal';
|
|
38
|
+
|
|
39
|
+
_canvas = document.createElement('canvas');
|
|
40
|
+
|
|
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
|
+
|
|
68
|
+
_genCanvas() {
|
|
69
|
+
const canvas = this._canvas;
|
|
70
|
+
const ctx = canvas.getContext('2d');
|
|
71
|
+
|
|
72
|
+
const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
|
|
73
|
+
const relBorder = border.map(b => b * this.fontSize * 0.1) as [number, number]; // border in canvas units
|
|
74
|
+
|
|
75
|
+
const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
|
|
76
|
+
const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
|
|
77
|
+
|
|
78
|
+
const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
|
|
79
|
+
const relPadding = padding.map(p => p * this.fontSize * 0.1) as [number, number];; // padding in canvas units
|
|
80
|
+
|
|
81
|
+
const lines = this.text.split('\n');
|
|
82
|
+
const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
|
|
83
|
+
|
|
84
|
+
ctx.font = font; // measure canvas with appropriate font
|
|
85
|
+
const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
|
|
86
|
+
const innerHeight = this.fontSize * lines.length;
|
|
87
|
+
canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
|
|
88
|
+
canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
|
|
89
|
+
|
|
90
|
+
// paint border
|
|
91
|
+
if (this.borderWidth) {
|
|
92
|
+
ctx.strokeStyle = this.borderColor;
|
|
93
|
+
|
|
94
|
+
if (relBorder[0]) { // left + right borders
|
|
95
|
+
const hb = relBorder[0] / 2;
|
|
96
|
+
ctx.lineWidth = relBorder[0];
|
|
97
|
+
ctx.beginPath();
|
|
98
|
+
ctx.moveTo(hb, relBorderRadius[0]);
|
|
99
|
+
ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
|
|
100
|
+
ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
|
|
101
|
+
ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
|
|
102
|
+
ctx.stroke();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (relBorder[1]) { // top + bottom borders
|
|
106
|
+
const hb = relBorder[1] / 2;
|
|
107
|
+
ctx.lineWidth = relBorder[1];
|
|
108
|
+
ctx.beginPath();
|
|
109
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
|
|
110
|
+
ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
|
|
111
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
|
|
112
|
+
ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
|
|
113
|
+
ctx.stroke();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.borderRadius) { // strike rounded corners
|
|
117
|
+
const cornerWidth = Math.max(...relBorder);
|
|
118
|
+
const hb = cornerWidth / 2;
|
|
119
|
+
ctx.lineWidth = cornerWidth;
|
|
120
|
+
ctx.beginPath();
|
|
121
|
+
[
|
|
122
|
+
!!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
|
|
123
|
+
!!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
|
|
124
|
+
!!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
|
|
125
|
+
!!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
|
|
126
|
+
].filter(d => d).forEach(([x0, x1, y0, y1]) => {
|
|
127
|
+
ctx.moveTo(x0, y0);
|
|
128
|
+
ctx.quadraticCurveTo(x1, y0, x1, y1);
|
|
129
|
+
});
|
|
130
|
+
ctx.stroke();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// paint background
|
|
135
|
+
if (this.backgroundColor) {
|
|
136
|
+
//@ts-ignore
|
|
137
|
+
ctx.fillStyle = this.backgroundColor;
|
|
138
|
+
if (!this.borderRadius) {
|
|
139
|
+
ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
|
|
140
|
+
} else { // fill with rounded corners
|
|
141
|
+
ctx.beginPath();
|
|
142
|
+
ctx.moveTo(relBorder[0], relBorderRadius[0]);
|
|
143
|
+
[
|
|
144
|
+
[relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]], // t
|
|
145
|
+
[canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]], // r
|
|
146
|
+
[canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]], // b
|
|
147
|
+
[relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
|
|
148
|
+
].forEach(([x0, x1, x2, y0, y1, y2]) => {
|
|
149
|
+
ctx.quadraticCurveTo(x0, y0, x1, y1);
|
|
150
|
+
ctx.lineTo(x2, y2);
|
|
151
|
+
});
|
|
152
|
+
ctx.closePath();
|
|
153
|
+
ctx.fill();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
ctx.translate(...relBorder);
|
|
158
|
+
ctx.translate(...relPadding);
|
|
159
|
+
|
|
160
|
+
// paint text
|
|
161
|
+
ctx.font = font; // Set font again after canvas is resized, as context properties are reset
|
|
162
|
+
ctx.fillStyle = this.color;
|
|
163
|
+
ctx.textBaseline = 'bottom';
|
|
164
|
+
|
|
165
|
+
const drawTextStroke = this.strokeWidth > 0;
|
|
166
|
+
if (drawTextStroke) {
|
|
167
|
+
ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
|
|
168
|
+
ctx.strokeStyle = this.strokeColor;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
lines.forEach((line, index) => {
|
|
172
|
+
const lineX = (innerWidth - ctx.measureText(line).width) / 2;
|
|
173
|
+
const lineY = (index + 1) * this.fontSize;
|
|
174
|
+
|
|
175
|
+
drawTextStroke && ctx.strokeText(line, lineX, lineY);
|
|
176
|
+
ctx.fillText(line, lineX, lineY);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Inject canvas into sprite
|
|
180
|
+
if (this.material.map) this.material.map.dispose(); // gc previous texture
|
|
181
|
+
const texture = this.material.map = new Texture(canvas);
|
|
182
|
+
texture.minFilter = LinearFilter;
|
|
183
|
+
texture.needsUpdate = true;
|
|
184
|
+
|
|
185
|
+
const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
|
|
186
|
+
this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
}
|
package/lib/SpriteText2.d.ts
CHANGED
|
@@ -19,10 +19,10 @@ export default class SpriteText2 extends Sprite {
|
|
|
19
19
|
set text(text: string);
|
|
20
20
|
get textHeight(): number;
|
|
21
21
|
set textHeight(textHeight: number);
|
|
22
|
-
get color():
|
|
23
|
-
set color(color:
|
|
24
|
-
get backgroundColor(): false |
|
|
25
|
-
set backgroundColor(color: false |
|
|
22
|
+
get color(): `#${string}` | `rgba(${string})` | `rgb(${string})`;
|
|
23
|
+
set color(color: `#${string}` | `rgba(${string})` | `rgb(${string})`);
|
|
24
|
+
get backgroundColor(): false | `#${string}` | `rgba(${string})` | `rgb(${string})`;
|
|
25
|
+
set backgroundColor(color: false | `#${string}` | `rgba(${string})` | `rgb(${string})`);
|
|
26
26
|
get padding(): number;
|
|
27
27
|
set padding(padding: number);
|
|
28
28
|
get borderWidth(): number;
|
package/lib/threeUtils.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-02-24 22:49:42
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2023-03
|
|
5
|
+
* @Last Modified time: 2023-05-03 00:36:18
|
|
6
6
|
*/
|
|
7
7
|
import { Box3, BufferGeometry, Clock, EventDispatcher, Object3D, Vector3 } from "three";
|
|
8
8
|
class ThreeUtils extends EventDispatcher {
|
|
@@ -168,6 +168,7 @@ class ThreeUtils extends EventDispatcher {
|
|
|
168
168
|
const scale = size / Math.max(originSize.x, originSize.y, originSize.z);
|
|
169
169
|
if (object3d instanceof Object3D) {
|
|
170
170
|
object3d.scale.multiplyScalar(scale);
|
|
171
|
+
object3d.updateMatrixWorld(true);
|
|
171
172
|
}
|
|
172
173
|
else if (object3d instanceof BufferGeometry) {
|
|
173
174
|
object3d.scale(scale, scale, scale);
|
package/lib/threeUtils.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-02-24 22:49:42
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2023-03
|
|
5
|
+
* @Last Modified time: 2023-05-03 00:36:18
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { Box3, BufferGeometry, Clock, EventDispatcher, Object3D, Vector3 } from "three";
|
|
@@ -194,6 +194,7 @@ class ThreeUtils extends EventDispatcher {
|
|
|
194
194
|
const scale = size / Math.max(originSize.x, originSize.y, originSize.z);
|
|
195
195
|
if (object3d instanceof Object3D) {
|
|
196
196
|
object3d.scale.multiplyScalar(scale);
|
|
197
|
+
object3d.updateMatrixWorld(true);
|
|
197
198
|
} else if (object3d instanceof BufferGeometry) {
|
|
198
199
|
object3d.scale(scale, scale, scale);
|
|
199
200
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "three-base",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.83",
|
|
4
4
|
"description": "three 3d three-base",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
"keywords": [
|
|
10
10
|
"three",
|
|
11
11
|
"3d",
|
|
12
|
-
"three-base"
|
|
13
|
-
"three-spritetext"
|
|
12
|
+
"three-base"
|
|
14
13
|
],
|
|
15
14
|
"main": "index.js",
|
|
16
15
|
"module": "index.js",
|
|
@@ -44,7 +43,6 @@
|
|
|
44
43
|
"lodash": "^4",
|
|
45
44
|
"three": ">=0.150.0",
|
|
46
45
|
"dexie": "^3",
|
|
47
|
-
"three-spritetext": "^1",
|
|
48
46
|
"@types/three": ">=0.149.0",
|
|
49
47
|
"@types/element-resize-event": "^3",
|
|
50
48
|
"@types/lodash": "^4",
|