123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <!DOCTYPE HTML>
- <html lang="en">
- <head>
- <title>three.js canvas - particles with shapes</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
- <style type="text/css">
- body {
- font-family: Monospace;
- background-color: #f0f0f0;
- margin: 0px;
- overflow: hidden;
- }
- </style>
- </head>
- <body>
- <script type="text/javascript" src="../build/Three.js"></script>
- <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
- <script type="text/javascript" src="js/Stats.js"></script>
- <script type="text/javascript" src="js/Tween.js"></script>
- <script type="text/javascript" src="js/Sparks.js"></script>
-
- <!-- load the font file from canvas-text -->
- <script type="text/javascript" src="fonts/helvetiker_regular.typeface.js"></script>
- <script type="text/javascript">
- var container, stats;
- var camera, scene, renderer;
- var text, plane;
- var targetRotation = 0;
- var targetRotationOnMouseDown = 0;
- var mouseX = 0;
- var mouseXOnMouseDown = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
-
-
- var heartShape, particleCloud, sparksEmitter, emitterPos;
- var _rotation = 0;
- var timeOnShapePath = 0;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- var info = document.createElement( 'div' );
- info.style.position = 'absolute';
- info.style.top = '10px';
- info.style.width = '100%';
- info.style.textAlign = 'center';
- 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.';
- container.appendChild( info );
- camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.y = 150;
- camera.position.z = 700;
- camera.target.position.y = 150;
- scene = new THREE.Scene();
- // Get text from hash
- var theText = "THREE.JS";
- var hash = document.location.hash.substr( 1 );
- if ( hash.length !== 0 ) {
- theText = hash;
- }
- var text3d = new THREE.TextGeometry( theText, {
- size: 80,
- height: 20,
- curveSegments: 2,
- font: "helvetiker"
- });
- text3d.computeBoundingBox();
- var centerOffset = -0.5 * ( text3d.boundingBox.x[ 1 ] - text3d.boundingBox.x[ 0 ] );
- var textMaterial = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } );
-
- text = new THREE.Mesh( text3d, textMaterial );
- // Potentially, we can extract the vertices or faces of the text to generate particles too.
- // Geo > Vertices > Position
-
- text.doubleSided = false;
- text.position.x = centerOffset;
- text.position.y = 100;
- text.position.z = 0;
- text.rotation.x = 0;
- text.rotation.y = Math.PI * 2;
- text.overdraw = true;
- parent = new THREE.Object3D();
- parent.add( text );
-
- particleCloud = new THREE.Object3D(); // Just a group
- particleCloud.y = 800;
- parent.add( particleCloud );
-
- scene.add( parent );
-
-
- // Create Particle Systems
-
- // Heart
- var x = 0, y = 0;
- heartShape = new THREE.Shape();
- heartShape.moveTo( x + 25, y + 25 );
- heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
- heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
- heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
- heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
- heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
- heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
-
- var circleLines = function ( context ) {
- context.lineWidth = 0.05; //0.05
- context.beginPath();
- context.arc( 0, 0, 1, 0, Math.PI*2, true );
- context.closePath();
- context.stroke();
-
- context.globalAlpha = 0.2;
- context.fill();
- }
-
- var hue = 0;
-
- var hearts = function ( context ) {
- context.globalAlpha = 0.5;
- var x = 0, y = 0;
- context.scale(0.1, -0.1); // Scale so canvas render can redraw within bounds
- context.beginPath();
- // From http://blog.burlock.org/html5/130-paths
- context.bezierCurveTo( x + 2.5, y + 2.5, x + 2.0, y, x, y );
- context.bezierCurveTo( x - 3.0, y, x - 3.0, y + 3.5,x - 3.0,y + 3.5 );
- context.bezierCurveTo( x - 3.0, y + 5.5, x - 1.0, y + 7.7, x + 2.5, y + 9.5 );
- context.bezierCurveTo( x + 6.0, y + 7.7, x + 8.0, y + 5.5, x + 8.0, y + 3.5 );
- context.bezierCurveTo( x + 8.0, y + 3.5, x + 8.0, y, x + 5.0, y );
- context.bezierCurveTo( x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5 );
- context.closePath();
- context.fill();
- context.lineWidth = 0.5; //0.05
- context.stroke();
- }
-
- var setTargetParticle = function() {
-
- //hearts circleLines
- var material = new THREE.ParticleCanvasMaterial( { program: hearts, blending:THREE.AdditiveBlending } );
-
- material.color.setHSV(hue, 0.5, 1);
- hue += 0.001;
- if (hue>1) hue-=1;
-
- particle = new THREE.Particle( material );
- particle.scale.x = particle.scale.y = Math.random() * 20 +20;
- particleCloud.add( particle );
- return particle;
- };
-
- var onParticleCreated = function(p) {
- var position = p.position;
- p.target.position = position;
- };
-
- var onParticleDead = function(particle) {
- particle.target.visible = false;
- particleCloud.remove(particle.target);
- };
-
-
- sparksEmitter = new SPARKS.Emitter(new SPARKS.SteadyCounter(160));
- emitterpos = new THREE.Vector3(0,0,0);
-
- sparksEmitter.addInitializer(new SPARKS.Position( new SPARKS.PointZone( emitterpos ) ) );
- sparksEmitter.addInitializer(new SPARKS.Lifetime(0,2));
- sparksEmitter.addInitializer(new SPARKS.Target(null, setTargetParticle));
-
-
- sparksEmitter.addInitializer(new SPARKS.Velocity(new SPARKS.PointZone(new THREE.Vector3(0,-50,10))));
- // TOTRY Set velocity to move away from centroid
-
- sparksEmitter.addAction(new SPARKS.Age());
- //sparksEmitter.addAction(new SPARKS.Accelerate(0.2));
- sparksEmitter.addAction(new SPARKS.Move());
- sparksEmitter.addAction(new SPARKS.RandomDrift(50,50,2000));
-
-
- sparksEmitter.addCallback("created", onParticleCreated);
- sparksEmitter.addCallback("dead", onParticleDead);
- sparksEmitter.start();
-
- // End Particles
-
- renderer = new THREE.CanvasRenderer();
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- stats.domElement.style.position = 'absolute';
- stats.domElement.style.top = '0px';
- container.appendChild( stats.domElement );
- document.addEventListener( 'mousedown', onDocumentMouseDown, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- }
- //
-
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
-
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- mouseXOnMouseDown = event.clientX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
-
- if (sparksEmitter.isRunning()) {
- sparksEmitter.stop();
- } else {
- sparksEmitter.start();
- }
- }
- function onDocumentMouseMove( event ) {
- mouseX = event.clientX - windowHalfX;
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
- }
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
-
-
- function render() {
- timeOnShapePath += 0.0337;
- if (timeOnShapePath > 1) timeOnShapePath -= 1;
-
- // TODO Create a PointOnShape Action/Zone in the particle engine
- var pointOnShape = heartShape.getPointAt(timeOnShapePath);
-
- emitterpos.x = pointOnShape.x * 5 - 100;
- emitterpos.y = -pointOnShape.y * 5 + 400;
-
- // Pretty cool effect if you enable this
- //particleCloud.rotation.y += 0.05;
- parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
- renderer.render( scene, camera );
-
-
- }
- </script>
- </body>
- </html>
|