UNPKG

awv3

Version:
40 lines (33 loc) 1.04 kB
import * as THREE from 'three'; var createCaption = function createCaption(text, color, scale, font, fontSizePx) { if (color === void 0) { color = 'lightgrey'; } if (scale === void 0) { scale = 1; } if (font === void 0) { font = 'Arial'; } if (fontSizePx === void 0) { fontSizePx = 260; } var fontFace = font; var canvas = document.createElement('canvas'); canvas.width = canvas.height = 1024; // should be always a power of two (2^x) (WebGL performance) var context = canvas.getContext('2d'); context.font = fontSizePx + 'px ' + fontFace; context.fillStyle = color; var w = context.measureText(text).width; var h = fontSizePx; context.fillText(text, (canvas.width - w) / 2, (canvas.height - h) / 2); var texture = new THREE.Texture(canvas); texture.needsUpdate = true; var spriteMaterial = new THREE.SpriteMaterial({ map: texture }); var sprite = new THREE.Sprite(spriteMaterial); sprite.scale.set(20 * scale, 20 * scale, 1); return sprite; }; export { createCaption };