Skip to content

Instantly share code, notes, and snippets.

@studioijeoma
Created September 1, 2011 18:47
Show Gist options
  • Save studioijeoma/1186920 to your computer and use it in GitHub Desktop.
Save studioijeoma/1186920 to your computer and use it in GitHub Desktop.
Three.js WebGL Canvas Texture Text Example
<!doctype html>
<html>
<head>
<title>Three.js : WebGL Canvas Texture Text Example</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
overflow: hidden;
}
</style>
<script type="text/javascript" src="js/Three.js"></script>
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="js/Stats.js"></script>
<script type="text/javascript">
// based on https://github.com/mrdoob/three.js/issues/229
window.onload = function() {
var container, stats;
var camera, scene, renderer;
var mesh;
init();
animate();
function init() {
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.Camera(75, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 1000;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({
antialias : true
});
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild(stats.domElement);
var x = window.innerWidth / 2 - 300;
var y = window.innerHeight / 2 - 300;
mesh = createLabel("HELLO WORLD", x, y, 0, 100, "black", "yellow");
scene.addObject(mesh);
}
function createLabel(text, x, y, z, size, color, backGroundColor, backgroundMargin) {
if(!backgroundMargin)
backgroundMargin = 50;
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
context.font = size + "pt Arial";
var textWidth = context.measureText(text).width;
canvas.width = textWidth + backgroundMargin;
canvas.height = size + backgroundMargin;
context = canvas.getContext("2d");
context.font = size + "pt Arial";
if(backGroundColor) {
context.fillStyle = backGroundColor;
context.fillRect(canvas.width / 2 - textWidth / 2 - backgroundMargin / 2, canvas.height / 2 - size / 2 - +backgroundMargin / 2, textWidth + backgroundMargin, size + backgroundMargin);
}
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, canvas.width / 2, canvas.height / 2);
// context.strokeStyle = "black";
// context.strokeRect(0, 0, canvas.width, canvas.height);
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
var material = new THREE.MeshBasicMaterial({
map : texture
});
var mesh = new THREE.Mesh(new THREE.PlaneGeometry(canvas.width, canvas.height), material);
// mesh.overdraw = true;
mesh.doubleSided = true;
mesh.position.x = x - canvas.width;
mesh.position.y = y - canvas.height;
mesh.position.z = z;
return mesh;
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
stats.update();
}
};
</script>
</head>
<body></body>
</html>
@finalyXG
Copy link

finalyXG commented May 6, 2013

why can't i see anything in my google browser?? Can you show me a sreenshot as an running result???

@bartburkhardt
Copy link

I tried this code, added the external js libraries. and changed screne.addObject into scene.add. But then nothing happens, only the stats is visible.. any ideas?

@mattlockyer
Copy link

error: S3TC compressed textures not supported

@nuthinking
Copy link

Same as @bartburkhardt here. Is it possible to see a live demo? Thanks!

@MartinBspheroid
Copy link

seems like function createLabel() is defined, but it's not called in animate(). I will test it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment