h5 3d代码
时间: 2023-10-10 22:14:56 浏览: 42
以下是一个简单的 h5 3d 代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>h5 3d 示例</title>
<style>
#container {
width: 800px;
height: 600px;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script>
// 初始化场景、相机、渲染器
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, 800 / 600, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(800, 600);
document.getElementById("container").appendChild(renderer.domElement);
// 添加立方体
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({color: 0x00ff00});
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// 设置相机位置
camera.position.z = 5;
// 渲染场景
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```
该代码演示了如何使用 Three.js 库创建简单的 3D 场景,并在场景中添加一个立方体。在渲染场景时,立方体会每帧旋转一定角度,从而呈现出 3D 效果。