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

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