[英]width different from 100% for div container
I have a simple WebGL code which displays a 3D graph. 我有一个显示3D图形的简单WebGL代码。 It works well but I would like to set a value for the width different from 100%; 效果很好,但是我想为宽度设置一个不同于100%的值; ie I would like to put the webGL animation in a small box. 即我想把webGL动画放在一个小盒子里。
Below my code; 在我的代码下面; I tried to put the WebGL into a 500px box with CSS "height" and "width" but it doesn't work : the animation still takes 100% of width : 我试图将WebGL放到CSS为“ height”和“ width”的500px框中,但是它不起作用:动画仍然占用宽度的100%:
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - trackball controls</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: #000;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
background-color: #fff;
margin: 0px;
overflow: hidden;
}
.container {
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<center><div id="container"></div></center>
<script src="three.js/build/three.min.js"></script>
<script src="three.js/examples/js/controls/TrackballControls.js"></script>
<script src="three.js/examples/js/Detector.js"></script>
<script src="three.js/examples/js/libs/stats.min.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var container, stats;
var camera, controls, scene, renderer;
var cross;
init();
animate();
function init() {
...
}
function onWindowResize() {
...
}
function animate() {
requestAnimationFrame( animate );
controls.update();
// For displaying
render();
}
function render() {
renderer.render( scene, camera );
stats.update();
}
</script>
</body>
</html>
How to circumvent this issue ? 如何规避这个问题?
Thanks 谢谢
UPDATE 1 : 更新1:
Ok sorry for the class-id confusion. 好的,很抱歉您对class-id感到困惑。
Concerning the div container, I am using appenChild
in WebGL
code to add a renderer domElement
. 关于div容器,我在WebGL
代码中使用appenChild
添加了renderer domElement
。
With the following code : 使用以下代码:
<html lang="en">
<head>
<title>three.js webgl - trackball controls</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: #000;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
background-color: #fff;
margin: 0px;
overflow: hidden;
}
#container {
width: 300px;
height: 300px;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="three.js/build/three.min.js"></script>
<script src="three.js/examples/js/controls/TrackballControls.js"></script>
<script src="three.js/examples/js/Detector.js"></script>
<script src="three.js/examples/js/libs/stats.min.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
...
</script>
</body>
</html>
I don't get a centered box with WebGL animation. 我没有获得带有WebGL动画的居中框。 The WebGL window is shifted on the right like this (width is set to 300 px) but it is not centered like I would. WebGL窗口像这样向右移动(宽度设置为300 px),但没有像我那样居中。
PS: margin-left:auto and margin-right:auto don't seem to work for centering. PS:margin-left:auto和margin-right:auto似乎不适合居中。
Thanks. 谢谢。
There are multiple issues with your html. 您的html有多个问题。
your container is defined in your css as a class, you have it in your html as an id 您的容器在CSS中定义为类,在html中将其作为ID
you have nothing in the div container div容器中没有任何内容
<center></center>
is deprecated Use margin-left:auto; margin:right:auto;
不推荐使用<center></center>
用途margin-left:auto; margin:right:auto;
margin-left:auto; margin:right:auto;
in your css. 在你的CSS。
Give that another go and see how you get on 再试一次,看看你过得如何
1) 1)
The canvas is still 100% of the page width because you probably have that set up in your onWindowResize
function. 画布仍然是页面宽度的100%,因为您可能已在onWindowResize
函数中进行了设置。 In order for your canvas to be a certain size, you need to tell three.js that, not CSS. 为了使画布达到特定大小,您需要告诉three.js,而不是CSS。
To highlight, these couple lines are dependent on width and height: 突出显示,这些几行取决于宽度和高度:
camera = new THREE.PerspectiveCamera(50, width / height, 1, 10000);
renderer.setSize(width, height);
2) 2)
You want to set margin-left: auto; margin-right: auto
您想设置margin-left: auto; margin-right: auto
margin-left: auto; margin-right: auto
to the canvas if your container is truly a "container." margin-left: auto; margin-right: auto
如果您的容器确实是“容器”,则margin-left: auto; margin-right: auto
转到画布。 Also, since canvas' display is by default inline
, you need to use display: block
. 另外,由于canvas的显示默认为inline
,因此您需要使用display: block
。
See it in action: http://jsfiddle.net/60uzdyss/ 实际观看: http : //jsfiddle.net/60uzdyss/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.