canvas_particles_shapes.html 8.7 KB

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