Three.js base
A base/starter fiddle for Three.js
by Chris Brown
HTML
<script src="https://rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
<script src="https://rawgit.com/mrdoob/stats.js/master/src/Stats.js"></script>
CSS
body {
padding: 0;
margin: 0;
}
JavaScript
var camera, scene, renderer, mesh, material;
init();
animate();
// create a canvas element
var canvas1 = document.createElement('canvas');
canvas1.width = 256;
canvas1.height = 256;
var context1 = canvas1.getContext('2d');
context1.font = "Bold 20px Arial";
//context1.fillStyle = "rgba(1.0,0,0,0.95)";
context1.fillStyle = '#ff0000';
context1.fillText('Hello, world!', 0, 20);
// canvas contents will be used for a texture
var texture1 = new THREE.Texture(canvas1)
texture1.needsUpdate = true;
// use the texture as material
var spriteMaterial = new THREE.SpriteMaterial({
map: texture1,
//useScreenCoordinates: false
});
//var spriteMaterial = new THREE.SpriteMaterial();
// create the sprite and add it tho the scene
var sprite1 = new THREE.Sprite(spriteMaterial);
sprite1.scale.set(500, 500, 1.0);
sprite1.position.set(0, 0, 0);
scene.add(sprite1);
var i = 0;
// change the canvas' content
setInterval(function () {
context1.clearRect(0, 0, canvas1.width, canvas1.height);
var message = 'test' + i++;
context1.fillText(message, 4, 20);
texture1.needsUpdate = true;
}, 1000)
function init() {
// Renderer.
renderer = new THREE.WebGLRenderer();
//renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// Add renderer to page
document.body.appendChild(renderer.domElement);
// Create camera.
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 400;
// Create scene.
scene = new THREE.Scene();
// Create material
material = new THREE.MeshPhongMaterial();
// Create cube and add to scene.
var geometry = new THREE.BoxGeometry(200, 200, 200);
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// Create ambient light and add to scene.
var light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
// Create directional light...