html3d粒子球,Canvas粒子系统:3D球体

前些日子在Codepen上看到了一个很惊艳的3D特效,一时惊叹,fork下来后,读了一下作者的源码,200多行,十分精简,但是内劲无穷。这里和大家分享一下作者的思路和一些基础的数学知识,

希望能给大家带来一点思考和启发,先来看一个效果:

动画生成过程

创建canvas,设置canvas中心点,变量初始化:function setParameters

生成指定数量的例子 同时完成例子的初始化(设置粒子x,y,z 以及增量vx,vy,vz):function createParticles

初始化不同形状:function setupFigure

给click(立即切换到下一图形) 和 onmouseover(旋转), 绑定this 因为addEventListener 会把this 绑定在DOM上:function reconstructMethod

bindEvent 给 click(立即切换到下一图形) 和 onmouseover(旋转) 绑定事件

绘图:function drawFigure

其中涉及坐标旋转的代码可以参照之前3D旋转球的博文 有详细说明 这里不做赘述:如何用Canvas做一个3D球

重点 (绘图: drawFigure)

球体制作在之前的博文里提到过,不做赘述.

对于环形制作,看看源码里的环形公式:

createTorus : function(){

var theta = Math.random() * Math.PI * 2,

x = this.SCATTER_RADIUS + this.SCATTER_RADIUS / 6 * Math.cos(theta),

y = this.SCATTER_RADIUS / 6 * Math.sin(theta),

phi = Math.random() * Math.PI * 2;

return {

x : x * Math.cos(phi),

y : y,

z : x * Math.sin(phi),

hue : Math.round(phi / Math.PI * 30)

};

}

环形 x, y, z 推导 (x屏幕面的x方向, y屏幕面的y方向, z 垂直于屏幕面的方向)

96e4f7e7ac7ce4049cd7c64287c9d5b1.png

环形的z轴是相对屏幕对称的。

圆锥制作,看看源码里的公式

createCone : function(){

var status = Math.random() > 1 / 3,

x,

y,

phi = Math.random() * Math.PI * 2,

rate = Math.tan(30 / 180 * Math.PI) / this.CONE_ASPECT_RATIO;

if(status){

y = this.SCATTER_RADIUS * (1 - Math.random() * 2);

x = (this.SCATTER_RADIUS - y) * rate;

}else{

y = -this.SCATTER_RADIUS;

x = this.SCATTER_RADIUS * 2 * rate * Math.random();

}

return {

x : x * Math.cos(phi),

y : y,

z : x * Math.sin(phi),

hue : Math.round(phi / Math.PI * 30)

};

},

5a3e59297249baea0e9e20a7b1fcb5db.png

这里 tana 被作者替换成 1.5 tan30, 这里我们可以理解为a 为30,宽高比为1.5。

花瓶形(不知道数学里叫什么) 看看源码里的公式

createVase : function(){

var theta = Math.random() * Math.PI,

// x = Math.abs(this.SCATTER_RADIUS * Math.cos(theta) / 2) + this.SCATTER_RADIUS / 8,

x = Math.abs(this.SCATTER_RADIUS * Math.cos(theta) / 2) + this.SCATTER_RADIUS / 8,

y = this.SCATTER_RADIUS * Math.cos(theta) * 1.2,

phi = Math.random() * Math.PI * 2;

return {

x : x * Math.cos(phi),

y : y,

z : x * Math.sin(phi),

hue : Math.round(phi / Math.PI * 30)

};

}

d3c18248a249278a1756b1a63071bd61.png

结语

Canvas 3D 坐标转换可谓基础,掌握好canvas坐标转换,配合图形的三维方程,可以让粒子做出美妙的运动,释放无穷尽的想象。

82d80b7da3e8be3b625a240a2127e454.png

一名在校研究生,热爱前端,热爱英语,热爱摄影,喜欢一切能表达情绪的东西,喜欢黑色,喜欢秋天,喜欢孤独。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个3D粒子旋转,可以使用Three.js库来实现。下面是一个简单的实现思路: 1. 首先创建一个Canvas元素,并设置它的宽度和高度。然后获取Canvas的上下文对象。 2. 使用Three.js创建一个场景、相机和渲染器。将渲染器的输出目标设置为Canvas。 3. 创建粒子模型。可以使用THREE.Points和THREE.BufferGeometry等来创建粒子模型,然后将粒子模型添加到场景中。 4. 实现粒子的旋转动画。可以使用requestAnimationFrame来实现平滑的动画效果。 下面是一个示例代码,可以参考一下: ```html <canvas id="canvas"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r121/three.min.js"></script> <script> const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const width = canvas.width = window.innerWidth; const height = canvas.height = window.innerHeight; // 创建场景、相机和渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000); camera.position.z = 500; const renderer = new THREE.WebGLRenderer({ canvas }); renderer.setSize(width, height); // 创建粒子模型 const particleCount = 10000; const particles = new THREE.BufferGeometry(); const positions = new Float32Array(particleCount * 3); const colors = new Float32Array(particleCount * 3); for (let i = 0; i < particleCount; i++) { const x = Math.random() * 200 - 100; const y = Math.random() * 200 - 100; const z = Math.random() * 200 - 100; positions[i * 3] = x; positions[i * 3 + 1] = y; positions[i * 3 + 2] = z; colors[i * 3] = Math.random(); colors[i * 3 + 1] = Math.random(); colors[i * 3 + 2] = Math.random(); } particles.setAttribute('position', new THREE.BufferAttribute(positions, 3)); particles.setAttribute('color', new THREE.BufferAttribute(colors, 3)); particles.computeBoundingSphere(); const particleMaterial = new THREE.PointsMaterial({ size: 3, vertexColors: true, }); const particleSystem = new THREE.Points(particles, particleMaterial); scene.add(particleSystem); // 实现粒子的旋转动画 function animate() { requestAnimationFrame(animate); particleSystem.rotation.x += 0.01; particleSystem.rotation.y += 0.01; renderer.render(scene, camera); } animate(); </script> ``` 这段代码使用Three.js创建了一个场景、相机和渲染器,并创建了一个包含10000个随机位置和颜色的粒子模型。然后使用requestAnimationFrame实现了粒子的旋转动画。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值