当前位置:首页 > JavaScript

js实现飘雪

2026-04-06 22:24:15JavaScript

实现飘雪效果的方法

在JavaScript中实现飘雪效果可以通过Canvas或CSS动画来完成。以下是两种常见的方法:

使用Canvas实现飘雪

Canvas适合需要大量粒子效果的场景,性能较好。

const canvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');

// 设置canvas为全屏
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// 雪花数组
const snowflakes = [];

// 创建雪花
class Snowflake {
  constructor() {
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * canvas.height;
    this.radius = Math.random() * 4 + 1;
    this.speed = Math.random() * 3 + 1;
    this.opacity = Math.random();
  }

  draw() {
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
    ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
    ctx.fill();
  }

  update() {
    this.y += this.speed;
    if (this.y > canvas.height) {
      this.y = 0;
      this.x = Math.random() * canvas.width;
    }
    this.draw();
  }
}

// 初始化雪花
for (let i = 0; i < 100; i++) {
  snowflakes.push(new Snowflake());
}

// 动画循环
function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  snowflakes.forEach(snowflake => snowflake.update());
  requestAnimationFrame(animate);
}

animate();

// 窗口大小改变时重置canvas
window.addEventListener('resize', () => {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
});

HTML中需要添加一个canvas元素:

<canvas id="snowCanvas"></canvas>

使用CSS实现飘雪

CSS方法更简单,适合少量雪花效果。

<div class="snow-container"></div>
.snow-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
}

.snow {
  position: absolute;
  background-color: white;
  border-radius: 50%;
  pointer-events: none;
  animation: fall linear infinite;
}

@keyframes fall {
  to {
    transform: translateY(100vh);
  }
}
function createSnow() {
  const snow = document.createElement('div');
  snow.className = 'snow';

  // 随机大小
  const size = Math.random() * 10 + 5;
  snow.style.width = `${size}px`;
  snow.style.height = `${size}px`;

  // 随机位置
  snow.style.left = `${Math.random() * window.innerWidth}px`;
  snow.style.top = '-10px';

  // 随机透明度
  snow.style.opacity = Math.random();

  // 随机动画时长
  const duration = Math.random() * 5 + 5;
  snow.style.animationDuration = `${duration}s`;

  document.querySelector('.snow-container').appendChild(snow);

  // 动画结束后移除
  setTimeout(() => {
    snow.remove();
  }, duration * 1000);
}

// 每隔一段时间创建雪花
setInterval(createSnow, 100);

性能优化建议

对于大量雪花效果,Canvas通常性能更好。可以调整以下参数优化性能:

js实现飘雪

  • 减少雪花数量
  • 降低动画帧率
  • 使用requestAnimationFrame代替setInterval
  • 避免在动画循环中频繁创建对象

两种方法都可以根据需求进行调整,Canvas更适合复杂的粒子效果,CSS方法则更简单易用。

标签: js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterva…

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swipe…

js实现拖拽

js实现拖拽

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

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js实现吸色

js实现吸色

使用Canvas实现吸色功能 通过Canvas的getImageData方法获取像素颜色数据。创建一个Canvas元素,将目标图像绘制到Canvas上,通过鼠标事件获取坐标对应的颜色值。 c…