<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <meta name="keywords" content=""> <meta name="description" content=""> <style type="text/css"> .dt_banner { 100%; height: 710px; background-color: #0a2240; background-size: cover; position: relative; overflow: hidden; } .abc { position: absolute; top: 0px; left: 0; z-index: 1; } </style> </head> <!--banner--> <div class="dt_banner"></div> <script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script> <script src="js/three.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> //SEPARATION控制密度,AMOUNTX控制x轴,AMOUNTY控制Y轴 var SEPARATION = 60, AMOUNTX = 70, AMOUNTY = 20; var container; // 1.scene(场景): 场景中包含了所有的3D对象数据 // 2.camera(相机): 相机有位置(position),旋转(rotation)和视野属性(field of view) // 3.renderer(渲染器): 决定场景中的一个物体在照相机的视角看来是什么样子 var camera, scene, renderer; var banner = $(".dt_banner") var particles, particle, count = 0; var mouseX = 0, mouseY = 0; var windowHalfX = window.innerWidth / 2; var windowHalfY = window.innerHeight / 2; init(); animate(); function init() { console.log(banner) container = document.createElement('div'); // container = container.setAttribute('class','abc'); // 将渲染器的canvas domElement加入到body中 banner.append(container); // 照相机参数 camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 1, 10000); // 将相机向后(即屏幕外)移 camera.position.z = 1000; scene = new THREE.Scene(); //将粒子加入到particles数组中 particles = new Array(); var PI2 = Math.PI * 2; var material = new THREE.ParticleCanvasMaterial({ //修改小点颜色 color: 0xffffff, // transparent: true,//是否透明 // opacity:0.5, program: function(context) { context.beginPath(); context.arc(0, 0, 1, 0, PI2, false); context.fill(); } }); var i = 0; for(var ix = 0; ix < AMOUNTX; ix++) { for(var iy = 0; iy < AMOUNTY; iy++) { particle = particles[i++] = new THREE.Particle(material); particle.position.x = ix * SEPARATION - ((AMOUNTX * SEPARATION) / 2); particle.position.z = iy * SEPARATION - ((AMOUNTY * SEPARATION) / 1.3); scene.add(particle); } } renderer = new THREE.CanvasRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); container.appendChild(renderer.domElement); container = container.setAttribute('class', 'abc'); // document.addEventListener( 'mousemove', onDocumentMouseMove, false ); // document.addEventListener( 'touchstart', onDocumentTouchStart, false ); // document.addEventListener( 'touchmove', onDocumentTouchMove, 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; mouseY = event.clientY - windowHalfY; } function onDocumentTouchStart(event) { if(event.touches.length === 1) { event.preventDefault(); mouseX = event.touches[0].pageX - windowHalfX; mouseY = event.touches[0].pageY - windowHalfY; } } function onDocumentTouchMove(event) { if(event.touches.length === 1) { event.preventDefault(); mouseX = event.touches[0].pageX - windowHalfX; mouseY = event.touches[0].pageY - windowHalfY; } } // 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); var i = 0; for(var ix = 0; ix < AMOUNTX; ix++) { for(var iy = 0; iy < AMOUNTY; iy++) { particle = particles[i++]; // particle.position.x = (Math.asin((iy + count) * 0.3) * 500); particle.position.y = (Math.sin((ix + count) * 0.3) * 200) + (Math.cos((iy + count) * 0.3) * 150); particle.scale.x = particle.scale.y = (Math.sin((ix + count) * 0.3) + 1) * 2 + (Math.sin((iy + count) * 0.5) + 1) * 2; // particle.scale.z = particle.scale.y = (Math.sin((ix + count) * 0.3) + 1) * 2 + (Math.sin((iy + count) * 0.5) + 1) * 2; } } renderer.render(scene, camera); count += 0.03; } </script> </html>
three.js - http://github.com/mrdoob/three.js
效果图: