drawMeasuresSprite.js
3.67 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 = 20) => {
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'] : 'rgb(158,165,255,0)';
const backgroundColor = parameters.hasOwnProperty('backgroundColor')
? parameters['backgroundColor']
: { r: 255, g: 255, b: 255, a: 1.0 };
const textColor = parameters.hasOwnProperty('textColor') ? parameters['textColor'] : { r: 0, g: 0, b: 0, a: 1.0 };
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const textWidth = fontsize * 2;
// get size data (height depends only on font size)
// background color
const canvasHeight = 1400;
const canvasWidth = fontsize * ((message.length / 6) * 4.2);
canvas.width = canvasWidth;
canvas.height = canvasHeight;
const x = borderThickness / 2;
const y = borderThickness / 2;
const w = textWidth * 10 + borderThickness;
const h = textWidth * 0.7 + borderThickness;
context.fillStyle =
'rgba(' + backgroundColor.r + ',' + backgroundColor.g + ',' + backgroundColor.b + ',' + backgroundColor.a + ')';
context.lineWidth = 0.0;
context.lineWidth = borderThickness;
//createDiamond(context, borderColor, x, y, w, h);
roundRect(context, borderColor, x, y, w, h);
// text color
context.fillStyle = 'rgba(' + textColor.r + ',' + textColor.g + ',' + textColor.b + ',' + textColor.a + ')';
context.font = `${fontsize}px ${fontface}`;
const marginLeft = canvasWidth * 0.07;
const marginTop = -20;
context.fillText(message, marginLeft, (y + y + h + textWidth / 2) / 2 + marginTop);
// canvas contents will be used for a texture
const texture = new Texture(canvas);
texture.needsUpdate = true;
return { texture, canvas };
};
export const drawMeasuresSprite = (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 = 5;
const spriteScale = 2.5;
sprite.scale.set(canvas.width / canvas.height, 1, 1).multiplyScalar(spriteScale);
sprite.scale.z = 1;
return sprite;
};