canvas_particles_shapes.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - particles with 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. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/renderers/Projector.js"></script>
  19. <script src="js/renderers/CanvasRenderer.js"></script>
  20. <script src="js/libs/stats.min.js"></script>
  21. <script src="js/libs/tween.min.js"></script>
  22. <script src="js/Sparks.js"></script>
  23. <!-- load the font file from canvas-text -->
  24. <script src="fonts/helvetiker_regular.typeface.js"></script>
  25. <script>
  26. var container, stats;
  27. var camera, scene, renderer;
  28. var group, text, plane;
  29. var targetRotation = 0;
  30. var targetRotationOnMouseDown = 0;
  31. var mouseX = 0;
  32. var mouseXOnMouseDown = 0;
  33. var windowHalfX = window.innerWidth / 2;
  34. var windowHalfY = window.innerHeight / 2;
  35. var heartShape, particleCloud, sparksEmitter, emitterPos;
  36. var _rotation = 0;
  37. var timeOnShapePath = 0;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. var info = document.createElement( 'div' );
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. info.innerHTML = 'Three.js with Love. Simple Particle Systems with Shapes by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br>Move your mouse.';
  49. container.appendChild( info );
  50. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  51. camera.position.set( 0, 150, 700 );
  52. scene = new THREE.Scene();
  53. group = new THREE.Group();
  54. scene.add( group );
  55. // Get text from hash
  56. var string = "THREE.JS";
  57. var hash = document.location.hash.substr( 1 );
  58. if ( hash.length !== 0 ) {
  59. string = hash;
  60. }
  61. var text3d = new THREE.TextGeometry( string, {
  62. size: 80,
  63. height: 20,
  64. curveSegments: 2,
  65. font: "helvetiker"
  66. });
  67. text3d.computeBoundingBox();
  68. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  69. var textMaterial = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, overdraw: 0.5 } );
  70. text = new THREE.Mesh( text3d, textMaterial );
  71. // Potentially, we can extract the vertices or faces of the text to generate particles too.
  72. // Geo > Vertices > Position
  73. text.position.x = centerOffset;
  74. text.position.y = 100;
  75. text.position.z = 0;
  76. text.rotation.x = 0;
  77. text.rotation.y = Math.PI * 2;
  78. group.add( text );
  79. particleCloud = new THREE.Object3D(); // Just a group
  80. particleCloud.y = 800;
  81. group.add( particleCloud );
  82. // Create Particle Systems
  83. // Heart
  84. var x = 0, y = 0;
  85. heartShape = new THREE.Shape();
  86. heartShape.moveTo( x + 25, y + 25 );
  87. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  88. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  89. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  90. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  91. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  92. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  93. var hue = 0;
  94. var hearts = function ( context ) {
  95. context.globalAlpha = 0.5;
  96. var x = 0, y = 0;
  97. context.scale(0.05, -0.05); // Scale so canvas render can redraw within bounds
  98. context.beginPath();
  99. // From http://blog.burlock.org/html5/130-paths
  100. context.bezierCurveTo( x + 2.5, y + 2.5, x + 2.0, y, x, y );
  101. context.bezierCurveTo( x - 3.0, y, x - 3.0, y + 3.5,x - 3.0,y + 3.5 );
  102. context.bezierCurveTo( x - 3.0, y + 5.5, x - 1.0, y + 7.7, x + 2.5, y + 9.5 );
  103. context.bezierCurveTo( x + 6.0, y + 7.7, x + 8.0, y + 5.5, x + 8.0, y + 3.5 );
  104. context.bezierCurveTo( x + 8.0, y + 3.5, x + 8.0, y, x + 5.0, y );
  105. context.bezierCurveTo( x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5 );
  106. context.fill();
  107. context.lineWidth = 0.5; //0.05
  108. context.stroke();
  109. }
  110. var setTargetParticle = function() {
  111. var material = new THREE.SpriteCanvasMaterial( {
  112. program: hearts
  113. } );
  114. material.color.setHSL(hue, 1, 0.75);
  115. hue += 0.001;
  116. if (hue>1) hue-=1;
  117. particle = new THREE.Sprite( material );
  118. particle.scale.x = particle.scale.y = Math.random() * 40 + 40;
  119. particleCloud.add( particle );
  120. return particle;
  121. };
  122. var onParticleCreated = function( p ) {
  123. p.target.position.copy( p.position );
  124. };
  125. var onParticleDead = function( particle ) {
  126. particle.target.visible = false;
  127. particleCloud.remove( particle.target );
  128. };
  129. sparksEmitter = new SPARKS.Emitter(new SPARKS.SteadyCounter(160));
  130. emitterpos = new THREE.Vector3();
  131. sparksEmitter.addInitializer(new SPARKS.Position( new SPARKS.PointZone( emitterpos ) ) );
  132. sparksEmitter.addInitializer(new SPARKS.Lifetime(0,2));
  133. sparksEmitter.addInitializer(new SPARKS.Target(null, setTargetParticle));
  134. sparksEmitter.addInitializer(new SPARKS.Velocity(new SPARKS.PointZone(new THREE.Vector3(0,-50,10))));
  135. // TOTRY Set velocity to move away from centroid
  136. sparksEmitter.addAction(new SPARKS.Age());
  137. //sparksEmitter.addAction(new SPARKS.Accelerate(0.2));
  138. sparksEmitter.addAction(new SPARKS.Move());
  139. sparksEmitter.addAction(new SPARKS.RandomDrift(50,50,2000));
  140. sparksEmitter.addCallback("created", onParticleCreated);
  141. sparksEmitter.addCallback("dead", onParticleDead);
  142. sparksEmitter.addCallback("updated", function( particle ) {
  143. particle.target.position.copy( particle.position );
  144. });
  145. sparksEmitter.start();
  146. // End Particles
  147. renderer = new THREE.CanvasRenderer();
  148. renderer.setClearColor( 0xf0f0f0 );
  149. renderer.setPixelRatio( window.devicePixelRatio );
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. container.appendChild( renderer.domElement );
  152. stats = new Stats();
  153. stats.domElement.style.position = 'absolute';
  154. stats.domElement.style.top = '0px';
  155. container.appendChild( stats.domElement );
  156. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  157. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  158. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  159. //
  160. window.addEventListener( 'resize', onWindowResize, false );
  161. }
  162. function onWindowResize() {
  163. windowHalfX = window.innerWidth / 2;
  164. windowHalfY = window.innerHeight / 2;
  165. camera.aspect = window.innerWidth / window.innerHeight;
  166. camera.updateProjectionMatrix();
  167. renderer.setSize( window.innerWidth, window.innerHeight );
  168. }
  169. //
  170. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  171. function onDocumentMouseDown( event ) {
  172. event.preventDefault();
  173. mouseXOnMouseDown = event.clientX - windowHalfX;
  174. targetRotationOnMouseDown = targetRotation;
  175. if ( sparksEmitter.isRunning() ) {
  176. sparksEmitter.stop();
  177. } else {
  178. sparksEmitter.start();
  179. }
  180. }
  181. function onDocumentMouseMove( event ) {
  182. mouseX = event.clientX - windowHalfX;
  183. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  184. }
  185. function onDocumentTouchStart( event ) {
  186. if ( event.touches.length == 1 ) {
  187. event.preventDefault();
  188. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  189. targetRotationOnMouseDown = targetRotation;
  190. }
  191. }
  192. function onDocumentTouchMove( event ) {
  193. if ( event.touches.length == 1 ) {
  194. event.preventDefault();
  195. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  196. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  197. }
  198. }
  199. //
  200. function animate() {
  201. requestAnimationFrame( animate );
  202. render();
  203. stats.update();
  204. }
  205. function render() {
  206. timeOnShapePath += 0.0337;
  207. if (timeOnShapePath > 1) timeOnShapePath -= 1;
  208. // TODO Create a PointOnShape Action/Zone in the particle engine
  209. var pointOnShape = heartShape.getPointAt( timeOnShapePath );
  210. emitterpos.x = pointOnShape.x * 5 - 100;
  211. emitterpos.y = -pointOnShape.y * 5 + 400;
  212. // Pretty cool effect if you enable this
  213. // particleCloud.rotation.y += 0.05;
  214. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  215. renderer.render( scene, camera );
  216. }
  217. </script>
  218. </body>
  219. </html>