<!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>旋转</title> <style type="text/css"> div#canvas-frame { border: none; cursor: pointer; width: 100%; height: 600px; background-color: #EEEEEE; } </style> <script type="text/javascript" src="js/three.js"></script> <script src="js/stats.min.js"></script> <script src="js/tween.min.js"></script> <script> var renderer,stats; //照相机配置 var fov = 45,//拍摄距离 视野角值越大,场景中的物体越小 near = 1,//最小范围 far = 1000;//最大范围 //var canvas = document.getElementById('canvas-frame'); function initThree(){ width = document.getElementById('canvas-frame').clientWidth; height = document.getElementById('canvas-frame').clientHeight; renderer = new THREE.WebGLRenderer({ antialias:true }); renderer.setSize(width, height); document.getElementById('canvas-frame').appendChild(renderer.domElement); renderer.setClearColor(0xFFFFFF, 1.0); stats = new Stats(); stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px'; document.getElementById('canvas-frame').appendChild(stats.domElement); } var camera; function initCamera() { camera = new THREE.PerspectiveCamera(fov, width / height, near, far); camera.position.x = 100; camera.position.y = 300; camera.position.z = 600; camera.up.x = 0; camera.up.y = 1; camera.up.z = 0; camera.lookAt({ x : 0, y : 0, z : 0 }); } var scene; function initScene() { scene = new THREE.Scene(); } var light; function initLight() { light = new THREE.AmbientLight(0xFF0000); light.position.set(100, 100, 200); scene.add(light); } var cube; var mesh; function initObject() { var geometry = new THREE.BoxGeometry( 100, 100, 100 ); for ( var i = 0; i < geometry.faces.length; i += 2 ) { var hex = Math.random() * 0xffffff; geometry.faces[ i ].color.setHex( hex ); geometry.faces[ i + 1 ].color.setHex( hex ); } var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors} ); mesh = new THREE.Mesh( geometry,material); mesh.position = new THREE.Vector3(0,0,0); scene.add(mesh); } function initGrid(){ var helper = new THREE.GridHelper( 1000, 50 ); helper.setColors( 0x0000ff, 0x808080 ); scene.add( helper ); } function threeStart() { initThree(); initCamera(); initScene(); initLight(); initObject(); initGrid(); animation(); document.getElementById('canvas-frame').addEventListener('mousewheel',mousewheel,false); } // 帧循环、游戏循环 function animation() { // mesh.rotation.y +=0.01; // renderer.render(scene, camera); // requestAnimationFrame(animation); //相机围绕y轴旋转,并且保持场景中的物体一直再相机的视野中 //实时渲染成像 var timer = Date.now()*0.0001; camera.position.x = Math.cos(timer)*100; camera.position.z = Math.sin(timer)*100; camera.lookAt(scene.position); renderer.render(scene, camera); requestAnimationFrame(animation); } function mousewheel(e){ e.preventDefault(); if(e.wheelDelta){ //判断浏览器是谷歌、ie滑轮事件 if(e.wheelDelta > 0){ //滑轮向上滚动 fov -=(near < fov ? 1 : 0); } if (e.wheelDelta < 0) { //当滑轮向下滚动时 fov += (fov < far ? 1 : 0); } }else if(e.detail){//firefox滑轮事件 if (e.detail > 0) { //当滑轮向上滚动时 fov -= 1; } if (e.detail < 0) { //当滑轮向下滚动时 fov += 1; } } //改变fov值,并更新场景的渲染 camera.fov = fov; camera.updateProjectionMatrix(); renderer.render(scene, camera); } </script> </head> <body onload="threeStart()"> <div id="canvas-frame"></div> </body> </html>
上述是代码,下面效果图如下: