当前位置:首页 > JavaScript

js实现烟雾

2026-02-01 03:13:55JavaScript

烟雾效果的实现方法

在JavaScript中实现烟雾效果可以通过Canvas或WebGL技术来完成。以下是几种常见的实现方式:

使用Canvas绘制粒子系统

创建一个基于Canvas的粒子系统来模拟烟雾效果:

js实现烟雾

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const particles = [];
const particleCount = 100;

class Particle {
  constructor() {
    this.x = canvas.width / 2;
    this.y = canvas.height;
    this.size = Math.random() * 15 + 5;
    this.speedX = Math.random() * 3 - 1.5;
    this.speedY = Math.random() * -3 - 1;
    this.color = `rgba(200, 200, 200, ${Math.random() * 0.5})`;
  }

  update() {
    this.x += this.speedX;
    this.y += this.speedY;
    if (this.size > 0.2) this.size -= 0.1;
  }

  draw() {
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fill();
  }
}

function init() {
  for (let i = 0; i < particleCount; i++) {
    particles.push(new Particle());
  }
}

function animate() {
  ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  for (let i = 0; i < particles.length; i++) {
    particles[i].update();
    particles[i].draw();

    if (particles[i].size <= 0.2) {
      particles.splice(i, 1);
      i--;
    }
  }

  if (particles.length < particleCount) {
    particles.push(new Particle());
  }

  requestAnimationFrame(animate);
}

init();
animate();

使用WebGL实现更真实的烟雾

对于更真实的烟雾效果,可以使用WebGL和着色器:

js实现烟雾

// 需要Three.js库
import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建烟雾纹理
const smokeTexture = new THREE.TextureLoader().load('smoke.png');
const smokeMaterial = new THREE.MeshLambertMaterial({
  color: 0x888888,
  map: smokeTexture,
  transparent: true,
  opacity: 0.4
});

const smokeParticles = [];
for (let i = 0; i < 50; i++) {
  const particle = new THREE.Mesh(
    new THREE.PlaneGeometry(10, 10),
    smokeMaterial
  );
  particle.position.set(
    Math.random() * 50 - 25,
    Math.random() * 50 - 25,
    Math.random() * 50 - 25
  );
  particle.rotation.z = Math.random() * 360;
  scene.add(particle);
  smokeParticles.push(particle);
}

camera.position.z = 50;

function animate() {
  smokeParticles.forEach(p => {
    p.rotation.z += 0.01;
    p.position.y += 0.1;
    if (p.position.y > 25) p.position.y = -25;
  });
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

animate();

使用CSS动画实现简单烟雾

对于简单的UI效果,可以使用CSS动画:

<div class="smoke"></div>

<style>
.smoke {
  position: absolute;
  width: 50px;
  height: 50px;
  background: radial-gradient(circle, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0) 70%);
  border-radius: 50%;
  animation: smoke 4s ease-out infinite;
}

@keyframes smoke {
  0% {
    transform: translateY(0) scale(0.5);
    opacity: 0.8;
  }
  100% {
    transform: translateY(-100px) scale(2);
    opacity: 0;
  }
}
</style>

性能优化建议

对于大量粒子,使用对象池技术回收和重用粒子对象。考虑使用Web Workers处理粒子计算,避免阻塞主线程。在移动设备上减少粒子数量以确保流畅运行。

烟雾效果的逼真程度取决于粒子行为、透明度变化和运动轨迹的随机性。通过调整这些参数可以获得不同的视觉效果。

标签: 烟雾js
分享给朋友:

相关文章

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document…

js实现图片轮播

js实现图片轮播

实现基础图片轮播 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS设置样式和动画效果,JavaScript处理轮播逻辑。 <div cl…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-contain…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…