龙空技术网

利用three.js实现大波浪特效

东少笔记 536

前言:

当前兄弟们对“js间隔器”可能比较着重,同学们都想要了解一些“js间隔器”的相关知识。那么小编同时在网摘上搜集了一些关于“js间隔器””的相关资讯,希望兄弟们能喜欢,看官们快快来学习一下吧!

效果展示功能实现引入所需的库

<script src="js/three.js"></script><script src="js/Projector.js"></script><script src="js/CanvasRenderer.js"></script>
定义所需变量
var SEPARATION = 150, // 间隔    AMOUNTX = 100, // 控制X轴数量    AMOUNTY = 50; // 控制Y轴数量var camera, scene, renderer;var particles, particle, count = 0;
定义方法init,用来初始化相机、场景、粒子和渲染器;
  function init() {    // 相机    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);    camera.position.z = 1000;    camera.position.y = 500;    scene = new THREE.Scene(); // 场景    // 粒子    particles = new Array();    var PI2 = Math.PI * 2;    var material = new THREE.SpriteCanvasMaterial({      color: 0xffffff,      program: function(context) {        context.beginPath();        context.arc(0, 0, 0.5, 0, PI2, true);        context.fill();      }    });    var i = 0;    for (var ix = 0; ix < AMOUNTX; ix++) {      for (var iy = 0; iy < AMOUNTY; iy++) {        particle = particles[i++] = new THREE.Sprite(material);        particle.position.x = ix * SEPARATION - ((AMOUNTX * SEPARATION) / 2);        particle.position.z = iy * SEPARATION - ((AMOUNTY * SEPARATION) / 2);        scene.add(particle);      }    }    // 渲染器    renderer = new THREE.CanvasRenderer();    renderer.setPixelRatio(window.devicePixelRatio);    renderer.setSize(window.innerWidth, window.innerHeight);    document.getElementById('waves-box').appendChild(renderer.domElement);  }
定义方法animate,使用requestAnimationFrame进行动画渲染;
  function animate() {    requestAnimationFrame(animate);    render();  }
定义方法render,用来把所有粒子渲染到场景上;
  function render() {    var i = 0;    for (var ix = 0; ix < AMOUNTX; ix++) {      for (var iy = 0; iy < AMOUNTY; iy++) {        particle = particles[i++];        particle.position.y = (Math.sin((ix + count) * 0.3) * 50) +          (Math.sin((iy + count) * 0.5) * 50);        particle.scale.x = particle.scale.y = (Math.sin((ix + count) * 0.3) + 1) * 4 + (Math.sin((iy + count) * 0.5) + 1) * 4;      }    }    renderer.render(scene, camera);    count += 0.1;  }
全部代码如下。

Tips:在官方示例“canvas_particles_waves.html”基础上,修改一些参数和删除一些多余代码。

<!DOCTYPE html><html lang="en"><head>  <title>three.js canvas - particles - waves</title>  <meta charset="utf-8">  <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">  <style>  body {    background-color: #000000;    margin: 0px;    overflow: hidden;  }  #waves-box {    opacity: 0.6;  }</style></head><body>  <div id="waves-box"></div>  <script src="js/three.js"></script>  <script src="js/Projector.js"></script>  <script src="js/CanvasRenderer.js"></script>  <script>  var SEPARATION = 150, // 间隔    AMOUNTX = 100, // 控制X轴数量    AMOUNTY = 50; // 控制Y轴数量  var camera, scene, renderer;  var particles, particle, count = 0;  init();  animate();  function init() {    // 相机    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);    camera.position.z = 1000;    camera.position.y = 500;    scene = new THREE.Scene(); // 场景    // 粒子    particles = new Array();    var PI2 = Math.PI * 2;    var material = new THREE.SpriteCanvasMaterial({      color: 0xffffff,      program: function(context) {        context.beginPath();        context.arc(0, 0, 0.5, 0, PI2, true);        context.fill();      }    });    var i = 0;    for (var ix = 0; ix < AMOUNTX; ix++) {      for (var iy = 0; iy < AMOUNTY; iy++) {        particle = particles[i++] = new THREE.Sprite(material);        particle.position.x = ix * SEPARATION - ((AMOUNTX * SEPARATION) / 2);        particle.position.z = iy * SEPARATION - ((AMOUNTY * SEPARATION) / 2);        scene.add(particle);      }    }    // 渲染器    renderer = new THREE.CanvasRenderer();    renderer.setPixelRatio(window.devicePixelRatio);    renderer.setSize(window.innerWidth, window.innerHeight);    document.getElementById('waves-box').appendChild(renderer.domElement);  }  // 动画  function animate() {    requestAnimationFrame(animate);    render();  }  // 渲染  function render() {    var i = 0;    for (var ix = 0; ix < AMOUNTX; ix++) {      for (var iy = 0; iy < AMOUNTY; iy++) {        particle = particles[i++];        particle.position.y = (Math.sin((ix + count) * 0.3) * 50) +          (Math.sin((iy + count) * 0.5) * 50);        particle.scale.x = particle.scale.y = (Math.sin((ix + count) * 0.3) + 1) * 4 + (Math.sin((iy + count) * 0.5) + 1) * 4;      }    }    renderer.render(scene, camera);    count += 0.1;  }</script></body></html>

标签: #js间隔器 #免费js特效 #js选择特效