当前位置:首页 > 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 实现方法 JavaScript 是一种广泛使用的编程语言,适用于网页开发、服务器端编程以及移动应用开发。以下是几种常见的 JavaScript 实现方法: 网页交互功能 使用…

css3结合js制作

css3结合js制作

CSS3 结合 JavaScript 制作动态效果 CSS3 和 JavaScript 结合可以实现丰富的动态效果,包括动画、交互和响应式设计。以下是几种常见的实现方式: CSS3 动画与 Java…

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js 进度条的实现

js 进度条的实现

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

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js实现报表

js实现报表

使用JavaScript实现报表 在JavaScript中实现报表功能可以通过多种方式完成,常见的方法包括使用原生JavaScript、第三方库(如Chart.js、D3.js)或结合后端数据渲染。以…