canvas_geometry_shapes.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - shapes</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. #info {
  15. position: absolute;
  16. top: 0px;
  17. width: 100%;
  18. padding: 5px;
  19. text-align:center;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <canvas id="debug" style="position:absolute; left:100px"></canvas>
  25. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - shape geometry</div>
  26. <script src="../build/three.min.js"></script>
  27. <script src="js/libs/stats.min.js"></script>
  28. <script>
  29. var container, stats;
  30. var camera, scene, renderer;
  31. var text, plane;
  32. var targetRotation = 0;
  33. var targetRotationOnMouseDown = 0;
  34. var mouseX = 0;
  35. var mouseXOnMouseDown = 0;
  36. var windowHalfX = window.innerWidth / 2;
  37. var windowHalfY = window.innerHeight / 2;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  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. parent = new THREE.Object3D();
  47. parent.position.y = 50;
  48. scene.add( parent );
  49. function addShape( shape, color, x, y, z, rx, ry, rz, s ) {
  50. // flat shape
  51. var geometry = new THREE.ShapeGeometry( shape );
  52. var material = new THREE.MeshBasicMaterial( { color: color, overdraw: true } );
  53. var mesh = new THREE.Mesh( geometry, material );
  54. mesh.position.set( x, y, z );
  55. mesh.rotation.set( rx, ry, rz );
  56. mesh.scale.set( s, s, s );
  57. parent.add( mesh );
  58. // line
  59. var geometry = shape.createPointsGeometry();
  60. var material = new THREE.LineBasicMaterial( { linewidth: 10, color: 0x333333, transparent: true } );
  61. var line = new THREE.Line( geometry, material );
  62. line.position.set( x, y, z );
  63. line.rotation.set( rx, ry, rz );
  64. line.scale.set( s, s, s );
  65. parent.add( line );
  66. }
  67. // California
  68. var californiaPts = [];
  69. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  70. californiaPts.push( new THREE.Vector2 ( 450, 300 ) );
  71. californiaPts.push( new THREE.Vector2 ( 392, 392 ) );
  72. californiaPts.push( new THREE.Vector2 ( 266, 438 ) );
  73. californiaPts.push( new THREE.Vector2 ( 190, 570 ) );
  74. californiaPts.push( new THREE.Vector2 ( 190, 600 ) );
  75. californiaPts.push( new THREE.Vector2 ( 160, 620 ) );
  76. californiaPts.push( new THREE.Vector2 ( 160, 650 ) );
  77. californiaPts.push( new THREE.Vector2 ( 180, 640 ) );
  78. californiaPts.push( new THREE.Vector2 ( 165, 680 ) );
  79. californiaPts.push( new THREE.Vector2 ( 150, 670 ) );
  80. californiaPts.push( new THREE.Vector2 ( 90, 737 ) );
  81. californiaPts.push( new THREE.Vector2 ( 80, 795 ) );
  82. californiaPts.push( new THREE.Vector2 ( 50, 835 ) );
  83. californiaPts.push( new THREE.Vector2 ( 64, 870 ) );
  84. californiaPts.push( new THREE.Vector2 ( 60, 945 ) );
  85. californiaPts.push( new THREE.Vector2 ( 300, 945 ) );
  86. californiaPts.push( new THREE.Vector2 ( 300, 743 ) );
  87. californiaPts.push( new THREE.Vector2 ( 600, 473 ) );
  88. californiaPts.push( new THREE.Vector2 ( 626, 425 ) );
  89. californiaPts.push( new THREE.Vector2 ( 600, 370 ) );
  90. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  91. var californiaShape = new THREE.Shape( californiaPts );
  92. // Triangle
  93. var triangleShape = new THREE.Shape();
  94. triangleShape.moveTo( 80, 20 );
  95. triangleShape.lineTo( 40, 80 );
  96. triangleShape.lineTo( 120, 80 );
  97. triangleShape.lineTo( 80, 20 ); // close path
  98. // Heart
  99. var x = 0, y = 0;
  100. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  101. heartShape.moveTo( x + 25, y + 25 );
  102. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  103. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  104. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  105. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  106. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  107. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  108. // Square
  109. var sqLength = 80;
  110. var squareShape = new THREE.Shape();
  111. squareShape.moveTo( 0,0 );
  112. squareShape.lineTo( 0, sqLength );
  113. squareShape.lineTo( sqLength, sqLength );
  114. squareShape.lineTo( sqLength, 0 );
  115. squareShape.lineTo( 0, 0 );
  116. // Rectangle
  117. var rectLength = 120, rectWidth = 40;
  118. var rectShape = new THREE.Shape();
  119. rectShape.moveTo( 0,0 );
  120. rectShape.lineTo( 0, rectWidth );
  121. rectShape.lineTo( rectLength, rectWidth );
  122. rectShape.lineTo( rectLength, 0 );
  123. rectShape.lineTo( 0, 0 );
  124. // Rounded rectangle
  125. var roundedRectShape = new THREE.Shape();
  126. ( function roundedRect( ctx, x, y, width, height, radius ){
  127. ctx.moveTo( x, y + radius );
  128. ctx.lineTo( x, y + height - radius );
  129. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  130. ctx.lineTo( x + width - radius, y + height) ;
  131. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  132. ctx.lineTo( x + width, y + radius );
  133. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  134. ctx.lineTo( x + radius, y );
  135. ctx.quadraticCurveTo( x, y, x, y + radius );
  136. } )( roundedRectShape, 0, 0, 50, 50, 20 );
  137. // Circle
  138. var circleRadius = 40;
  139. var circleShape = new THREE.Shape();
  140. circleShape.moveTo( 0, circleRadius );
  141. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  142. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  143. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  144. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius );
  145. // Fish
  146. x = y = 0;
  147. var fishShape = new THREE.Shape();
  148. fishShape.moveTo(x,y);
  149. fishShape.quadraticCurveTo(x + 50, y - 80, x + 90, y - 10);
  150. fishShape.quadraticCurveTo(x + 100, y - 10, x + 115, y - 40);
  151. fishShape.quadraticCurveTo(x + 115, y, x + 115, y + 40);
  152. fishShape.quadraticCurveTo(x + 100, y + 10, x + 90, y + 10);
  153. fishShape.quadraticCurveTo(x + 50, y + 80, x, y);
  154. // Arc circle
  155. var arcShape = new THREE.Shape();
  156. arcShape.moveTo( 50, 10 );
  157. arcShape.absarc( 10, 10, 40, 0, Math.PI*2, false );
  158. var holePath = new THREE.Path();
  159. holePath.moveTo( 20, 10 );
  160. holePath.absarc( 10, 10, 10, 0, Math.PI*2, true );
  161. arcShape.holes.push( holePath );
  162. // Smiley
  163. var smileyShape = new THREE.Shape();
  164. smileyShape.moveTo( 80, 40 );
  165. smileyShape.absarc( 40, 40, 40, 0, Math.PI*2, false );
  166. var smileyEye1Path = new THREE.Path();
  167. smileyEye1Path.moveTo( 35, 20 );
  168. // smileyEye1Path.absarc( 25, 20, 10, 0, Math.PI*2, true );
  169. smileyEye1Path.absellipse( 25, 20, 10, 10, 0, Math.PI*2, true );
  170. smileyShape.holes.push( smileyEye1Path );
  171. var smileyEye2Path = new THREE.Path();
  172. smileyEye2Path.moveTo( 65, 20 );
  173. smileyEye2Path.absarc( 55, 20, 10, 0, Math.PI*2, true );
  174. smileyShape.holes.push( smileyEye2Path );
  175. var smileyMouthPath = new THREE.Path();
  176. // ugly box mouth
  177. // smileyMouthPath.moveTo( 20, 40 );
  178. // smileyMouthPath.lineTo( 60, 40 );
  179. // smileyMouthPath.lineTo( 60, 60 );
  180. // smileyMouthPath.lineTo( 20, 60 );
  181. // smileyMouthPath.lineTo( 20, 40 );
  182. smileyMouthPath.moveTo( 20, 40 );
  183. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  184. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  185. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  186. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  187. smileyShape.holes.push( smileyMouthPath );
  188. // Spline shape + path extrusion
  189. var splinepts = [];
  190. splinepts.push( new THREE.Vector2 ( 350, 100 ) );
  191. splinepts.push( new THREE.Vector2 ( 400, 450 ) );
  192. splinepts.push( new THREE.Vector2 ( -140, 350 ) );
  193. splinepts.push( new THREE.Vector2 ( 0, 0 ) );
  194. var splineShape = new THREE.Shape( );
  195. splineShape.moveTo( 0, 0 );
  196. splineShape.splineThru( splinepts );
  197. // addShape( shape, color, x, y, z, rx, ry,rz, s );
  198. addShape( californiaShape, 0xffaa00, -300, -100, 0, 0, 0, 0, 0.25 );
  199. addShape( triangleShape, 0xffee00, -180, 0, 0, 0, 0, 0, 1 );
  200. addShape( roundedRectShape, 0x005500, -150, 150, 0, 0, 0, 0, 1 );
  201. addShape( squareShape, 0x0055ff, 150, 100, 0, 0, 0, 0, 1 );
  202. addShape( heartShape, 0xff1100, 60, 100, 0, 0, 0, Math.PI, 1 );
  203. addShape( circleShape, 0x00ff11, 120, 250, 0, 0, 0, 0, 1 );
  204. addShape( fishShape, 0x222222, -60, 200, 0, 0, 0, 0, 1 );
  205. addShape( smileyShape, 0xee00ff, -200, 250, 0, 0, 0, Math.PI, 1 );
  206. addShape( arcShape, 0xbb4422, 150, 0, 0, 0, 0, 0, 1 );
  207. addShape( splineShape, 0x888888, -50, -100, 0, 0, 0, 0, 0.2 );
  208. //
  209. renderer = new THREE.CanvasRenderer( { antialias: true } );
  210. renderer.setSize( window.innerWidth, window.innerHeight );
  211. renderer.sortObjects = false;
  212. renderer.sortElements = false;
  213. container.appendChild( renderer.domElement );
  214. stats = new Stats();
  215. stats.domElement.style.position = 'absolute';
  216. stats.domElement.style.top = '0px';
  217. container.appendChild( stats.domElement );
  218. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  219. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  220. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  221. //
  222. window.addEventListener( 'resize', onWindowResize, false );
  223. }
  224. function onWindowResize() {
  225. windowHalfX = window.innerWidth / 2;
  226. windowHalfY = window.innerHeight / 2;
  227. camera.aspect = window.innerWidth / window.innerHeight;
  228. camera.updateProjectionMatrix();
  229. renderer.setSize( window.innerWidth, window.innerHeight );
  230. }
  231. //
  232. function onDocumentMouseDown( event ) {
  233. event.preventDefault();
  234. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  235. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  236. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  237. mouseXOnMouseDown = event.clientX - windowHalfX;
  238. targetRotationOnMouseDown = targetRotation;
  239. }
  240. function onDocumentMouseMove( event ) {
  241. mouseX = event.clientX - windowHalfX;
  242. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  243. }
  244. function onDocumentMouseUp( event ) {
  245. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  246. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  247. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  248. }
  249. function onDocumentMouseOut( event ) {
  250. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  251. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  252. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  253. }
  254. function onDocumentTouchStart( event ) {
  255. if ( event.touches.length == 1 ) {
  256. event.preventDefault();
  257. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  258. targetRotationOnMouseDown = targetRotation;
  259. }
  260. }
  261. function onDocumentTouchMove( event ) {
  262. if ( event.touches.length == 1 ) {
  263. event.preventDefault();
  264. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  265. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  266. }
  267. }
  268. //
  269. function animate() {
  270. requestAnimationFrame( animate );
  271. render();
  272. stats.update();
  273. }
  274. function render() {
  275. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  276. renderer.render( scene, camera );
  277. }
  278. </script>
  279. </body>
  280. </html>