当前位置:首页 > 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手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

js实现弹框

js实现弹框

使用 alert 实现简单弹框 JavaScript 的 alert 方法可以直接显示一个简单的弹框,内容为纯文本: alert("这是一个简单的弹框"); 使用 confirm 实现确认弹框…

js 实现mvc

js 实现mvc

MVC 模式简介 MVC(Model-View-Controller)是一种软件设计模式,将应用程序分为三个核心组件:模型(Model)处理数据和业务逻辑,视图(View)负责展示数据,控制器(Con…

js实现刷新

js实现刷新

刷新当前页面 使用 location.reload() 方法可以强制刷新当前页面。该方法接受一个布尔参数,默认为 false,表示从缓存中加载页面;设为 true 时强制从服务器重新加载。 // 从…

vue js实现弹窗

vue js实现弹窗

Vue.js 实现弹窗的方法 使用组件实现弹窗 创建一个独立的弹窗组件,例如 Modal.vue,包含弹窗的 HTML 结构和样式。在组件中定义 props 控制弹窗的显示与隐藏。 <temp…