webgl photo box

本文展示了如何使用Three.js库创建一个简单的3D立方体动画,包括设置场景、相机、光源、纹理映射和动画控制。通过添加鼠标事件监听,实现立方体的旋转效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

style="width: 100%; height: 300px" src="http://omni360.github.io/webgl/chapter2.1.html" allowfullscreen="allowfullscreen" frameborder="0">


以下是源码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>welcome to webgl chapter2.1</title>
			<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r68/three.min.js"></script>
        <script type="text/javascript">
        var renderer = null,
        	scene=null,
        	camera=null,
        	cube=null,
        	cubea=null,
        	animating=false;
        	function onload(){
        		//抓取作为Canvas容器的<div>标签
        		var container = document.getElementById("container");

        		//创建Three.js渲染器,并添加到<div>标签中
        		renderer = new THREE.WebGLRenderer( {antialias: true} );
        		renderer.setSize(container.offsetWidth,container.offsetHeight)
        		container.appendChild(renderer.domElement);

        		//创建Three.js场景
        		scene = new THREE.Scene();
        		
        		//创建相机,并添加到场景中。
        		camera=new THREE.PerspectiveCamera( 45, container.offsetWidth/container.offsetHeight, 1, 4000 );
        		camera.position.set(0,0,3);

        		//创建一个平行光光源照射到物体上。
        		var light = new THREE.DirectionalLight( 0xffffff,1.5 );
        		light.position.set(0,0,1);
        		scene.add(light);

        		//创建一个接受光照并带有纹理映射的立方体,并添加到场景中
        		//首先,创建一个带纹理映射的立方体。
        		var mapUrl="http://sandbox.runjs.cn/uploads/rs/471/kdnkkh9t/rocy.hou.jpg";
        		var map = THREE.ImageUtils.loadTexture(mapUrl);

        		//创建一个接受光照并带有纹理映射的立方体,并添加到场景中
        		//首先,创建一个带纹理映射的立方体。
        		var mapUrla="http://sandbox.runjs.cn/uploads/rs/471/kdnkkh9t/joe1.jpg";
        		var mapa = THREE.ImageUtils.loadTexture(mapUrla);

        		//然后创建一个Phong材质来处理着色,并传递给纹理映射。
        		var material = new THREE.MeshPhongMaterial( {map:map} );

        		//然后创建一个Phong材质来处理着色,并传递给纹理映射。
        		var materiala = new THREE.MeshPhongMaterial( {map:mapa} );

        		//创建一个立方体的几何体
        		var geometry = new THREE.CubeGeometry( 1, 1, 1 );

        		//创建一个立方体的几何体
        		var geometrya = new THREE.CubeGeometry( 1, 1, 1 );

        		//将几何体和材质放到一个网格中
        		cube = new THREE.Mesh(geometry, material);
        		cube.position.set(-1.5,0,0);

        		//将几何体和材质放到一个网格中
        		cubea = new THREE.Mesh(geometrya, materiala);
        		cubea.position.set(1.5,0,0);

	    		//设置网格在场景中的朝向,否则我们将不会看到立方体的形状。
        		cube.rotation.x=Math.PI /5;
        		cube.rotation.y=Math.PI/5;

	    		//设置网格在场景中的朝向,否则我们将不会看到立方体的形状。
        		cubea.rotation.x=Math.PI /5;
        		cubea.rotation.y=Math.PI /5;


        		//将立方体网格添加到场景中
        		scene.add(cube);

        		//将立方体网格添加到场景中
        		scene.add(cubea);


        		//添加处理鼠标事件的函数,用于控制动画的开关。
        		addMouseHandler();

        		//运行渲染循环
        		run();

        	}
/*    		function onload()
			{
				// Grab our container div
		        var container = document.getElementById("container");

		        // Create the Three.js renderer, add it to our div
			    renderer = new THREE.WebGLRenderer( { antialias: true } );
			    renderer.setSize(container.offsetWidth, container.offsetHeight);
			    container.appendChild( renderer.domElement );

			    // Create a new Three.js scene
			    scene = new THREE.Scene();

			    // Put in a camera
		        camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 4000 );
		        camera.position.set( 0, 0, 3 );

		        // Create a directional light to show off the object
				var light = new THREE.DirectionalLight( 0xffffff, 1.5);
				light.position.set(0, 0, 1);
				scene.add( light );

		        // Create a shaded, texture-mapped cube and add it to the scene
		        // First, create the texture map
		        var mapUrl = "./images/rocy1.jpg";
		        var map = THREE.ImageUtils.loadTexture(mapUrl);
		        
		        // Now, create a Phong material to show shading; pass in the map
		        var material = new THREE.MeshPhongMaterial({ map: map });

		        // Create the cube geometry
		        var geometry = new THREE.CubeGeometry(1, 1, 1);

		        // And put the geometry and material together into a mesh
		        cube = new THREE.Mesh(geometry, material);

		        // Turn it toward the scene, or we won't see the cube shape!
		        cube.rotation.x = Math.PI / 5;
		        cube.rotation.y = Math.PI / 5;

		        // Add the cube to our scene
		        scene.add(cube);

		        // Add a mouse up handler to toggle the animation
		        addMouseHandler();

		        // Run our render loop
		        run();
			}*/
        	function run(){
        		//渲染场景
        		renderer.render(scene,camera);

        		//在下一帧中旋转立方体
        		if(animating){
        			cube.rotation.y-=0.01;
        			cubea.rotation.y+=0.01;
        		}

        		//在另一帧中回调
        		requestAnimationFrame(run);
        	}
        	function addMouseHandler(){
        		var dom = renderer.domElement;
        		dom.addEventListener('mouseup',onMouseUp,false);
        	}
        	function onMouseUp(event){
        		event.preventDefault();
        		animating= !animating;
        	}
        </script>
    </head>
    <body onload="onload()" style="">
    	<center><h1>Rocy and Joe!</h1></center>
    	<div id="container" style="width:95%; height:80%;position:absolute;"></div>
    	<div id="prompt" style="width:95%; height:6%;bottom:0;position:absolute;">
    		Click to animate the cube
    	</div>

    </body>
</html>


requestAnimationFrame源码:

/**
 * Provides requestAnimationFrame in a cross browser way.
 * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 */

if ( !window.requestAnimationFrame ) {

	window.requestAnimationFrame = ( function() {

		return window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {

			window.setTimeout( callback, 1000 / 60 );

		};

	} )();

}

css源码:

* {
    font-family: Arial;
    font-style: italic;
}

body {
	background-color:#eeeeee;
	color:#212121;
}

#container {
	z-index:-1;
	background-color:#eeeeee;

}






<think>好的,用户想要实现一个3D立体图片相册功能,需要代码示例。首先,我需要确定用户的技术背景和需求。可能用户是前端开发者,熟悉HTML、CSS和JavaScript,想通过Web技术实现3D效果。 接下来,考虑如何实现3D效果。常用的方法有CSS 3D变换和WebGL。对于简单的相册,CSS可能更简单,适合快速实现。但WebGL能提供更复杂的3D效果,但学习曲线陡峭。用户可能需要一个简单易懂的示例,所以先考虑CSS方案。 然后,参考现有的技术文档或示例。例如,使用CSS的transform-style: preserve-3d来创建3D空间,结合JavaScript处理用户交互,比如鼠标拖动旋转。需要结构分为场景、相册容器和图片项。图片排列在3D空间中的圆柱体或球体表面,通过旋转展示。 另外,可能需要响应式设计,适应不同屏幕尺寸。动画方面,使用CSS过渡或JavaScript的requestAnimationFrame实现平滑效果。交互方面,监听鼠标事件,计算移动距离并转换为旋转角度。 还需要检查浏览器兼容性,CSS 3D在现代浏览器支持良好,但旧版本可能需要前缀。代码示例应尽量简洁,突出核心逻辑,避免复杂配置。 最后,考虑用户可能需要进一步的功能,比如点击放大、过渡效果或动态加载图片。在相关问题中,可以建议WebGL库如Three.js的示例,优化性能的方法,以及移动端适配技巧,帮助用户扩展功能。</think>以下是一个基于Web技术(HTML/CSS/JavaScript)实现基础3D立体图片相册的代码示例,使用CSS 3D变换和透视原理实现立体效果: ```html <!DOCTYPE html> <html> <head> <style> .gallery-container { perspective: 1000px; width: 800px; height: 500px; margin: 100px auto; } .carousel { position: relative; transform-style: preserve-3d; transition: transform 1s; width: 200px; height: 200px; margin: 0 auto; } .photo { position: absolute; width: 200px; height: 200px; border: 2px solid white; box-shadow: 0 0 10px rgba(0,0,0,0.3); transform-origin: center center; backface-visibility: hidden; } </style> </head> <body> <div class="gallery-container"> <div class="carousel" id="carousel"> <!-- 图片将通过JavaScript动态插入 --> </div> </div> <script> const carousel = document.getElementById('carousel'); const photoCount = 8; // 图片数量 const radius = 300; // 3D旋转半径 // 动态创建图片元素 for(let i = 0; i < photoCount; i++) { const angle = (i * 360) / photoCount; const photo = document.createElement('img'); photo.className = 'photo'; photo.src = `path/to/image${i+1}.jpg`; // 替换为实际图片路径 photo.style.transform = ` rotateY(${angle}deg) translateZ(${radius}px) `; carousel.appendChild(photo); } // 鼠标交互控制 let rotateX = 0; let rotateY = 0; let isDragging = false; let startX, startY; carousel.addEventListener('mousedown', (e) => { isDragging = true; startX = e.clientX; startY = e.clientY; }); document.addEventListener('mousemove', (e) => { if (!isDragging) return; const deltaX = e.clientX - startX; const deltaY = e.clientY - startY; rotateY += deltaX * 0.5; rotateX -= deltaY * 0.2; carousel.style.transform = ` rotateX(${rotateX}deg) rotateY(${rotateY}deg) `; startX = e.clientX; startY = e.clientY; }); document.addEventListener('mouseup', () => { isDragging = false; }); </script> </body> </html> ``` 关键实现原理: 1. 使用CSS的`perspective`属性创建3D透视空间 2. 通过`transform-style: preserve-3d`保持3D变换上下文 3. 使用三角函数计算图片在3D空间中的位置,形成环形布局 4. 通过鼠标事件实现交互式旋转控制 5. 动态生成图片元素并应用3D变换矩阵
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值