TextLabel.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import * as THREE from "three";
  2. export default class TextLabel extends THREE.Mesh {
  3. constructor(text, outline) {
  4. let res = 5;
  5. const width = 150 * res;
  6. const height = 15 * res;
  7. var canvas = document.createElement("canvas");
  8. canvas.width = width;
  9. canvas.height = height;
  10. let fontFamily = "Arial";
  11. let fontSize = 7 * res;
  12. let offsetX = 75 * res;
  13. let offsetY = 10 * res;
  14. var context = canvas.getContext("2d");
  15. context.fillStyle = "#ffffff";
  16. context.rect(0, 0, width, height);
  17. context.fill();
  18. context.font = "normal " + fontSize + "px " + fontFamily;
  19. context.fillStyle = "#000000";
  20. context.textAlign = "center";
  21. context.fillText(text, offsetX, offsetY);
  22. const canvas_map = new THREE.Texture(canvas);
  23. canvas_map.colorSpace = THREE.SRGBColorSpace;
  24. canvas_map.needsUpdate = true;
  25. canvas_map.anisotropy = 4;
  26. const g = new THREE.PlaneGeometry(1.5, 0.15);
  27. g.rotateX(-Math.PI / 2);
  28. const m = new THREE.MeshBasicMaterial({
  29. map: canvas_map,
  30. });
  31. super(g, m);
  32. const edges = new THREE.EdgesGeometry(g);
  33. const line = new THREE.LineSegments(
  34. edges,
  35. new THREE.LineBasicMaterial({ color: 0xcccccc })
  36. );
  37. line.position.y += 0.5;
  38. this.add(line);
  39. this.name = "textlabel_" + text;
  40. }
  41. }