three-base 1.0.82 → 1.0.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,142 +4,64 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2023-04-15 12:38:09
6
6
  */
7
- var __extends = (this && this.__extends) || (function () {
8
- var extendStatics = function (d, b) {
9
- extendStatics = Object.setPrototypeOf ||
10
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12
- return extendStatics(d, b);
13
- };
14
- return function (d, b) {
15
- if (typeof b !== "function" && b !== null)
16
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17
- extendStatics(d, b);
18
- function __() { this.constructor = d; }
19
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
- };
21
- })();
22
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
- return new (P || (P = Promise))(function (resolve, reject) {
25
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
- step((generator = generator.apply(thisArg, _arguments || [])).next());
29
- });
30
- };
31
- var __generator = (this && this.__generator) || function (thisArg, body) {
32
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
33
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
34
- function verb(n) { return function (v) { return step([n, v]); }; }
35
- function step(op) {
36
- if (f) throw new TypeError("Generator is already executing.");
37
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
38
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
39
- if (y = 0, t) op = [op[0] & 2, t.value];
40
- switch (op[0]) {
41
- case 0: case 1: t = op; break;
42
- case 4: _.label++; return { value: op[1], done: false };
43
- case 5: _.label++; y = op[1]; op = [0]; continue;
44
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
45
- default:
46
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
47
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
48
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
49
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
50
- if (t[2]) _.ops.pop();
51
- _.trys.pop(); continue;
52
- }
53
- op = body.call(thisArg, _);
54
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
55
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
56
- }
57
- };
58
7
  import html2canvas from "html2canvas";
59
8
  import { CanvasTexture, DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector2 } from "three";
60
9
  import { degToRad } from "three/src/math/MathUtils";
61
10
  import threeLoader from "./threeLoader";
62
11
  // html 文本面板
63
- var PlaneWithHtml = /** @class */ (function (_super) {
64
- __extends(PlaneWithHtml, _super);
65
- function PlaneWithHtml(html, backgroundColor) {
66
- if (html === void 0) { html = ""; }
67
- if (backgroundColor === void 0) { backgroundColor = "#00000000"; }
68
- var _this = _super.call(this) || this;
69
- _this.visible = false;
70
- _this.size = new Vector2(10, 10);
71
- _this.geometry = (function () {
72
- var geometry = new PlaneGeometry(10, 10, 1, 1);
73
- geometry.rotateX(degToRad(-90));
74
- return geometry;
75
- })();
76
- _this.material = new MeshBasicMaterial({
77
- side: DoubleSide,
78
- transparent: true
79
- });
80
- _this.div = (function () {
81
- var div = document.createElement("div");
82
- div.style.position = "fixed";
83
- div.style.top = "100%";
84
- div.style.left = "100%";
85
- div.style.pointerEvents = "none";
86
- div.style.userSelect = "none";
87
- div.style.lineHeight = "1";
88
- div.style.whiteSpace = "nowrap";
89
- return div;
90
- })();
91
- _this.setHtml(html, backgroundColor);
92
- return _this;
12
+ export default class PlaneWithHtml extends Mesh {
13
+ constructor(html = "", backgroundColor = "#00000000") {
14
+ super();
15
+ this.setHtml(html, backgroundColor);
93
16
  }
17
+ visible = false;
18
+ size = new Vector2(10, 10);
19
+ geometry = (() => {
20
+ const geometry = new PlaneGeometry(10, 10, 1, 1);
21
+ geometry.rotateX(degToRad(-90));
22
+ return geometry;
23
+ })();
24
+ material = new MeshBasicMaterial({
25
+ side: DoubleSide,
26
+ transparent: true
27
+ });
28
+ div = (() => {
29
+ const div = document.createElement("div");
30
+ div.style.position = "fixed";
31
+ div.style.top = "100%";
32
+ div.style.left = "100%";
33
+ div.style.pointerEvents = "none";
34
+ div.style.userSelect = "none";
35
+ div.style.lineHeight = "1";
36
+ div.style.whiteSpace = "nowrap";
37
+ return div;
38
+ })();
94
39
  // 设置网格尺寸
95
- PlaneWithHtml.prototype.setGeoSize = function (width, height) {
40
+ setGeoSize(width, height) {
96
41
  this.size.set(width, height);
97
42
  this.geometry.dispose();
98
- var geometry = new PlaneGeometry(width, height, 1, 1);
43
+ const geometry = new PlaneGeometry(width, height, 1, 1);
99
44
  geometry.rotateX(degToRad(-90));
100
45
  this.geometry = geometry;
101
- };
102
- PlaneWithHtml.prototype.setHtml = function (html, backgroundColor) {
103
- if (html === void 0) { html = ""; }
104
- if (backgroundColor === void 0) { backgroundColor = "#00000000"; }
105
- return __awaiter(this, void 0, void 0, function () {
106
- var _this = this;
107
- return __generator(this, function (_a) {
108
- switch (_a.label) {
109
- case 0:
110
- this.div.innerHTML = html;
111
- document.body.appendChild(this.div);
112
- return [4 /*yield*/, new Promise(function (resolve, reject) {
113
- html2canvas(_this.div, {
114
- backgroundColor: backgroundColor,
115
- removeContainer: true
116
- }).then(function (canvas) { return __awaiter(_this, void 0, void 0, function () {
117
- return __generator(this, function (_a) {
118
- switch (_a.label) {
119
- case 0:
120
- this.setGeoSize(canvas.width, canvas.height);
121
- this.material.map = new CanvasTexture(canvas);
122
- this.material.map.anisotropy = threeLoader.anisotropy;
123
- this.material.needsUpdate = true;
124
- this.visible = true;
125
- return [4 /*yield*/, new Promise(function (resolve) { return requestAnimationFrame(resolve); })];
126
- case 1:
127
- _a.sent();
128
- this.div.remove();
129
- resolve();
130
- return [2 /*return*/];
131
- }
132
- });
133
- }); });
134
- })];
135
- case 1:
136
- _a.sent();
137
- this.dispatchEvent({ type: "loaded" });
138
- return [2 /*return*/];
139
- }
46
+ }
47
+ async setHtml(html = "", backgroundColor = "#00000000") {
48
+ this.div.innerHTML = html;
49
+ document.body.appendChild(this.div);
50
+ await new Promise((resolve, reject) => {
51
+ html2canvas(this.div, {
52
+ backgroundColor,
53
+ removeContainer: true
54
+ }).then(async (canvas) => {
55
+ this.setGeoSize(canvas.width, canvas.height);
56
+ this.material.map = new CanvasTexture(canvas);
57
+ this.material.map.anisotropy = threeLoader.anisotropy;
58
+ this.material.needsUpdate = true;
59
+ this.visible = true;
60
+ await new Promise(resolve => requestAnimationFrame(resolve));
61
+ this.div.remove();
62
+ resolve();
140
63
  });
141
64
  });
142
- };
143
- return PlaneWithHtml;
144
- }(Mesh));
145
- export default PlaneWithHtml;
65
+ this.dispatchEvent({ type: "loaded" });
66
+ }
67
+ }
@@ -1,5 +1,5 @@
1
1
  import { Sprite, PointLight as THREEPointLight } from "three";
2
- import threeSpritetext from "three-spritetext";
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: threeSpritetext;
12
+ label: SpriteText;
13
13
  }
@@ -2,82 +2,62 @@
2
2
  * @Author: xiaosihan
3
3
  * @Date: 2022-05-18 16:02:55
4
4
  * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-21 17:22:59
5
+ * @Last Modified time: 2023-05-03 02:55:28
6
6
  */
7
- var __extends = (this && this.__extends) || (function () {
8
- var extendStatics = function (d, b) {
9
- extendStatics = Object.setPrototypeOf ||
10
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12
- return extendStatics(d, b);
13
- };
14
- return function (d, b) {
15
- if (typeof b !== "function" && b !== null)
16
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17
- extendStatics(d, b);
18
- function __() { this.constructor = d; }
19
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
- };
21
- })();
22
7
  import { Sprite, PointLight as THREEPointLight } from "three";
23
- import threeSpritetext from "three-spritetext";
24
8
  import threeLoader from "../threeLoader";
25
9
  import threeUtils from "../threeUtils";
26
10
  import pointLight_mapPNG from "./pointLight_map.png";
11
+ import SpriteText from "../SpriteText";
27
12
  /**
28
13
  * 点光源
29
14
  * @export
30
15
  * @class PointLight
31
16
  * @extends {THREEPointLight}
32
17
  */
33
- var PointLight = /** @class */ (function (_super) {
34
- __extends(PointLight, _super);
35
- function PointLight(color, intensity, distance, decay) {
36
- if (color === void 0) { color = "#fff"; }
37
- var _this = _super.call(this, color, intensity, distance, decay) || this;
38
- // 灯光辅助器
39
- _this.pointLinghtHelper = threeUtils.isDev && (function () {
40
- var pointLinghtHelper = new Sprite();
41
- pointLinghtHelper.scale.set(0.05, 0.05, 0.05);
42
- pointLinghtHelper.material.color = _this.color;
43
- pointLinghtHelper.material.map = threeLoader.getTexture(pointLight_mapPNG);
44
- pointLinghtHelper.material.sizeAttenuation = false;
45
- pointLinghtHelper.userData = {
46
- cursor: "pointer"
47
- };
48
- pointLinghtHelper.addEventListener("mouseenter", function () {
49
- if (_this.label) {
50
- _this.label.visible = true;
51
- }
52
- });
53
- pointLinghtHelper.addEventListener("mouseleave", function () {
54
- if (_this.label) {
55
- _this.label.visible = false;
56
- }
57
- });
58
- _this.add(pointLinghtHelper);
59
- return pointLinghtHelper;
60
- })();
61
- _this.label = threeUtils.isDev && (function () {
62
- var label = new threeSpritetext("点光源\n\n\n");
63
- label.renderOrder = Infinity;
64
- // label.backgroundColor = "rgba(0, 0, 0, 0.5)";
65
- label.padding = 4;
66
- label.strokeColor = "#333";
67
- label.strokeWidth = 2;
68
- // label.borderRadius = 4;
69
- // label.borderWidth = 1;
70
- label.position.set(0, 0, 0);
71
- label.visible = false;
72
- label.material.color = _this.color;
73
- label.material.depthTest = false;
74
- label.material.sizeAttenuation = false;
75
- label.scale.multiplyScalar(0.002);
76
- _this.add(label);
77
- return label;
78
- })();
79
- return _this;
18
+ export default class PointLight extends THREEPointLight {
19
+ constructor(color = "#fff", intensity, distance, decay) {
20
+ super(color, intensity, distance, decay);
80
21
  }
81
- return PointLight;
82
- }(THREEPointLight));
83
- export default PointLight;
22
+ // 灯光辅助器
23
+ pointLinghtHelper = threeUtils.isDev && (() => {
24
+ const pointLinghtHelper = new Sprite();
25
+ pointLinghtHelper.scale.set(0.05, 0.05, 0.05);
26
+ pointLinghtHelper.material.color = this.color;
27
+ pointLinghtHelper.material.map = threeLoader.getTexture(pointLight_mapPNG);
28
+ pointLinghtHelper.material.sizeAttenuation = false;
29
+ pointLinghtHelper.userData = {
30
+ cursor: "pointer"
31
+ };
32
+ pointLinghtHelper.addEventListener("mouseenter", () => {
33
+ if (this.label) {
34
+ this.label.visible = true;
35
+ }
36
+ });
37
+ pointLinghtHelper.addEventListener("mouseleave", () => {
38
+ if (this.label) {
39
+ this.label.visible = false;
40
+ }
41
+ });
42
+ this.add(pointLinghtHelper);
43
+ return pointLinghtHelper;
44
+ })();
45
+ label = threeUtils.isDev && (() => {
46
+ const label = new SpriteText("点光源\n\n\n");
47
+ label.renderOrder = Infinity;
48
+ // label.backgroundColor = "rgba(0, 0, 0, 0.5)";
49
+ label.padding = 4;
50
+ label.strokeColor = "#333";
51
+ label.strokeWidth = 2;
52
+ // label.borderRadius = 4;
53
+ // label.borderWidth = 1;
54
+ label.position.set(0, 0, 0);
55
+ label.visible = false;
56
+ label.material.color = this.color;
57
+ label.material.depthTest = false;
58
+ label.material.sizeAttenuation = false;
59
+ label.scale.multiplyScalar(0.002);
60
+ this.add(label);
61
+ return label;
62
+ })();
63
+ }
@@ -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-04-21 17:22:59
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 threeSpritetext("点光源\n\n\n");
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;
package/lib/Sky/Sky.js CHANGED
@@ -4,44 +4,25 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2022-12-27 09:54:23
6
6
  */
7
- var __extends = (this && this.__extends) || (function () {
8
- var extendStatics = function (d, b) {
9
- extendStatics = Object.setPrototypeOf ||
10
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12
- return extendStatics(d, b);
13
- };
14
- return function (d, b) {
15
- if (typeof b !== "function" && b !== null)
16
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17
- extendStatics(d, b);
18
- function __() { this.constructor = d; }
19
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
- };
21
- })();
22
7
  import { DoubleSide, Mesh, MeshBasicMaterial, SphereGeometry } from "three";
23
8
  import threeLoader from "../threeLoader";
24
9
  import blueskyJPG from "./blueSky.jpg";
25
10
  /**
26
11
  * 天空球
27
12
  */
28
- var Sky = /** @class */ (function (_super) {
29
- __extends(Sky, _super);
30
- function Sky() {
31
- var _this = _super.call(this, Sky.geometry) || this;
32
- _this.material = new MeshBasicMaterial({
33
- side: DoubleSide,
34
- map: threeLoader.getTexture(blueskyJPG)
35
- });
36
- return _this;
13
+ export default class Sky extends Mesh {
14
+ constructor() {
15
+ super(Sky.geometry);
37
16
  }
17
+ static geometry = new SphereGeometry(500000, 100, 100);
18
+ material = new MeshBasicMaterial({
19
+ side: DoubleSide,
20
+ map: threeLoader.getTexture(blueskyJPG)
21
+ });
38
22
  /**
39
23
  *设置贴图
40
24
  */
41
- Sky.prototype.setMap = function (src) {
25
+ setMap(src) {
42
26
  this.material.map = threeLoader.getTexture(src);
43
- };
44
- Sky.geometry = new SphereGeometry(500000, 100, 100);
45
- return Sky;
46
- }(Mesh));
47
- export default Sky;
27
+ }
28
+ }
@@ -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
+ }