canvas实现下雨夜晚十种不同形状烟花绽放粒子动画效果代码

代码语言:html

所属分类:粒子

代码描述:canvas实现下雨夜晚十种不同形状烟花绽放粒子动画效果代码

代码标签: canvas 下雨 夜晚 十种 不同 形状 烟花 绽放 粒子 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/bootstrap.4.3.1.min.css">
<style>
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");

body {
  margin: 0;
  overflow: hidden;
  background: linear-gradient(to bottom, #000033, #000066);
  cursor: crosshair;
  font-family: "Roboto", serif;
}

.controls {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.1);
  padding: 15px;
  border-radius: 10px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

#fullscreenBtn {
  position: fixed;
  top: 20px;
  left: 20px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 5px;
  padding: 8px 12px;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

#fullscreenBtn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

button {
  padding: 2px 4px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
}

.type1 {
  background: #ff4444;
}
.type2 {
  background: #44ff44;
}
.type3 {
  background: #4444ff;
}
.type4 {
  background: #ffff44;
}
.type5 {
  background: #ff44ff;
}
.type6 {
  background: #44ffff;
}
.type7 {
  background: #ff8844;
}
.type8 {
  background: #8844ff;
}
.type9 {
  background: #ff4488;
}
.type10 {
  background: #44ff88;
}

button:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 5px rgba(255, 255, 255, 0.2);
}

.snowflake {
  position: absolute;
  color: white;
  pointer-events: none;
}

canvas {
  filter: blur(0.5px);
}
</style>

  
  
</head>

<body translate="no">
  <canvas id="canvas"></canvas>
<button id="fullscreenBtn">⤢</button>
<div class="controls">
  <button class="type1" onclick="launchFirework(1)">Burst</button>
  <button class="type2" onclick="launchFirework(2)">Spider</button>
  <button class="type3" onclick="launchFirework(3)">Circle</button>
  <button class="type4" onclick="launchFirework(4)">Heart</button>
  <button class="type5" onclick="launchFirework(5)">Spiral</button>
  <button class="type6" onclick="launchFirework(6)">Double Ring</button>
  <button class="type7" onclick="launchFirework(7)">Cross</button>
  <button class="type8" onclick="launchFirework(8)">Star</button>
  <button class="type9" onclick="launchFirework(9)">Wave</button>
  <button class="type10" onclick="launchFirework(10)">Explosion</button>
</div>
  
      <script >
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const fullscreenBtn = document.getElementById("fullscreenBtn");

// Fullscreen handling
fullscreenBtn.addEventListener("click", () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
    fullscreenBtn.textContent = "⤡";
  } else {
    document.exitFullscreen();
    fullscreenBtn.textContent = "⤢";
  }
});

document.addEventListener("fullscreenchange", () => {
  if (!document.fullscreenElement) {
    fullscreenBtn.textContent = "⤢";
  } else {
    fullscreenBtn.textContent = "⤡";
  }
});

// Enable motion blur
ctx.globalCompositeOperation = "lighter";

function resize() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}
resize();
window.addEventListener("resize", resize);

class Smoke {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.size = Math.random() * 10 + 5; // Reduced from 20+10 to 10+5
    this.speed = {
      x: (Math.random() - 0.5) * 1, // Reduced from 2 to 1
      y: -Math.random() * 1 // Reduced from 2 to 1
    };
    this.alpha = 0.25; // Reduced from 0.5 to 0.25
    this.decay = Math.random() * 0.01 + 0.005;
  }

  draw() {
    ctx.save();
    ctx.globalAlpha = this.alpha;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fillStyle = "rgba(100, 100, 100, 0.25)"; // Reduced from 0.5 to 0.25
    ctx.fill();
    ctx.restore();
  }

  update() {
    this.x += this.speed.x;
    this.y += this.speed.y;
    this.alpha -= this.decay;
    this.size += 0.2;
  }}


class Raindrop {
  constructor() {
    this.reset();
  }

  reset() {
    this.x = Math.random() * canvas.width;
    this.y = -10;
    this.speed = Math.random() * 10;
    this.length = Math.random() * 10 + 10;
  }

  dr.........完整代码请登录后点击上方下载按钮下载查看

网友评论0