2
0

canvas_geometry_shapes.html 12 KB

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