Tackling three.js

I want to learn to use the JavaScript library three.js to get WebGL to render things like this:

The trouble is, I don’t know anything about JavaScript, three.js, or WebGL. Are you in the same boat? If so, then we might as well paddle together. I’m building an online “Wisdom Collector” that organizes all the resources, videos, online courses, books, and articles that are helping me to learn three.js. It’s a work in progress, so hit reload often to see what’s new. Press this button!


I managed to make the spinny graphic above after many days of scrutinizing this code from diale.org, meaning that I actually old-school printed it out and read it on the subway every day trying to figure out what it meant. The code itself wasn’t so bad in the end; what was difficult was getting everything set up and configured so that I could use three.js on my own computer, and then figuring out how to make all that work on WordPress for this blog. I’ll write about that in a separate post later, and/or put it in the three.js Collector, but for now I’ll just show you the code.

The HTML code I used in WordPress to display the spinny graphic is:

<div align="center">
    width="300" height="300" 
    frameborder="0" scrolling="no">

The 3D model being displayed is the instellated* Disdyakis Dodecahedron ornament model from the previous two posts.  Of course the short burst of HTML above is just a container for a much larger code file that is loaded from our geekhaus.com server:

<script src="./js/three.min.js"></script>
<script src="./js/STLLoader.js"></script>

var container, camera, scene, renderer;


function init(){

    camera=new THREE.PerspectiveCamera(
         20, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(3, 0.5, 3);

    scene=new THREE.Scene();

    // object
    var loader=new THREE.STLLoader();
    loader.addEventListener('load', function (event){
        var geometry=event.content;
        var material=new THREE.MeshLambertMaterial({ 
             ambient: 0xFBB917,color: 0xfdd017 });
        var mesh=new THREE.Mesh(geometry, material);

    // STL file to be loaded

    // lights
    scene.add(new THREE.AmbientLight(0x736F6E));

    var directionalLight=new THREE.DirectionalLight(0xffffff, .8);

    // renderer

    renderer=new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);


    window.addEventListener('resize', onWindowResize, false);

function addLight(x, y, z, color, intensity){
    var directionalLight=new THREE.DirectionalLight(color, intensity);
    directionalLight.position.set(x, y, z)

function onWindowResize(){
    camera.aspect=window.innerWidth / window.innerHeight;
    renderer.setSize(window.innerWidth, window.innerHeight);

function animate(){

function render(){
    var timer=Date.now() * 0.0002;
    camera.position.x = -r*Math.sqrt(1-.125*Math.cos(2*timer)
    camera.position.y = .25*r*Math.cos(2*timer);
    camera.position.z = r*Math.sqrt(1-.125*Math.cos(2*timer)
    renderer.render(scene, camera);
    renderer.setClearColor(0xffffff, 1);



That’s a lot of code to read at once, and there’s also some interesting math hiding in the “camera.position” coordinates; it’s your turn to look like a dork on the subway. And if you want to start at the very beginning, get in the boat at the three.js Wisdom Collector.

* It turns out that “instellated” is actually a word. However, it is a premium word.  At the Merriam-Webster online dictionary, they state:

This word doesn’t usually appear in our free dictionary, but the definition from our premium Unabridged Dictionary is offered here on a limited basis.

Leave a Reply

Your email address will not be published. Required fields are marked *