圣诞树网页代码html
时间: 2023-11-22 18:50:35 浏览: 210
以下是一个简单的3D圣诞树的HTML代码,你可以将其复制到HTML文件中并在浏览器中打开以查看效果:
```html
<!DOCTYPE html>
<html>
<head>
<title>3D圣诞树</title>
<meta charset="utf-8">
<style type="text/css">
body {
background-color: #000;
margin: 0;
padding: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script type="text/javascript">
var container; var camera, scene, renderer;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 250;
scene = new THREE.Scene();
var geometry = new THREE.Geometry();
var material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.5 } );
var drawCount = 0;
var maxPoints = 30;
var radius = 100;
var angleStep = Math.PI*2/maxPoints;
for ( var i = 0; i <= maxPoints; i ++ ) {
var angle = i * angleStep;
var x = Math.cos( angle ) * radius;
var y = Math.sin( angle ) * radius;
var z = 0;
geometry.vertices.push( new THREE.Vector3( x, y, z ) );
drawCount ++;
}
geometry.drawRange.count = drawCount;
var line = new THREE.Line( geometry, material );
scene.add( line );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX ) / 2;
mouseY = ( event.clientY - windowHalfY ) / 2;
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;
camera.lookAt( scene.position );
renderer.render( scene, camera );
}
</script>
</body>
</html>
```
阅读全文