drawTextSprite.js
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { SpriteMaterial, Sprite, Texture } from 'three';
const createDiamond = (ctx, borderColor, x, y, w, h) => {
ctx.beginPath();
ctx.moveTo(x, (y + y + h) / 2);
ctx.lineTo((x + x + w) / 2, y);
ctx.lineTo(x + w, (y + y + h) / 2);
ctx.lineTo((x + x + w) / 2, y + h);
ctx.lineTo(x, (y + y + h) / 2);
ctx.closePath();
//ctx.shadowColor = 'rgba(100,100,100,0.3)';
//ctx.shadowOffsetX = 1.1;
//ctx.shadowOffsetY = 1.1;
//ctx.shadowBlur = 6;
ctx.strokeStyle = borderColor;
ctx.fill();
ctx.stroke();
};
const roundRect = (ctx, borderColor, x, y, w, h, r = 3) => {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
ctx.strokeStyle = borderColor;
ctx.fill();
ctx.stroke();
};
export const createTexture = (message, parameters) => {
if (parameters === undefined) parameters = {};
const fontface = parameters.hasOwnProperty('fontface')
? parameters['fontface']
: 'Inter, Arial, Helvetica, sans-serif';
const fontsize = parameters.hasOwnProperty('fontsize') ? parameters['fontsize'] : 16;
const borderThickness = parameters.hasOwnProperty('borderThickness') ? parameters['borderThickness'] : 0.4;
const borderColor = parameters.hasOwnProperty('borderColor')
? parameters['borderColor']
: { r: 0, g: 0, b: 0, a: 1.0 };
const backgroundColor = parameters.hasOwnProperty('backgroundColor')
? parameters['backgroundColor']
: { r: 0, g: 0, b: 255, a: 1.0 };
const textColor = parameters.hasOwnProperty('textColor') ? parameters['textColor'] : { r: 0, g: 0, b: 0, a: 1.0 };
const yOffset = parameters.hasOwnProperty('yOffset') ? parameters['yOffset'] : 70;
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
context.font = 'Bold ' + fontsize + 'px ' + fontface;
const metrics = context.measureText(message);
const textWidth = metrics.width;
context.fillStyle =
'rgba(' + backgroundColor.r + ',' + backgroundColor.g + ',' + backgroundColor.b + ',' + backgroundColor.a + ')';
context.strokeStyle =
'rgba(' + borderColor.r + ',' + borderColor.g + ',' + borderColor.b + ',' + borderColor.a + ')';
context.fillStyle = 'rgba(' + textColor.r + ', ' + textColor.g + ', ' + textColor.b + ', 1.0)';
context.fillText(message, borderThickness + 70, fontsize + borderThickness + yOffset);
// canvas contents will be used for a texture
const texture = new Texture(canvas);
texture.needsUpdate = true;
return { texture, canvas };
};
export const drawTextSprite = (message, parameters) => {
const { texture, canvas } = createTexture(message, parameters);
const spriteMaterial = new SpriteMaterial({ map: texture, depthTest: false, transparent: true });
const sprite = new Sprite(spriteMaterial);
Sprite.renderOrder = 4;
const spriteScale = 4;
sprite.scale.set(canvas.width / canvas.height, 1, 1).multiplyScalar(spriteScale);
sprite.scale.z = 1;
return sprite;
};