three-base 2.0.137 → 2.0.145
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/DirectionalLight.d.ts +2 -2
- package/DirectionalLight.js +1 -1
- package/DirectionalLight.ts +70 -0
- package/EventMap.d.ts +103 -0
- package/EventMap.js +17 -0
- package/EventMap.ts +139 -0
- package/EventMesh.ts +60 -0
- package/ExtrudeMesh.js +1 -25
- package/ExtrudeMesh.ts +553 -0
- package/FPScontrol.d.ts +448 -15
- package/FPScontrol.js +1448 -266
- package/FPScontrol.ts +1855 -0
- package/Fire/Fire.d.ts +4 -4
- package/Fire/Fire.js +8 -7
- package/Fire/Fire.ts +89 -0
- package/Fire/{FireGromtry.d.ts → FireGeometry.d.ts} +2 -2
- package/Fire/{FireGromtry.js → FireGeometry.js} +1 -1
- package/Fire/FireGeometry.ts +40 -0
- package/Fire/FireMaterial.d.ts +3 -3
- package/Fire/FireMaterial.js +1 -1
- package/Fire/FireMaterial.ts +89 -0
- package/Fire/index.d.ts +1 -0
- package/Fire/index.js +1 -0
- package/Fire/index.ts +2 -0
- package/GLTFDracoLoader.d.ts +71 -0
- package/GLTFDracoLoader.js +266 -0
- package/GLTFDracoLoader.ts +430 -0
- package/Grass/Grass.d.ts +2 -2
- package/Grass/Grass.ts +38 -0
- package/Grass/index.d.ts +1 -0
- package/Grass/index.js +1 -0
- package/Grass/index.ts +2 -0
- package/Ground1/Ground1.d.ts +2 -3
- package/Ground1/Ground1.js +64 -60
- package/Ground1/Ground1.ts +114 -0
- package/Ground1/index.ts +1 -0
- package/MeshBase.d.ts +1 -0
- package/MeshBase.js +37 -13
- package/MeshBase.ts +253 -0
- package/Mirror.d.ts +3 -3
- package/Mirror.js +7 -3
- package/Mirror.ts +203 -0
- package/MktMesh/MktMesh.d.ts +301 -0
- package/MktMesh/MktMesh.js +1064 -0
- package/MktMesh/MktMesh.ts +1096 -0
- package/MktMesh/assetUrl.d.ts +59 -0
- package/MktMesh/assetUrl.js +154 -0
- package/MktMesh/assetUrl.ts +155 -0
- package/MktMesh/mktMappingShader.d.ts +12 -0
- package/MktMesh/mktMappingShader.js +139 -0
- package/MktMesh/mktMappingShader.ts +139 -0
- package/MktMesh/mktShader.d.ts +263 -0
- package/MktMesh/mktShader.js +1248 -0
- package/MktMesh/mktShader.ts +1344 -0
- package/MktMesh/panoBitmapTexture.d.ts +25 -0
- package/MktMesh/panoBitmapTexture.js +52 -0
- package/MktMesh/panoBitmapTexture.ts +54 -0
- package/MktMesh/panoSlotBind.d.ts +29 -0
- package/MktMesh/panoSlotBind.js +106 -0
- package/MktMesh/panoSlotBind.ts +153 -0
- package/MktMesh/panoSlotRoster.d.ts +22 -0
- package/MktMesh/panoSlotRoster.js +169 -0
- package/MktMesh/panoSlotRoster.ts +168 -0
- package/MktMesh/panoTileStitcher.d.ts +119 -0
- package/MktMesh/panoTileStitcher.js +533 -0
- package/MktMesh/panoTileStitcher.ts +616 -0
- package/MktMesh/types.d.ts +75 -0
- package/MktMesh/types.js +1 -0
- package/MktMesh/types.ts +87 -0
- package/MktMesh/wallCapTessellate.d.ts +33 -0
- package/MktMesh/wallCapTessellate.js +97 -0
- package/MktMesh/wallCapTessellate.ts +68 -0
- package/PipeMesh/PathGeometry.js +9 -2
- package/PipeMesh/PathGeometry.ts +343 -0
- package/PipeMesh/PathPoint.ts +36 -0
- package/PipeMesh/PathPointList.js +1 -0
- package/PipeMesh/PathPointList.ts +227 -0
- package/PipeMesh/PathTubeGeometry.ts +146 -0
- package/PipeMesh/PipeMesh.d.ts +1 -1
- package/PipeMesh/PipeMesh.js +1 -1
- package/PipeMesh/PipeMesh.ts +76 -0
- package/PipeMesh/index.ts +1 -0
- package/PlaneModal1/PlaneModal1.d.ts +16 -13
- package/PlaneModal1/PlaneModal1.js +4 -0
- package/PlaneModal1/PlaneModal1.ts +138 -0
- package/PlaneModal1/index.ts +1 -0
- package/PlaneText.d.ts +2 -14
- package/PlaneText.js +7 -2
- package/PlaneText.ts +55 -0
- package/PlaneWithHtml.d.ts +4 -2
- package/PlaneWithHtml.js +2 -1
- package/PlaneWithHtml.ts +70 -0
- package/PointLight/PointLight.d.ts +3 -4
- package/PointLight/PointLight.js +1 -7
- package/PointLight/PointLight.ts +67 -0
- package/PointLight/index.ts +1 -0
- package/README.md +110 -79
- package/Sky/Sky.ts +22 -0
- package/Sky/index.ts +1 -0
- package/SpotLight.d.ts +2 -2
- package/SpotLight.js +1 -1
- package/SpotLight.ts +74 -0
- package/SpriteModal1/SpriteModal1.d.ts +16 -13
- package/SpriteModal1/SpriteModal1.js +8 -2
- package/SpriteModal1/SpriteModal1.ts +146 -0
- package/SpriteModal1/index.ts +1 -0
- package/SpriteText.d.ts +14 -13
- package/SpriteText.js +22 -9
- package/SpriteText.ts +205 -0
- package/SpriteText2.d.ts +9 -8
- package/SpriteText2.js +57 -32
- package/SpriteText2.ts +258 -0
- package/SpriteText3.d.ts +86 -0
- package/SpriteText3.js +519 -0
- package/SpriteText3.ts +599 -0
- package/SpriteWithHtml.d.ts +4 -2
- package/SpriteWithHtml.ts +50 -0
- package/Text3D/Text3D.js +17 -21
- package/Text3D/Text3D.ts +77 -0
- package/Text3D/fontController.d.ts +10 -9
- package/Text3D/fontController.js +17 -50
- package/Text3D/fontController.ts +209 -0
- package/Text3D/index.ts +1 -0
- package/ThreeBase.d.ts +168 -31
- package/ThreeBase.js +663 -117
- package/ThreeBase.ts +1314 -0
- package/ThreeMouse.d.ts +20 -7
- package/ThreeMouse.js +78 -32
- package/ThreeMouse.ts +472 -0
- package/Transition.d.ts +12 -7
- package/Transition.js +26 -27
- package/Transition.ts +242 -0
- package/Water/Water.js +21 -3
- package/Water/Water.ts +45 -0
- package/Water/index.d.ts +2 -0
- package/Water/index.js +2 -0
- package/Water/index.ts +2 -0
- package/WaterFlow/FloatingDuck.d.ts +88 -0
- package/WaterFlow/FloatingDuck.js +307 -0
- package/WaterFlow/FloatingDuck.ts +445 -0
- package/WaterFlow/GPUFluidSimulation.d.ts +56 -0
- package/WaterFlow/GPUFluidSimulation.js +785 -0
- package/WaterFlow/GPUFluidSimulation.ts +827 -0
- package/WaterFlow/ReservoirSystem.d.ts +106 -0
- package/WaterFlow/ReservoirSystem.js +621 -0
- package/WaterFlow/ReservoirSystem.ts +754 -0
- package/WaterFlow/SPHField.d.ts +35 -0
- package/WaterFlow/SPHField.js +204 -0
- package/WaterFlow/SPHField.ts +228 -0
- package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -0
- package/WaterFlow/ScreenSpaceFluidPass.js +736 -0
- package/WaterFlow/ScreenSpaceFluidPass.ts +789 -0
- package/WaterFlow/ShallowWaterSurface.d.ts +45 -0
- package/WaterFlow/ShallowWaterSurface.js +441 -0
- package/WaterFlow/ShallowWaterSurface.ts +510 -0
- package/WaterFlow/WaterFlow.d.ts +87 -0
- package/WaterFlow/WaterFlow.js +171 -0
- package/WaterFlow/WaterFlow.ts +219 -0
- package/WaterFlow/types.d.ts +161 -0
- package/WaterFlow/types.js +1 -0
- package/WaterFlow/types.ts +189 -0
- package/WaterFlow/waterFlowPass.d.ts +3 -0
- package/WaterFlow/waterFlowPass.js +20 -0
- package/WaterFlow/waterFlowPass.ts +40 -0
- package/collisionOBB.d.ts +42 -0
- package/collisionOBB.js +112 -0
- package/collisionOBB.ts +108 -0
- package/declare.d.ts +25 -167
- package/index.js +0 -1
- package/index.ts +1 -0
- package/instanced/InstancedBatch.d.ts +57 -0
- package/instanced/InstancedBatch.js +348 -0
- package/instanced/InstancedBatch.ts +406 -0
- package/instanced/InstancedChild.d.ts +174 -0
- package/instanced/InstancedChild.js +264 -0
- package/instanced/InstancedChild.ts +327 -0
- package/instanced/InstancedGroup.d.ts +43 -0
- package/instanced/InstancedGroup.js +126 -0
- package/instanced/InstancedGroup.ts +137 -0
- package/{InstancedMeshBase.d.ts → instanced/InstancedMeshBase.d.ts} +6 -7
- package/{InstancedMeshBase.js → instanced/InstancedMeshBase.js} +36 -47
- package/instanced/InstancedMeshBase.ts +292 -0
- package/instanced/InstancedTemplate.d.ts +46 -0
- package/instanced/InstancedTemplate.js +125 -0
- package/instanced/InstancedTemplate.ts +188 -0
- package/keyControls.d.ts +29 -0
- package/{keyContorls.js → keyControls.js} +10 -7
- package/keyControls.ts +107 -0
- package/lensflare/LensFlare.d.ts +39 -0
- package/lensflare/LensFlare.js +373 -0
- package/lensflare/LensFlare.ts +448 -0
- package/lensflare/lensflare0.png +0 -0
- package/lensflare/lensflare3.png +0 -0
- package/lensflare/lensflare_custom.png +0 -0
- package/lensflare/lensflare_hex_ghost.png +0 -0
- package/lensflare/lensflare_hex_main.png +0 -0
- package/package.json +104 -25
- package/pass/bloom_pass.d.ts +1 -1
- package/pass/bloom_pass.js +127 -40
- package/pass/bloom_pass.ts +243 -0
- package/pass/cover_pass.d.ts +1 -1
- package/pass/cover_pass.js +55 -32
- package/pass/cover_pass.ts +182 -0
- package/pass/debugDepthState.d.ts +20 -0
- package/pass/debugDepthState.js +9 -0
- package/pass/debugDepthState.ts +22 -0
- package/pass/depthTestBias.d.ts +12 -0
- package/pass/depthTestBias.js +12 -0
- package/pass/depthTestBias.ts +12 -0
- package/pass/effectUserData.d.ts +27 -0
- package/pass/effectUserData.js +1 -0
- package/pass/effectUserData.ts +46 -0
- package/pass/frosted_glass_pass.d.ts +1 -1
- package/pass/frosted_glass_pass.js +45 -64
- package/pass/frosted_glass_pass.ts +288 -0
- package/pass/fsQuad.d.ts +2 -2
- package/pass/fsQuad.ts +34 -0
- package/pass/glow_pass.d.ts +1 -1
- package/pass/glow_pass.js +288 -72
- package/pass/glow_pass.ts +654 -0
- package/pass/screen_bloom_pass.d.ts +11 -0
- package/pass/screen_bloom_pass.js +235 -0
- package/pass/screen_bloom_pass.ts +263 -0
- package/pass/ssao_pass.d.ts +78 -3
- package/pass/ssao_pass.js +642 -206
- package/pass/ssao_pass.ts +918 -0
- package/pass/stroke_pass.d.ts +1 -1
- package/pass/stroke_pass.js +187 -116
- package/pass/stroke_pass.ts +638 -0
- package/shaders/FXAAShaderMaterial.js +5 -48
- package/shaders/FXAAShaderMaterial.ts +201 -0
- package/shaders/baseDepthVisShader.d.ts +11 -0
- package/shaders/baseDepthVisShader.js +36 -0
- package/shaders/baseDepthVisShader.ts +36 -0
- package/shaders/baseTextureToRenderShader.js +46 -51
- package/shaders/baseTextureToRenderShader.ts +232 -0
- package/shaders/copyShader.js +2 -3
- package/shaders/copyShader.ts +32 -0
- package/shaders/debugDepthCompareShader.d.ts +14 -0
- package/shaders/debugDepthCompareShader.js +49 -0
- package/shaders/debugDepthCompareShader.ts +50 -0
- package/shaders/debugDepthJudgeShader.d.ts +18 -0
- package/shaders/debugDepthJudgeShader.js +52 -0
- package/shaders/debugDepthJudgeShader.ts +54 -0
- package/shaders/debugExcludedShader.d.ts +13 -0
- package/shaders/debugExcludedShader.js +46 -0
- package/shaders/debugExcludedShader.ts +48 -0
- package/spriteTextBg.d.ts +1 -0
- package/spriteTextBg.js +20 -0
- package/spriteTextBg.ts +20 -0
- package/threeLoader.d.ts +8 -7
- package/threeLoader.js +58 -67
- package/threeLoader.ts +525 -0
- package/threeUtils.d.ts +2 -1
- package/threeUtils.js +24 -25
- package/threeUtils.ts +373 -0
- package/types.d.ts +2 -0
- package/types.js +6 -0
- package/types.ts +8 -0
- package/ApngTexture.d.ts +0 -13
- package/ApngTexture.js +0 -61
- package/ApngTextureManage.d.ts +0 -18
- package/ApngTextureManage.js +0 -87
- package/KeyFrameAnimation.d.ts +0 -25
- package/KeyFrameAnimation.js +0 -78
- package/keyContorls.d.ts +0 -25
- package/lib/draco/draco_decoder.js +0 -34
- package/lib/draco/draco_decoder.wasm +0 -0
- package/lib/draco/draco_wasm_wrapper.js +0 -117
- package/pass/emissive_ao_pass.d.ts +0 -13
- package/pass/emissive_ao_pass.js +0 -370
- package/shaders.d.ts +0 -5
- package/shaders.js +0 -93
package/SpriteText.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { Sprite } from "three";
|
|
2
|
+
import type { ColorString } from "./types";
|
|
2
3
|
export default class SpriteText extends Sprite {
|
|
3
|
-
constructor(text?: string, textHeight?: number, color?:
|
|
4
|
+
constructor(text?: string, textHeight?: number, color?: ColorString);
|
|
4
5
|
_text: string;
|
|
5
6
|
_textHeight: number;
|
|
6
|
-
_color:
|
|
7
|
-
_backgroundColor: boolean |
|
|
7
|
+
_color: ColorString;
|
|
8
|
+
_backgroundColor: boolean | ColorString;
|
|
8
9
|
_padding: number;
|
|
9
10
|
_borderWidth: number;
|
|
10
11
|
_borderRadius: number;
|
|
11
|
-
_borderColor:
|
|
12
|
+
_borderColor: ColorString;
|
|
12
13
|
_strokeWidth: number;
|
|
13
|
-
_strokeColor:
|
|
14
|
+
_strokeColor: ColorString;
|
|
14
15
|
_fontFace: string;
|
|
15
16
|
_fontSize: number;
|
|
16
17
|
_fontWeight: string;
|
|
@@ -19,18 +20,18 @@ export default class SpriteText extends Sprite {
|
|
|
19
20
|
set text(text: string);
|
|
20
21
|
get textHeight(): number;
|
|
21
22
|
set textHeight(textHeight: number);
|
|
22
|
-
get color():
|
|
23
|
-
set color(color:
|
|
24
|
-
get backgroundColor(): boolean |
|
|
25
|
-
set backgroundColor(color: boolean |
|
|
23
|
+
get color(): ColorString;
|
|
24
|
+
set color(color: ColorString);
|
|
25
|
+
get backgroundColor(): boolean | ColorString;
|
|
26
|
+
set backgroundColor(color: boolean | ColorString);
|
|
26
27
|
get padding(): number;
|
|
27
28
|
set padding(padding: number);
|
|
28
29
|
get borderWidth(): number;
|
|
29
30
|
set borderWidth(borderWidth: number);
|
|
30
31
|
get borderRadius(): number;
|
|
31
32
|
set borderRadius(borderRadius: number);
|
|
32
|
-
get borderColor():
|
|
33
|
-
set borderColor(borderColor:
|
|
33
|
+
get borderColor(): ColorString;
|
|
34
|
+
set borderColor(borderColor: ColorString);
|
|
34
35
|
get fontFace(): string;
|
|
35
36
|
set fontFace(fontFace: string);
|
|
36
37
|
get fontSize(): number;
|
|
@@ -39,7 +40,7 @@ export default class SpriteText extends Sprite {
|
|
|
39
40
|
set fontWeight(fontWeight: string);
|
|
40
41
|
get strokeWidth(): number;
|
|
41
42
|
set strokeWidth(strokeWidth: number);
|
|
42
|
-
get strokeColor():
|
|
43
|
-
set strokeColor(strokeColor:
|
|
43
|
+
get strokeColor(): ColorString;
|
|
44
|
+
set strokeColor(strokeColor: ColorString);
|
|
44
45
|
_genCanvas(): void;
|
|
45
46
|
}
|
package/SpriteText.js
CHANGED
|
@@ -16,7 +16,7 @@ export default class SpriteText extends Sprite {
|
|
|
16
16
|
}
|
|
17
17
|
_text = "";
|
|
18
18
|
_textHeight = 10;
|
|
19
|
-
_color = "#
|
|
19
|
+
_color = "#ffffff";
|
|
20
20
|
_backgroundColor = false; // no background color
|
|
21
21
|
_padding = 0;
|
|
22
22
|
_borderWidth = 0;
|
|
@@ -57,6 +57,8 @@ export default class SpriteText extends Sprite {
|
|
|
57
57
|
_genCanvas() {
|
|
58
58
|
const canvas = this._canvas;
|
|
59
59
|
const ctx = canvas.getContext('2d');
|
|
60
|
+
if (!ctx)
|
|
61
|
+
return;
|
|
60
62
|
const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
|
|
61
63
|
const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
|
|
62
64
|
const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
|
|
@@ -104,7 +106,7 @@ export default class SpriteText extends Sprite {
|
|
|
104
106
|
!!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
|
|
105
107
|
!!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
|
|
106
108
|
!!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
|
|
107
|
-
].filter(d => d).forEach(([x0, x1, y0, y1]) => {
|
|
109
|
+
].filter((d) => !!d).forEach(([x0, x1, y0, y1]) => {
|
|
108
110
|
ctx.moveTo(x0, y0);
|
|
109
111
|
ctx.quadraticCurveTo(x1, y0, x1, y1);
|
|
110
112
|
});
|
|
@@ -112,8 +114,7 @@ export default class SpriteText extends Sprite {
|
|
|
112
114
|
}
|
|
113
115
|
}
|
|
114
116
|
// paint background
|
|
115
|
-
if (this.backgroundColor) {
|
|
116
|
-
//@ts-ignore
|
|
117
|
+
if (this.backgroundColor && typeof this.backgroundColor === 'string') {
|
|
117
118
|
ctx.fillStyle = this.backgroundColor;
|
|
118
119
|
if (!this.borderRadius) {
|
|
119
120
|
ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
|
|
@@ -139,17 +140,29 @@ export default class SpriteText extends Sprite {
|
|
|
139
140
|
// paint text
|
|
140
141
|
ctx.font = font; // Set font again after canvas is resized, as context properties are reset
|
|
141
142
|
ctx.fillStyle = this.color;
|
|
142
|
-
|
|
143
|
+
// 精确垂直居中:用 measureText 的 actualBoundingBox 实测字形上下沿,
|
|
144
|
+
// 不再依赖 textBaseline='middle'(middle 是 em 盒中线,与真实字形视觉中线常偏差,
|
|
145
|
+
// 导致整行文字偏上/偏下、看着不居中)。改为 alphabetic 基线 + 实测字形高度,
|
|
146
|
+
// 把每行字形放进其 fontSize 高的槽中心。
|
|
147
|
+
ctx.textAlign = 'center';
|
|
148
|
+
ctx.textBaseline = 'alphabetic';
|
|
143
149
|
const drawTextStroke = this.strokeWidth > 0;
|
|
144
150
|
if (drawTextStroke) {
|
|
145
151
|
ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
|
|
146
152
|
ctx.strokeStyle = this.strokeColor;
|
|
147
153
|
}
|
|
154
|
+
const slotH = this.fontSize; // 每行槽高
|
|
148
155
|
lines.forEach((line, index) => {
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
156
|
+
const metrics = ctx.measureText(line);
|
|
157
|
+
const ascent = metrics.actualBoundingBoxAscent; // 字形顶部到基线
|
|
158
|
+
const descent = metrics.actualBoundingBoxDescent; // 基线到字形底部
|
|
159
|
+
const glyphH = (ascent || 0) + (descent || 0);
|
|
160
|
+
const slotTop = index * slotH; // 本行槽顶(内容框坐标系,已平移)
|
|
161
|
+
// 让字形在槽中垂直居中:先平移到槽中线,再上移 ascent 回到基线
|
|
162
|
+
const baselineY = slotTop + (slotH - glyphH) / 2 + (ascent || slotH / 2);
|
|
163
|
+
const lineX = innerWidth / 2;
|
|
164
|
+
drawTextStroke && ctx.strokeText(line, lineX, baselineY);
|
|
165
|
+
ctx.fillText(line, lineX, baselineY);
|
|
153
166
|
});
|
|
154
167
|
// Inject canvas into sprite
|
|
155
168
|
if (this.material.map)
|
package/SpriteText.ts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
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
|
+
import type { ColorString } from "./types";
|
|
9
|
+
|
|
10
|
+
export default class SpriteText extends Sprite {
|
|
11
|
+
constructor(text: string = '', textHeight: number = 10, color: ColorString = 'rgba(255, 255, 255, 1)') {
|
|
12
|
+
super(new SpriteMaterial());
|
|
13
|
+
this.material.alphaTest = 0.1;
|
|
14
|
+
this._text = `${text}`;
|
|
15
|
+
this._textHeight = textHeight;
|
|
16
|
+
this._color = color;
|
|
17
|
+
this._genCanvas();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
_text: string = "";
|
|
21
|
+
_textHeight: number = 10;
|
|
22
|
+
_color: ColorString = "#ffffff";
|
|
23
|
+
_backgroundColor: boolean | ColorString = false; // no background color
|
|
24
|
+
_padding: number = 0;
|
|
25
|
+
_borderWidth: number = 0;
|
|
26
|
+
_borderRadius: number = 0;
|
|
27
|
+
_borderColor: ColorString = "#fffff";
|
|
28
|
+
_strokeWidth: number = 0;
|
|
29
|
+
_strokeColor: ColorString = "#fffff";
|
|
30
|
+
_fontFace: string = 'Arial';
|
|
31
|
+
_fontSize: number = 90; // defines text resolution
|
|
32
|
+
_fontWeight: string = 'normal';
|
|
33
|
+
_canvas: HTMLCanvasElement = document.createElement('canvas');
|
|
34
|
+
|
|
35
|
+
get text(): string { return this._text; }
|
|
36
|
+
set text(text: string) { this._text = text; this._genCanvas(); }
|
|
37
|
+
|
|
38
|
+
get textHeight(): number { return this._textHeight; }
|
|
39
|
+
set textHeight(textHeight: number) { this._textHeight = textHeight; this._genCanvas(); }
|
|
40
|
+
|
|
41
|
+
get color(): ColorString { return this._color; }
|
|
42
|
+
set color(color: ColorString) { this._color = color; this._genCanvas(); }
|
|
43
|
+
|
|
44
|
+
get backgroundColor(): boolean | ColorString { return this._backgroundColor; }
|
|
45
|
+
set backgroundColor(color: boolean | ColorString) { this._backgroundColor = color; this._genCanvas(); }
|
|
46
|
+
|
|
47
|
+
get padding(): number { return this._padding; }
|
|
48
|
+
set padding(padding: number) { this._padding = padding; this._genCanvas(); }
|
|
49
|
+
|
|
50
|
+
get borderWidth(): number { return this._borderWidth; }
|
|
51
|
+
set borderWidth(borderWidth: number) { this._borderWidth = borderWidth; this._genCanvas(); }
|
|
52
|
+
|
|
53
|
+
get borderRadius(): number { return this._borderRadius; }
|
|
54
|
+
set borderRadius(borderRadius: number) { this._borderRadius = borderRadius; this._genCanvas(); }
|
|
55
|
+
|
|
56
|
+
get borderColor(): ColorString { return this._borderColor; }
|
|
57
|
+
set borderColor(borderColor: ColorString) { this._borderColor = borderColor; this._genCanvas(); }
|
|
58
|
+
|
|
59
|
+
get fontFace(): string { return this._fontFace; }
|
|
60
|
+
set fontFace(fontFace: string) { this._fontFace = fontFace; this._genCanvas(); }
|
|
61
|
+
|
|
62
|
+
get fontSize(): number { return this._fontSize; }
|
|
63
|
+
set fontSize(fontSize: number) { this._fontSize = fontSize; this._genCanvas(); }
|
|
64
|
+
|
|
65
|
+
get fontWeight(): string { return this._fontWeight; }
|
|
66
|
+
set fontWeight(fontWeight: string) { this._fontWeight = fontWeight; this._genCanvas(); }
|
|
67
|
+
|
|
68
|
+
get strokeWidth(): number { return this._strokeWidth; }
|
|
69
|
+
set strokeWidth(strokeWidth: number) { this._strokeWidth = strokeWidth; this._genCanvas(); }
|
|
70
|
+
|
|
71
|
+
get strokeColor(): ColorString { return this._strokeColor; }
|
|
72
|
+
set strokeColor(strokeColor: ColorString) { this._strokeColor = strokeColor; this._genCanvas(); }
|
|
73
|
+
|
|
74
|
+
_genCanvas(): void {
|
|
75
|
+
const canvas = this._canvas;
|
|
76
|
+
const ctx = canvas.getContext('2d');
|
|
77
|
+
if (!ctx) return;
|
|
78
|
+
|
|
79
|
+
const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
|
|
80
|
+
const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
|
|
81
|
+
const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
|
|
82
|
+
const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
|
|
83
|
+
const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
|
|
84
|
+
const relPadding = padding.map(p => p * this.fontSize * 0.1);
|
|
85
|
+
; // padding in canvas units
|
|
86
|
+
const lines = this.text.split('\n');
|
|
87
|
+
const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
|
|
88
|
+
|
|
89
|
+
ctx.font = font; // measure canvas with appropriate font
|
|
90
|
+
const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
|
|
91
|
+
const innerHeight = this.fontSize * lines.length;
|
|
92
|
+
canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
|
|
93
|
+
canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
|
|
94
|
+
|
|
95
|
+
// paint border
|
|
96
|
+
if (this.borderWidth) {
|
|
97
|
+
ctx.strokeStyle = this.borderColor;
|
|
98
|
+
|
|
99
|
+
if (relBorder[0]) { // left + right borders
|
|
100
|
+
const hb = relBorder[0] / 2;
|
|
101
|
+
ctx.lineWidth = relBorder[0];
|
|
102
|
+
ctx.beginPath();
|
|
103
|
+
ctx.moveTo(hb, relBorderRadius[0]);
|
|
104
|
+
ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
|
|
105
|
+
ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
|
|
106
|
+
ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
|
|
107
|
+
ctx.stroke();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (relBorder[1]) { // top + bottom borders
|
|
111
|
+
const hb = relBorder[1] / 2;
|
|
112
|
+
ctx.lineWidth = relBorder[1];
|
|
113
|
+
ctx.beginPath();
|
|
114
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
|
|
115
|
+
ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
|
|
116
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
|
|
117
|
+
ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
|
|
118
|
+
ctx.stroke();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (this.borderRadius) { // strike rounded corners
|
|
122
|
+
const cornerWidth = Math.max(...relBorder);
|
|
123
|
+
const hb = cornerWidth / 2;
|
|
124
|
+
ctx.lineWidth = cornerWidth;
|
|
125
|
+
ctx.beginPath();
|
|
126
|
+
[
|
|
127
|
+
!!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
|
|
128
|
+
!!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
|
|
129
|
+
!!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
|
|
130
|
+
!!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
|
|
131
|
+
].filter((d): d is number[] => !!d).forEach(([x0, x1, y0, y1]) => {
|
|
132
|
+
ctx.moveTo(x0, y0);
|
|
133
|
+
ctx.quadraticCurveTo(x1, y0, x1, y1);
|
|
134
|
+
});
|
|
135
|
+
ctx.stroke();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// paint background
|
|
140
|
+
if (this.backgroundColor && typeof this.backgroundColor === 'string') {
|
|
141
|
+
ctx.fillStyle = this.backgroundColor;
|
|
142
|
+
if (!this.borderRadius) {
|
|
143
|
+
ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
|
|
144
|
+
}
|
|
145
|
+
else { // fill with rounded corners
|
|
146
|
+
ctx.beginPath();
|
|
147
|
+
ctx.moveTo(relBorder[0], relBorderRadius[0]);
|
|
148
|
+
[
|
|
149
|
+
[relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]], // t
|
|
150
|
+
[canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]], // r
|
|
151
|
+
[canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]], // b
|
|
152
|
+
[relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
|
|
153
|
+
].forEach(([x0, x1, x2, y0, y1, y2]) => {
|
|
154
|
+
ctx.quadraticCurveTo(x0, y0, x1, y1);
|
|
155
|
+
ctx.lineTo(x2, y2);
|
|
156
|
+
});
|
|
157
|
+
ctx.closePath();
|
|
158
|
+
ctx.fill();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
ctx.translate(...relBorder as [number, number]);
|
|
163
|
+
ctx.translate(...relPadding as [number, number]);
|
|
164
|
+
|
|
165
|
+
// paint text
|
|
166
|
+
ctx.font = font; // Set font again after canvas is resized, as context properties are reset
|
|
167
|
+
ctx.fillStyle = this.color;
|
|
168
|
+
// 精确垂直居中:用 measureText 的 actualBoundingBox 实测字形上下沿,
|
|
169
|
+
// 不再依赖 textBaseline='middle'(middle 是 em 盒中线,与真实字形视觉中线常偏差,
|
|
170
|
+
// 导致整行文字偏上/偏下、看着不居中)。改为 alphabetic 基线 + 实测字形高度,
|
|
171
|
+
// 把每行字形放进其 fontSize 高的槽中心。
|
|
172
|
+
ctx.textAlign = 'center';
|
|
173
|
+
ctx.textBaseline = 'alphabetic';
|
|
174
|
+
|
|
175
|
+
const drawTextStroke = this.strokeWidth > 0;
|
|
176
|
+
if (drawTextStroke) {
|
|
177
|
+
ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
|
|
178
|
+
ctx.strokeStyle = this.strokeColor;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const slotH = this.fontSize; // 每行槽高
|
|
182
|
+
lines.forEach((line, index) => {
|
|
183
|
+
const metrics = ctx.measureText(line);
|
|
184
|
+
const ascent = metrics.actualBoundingBoxAscent; // 字形顶部到基线
|
|
185
|
+
const descent = metrics.actualBoundingBoxDescent; // 基线到字形底部
|
|
186
|
+
const glyphH = (ascent || 0) + (descent || 0);
|
|
187
|
+
const slotTop = index * slotH; // 本行槽顶(内容框坐标系,已平移)
|
|
188
|
+
// 让字形在槽中垂直居中:先平移到槽中线,再上移 ascent 回到基线
|
|
189
|
+
const baselineY = slotTop + (slotH - glyphH) / 2 + (ascent || slotH / 2);
|
|
190
|
+
const lineX = innerWidth / 2;
|
|
191
|
+
drawTextStroke && ctx.strokeText(line, lineX, baselineY);
|
|
192
|
+
ctx.fillText(line, lineX, baselineY);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Inject canvas into sprite
|
|
196
|
+
if (this.material.map)
|
|
197
|
+
this.material.map.dispose(); // gc previous texture
|
|
198
|
+
const texture = this.material.map = new Texture(canvas);
|
|
199
|
+
texture.minFilter = LinearFilter;
|
|
200
|
+
texture.needsUpdate = true;
|
|
201
|
+
|
|
202
|
+
const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
|
|
203
|
+
this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
|
|
204
|
+
}
|
|
205
|
+
}
|
package/SpriteText2.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Sprite, Raycaster, Intersection } from 'three';
|
|
2
|
+
import type { ColorString } from "./types";
|
|
2
3
|
export default class SpriteText2 extends Sprite {
|
|
3
|
-
constructor(text?: string, textHeight?: number, color?:
|
|
4
|
+
constructor(text?: string, textHeight?: number, color?: ColorString);
|
|
4
5
|
_text: string;
|
|
5
6
|
_textHeight: number;
|
|
6
|
-
_color:
|
|
7
|
-
_backgroundColor:
|
|
7
|
+
_color: ColorString;
|
|
8
|
+
_backgroundColor: ColorString | false;
|
|
8
9
|
_padding: number;
|
|
9
10
|
_borderWidth: number;
|
|
10
11
|
_borderRadius: number;
|
|
@@ -19,10 +20,10 @@ export default class SpriteText2 extends Sprite {
|
|
|
19
20
|
set text(text: string);
|
|
20
21
|
get textHeight(): number;
|
|
21
22
|
set textHeight(textHeight: number);
|
|
22
|
-
get color():
|
|
23
|
-
set color(color:
|
|
24
|
-
get backgroundColor():
|
|
25
|
-
set backgroundColor(color:
|
|
23
|
+
get color(): ColorString;
|
|
24
|
+
set color(color: ColorString);
|
|
25
|
+
get backgroundColor(): ColorString | false;
|
|
26
|
+
set backgroundColor(color: ColorString | false);
|
|
26
27
|
get padding(): number;
|
|
27
28
|
set padding(padding: number);
|
|
28
29
|
get borderWidth(): number;
|
package/SpriteText2.js
CHANGED
|
@@ -68,6 +68,8 @@ export default class SpriteText2 extends Sprite {
|
|
|
68
68
|
_genCanvas() {
|
|
69
69
|
const canvas = this._canvas;
|
|
70
70
|
const ctx = canvas.getContext('2d');
|
|
71
|
+
if (!ctx)
|
|
72
|
+
return;
|
|
71
73
|
const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
|
|
72
74
|
const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
|
|
73
75
|
const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
|
|
@@ -79,8 +81,18 @@ export default class SpriteText2 extends Sprite {
|
|
|
79
81
|
ctx.font = font; // measure canvas with appropriate font
|
|
80
82
|
const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
|
|
81
83
|
const innerHeight = this.fontSize * lines.length;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
+
// 内容框尺寸(含 border + padding)
|
|
85
|
+
const contentW = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
|
|
86
|
+
const contentH = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
|
|
87
|
+
// 箭头区:底部预留 arrowZone 给向下箭头(raycast 用 uv.y 判定点击区)。
|
|
88
|
+
// 顶部同样预留等量空间,使内容框在画布中【垂直居中】,不再因底部箭头区而整体偏上、视觉不居中。
|
|
89
|
+
const arrowZone = 40;
|
|
90
|
+
const boxTop = arrowZone; // 内容框顶边(顶留白 = arrowZone)
|
|
91
|
+
const boxBottom = boxTop + contentH; // 内容框底边(箭头从此处向下延伸)
|
|
92
|
+
canvas.width = contentW;
|
|
93
|
+
canvas.height = boxBottom + arrowZone; // 顶 arrowZone + 内容 + 底 arrowZone
|
|
94
|
+
// 统一平移到内容框坐标系:之后所有绘制都以 (0, boxTop) 为内容框左上角。
|
|
95
|
+
ctx.translate(0, boxTop);
|
|
84
96
|
// paint border
|
|
85
97
|
if (this.borderWidth) {
|
|
86
98
|
ctx.strokeStyle = this.borderColor;
|
|
@@ -89,9 +101,9 @@ export default class SpriteText2 extends Sprite {
|
|
|
89
101
|
ctx.lineWidth = relBorder[0];
|
|
90
102
|
ctx.beginPath();
|
|
91
103
|
ctx.moveTo(hb, relBorderRadius[0]);
|
|
92
|
-
ctx.lineTo(hb,
|
|
93
|
-
ctx.moveTo(
|
|
94
|
-
ctx.lineTo(
|
|
104
|
+
ctx.lineTo(hb, contentH - relBorderRadius[3]);
|
|
105
|
+
ctx.moveTo(contentW - hb, relBorderRadius[1]);
|
|
106
|
+
ctx.lineTo(contentW - hb, contentH - relBorderRadius[2]);
|
|
95
107
|
ctx.stroke();
|
|
96
108
|
}
|
|
97
109
|
if (relBorder[1]) { // top + bottom borders
|
|
@@ -99,12 +111,12 @@ export default class SpriteText2 extends Sprite {
|
|
|
99
111
|
ctx.lineWidth = relBorder[1];
|
|
100
112
|
ctx.beginPath();
|
|
101
113
|
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
|
|
102
|
-
ctx.lineTo(
|
|
103
|
-
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]),
|
|
104
|
-
ctx.lineTo((
|
|
105
|
-
ctx.lineTo((
|
|
106
|
-
ctx.lineTo((
|
|
107
|
-
ctx.lineTo(
|
|
114
|
+
ctx.lineTo(contentW - Math.max(relBorder[0], relBorderRadius[1]), hb);
|
|
115
|
+
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), contentH - hb);
|
|
116
|
+
ctx.lineTo((contentW / 2 - 20), contentH - hb);
|
|
117
|
+
ctx.lineTo((contentW / 2), contentH + 20 - hb);
|
|
118
|
+
ctx.lineTo((contentW / 2 + 20), contentH - hb);
|
|
119
|
+
ctx.lineTo(contentW - Math.max(relBorder[0], relBorderRadius[2]), contentH - hb);
|
|
108
120
|
ctx.stroke();
|
|
109
121
|
}
|
|
110
122
|
if (this.borderRadius) { // strike rounded corners
|
|
@@ -114,10 +126,10 @@ export default class SpriteText2 extends Sprite {
|
|
|
114
126
|
ctx.beginPath();
|
|
115
127
|
[
|
|
116
128
|
!!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
|
|
117
|
-
!!relBorderRadius[1] && [
|
|
118
|
-
!!relBorderRadius[2] && [
|
|
119
|
-
!!relBorderRadius[3] && [relBorderRadius[3], hb,
|
|
120
|
-
].filter(d => d).forEach(([x0, x1, y0, y1]) => {
|
|
129
|
+
!!relBorderRadius[1] && [contentW - relBorderRadius[1], contentW - hb, hb, relBorderRadius[1]],
|
|
130
|
+
!!relBorderRadius[2] && [contentW - relBorderRadius[2], contentW - hb, contentH - hb, contentH - relBorderRadius[2]],
|
|
131
|
+
!!relBorderRadius[3] && [relBorderRadius[3], hb, contentH - hb, contentH - relBorderRadius[3]]
|
|
132
|
+
].filter((d) => !!d).forEach(([x0, x1, y0, y1]) => {
|
|
121
133
|
ctx.moveTo(x0, y0);
|
|
122
134
|
ctx.quadraticCurveTo(x1, y0, x1, y1);
|
|
123
135
|
});
|
|
@@ -128,41 +140,54 @@ export default class SpriteText2 extends Sprite {
|
|
|
128
140
|
if (this.backgroundColor) {
|
|
129
141
|
ctx.fillStyle = this.backgroundColor;
|
|
130
142
|
if (!this.borderRadius) {
|
|
131
|
-
ctx.fillRect(relBorder[0], relBorder[1],
|
|
143
|
+
ctx.fillRect(relBorder[0], relBorder[1], contentW - relBorder[0] * 2, contentH - relBorder[1] * 2);
|
|
132
144
|
}
|
|
133
145
|
else { // fill with rounded corners
|
|
134
146
|
ctx.beginPath();
|
|
135
147
|
ctx.moveTo(relBorder[0], relBorderRadius[0]);
|
|
136
148
|
ctx.quadraticCurveTo(relBorder[0], relBorder[1], relBorderRadius[0], relBorder[1]);
|
|
137
|
-
ctx.lineTo(
|
|
138
|
-
ctx.quadraticCurveTo(
|
|
139
|
-
ctx.lineTo(
|
|
140
|
-
ctx.quadraticCurveTo(
|
|
141
|
-
ctx.lineTo((
|
|
142
|
-
ctx.lineTo((
|
|
143
|
-
ctx.lineTo((
|
|
144
|
-
ctx.lineTo(relBorderRadius[3],
|
|
145
|
-
ctx.quadraticCurveTo(relBorder[0],
|
|
149
|
+
ctx.lineTo(contentW - relBorderRadius[1], relBorder[1]);
|
|
150
|
+
ctx.quadraticCurveTo(contentW - relBorder[0], relBorder[1], contentW - relBorder[0], relBorderRadius[1]);
|
|
151
|
+
ctx.lineTo(contentW - relBorder[0], contentH - relBorderRadius[2]);
|
|
152
|
+
ctx.quadraticCurveTo(contentW - relBorder[0], contentH - relBorder[1], contentW - relBorderRadius[2], contentH - relBorder[1]);
|
|
153
|
+
ctx.lineTo((contentW / 2 + 20), contentH - relBorder[1]);
|
|
154
|
+
ctx.lineTo((contentW / 2), contentH + 20 - relBorder[1]);
|
|
155
|
+
ctx.lineTo((contentW / 2 - 20), contentH - relBorder[1]);
|
|
156
|
+
ctx.lineTo(relBorderRadius[3], contentH - relBorder[1]);
|
|
157
|
+
ctx.quadraticCurveTo(relBorder[0], contentH - relBorder[1], relBorder[0], contentH - relBorderRadius[3]);
|
|
146
158
|
ctx.closePath();
|
|
147
159
|
ctx.fill();
|
|
148
160
|
}
|
|
149
161
|
}
|
|
162
|
+
// 文字区:在内容框内(已平移到 boxTop)再叠加 border + padding 偏移,使文字在框内居中。
|
|
150
163
|
ctx.translate(relBorder[0], relBorder[1]);
|
|
151
|
-
ctx.translate(relPadding[0], relPadding[
|
|
164
|
+
ctx.translate(relPadding[0], relPadding[1]);
|
|
152
165
|
// paint text
|
|
153
166
|
ctx.font = font; // Set font again after canvas is resized, as context properties are reset
|
|
154
167
|
ctx.fillStyle = this.color;
|
|
155
|
-
|
|
168
|
+
// 精确垂直居中:用 measureText 的 actualBoundingBox 实测字形上下沿,
|
|
169
|
+
// 不再依赖 textBaseline='middle'(middle 是 em 盒中线,与真实字形视觉中线常偏差,
|
|
170
|
+
// 导致整行文字偏上/偏下、看着不居中)。改为 alphabetic 基线 + 实测字形高度,
|
|
171
|
+
// 把每行字形放进其 fontSize 高的槽中心。
|
|
172
|
+
ctx.textAlign = 'center';
|
|
173
|
+
ctx.textBaseline = 'alphabetic';
|
|
156
174
|
const drawTextStroke = this.strokeWidth > 0;
|
|
157
175
|
if (drawTextStroke) {
|
|
158
176
|
ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
|
|
159
177
|
ctx.strokeStyle = this.strokeColor;
|
|
160
178
|
}
|
|
179
|
+
const slotH = this.fontSize; // 每行槽高
|
|
161
180
|
lines.forEach((line, index) => {
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
181
|
+
const metrics = ctx.measureText(line);
|
|
182
|
+
const ascent = metrics.actualBoundingBoxAscent; // 字形顶部到基线
|
|
183
|
+
const descent = metrics.actualBoundingBoxDescent; // 基线到字形底部
|
|
184
|
+
const glyphH = (ascent || 0) + (descent || 0);
|
|
185
|
+
const slotTop = index * slotH; // 本行槽顶(内容框坐标系,已平移)
|
|
186
|
+
// 让字形在槽中垂直居中:先平移到槽中线,再上移 ascent 回到基线
|
|
187
|
+
const baselineY = slotTop + (slotH - glyphH) / 2 + (ascent || slotH / 2);
|
|
188
|
+
const lineX = innerWidth / 2;
|
|
189
|
+
drawTextStroke && ctx.strokeText(line, lineX, baselineY);
|
|
190
|
+
ctx.fillText(line, lineX, baselineY);
|
|
166
191
|
});
|
|
167
192
|
// Inject canvas into sprite
|
|
168
193
|
if (this.material.map)
|
|
@@ -178,7 +203,7 @@ export default class SpriteText2 extends Sprite {
|
|
|
178
203
|
let intersect = [];
|
|
179
204
|
super.raycast(raycaster, intersect);
|
|
180
205
|
let inte = intersect[0];
|
|
181
|
-
if (inte && (inte.uv.y > 0.55)) {
|
|
206
|
+
if (inte && (inte.uv && inte.uv.y > 0.55)) {
|
|
182
207
|
intersects.push(inte);
|
|
183
208
|
}
|
|
184
209
|
}
|