Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- My scene -->
	<canvas id="scene"></canvas>

<!--
  I created a collection of small examples in Three.js to understand very quickly the purpose of some functions.
  You can visit the 'website' of the entire collection of pens here : http://mamboleoo.be/learnThree/

  You can also see the collection in CodePen
  https://codepen.io/collection/DrxLEd/

  Do not hesitate to comment if there is something wrong (I'm still learning)

  Thanks !
-->
              
            
!

CSS

              
                body,html{width:100%;height:100%;padding:0;margin:0;overflow: hidden;}
              
            
!

JS

              
                var ww = window.innerWidth,
	wh = window.innerHeight;

function init(){

	/* WEBGL RENDERER */
	renderer = new THREE.WebGLRenderer({canvas : document.getElementById('scene')});
	renderer.setSize(ww,wh);

	/* SCENE */
	scene = new THREE.Scene();

	/* CAMERA */
	camera = new THREE.OrthographicCamera(ww/-2,ww/2,wh/2,wh/-2,0,1000);
	camera.position.set(0, 0, 500);
	scene.add(camera);


	/* LIGHT */
	light = new THREE.DirectionalLight(0xffffff, 1);
	light.position.set( 0, 250, 700 );
	scene.add(light);

	//particles will be the 3D object containing all the particles
	particles = new THREE.Object3D();;
	scene.add(particles);

	//Add events listeners on the page
	document.addEventListener("mousemove", mouseMove);
	document.addEventListener("touchemove", mouseMove);

	renderer.render(scene,camera);

	//Init request animation frame
	animate();
};

//Init var with mouse position
var mouse = {x:0,y:0};
function mouseMove(e){

	//3D universe come from the center so I substract half of the screen on the mouse position
	//By doing that, the cursor on the left will give -window/2 and on the right window/2
	//For the Y axis it's a bit different because the top equal 0 but in the scene the top is the positive side
	mouse.x = e.clientX-(ww/2);
	mouse.y = (wh/2)-e.clientY;

};

function createParticle(){

	//Create a geometry used for the particles which contains nothing for now
	var geometry = new THREE.Geometry();
	//Create a vector which equal to the mouse position
	var vertices = new THREE.Vector3(
		mouse.x,
		mouse.y,
		-10
	);
	//We apply our vector inside the geometry
	geometry.vertices.push(vertices);
	//We create a white material
	//sizeAttenuation defines if the particle will be small if far from the camera
	var material = new THREE.PointsMaterial({
		color : 0X00ff00,
		size : 3,
		transparent : true,
		sizeAttenuation : false
    });
    //Point cloud is a specific Mesh for particles
	particle = new THREE.Points(geometry, material);
	//We create a random speed for each particle for aesthetics
	particle.speed = Math.random()/100+0.002
	//We set a random position for each particle
	particle.direction = {
		x : (Math.random()-.5)*ww*2,
		y : (Math.random()-.5)*wh*2
	};
	
	particles.add(particle);
}

var animate = function () {
	requestAnimationFrame(animate);

	//Create a new particle
	createParticle();

	//We loop through all our particles
	for(var i=0,j=particles.children.length;i<j;i++){
		//Get the next particle
		var particle = particles.children[i];

		//We move our particle closer to its destination
		particle.geometry.vertices[0].x += (particle.direction.x - particle.geometry.vertices[0].x)*particle.speed;
		particle.geometry.vertices[0].y += (particle.direction.y - particle.geometry.vertices[0].y)*particle.speed;
		//We reduce the opacity of the particle
		particle.material.opacity -= .005
		//Prevents ThreeJs the particle has moved
		particle.geometry.verticesNeedUpdate = true;

		//If the opacity of the particle is too low
		if(particle.material.opacity < .05){
			//We remove our particle from the scene
			particles.remove(particle);
			//The loop must go through the same 'i' because we removed one particle from the array
			i--;j--;
		}
	}

	renderer.render(scene, camera);
};

init();
              
            
!
999px

Console