我知道这是一个愚蠢的问题,但这对我来说很重要,我遇到的第一个问题是当我设置div目标时,我总是在页面底部有2px空间我怎么能解决这个问题
.canvas_3d {
width: calc(100vw - 300px);
background: purple;
height: calc(100vh - 2px);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div (window:resize)="onResize($event)" id="canvas_3D" class="canvas_3d"></div>
</body>
</html>
// The target 3d container.
container: any;
/**
* The 3D canvas Width & Height
*/
w: any;
h: any;
// Scene, Camera and Rendered variables.
scene: any;
camera: any;
renderer: any;
constructor() { }
ngOnInit() {
this.init3D();
}
init3D() {
this.container = document.getElementById('canvas_3D');
this.w = this.container.clientWidth - 2;
this.h = this.container.clientHeight - 2;
console.log('W: ', this.w);
console.log('H: ', this.h);
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera( 75, this.w / this.h, 0.1, 1000 );
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize( this.w, this.h );
this.container.appendChild( this.renderer.domElement );
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
this.scene.add( cube );
this.camera.position.z = 5;
this.createMesh();
const animate = () => {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
this.renderer.render(this.scene, this.camera);
};
animate();
}