I am trying to learn JavaScript by learning WebGL, which should be a semi-contained environment. I am trying to figure out how to run the code below, either from the command line or in a browser. I was able to require the THREE.js libraries, but it doesn't look like I can require the document object as easily. Perhaps I need to get a JavaScript shell? How can I get Mozilla or another browser to run the .js code below? I have the following code:
////////////////////////////////////////////////////////////////////////////////
/*global THREE, Coordinates, $, document, window*/
//var document = require('document');
var THREE = require('three');
var camera, scene, renderer;
var windowScale;
var cameraControls;
var clock = new THREE.Clock();
function drawGoldCube() {
var cube;
var cubeSizeLength = 100;
var goldColor = "#FFDF00";
var showFrame = true;
var wireMaterial = new THREE.MeshBasicMaterial( { color: goldColor, wireframe: showFrame } ) ;
var cubeGeometry = new THREE.CubeGeometry(cubeSizeLength, cubeSizeLength, cubeSizeLength);
cube = new THREE.Mesh( cubeGeometry, wireMaterial );
cube.position.x = 0; // centered at origin
cube.position.y = 0; // centered at origin
cube.position.z = 0; // centered at origin
scene.add( cube );
}
function init() {
var canvasWidth = 846;
var canvasHeight = 494;
// For grading the window is fixed in size; here's general code:
//var canvasWidth = window.innerWidth;
//var canvasHeight = window.innerHeight;
var canvasRatio = canvasWidth / canvasHeight;
// SCENE
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x808080, 2000, 4000 );
// LIGHTS
scene.add( new THREE.AmbientLight( 0x222222 ) );
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColor( scene.fog.color, 1 );
var container = document.getElementById('container');
container.appendChild( renderer.domElement );
// CAMERA
camera = new THREE.PerspectiveCamera( 45, canvasRatio, 1, 4000 );
camera.position.set( -200, 200, -150 );
// CONTROLS
cameraControls = new THREE.OrbitAndPanControls(camera, renderer.domElement);
cameraControls.target.set(0,0,0);
// draw the coordinate grid
Coordinates.drawGrid({size:1000,scale:0.01});
Coordinates.drawGrid({size:1000,scale:0.01, orientation:"y"});
//Coordinates.drawGrid(size:1000,scale:0.01, orientation:"y"});
Coordinates.drawGrid({size:1000,scale:0.01, orientation:"z"});
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
var delta = clock.getDelta();
cameraControls.update(delta);
renderer.render(scene, camera);
}
init();
drawGoldCube();
animate();
it's failing at the document.getElementById() line.
My question is - can I run this outside a browser? If I am at the command line, can I run "node my_webgl.js"? I was able to install THREE.js but I am not sure if I can require a "document" variable. So this line at the top "var document = require('document');" is failing if I uncomment it.
How can I run this code? Preferably from the command line. Thanks.
Unfortunately, no. Working with THREE.js requires you to be in an actual browser window that has WebGL support ( or canvas support if you're going to use the fallback ).