user824624
user824624

Reputation: 8080

how to assign a id to canvas in the three.js application

I have created a render object in three.js and connect it with DomElment, shown as followed

var renderer = new THREE.WebGLRenderer({
            antialias: true
        });
renderer.setClearColor( 0xAAAAAA, 1 );
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('webgl-container').appendChild(renderer.domElement);

so now three.js automatically create a canvas inside the webgl-container div, but now I want to give a canvas a id, how could I do it

Upvotes: 8

Views: 7519

Answers (2)

Caspar
Caspar

Reputation: 1176

A current approach that works well is to pass your canvas ID to the renderer when you create it:

const canvas = document.getElementById('myCanvas');
const renderer = new THREE.WebGLRenderer({canvas: canvas});

Upvotes: 1

Benedikt
Benedikt

Reputation: 787

Have you tried :

renderer.domElement.id = 'YourIDName';

Upvotes: 27

Related Questions