canvas_geometry_shape.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. <canvas id="debug" style="position:absolute; left:100px"></canvas>
  18. <script type="text/javascript" src="../build/Three.js"></script>
  19. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  20. <script type="text/javascript" src="js/Stats.js"></script>
  21. <!-- load the font file from canvas-text -->
  22. <script type="text/javascript" src="../src/extras/geometries/Path.js"></script>
  23. <script type="text/javascript">
  24. var container, stats;
  25. var camera, scene, renderer;
  26. var text, plane;
  27. var targetRotation = 0;
  28. var targetRotationOnMouseDown = 0;
  29. var mouseX = 0;
  30. var mouseXOnMouseDown = 0;
  31. var windowHalfX = window.innerWidth / 2;
  32. var windowHalfY = window.innerHeight / 2;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. var info = document.createElement( 'div' );
  39. info.style.position = 'absolute';
  40. info.style.top = '10px';
  41. info.style.width = '100%';
  42. info.style.textAlign = 'center';
  43. info.innerHTML = 'Simple Dynamic 3D Shapes Example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin the text';
  44. container.appendChild( info );
  45. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.y = 150;
  47. camera.position.z = 500;
  48. camera.target.position.y = 150;
  49. scene = new THREE.Scene();
  50. var californiaPts=[];
  51. californiaPts.push( new THREE.Vector2 (610, 320) );
  52. californiaPts.push( new THREE.Vector2 (450, 300) );
  53. californiaPts.push( new THREE.Vector2 (392, 392) );
  54. californiaPts.push( new THREE.Vector2 (266, 438) );
  55. californiaPts.push( new THREE.Vector2 (190, 570) );
  56. californiaPts.push( new THREE.Vector2 (190, 600) );
  57. californiaPts.push( new THREE.Vector2 (160, 620) );
  58. californiaPts.push( new THREE.Vector2 (160, 650) );
  59. californiaPts.push( new THREE.Vector2 (180, 640) );
  60. californiaPts.push( new THREE.Vector2 (165, 680) );
  61. californiaPts.push( new THREE.Vector2 (150, 670) );
  62. californiaPts.push( new THREE.Vector2 (90, 737) );
  63. californiaPts.push( new THREE.Vector2 (80, 795) );
  64. californiaPts.push( new THREE.Vector2 (50, 835) );
  65. californiaPts.push( new THREE.Vector2 (64, 870) );
  66. californiaPts.push( new THREE.Vector2 (60, 945) );
  67. californiaPts.push( new THREE.Vector2 (300, 945) );
  68. californiaPts.push( new THREE.Vector2 (300, 743) );
  69. californiaPts.push( new THREE.Vector2 (600, 473) );
  70. californiaPts.push( new THREE.Vector2 (626, 425) );
  71. californiaPts.push( new THREE.Vector2 (600, 370) );
  72. californiaPts.push( new THREE.Vector2 (610, 320) );
  73. var californiaShape = new THREE.Shape(californiaPts);
  74. var california3d = new THREE.ExtrudeGeometry( californiaShape, {
  75. amount: 20
  76. });
  77. var extrudeSettings = {
  78. amount: 30
  79. };
  80. var triangleShape = new THREE.Shape();
  81. triangleShape.moveTo(80, 20);
  82. triangleShape.lineTo(40, 80);
  83. triangleShape.lineTo(120, 80);
  84. triangleShape.lineTo(80,20); // close path
  85. var triangle3d = triangleShape.extrude(extrudeSettings);
  86. var x = 0, y = 0;
  87. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  88. heartShape.moveTo(x + 25, y + 25);
  89. heartShape.bezierCurveTo(x + 25, y + 25, x + 20, y, x, y);
  90. heartShape.bezierCurveTo(x - 30, y, x - 30, y + 35,x - 30,y + 35);
  91. heartShape.bezierCurveTo(x - 30, y + 55, x - 10, y + 77, x + 25, y + 95);
  92. heartShape.bezierCurveTo(x + 60, y + 77, x + 80, y + 55, x + 80, y + 35);
  93. heartShape.bezierCurveTo(x + 80, y + 35, x + 80, y, x + 50, y);
  94. heartShape.bezierCurveTo(x + 35, y, x + 25, y + 25, x + 25, y + 25);
  95. var heart3d = heartShape.extrude(extrudeSettings);
  96. //heartShape.debug(document.getElementById("debug"));
  97. var sqLength = 80;
  98. var squareShape = new THREE.Shape();
  99. squareShape.moveTo(0,0);
  100. squareShape.lineTo(0, sqLength);
  101. squareShape.lineTo(sqLength, sqLength);
  102. squareShape.lineTo(sqLength, 0);
  103. squareShape.lineTo(0, 0);
  104. var square3d = squareShape.extrude(extrudeSettings);
  105. var rectLength = 120, rectWidth = 40;
  106. var rectShape = new THREE.Shape();
  107. rectShape.moveTo(0,0);
  108. rectShape.lineTo(0, rectWidth);
  109. rectShape.lineTo(rectLength, rectWidth);
  110. rectShape.lineTo(rectLength, 0);
  111. rectShape.lineTo(0, 0);
  112. var rect3d = rectShape.extrude(extrudeSettings);
  113. var roundedRectShape = new THREE.Shape();
  114. roundedRect(roundedRectShape, 0, 0, 50,50, 20);
  115. var roundedRect3d = roundedRectShape.extrude(extrudeSettings);
  116. function roundedRect(ctx,x,y,width,height,radius){
  117. ctx.moveTo(x,y+radius);
  118. ctx.lineTo(x,y+height-radius);
  119. ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
  120. ctx.lineTo(x+width-radius,y+height);
  121. ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
  122. ctx.lineTo(x+width,y+radius);
  123. ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
  124. ctx.lineTo(x+radius,y);
  125. ctx.quadraticCurveTo(x,y,x,y+radius);
  126. }
  127. var circleRadius = 20;
  128. var circleShape = new THREE.Shape();
  129. circleShape.moveTo(0,circleRadius);
  130. circleShape.quadraticCurveTo(circleRadius, circleRadius,circleRadius,0);
  131. circleShape.quadraticCurveTo(circleRadius, -circleRadius,0,-circleRadius);
  132. circleShape.quadraticCurveTo(-circleRadius, -circleRadius,-circleRadius,0);
  133. circleShape.quadraticCurveTo(-circleRadius, circleRadius,0,circleRadius);
  134. var circle3d = circleShape.extrude(extrudeSettings);
  135. x = y = 0;
  136. var fishShape = new THREE.Shape();
  137. fishShape.moveTo(x,y);
  138. fishShape.quadraticCurveTo(x + 50, y - 80, x + 90, y - 10);
  139. fishShape.quadraticCurveTo(x + 100, y - 10, x + 115, y - 40);
  140. fishShape.quadraticCurveTo(x + 115, y, x + 115, y + 40);
  141. fishShape.quadraticCurveTo(x + 100, y + 10, x + 90, y + 10);
  142. fishShape.quadraticCurveTo(x + 50, y + 80, x, y);
  143. var fish3d = fishShape.extrude(extrudeSettings);
  144. // TOFIX: swap order of points around.
  145. var textMaterial = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe:false } );
  146. text = new THREE.Mesh( square3d, textMaterial );
  147. text.doubleSided = false;
  148. text.position.y = 100;
  149. // text.position.y = 100;
  150. text.position.z = 200;
  151. text.rotation.x = 0;
  152. text.rotation.y = Math.PI*2;
  153. text.overdraw = true;
  154. scene.addObject( text );
  155. var triangleMesh = new THREE.Mesh( triangle3d, textMaterial );
  156. triangleMesh.position.x = 180;
  157. triangleMesh.position.y = 50;
  158. scene.addObject( triangleMesh );
  159. // Plane
  160. plane = new THREE.Mesh( new THREE.PlaneGeometry( 800, 800 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0, wireframe:true }) );
  161. plane.rotation.x = - 90 * ( Math.PI / 180 );
  162. plane.position.x = 0;
  163. plane.overdraw = true;
  164. //scene.addObject( plane );
  165. renderer = new THREE.CanvasRenderer();
  166. renderer.setSize( window.innerWidth, window.innerHeight );
  167. container.appendChild( renderer.domElement );
  168. stats = new Stats();
  169. stats.domElement.style.position = 'absolute';
  170. stats.domElement.style.top = '0px';
  171. container.appendChild( stats.domElement );
  172. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  173. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  174. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  175. }
  176. //
  177. function onDocumentMouseDown( event ) {
  178. event.preventDefault();
  179. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  180. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  181. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  182. mouseXOnMouseDown = event.clientX - windowHalfX;
  183. targetRotationOnMouseDown = targetRotation;
  184. }
  185. function onDocumentMouseMove( event ) {
  186. mouseX = event.clientX - windowHalfX;
  187. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  188. }
  189. function onDocumentMouseUp( event ) {
  190. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  191. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  192. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  193. }
  194. function onDocumentMouseOut( event ) {
  195. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  196. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  197. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  198. }
  199. function onDocumentTouchStart( event ) {
  200. if ( event.touches.length == 1 ) {
  201. event.preventDefault();
  202. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  203. targetRotationOnMouseDown = targetRotation;
  204. }
  205. }
  206. function onDocumentTouchMove( event ) {
  207. if ( event.touches.length == 1 ) {
  208. event.preventDefault();
  209. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  210. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  211. }
  212. }
  213. //
  214. function animate() {
  215. requestAnimationFrame( animate );
  216. render();
  217. stats.update();
  218. }
  219. function render() {
  220. plane.rotation.z = text.rotation.y += ( targetRotation - text.rotation.y ) * 0.05;
  221. renderer.render( scene, camera );
  222. }
  223. </script>
  224. </body>
  225. </html>
粤ICP备19079148号