canvas-textured-labels-scale-to-fit.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Canvas Textured Labels Scale to Fit</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="module">
  24. import * as THREE from '../../build/three.module.js';
  25. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas});
  29. const fov = 75;
  30. const aspect = 2; // the canvas default
  31. const near = 0.1;
  32. const far = 50;
  33. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  34. camera.position.set(0, 2, 5);
  35. const controls = new OrbitControls(camera, canvas);
  36. controls.target.set(0, 2, 0);
  37. controls.update();
  38. const scene = new THREE.Scene();
  39. scene.background = new THREE.Color('white');
  40. function addLight(position) {
  41. const color = 0xFFFFFF;
  42. const intensity = 1;
  43. const light = new THREE.DirectionalLight(color, intensity);
  44. light.position.set(...position);
  45. scene.add(light);
  46. scene.add(light.target);
  47. }
  48. addLight([-3, 1, 1]);
  49. addLight([ 2, 1, .5]);
  50. const bodyRadiusTop = .4;
  51. const bodyRadiusBottom = .2;
  52. const bodyHeight = 2;
  53. const bodyRadialSegments = 6;
  54. const bodyGeometry = new THREE.CylinderGeometry(
  55. bodyRadiusTop, bodyRadiusBottom, bodyHeight, bodyRadialSegments);
  56. const headRadius = bodyRadiusTop * 0.8;
  57. const headLonSegments = 12;
  58. const headLatSegments = 5;
  59. const headGeometry = new THREE.SphereGeometry(
  60. headRadius, headLonSegments, headLatSegments);
  61. const labelGeometry = new THREE.PlaneGeometry(1, 1);
  62. function makeLabelCanvas(baseWidth, size, name) {
  63. const borderSize = 2;
  64. const ctx = document.createElement('canvas').getContext('2d');
  65. const font = `${size}px bold sans-serif`;
  66. ctx.font = font;
  67. // measure how long the name will be
  68. const textWidth = ctx.measureText(name).width;
  69. const doubleBorderSize = borderSize * 2;
  70. const width = baseWidth + doubleBorderSize;
  71. const height = size + doubleBorderSize;
  72. ctx.canvas.width = width;
  73. ctx.canvas.height = height;
  74. // need to set font again after resizing canvas
  75. ctx.font = font;
  76. ctx.textBaseline = 'middle';
  77. ctx.textAlign = 'center';
  78. ctx.fillStyle = 'blue';
  79. ctx.fillRect(0, 0, width, height);
  80. // scale to fit but don't stretch
  81. const scaleFactor = Math.min(1, baseWidth / textWidth);
  82. ctx.translate(width / 2, height / 2);
  83. ctx.scale(scaleFactor, 1);
  84. ctx.fillStyle = 'white';
  85. ctx.fillText(name, 0, 0);
  86. return ctx.canvas;
  87. }
  88. function makePerson(x, labelWidth, size, name, color) {
  89. const canvas = makeLabelCanvas(labelWidth, size, name);
  90. const texture = new THREE.CanvasTexture(canvas);
  91. // because our canvas is likely not a power of 2
  92. // in both dimensions set the filtering appropriately.
  93. texture.minFilter = THREE.LinearFilter;
  94. texture.wrapS = THREE.ClampToEdgeWrapping;
  95. texture.wrapT = THREE.ClampToEdgeWrapping;
  96. const labelMaterial = new THREE.MeshBasicMaterial({
  97. map: texture,
  98. side: THREE.DoubleSide,
  99. transparent: true,
  100. });
  101. const bodyMaterial = new THREE.MeshPhongMaterial({
  102. color,
  103. flatShading: true,
  104. });
  105. const root = new THREE.Object3D();
  106. root.position.x = x;
  107. const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
  108. root.add(body);
  109. body.position.y = bodyHeight / 2;
  110. const head = new THREE.Mesh(headGeometry, bodyMaterial);
  111. root.add(head);
  112. head.position.y = bodyHeight + headRadius * 1.1;
  113. const label = new THREE.Mesh(labelGeometry, labelMaterial);
  114. root.add(label);
  115. label.position.y = bodyHeight * 4 / 5;
  116. label.position.z = bodyRadiusTop * 1.01;
  117. // if units are meters then 0.01 here makes size
  118. // of the label into centimeters.
  119. const labelBaseScale = 0.01;
  120. label.scale.x = canvas.width * labelBaseScale;
  121. label.scale.y = canvas.height * labelBaseScale;
  122. scene.add(root);
  123. return root;
  124. }
  125. makePerson(-3, 150, 32, 'Purple People Eater', 'purple');
  126. makePerson(-0, 150, 32, 'Green Machine', 'green');
  127. makePerson(+3, 150, 32, 'Red Menace', 'red');
  128. function resizeRendererToDisplaySize(renderer) {
  129. const canvas = renderer.domElement;
  130. const width = canvas.clientWidth;
  131. const height = canvas.clientHeight;
  132. const needResize = canvas.width !== width || canvas.height !== height;
  133. if (needResize) {
  134. renderer.setSize(width, height, false);
  135. }
  136. return needResize;
  137. }
  138. function render() {
  139. if (resizeRendererToDisplaySize(renderer)) {
  140. const canvas = renderer.domElement;
  141. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  142. camera.updateProjectionMatrix();
  143. }
  144. renderer.render(scene, camera);
  145. requestAnimationFrame(render);
  146. }
  147. requestAnimationFrame(render);
  148. }
  149. main();
  150. </script>
  151. </html>