Skip to content

Instantly share code, notes, and snippets.

@MAKIO135
Last active August 22, 2019 22:53
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MAKIO135/eab7b74e85ed2be48eeb to your computer and use it in GitHub Desktop.
Using canvas as ThreeJS texture
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body style="background:#fff;">
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r68/three.min.js"></script>
<canvas id="canvas"></canvas>
<script id="jsbin-javascript">
var width = window.innerWidth, height = window.innerHeight / 2;
var size = 256;
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
var camera, scene, renderer, geometry, texture, mesh;
function changeCanvas() {
ctx.font = '20pt Arial';
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.fillRect(10, 10, canvas.width - 20, canvas.height - 20);
ctx.fillStyle = 'black';
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(new Date().getTime(), canvas.width / 2, canvas.height / 2);
}
function init() {
renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);
camera.position.z = 500;
scene.add(camera);
texture = new THREE.Texture(canvas);
var material = new THREE.MeshBasicMaterial({ map: texture });
geometry = new THREE.BoxGeometry( 200, 200, 200 );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
canvas.width = canvas.height = size;
}
function animate() {
requestAnimationFrame(animate);
changeCanvas();
texture.needsUpdate = true;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
}
init();
animate();
</script>
</body>
</html>
@cosmo-naut
Copy link

cosmo-naut commented Feb 2, 2018

Awesome, this should be in the three.js documentation as an example of a canvasTexture, and even how to apply a texture. The documentation is vague on this subject

My canvas square is black when applied as a texture, even though a newly created canvas should be transparent. Is there way to make textures support this transparency?

@Neptilo
Copy link

Neptilo commented Feb 17, 2018

@cosmo-naut Have a look at THREE.Material.alphaTest

@sowdowdow
Copy link

Appreciated your help 👍

@code-matt
Copy link

Thanks, going to give this a shot. CSS3D is way too buggy for displaying 2d elements in the 3d space.

@DenisKS
Copy link

DenisKS commented Aug 23, 2018

Try to use existed canvas as texture for object according this example, but have error about I think CORS but all source in one server maybe something other ..?

Here is example:

enter code herehttps://jsfiddle.net/denis_miroshnikov_ts/srm4oLq1/21/

Just press Red btn and look err at console :

FFox "THREE.WebGLState: DOMException: "The operation is insecure"

Chrome "THREE.WebGLState: DOMException: Failed to execute 'texImage2D' on 'WebGLRenderingContext': Tainted canvases may not be loaded."

@roboshoes
Copy link

@DenisKS: This is probably because you rendered a picture from a different URL onto your canvas and then tried to use said canvas as a texture, right?

This is prohibited for XSS reasons. If you replace the image code with either an image from your own server or no image at all, you should not have this error any longer.

@jjxtra
Copy link

jjxtra commented Aug 22, 2019

Fails if canvas is 8192x4096, far less than the max texture and framebuffer sizes for me, any ideas?

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