@mirage-engine/painter 0.0.1
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/package.json +12 -0
- package/src/TextGenerator.ts +85 -0
- package/src/index.ts +1 -0
package/package.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import { TextStyles } from "../../mirage-engine/src/types";
|
|
3
|
+
|
|
4
|
+
function wrapText(
|
|
5
|
+
ctx: CanvasRenderingContext2D,
|
|
6
|
+
text: string,
|
|
7
|
+
maxWidth: number
|
|
8
|
+
): string[] {
|
|
9
|
+
const forcedLines = text.split("\n");
|
|
10
|
+
const resultLines: string[] = [];
|
|
11
|
+
|
|
12
|
+
forcedLines.forEach((line) => {
|
|
13
|
+
const words = line.split(" ");
|
|
14
|
+
let currentLine = words[0];
|
|
15
|
+
|
|
16
|
+
for (let i = 1; i < words.length; i++) {
|
|
17
|
+
const word = words[i];
|
|
18
|
+
const width = ctx.measureText(currentLine + " " + word).width;
|
|
19
|
+
|
|
20
|
+
if (width < maxWidth) {
|
|
21
|
+
currentLine += " " + word;
|
|
22
|
+
} else {
|
|
23
|
+
resultLines.push(currentLine);
|
|
24
|
+
currentLine = word;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
resultLines.push(currentLine);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return resultLines;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createTextTexture(
|
|
34
|
+
text: string,
|
|
35
|
+
styles: TextStyles,
|
|
36
|
+
rectWidth: number,
|
|
37
|
+
rectHeight: number,
|
|
38
|
+
qualityFactor: number = 2
|
|
39
|
+
): THREE.CanvasTexture {
|
|
40
|
+
const canvas = document.createElement("canvas");
|
|
41
|
+
const ctx = canvas.getContext("2d");
|
|
42
|
+
|
|
43
|
+
if (!ctx) {
|
|
44
|
+
throw new Error("[Mirage] Failed to create canvas context");
|
|
45
|
+
}
|
|
46
|
+
// text Quality
|
|
47
|
+
|
|
48
|
+
const pixelRatio = window.devicePixelRatio || 1;
|
|
49
|
+
const scale = pixelRatio * qualityFactor;
|
|
50
|
+
|
|
51
|
+
canvas.width = rectWidth * scale;
|
|
52
|
+
canvas.height = rectHeight * scale;
|
|
53
|
+
ctx.scale(scale, scale);
|
|
54
|
+
|
|
55
|
+
ctx.font = styles.font;
|
|
56
|
+
ctx.fillStyle = styles.color;
|
|
57
|
+
ctx.textBaseline = "top";
|
|
58
|
+
|
|
59
|
+
ctx.globalAlpha = 1;
|
|
60
|
+
|
|
61
|
+
const lines = wrapText(ctx, text, rectWidth);
|
|
62
|
+
const lineHeight = styles.lineHeight;
|
|
63
|
+
|
|
64
|
+
lines.forEach((line, index) => {
|
|
65
|
+
const y = index * lineHeight + 2;
|
|
66
|
+
|
|
67
|
+
let x = 0;
|
|
68
|
+
if (styles.textAlign === "center") {
|
|
69
|
+
x = rectWidth / 2;
|
|
70
|
+
} else if (styles.textAlign === "right") {
|
|
71
|
+
x = rectWidth;
|
|
72
|
+
}
|
|
73
|
+
ctx.textAlign = styles.textAlign as CanvasTextAlign;
|
|
74
|
+
|
|
75
|
+
ctx.fillText(line, x, y);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const texture = new THREE.CanvasTexture(canvas);
|
|
79
|
+
texture.colorSpace = THREE.SRGBColorSpace;
|
|
80
|
+
texture.minFilter = THREE.LinearFilter;
|
|
81
|
+
texture.magFilter = THREE.LinearFilter;
|
|
82
|
+
texture.needsUpdate = true;
|
|
83
|
+
|
|
84
|
+
return texture;
|
|
85
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './TextGenerator';
|