canvas_geometry_text.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas/webgl - geometry - text</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  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 type="text/javascript" src="../build/Three.js"></script>
  18. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  19. <script type="text/javascript" src="js/Stats.js"></script>
  20. <!--
  21. <script type="text/javascript" src="../src/extras/geometries/Text.js"></script>
  22. <script type="text/javascript" src="helvetiker-normal-normal.js"></script>
  23. -->
  24. <script type="text/javascript" src="http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/helvetiker-normal-normal.js"></script>
  25. <script type="text/javascript">
  26. var container, stats;
  27. var camera, scene, renderer;
  28. var text, plane;
  29. var targetRotation = 0;
  30. var targetRotationOnMouseDown = 0;
  31. var mouseX = 0;
  32. var mouseXOnMouseDown = 0;
  33. var windowHalfX = window.innerWidth / 2;
  34. var windowHalfY = window.innerHeight / 2;
  35. init();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. var info = document.createElement( 'div' );
  41. info.style.position = 'absolute';
  42. info.style.top = '10px';
  43. info.style.width = '100%';
  44. info.style.textAlign = 'center';
  45. info.innerHTML = 'Drag to spin the text';
  46. container.appendChild( info );
  47. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  48. camera.position.y = 150;
  49. camera.position.z = 500;
  50. camera.target.position.y = 150;
  51. scene = new THREE.Scene();
  52. // Get text from hash
  53. var theText = "Hello three.js! :)";
  54. var hash = document.location.hash.substr(1);
  55. if (hash.length === 0) {
  56. } else {
  57. theText = hash;
  58. }
  59. var text3d = new THREE.Text(theText, 80, 40, 4, "");
  60. //try MeshPhongMaterial MeshLambertMaterial MeshPhongMaterial MeshNormalMaterial
  61. var textMaterial = new THREE.MeshBasicMaterial( { color: 0.5 * 0xffffff, wireframe:false });
  62. text = new THREE.Mesh( text3d, textMaterial);
  63. text.doubleSided = false;
  64. text.position.y = 0;
  65. text.position.y = 0;
  66. text.position.z = 0;
  67. text.rotation.x = 0*Math.PI;
  68. text.rotation.y = Math.PI*2;
  69. text.overdraw = true;
  70. scene.addObject( text );
  71. // Plane
  72. plane = new THREE.Mesh( new THREE.Plane( 800, 800 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0, wireframe:true}) );
  73. plane.rotation.x = - 90 * ( Math.PI / 180 );
  74. plane.position.x = 0;
  75. plane.overdraw = true;
  76. scene.addObject( plane );
  77. renderer = new THREE.CanvasRenderer();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. stats.domElement.style.position = 'absolute';
  82. stats.domElement.style.top = '0px';
  83. container.appendChild( stats.domElement );
  84. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  85. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  86. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  87. }
  88. //
  89. function onDocumentMouseDown( event ) {
  90. event.preventDefault();
  91. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  92. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  93. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  94. mouseXOnMouseDown = event.clientX - windowHalfX;
  95. targetRotationOnMouseDown = targetRotation;
  96. }
  97. function onDocumentMouseMove( event ) {
  98. mouseX = event.clientX - windowHalfX;
  99. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  100. }
  101. function onDocumentMouseUp( event ) {
  102. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  103. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  104. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  105. }
  106. function onDocumentMouseOut( event ) {
  107. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  108. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  109. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  110. }
  111. function onDocumentTouchStart( event ) {
  112. if ( event.touches.length == 1 ) {
  113. event.preventDefault();
  114. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  115. targetRotationOnMouseDown = targetRotation;
  116. }
  117. }
  118. function onDocumentTouchMove( event ) {
  119. if ( event.touches.length == 1 ) {
  120. event.preventDefault();
  121. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  122. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  123. }
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. plane.rotation.z = text.rotation.y += ( targetRotation - text.rotation.y ) * 0.05;
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. <script>
  137. var _gaq=[["_setAccount","UA-7549263-1"],["_trackPageview"]];
  138. (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
  139. g.src=("https:"==location.protocol?"//ssl":"//www")+".google-analytics.com/ga.js";
  140. s.parentNode.insertBefore(g,s)}(document,"script"));
  141. </script>
  142. </body>
  143. </html>