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>
	<div class="element">
		<h2>Hi !</h2>
		<p>This demo shows you how to connect DOM elements with Three Js objects</p>
		<p>I hope you will enjoy ;)</p>
		<span>Mamboleoo</span>
	</div>
	<a href="http://mamboleoo.be/learnThree/" target="_blank" class="element">
		Look at me, I'm a link ! <br />Yeah so awesome :)
	</a>
	<img class="element" src="http://placebear.com/150/150" />
              
            
!

CSS

              
                body,html, canvas{width:100%;height:100%;padding:0;margin:0;overflow: hidden;}
.element{
	padding: 12px;
	background: red;
	position: absolute;
	top: 0;
	left: 0;
	background: white;
	font-family: Helvetica, Verdana, sans-serif;
	font-size: 20px;
	max-width: 300px;
	z-index: 10;
}
a.element{color: grey;z-index:6;}
img.element{padding: 0;border: 5px solid white;z-index: 5;}
h2{margin:0;}
              
            
!

JS

              
                var renderer, scene, camera, cubes, vector;

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);

	light = new THREE.DirectionalLight(0xffffff, 1);
	light.position.set( 0, 0, 500 );
	scene.add(light);

	//Vector use to get position of vertice
	vector = new THREE.Vector3();

	//Generate cubes on the scene
	createCubes();

	//Render my scene
	render();
}


var createCubes = function(){

	//Create an object 3D that contains my cubes
	cubes = new THREE.Object3D();

	//Simple geometry and material
	var geometry = new THREE.BoxGeometry(50,50,50);
	var material = new THREE.MeshLambertMaterial({color: 0x00ff00});
	for(var i=0;i<3;i++){
		var cube = new THREE.Mesh(geometry, material);
		cubes.add(cube);
	}

	//Add my cubes on the scene
	scene.add(cubes);
};

var moveDom = function(i){

	//Vector equal a vertice from the cube (it can be any vertice)
    vector = cubes.children[i].geometry.vertices[i].clone();

    //Apply the matrix of its cube (rotation & translation)
    vector.applyMatrix4(cubes.children[i].matrix);

    //Get the position of the vector according to the rendered scene
    vector.project(camera);

    //Convert the vector values to our scene size (here it's the screen size)
	vector.x = (vector.x * ww/2) + ww/2;
	vector.y = -(vector.y * wh/2) + wh/2;

    //Get the DOM element and apply transforms on it
   	document.querySelectorAll(".element")[i].style.webkitTransform = "translate("+vector.x+"px,"+vector.y+"px)";
   	document.querySelectorAll(".element")[i].style.transform = "translate("+vector.x+"px,"+vector.y+"px)";
};


var counter = 0;
var render = function (a) {
	requestAnimationFrame(render);

	counter++;

	//Move my cubes
	for(var i=0;i<3;i++){
		cubes.children[i].position.x = Math.cos((counter+i*150)/200)*(ww/6+i*80);
		cubes.children[i].position.y = Math.sin((counter+i*150)/200)*(70+i*80);
		cubes.children[i].rotation.x += .001*i+.002;
		cubes.children[i].rotation.y += .001*i+.02;

		//Move my dom elements
		moveDom(i);
	}
	
	renderer.render(scene, camera);
};

init();
              
            
!
999px

Console