canvas_geometry_text.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - text</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/Three.js"></script>
  18. <script src="js/Stats.js"></script>
  19. <!-- load the font file from canvas-text -->
  20. <script src="fonts/helvetiker_regular.typeface.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var text, parent;
  25. var targetRotation = 0;
  26. var targetRotationOnMouseDown = 0;
  27. var mouseX = 0;
  28. var mouseXOnMouseDown = 0;
  29. var windowHalfX = window.innerWidth / 2;
  30. var windowHalfY = window.innerHeight / 2;
  31. init();
  32. animate();
  33. function init() {
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. var info = document.createElement( 'div' );
  37. info.style.position = 'absolute';
  38. info.style.top = '10px';
  39. info.style.width = '100%';
  40. info.style.textAlign = 'center';
  41. info.innerHTML = 'Simple Dynamic 3D Text Example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin the text';
  42. container.appendChild( info );
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.set( 0, 150, 500 );
  45. scene = new THREE.Scene();
  46. scene.add( camera );
  47. // Get text from hash
  48. var theText = "Hello three.js! :)";
  49. var hash = document.location.hash.substr( 1 );
  50. if ( hash.length !== 0 ) {
  51. theText = hash;
  52. }
  53. var text3d = new THREE.TextGeometry( theText, {
  54. size: 80,
  55. height: 20,
  56. curveSegments: 2,
  57. font: "helvetiker"
  58. });
  59. text3d.computeBoundingBox();
  60. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  61. var textMaterial = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, overdraw: true } );
  62. text = new THREE.Mesh( text3d, textMaterial );
  63. text.doubleSided = false;
  64. text.position.x = centerOffset;
  65. text.position.y = 100;
  66. text.position.z = 0;
  67. text.rotation.x = 0;
  68. text.rotation.y = Math.PI * 2;
  69. parent = new THREE.Object3D();
  70. parent.add( text );
  71. scene.add( parent );
  72. renderer = new THREE.CanvasRenderer();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. stats.domElement.style.position = 'absolute';
  77. stats.domElement.style.top = '0px';
  78. container.appendChild( stats.domElement );
  79. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  80. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  81. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  82. //
  83. window.addEventListener( 'resize', onWindowResize, false );
  84. }
  85. function onWindowResize() {
  86. windowHalfX = window.innerWidth / 2;
  87. windowHalfY = window.innerHeight / 2;
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. //
  93. function onDocumentMouseDown( event ) {
  94. event.preventDefault();
  95. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  96. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  97. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  98. mouseXOnMouseDown = event.clientX - windowHalfX;
  99. targetRotationOnMouseDown = targetRotation;
  100. }
  101. function onDocumentMouseMove( event ) {
  102. mouseX = event.clientX - windowHalfX;
  103. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  104. }
  105. function onDocumentMouseUp( event ) {
  106. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  107. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  108. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  109. }
  110. function onDocumentMouseOut( event ) {
  111. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  112. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  113. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  114. }
  115. function onDocumentTouchStart( event ) {
  116. if ( event.touches.length == 1 ) {
  117. event.preventDefault();
  118. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  119. targetRotationOnMouseDown = targetRotation;
  120. }
  121. }
  122. function onDocumentTouchMove( event ) {
  123. if ( event.touches.length == 1 ) {
  124. event.preventDefault();
  125. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  126. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  127. }
  128. }
  129. //
  130. function animate() {
  131. requestAnimationFrame( animate );
  132. render();
  133. stats.update();
  134. }
  135. function render() {
  136. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>