当前位置:首页 > JavaScript

js实现烟雾

2026-04-05 19:36:15JavaScript

烟雾效果实现方法

在JavaScript中实现烟雾效果通常涉及Canvas绘图或WebGL技术。以下是两种常见的实现方式:

js实现烟雾

Canvas 2D烟雾效果

使用HTML5 Canvas的2D上下文创建动态烟雾效果:

js实现烟雾

const canvas = document.getElementById('smokeCanvas');
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.05)';
  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);
      particles.push(new Particle());
    }
  }

  requestAnimationFrame(animate);
}

init();
animate();

WebGL/Three.js烟雾效果

使用Three.js库创建更逼真的3D烟雾:

import * as THREE from 'three';
import { GPUComputationRenderer } from 'three/examples/jsm/misc/GPUComputationRenderer';

// 初始化场景
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建烟雾纹理
const smokeTexture = new THREE.TextureLoader().load('smoke.png');

// 创建粒子系统
const particleCount = 1000;
const particles = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
const scales = new Float32Array(particleCount);

for (let i = 0; i < particleCount; i++) {
  positions[i * 3] = (Math.random() - 0.5) * 200;
  positions[i * 3 + 1] = (Math.random() - 0.5) * 200;
  positions[i * 3 + 2] = (Math.random() - 0.5) * 200;
  scales[i] = Math.random() * 5 + 1;
}

particles.setAttribute('position', new THREE.BufferAttribute(positions, 3));
particles.setAttribute('scale', new THREE.BufferAttribute(scales, 1));

const particleMaterial = new THREE.PointsMaterial({
  size: 4,
  map: smokeTexture,
  transparent: true,
  opacity: 0.6,
  blending: THREE.AdditiveBlending
});

const particleSystem = new THREE.Points(particles, particleMaterial);
scene.add(particleSystem);

// 动画循环
function animate() {
  requestAnimationFrame(animate);

  const positions = particles.attributes.position.array;
  for (let i = 0; i < particleCount; i++) {
    positions[i * 3 + 1] += 0.1;
    if (positions[i * 3 + 1] > 100) {
      positions[i * 3 + 1] = -100;
    }
  }
  particles.attributes.position.needsUpdate = true;

  renderer.render(scene, camera);
}

animate();

性能优化技巧

对于需要高性能的烟雾效果,可以考虑以下优化方法:

  • 使用着色器(Shader)实现GPU加速计算
  • 降低粒子数量但增加粒子大小变化
  • 实现视锥体剔除(Frustum Culling)
  • 使用粒子池(Particle Pool)避免频繁创建销毁对象

资源推荐

  1. Three.js官方示例包含多种粒子效果实现
  2. ShaderToy提供各种烟雾着色器代码
  3. Canvas API文档了解基础绘图方法

以上代码示例可根据实际需求调整参数如粒子数量、大小、运动速度和透明度等,以获得不同的烟雾视觉效果。

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

相关文章

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Pro…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

原生js实现轮播图

原生js实现轮播图

基本结构搭建 HTML部分需要包含轮播图容器、图片列表及导航按钮。结构示例如下: <div class="slider-container"> <div class="slid…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…