canvas_geometry_shapes.html 12 KB

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