canvas_particles_shapes.html 8.7 KB

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