最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to assign a id to canvas in the three.js application - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Oct 29, 2014 at 11:34 Sebastien C. 4,8332 gold badges23 silver badges33 bronze badges asked Oct 23, 2014 at 9:04 user824624user824624 8,06834 gold badges118 silver badges208 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 27

Have you tried :

renderer.domElement.id = 'YourIDName';

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});

发布评论

评论列表(0)

  1. 暂无评论