canvas_geometry_shapes.html 12 KB

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