admin管理员组文章数量:1025502
This is actually something I've solved, but for curiousity sake, I was wondering if someone could explain why this is the case?
Here's my html:
<!DOCTYPE html>
<html>
<head>
<script src="three.min.js"></script>
<script src="OrbitControls.js"></script>
<script type="text/javascript" src="webgl.js"></script>
</head>
<body style="margin: 0;">
</body>
</html>
and my JS:
var scene, camera, renderer;
init();
animate();
function init(){
scene = new THREE.Scene();
var width = window.innerWidth, height = window.innerHeight;
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
}
This does not work, I get "Uncaught TypeError: Cannot read property 'appendChild' of null "
But when I move my webgl.js script to the body, it renders the canvas element properly.
Why does this happen? Why does my script need to be in the body to append the dom element? The renderer object returns an canvas in my console, so what's the issue?
This is actually something I've solved, but for curiousity sake, I was wondering if someone could explain why this is the case?
Here's my html:
<!DOCTYPE html>
<html>
<head>
<script src="three.min.js"></script>
<script src="OrbitControls.js"></script>
<script type="text/javascript" src="webgl.js"></script>
</head>
<body style="margin: 0;">
</body>
</html>
and my JS:
var scene, camera, renderer;
init();
animate();
function init(){
scene = new THREE.Scene();
var width = window.innerWidth, height = window.innerHeight;
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
}
This does not work, I get "Uncaught TypeError: Cannot read property 'appendChild' of null "
But when I move my webgl.js script to the body, it renders the canvas element properly.
Why does this happen? Why does my script need to be in the body to append the dom element? The renderer object returns an canvas in my console, so what's the issue?
Share Improve this question asked Nov 7, 2014 at 19:47 jorblumejorblume 6077 silver badges20 bronze badges1 Answer
Reset to default 5When the script is called inside the head, the body does not exist yet. You can see this by calling console.log(document.body)
in both the head and the body. You'll find that document.body
is null
because the body has yet to be created.
This is actually something I've solved, but for curiousity sake, I was wondering if someone could explain why this is the case?
Here's my html:
<!DOCTYPE html>
<html>
<head>
<script src="three.min.js"></script>
<script src="OrbitControls.js"></script>
<script type="text/javascript" src="webgl.js"></script>
</head>
<body style="margin: 0;">
</body>
</html>
and my JS:
var scene, camera, renderer;
init();
animate();
function init(){
scene = new THREE.Scene();
var width = window.innerWidth, height = window.innerHeight;
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
}
This does not work, I get "Uncaught TypeError: Cannot read property 'appendChild' of null "
But when I move my webgl.js script to the body, it renders the canvas element properly.
Why does this happen? Why does my script need to be in the body to append the dom element? The renderer object returns an canvas in my console, so what's the issue?
This is actually something I've solved, but for curiousity sake, I was wondering if someone could explain why this is the case?
Here's my html:
<!DOCTYPE html>
<html>
<head>
<script src="three.min.js"></script>
<script src="OrbitControls.js"></script>
<script type="text/javascript" src="webgl.js"></script>
</head>
<body style="margin: 0;">
</body>
</html>
and my JS:
var scene, camera, renderer;
init();
animate();
function init(){
scene = new THREE.Scene();
var width = window.innerWidth, height = window.innerHeight;
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
}
This does not work, I get "Uncaught TypeError: Cannot read property 'appendChild' of null "
But when I move my webgl.js script to the body, it renders the canvas element properly.
Why does this happen? Why does my script need to be in the body to append the dom element? The renderer object returns an canvas in my console, so what's the issue?
Share Improve this question asked Nov 7, 2014 at 19:47 jorblumejorblume 6077 silver badges20 bronze badges1 Answer
Reset to default 5When the script is called inside the head, the body does not exist yet. You can see this by calling console.log(document.body)
in both the head and the body. You'll find that document.body
is null
because the body has yet to be created.
本文标签: javascriptThreeJSappendChild and returningStack Overflow
版权声明:本文标题:javascript - Three.js, appendChild and returning - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745611474a2159027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论