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, canvas{width:100%;height:100%;padding:0;margin:0;overflow: hidden;}

              
            
!

JS

              
                var renderer, scene, camera, cube;

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

function init(){

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

	scene = new THREE.Scene();

	camera = new THREE.PerspectiveCamera(50,ww/wh, 0.1, 10000 );
	camera.position.set(0,0,500);
	scene.add(camera);

	//Add a light in the scene
	directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
	directionalLight.position.set( 0, 0, 350 );
	directionalLight.lookAt(new THREE.Vector3(0,0,0));
	scene.add( directionalLight );

	//Add a cube in the scene add apply animation
	createCube();

	//Render the scene and start request animation frame
	render();
}

var createCube = function(){

	//Create a new cube with simple geometry & material
	var geometry = new THREE.BoxGeometry(150,150,150);
	var texture = new THREE.MeshLambertMaterial({color:0x00ff00, transparent: true });
	cube = new THREE.Mesh(geometry, texture);

	//Create a new timeline
	//I like to add my timeline to my element object so I know to which element it refers to
	//But we could declare a new variable which contains the timeline
	cube.tl = new TimelineMax({repeat:-1, repeatDelay: 1});
	//I add a few animations in my timeline
	//The cube turn on itself
	cube.tl.to(cube.rotation, 3, {y:-Math.PI*3.25, x : -Math.PI*1.25, ease:Back.easeInOut});

	//Then it will move to the left and fade out
	cube.tl.to(cube.position, 1, {x : -150, ease:Power3.easeOut});
	cube.tl.to(cube.material, 1, {opacity : 0, ease:Power3.easeOut}, "-=1");

	//Move the cube without transition
	cube.tl.set(cube.position,{x:100, y: -100});

	//Fade In the cube
	cube.tl.to(cube.material, 1, {opacity : 1, ease:Power3.easeOut});

	//It goes back to its initial position
	cube.tl.to(cube.position, 3, {x:0,y:0, ease: Back.easeInOut.config(2)});
	cube.tl.to(cube.rotation, 3, {x:0,y:0, ease: Back.easeInOut.config(2)},"-=3");
	

	//Add the cube in the scene
	scene.add(cube);
};


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

	//Simply render the scene with a request animation frame and the timeline from GreenSock will do the rest ;)
	renderer.render(scene, camera);
};

init();
              
            
!
999px

Console