canvas原生js实现火花四溅效果

代码语言:html

所属分类:粒子

代码描述:canvas原生js实现火花四溅效果

代码标签: 实现 火花 四溅 效果

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
* {
  font-family: 'arial black', Helvetica, verdana, monospace, sans-serif;
  letter-spacing: 0.2rem;
  margin: 0;
  padding: 0;
  color: #FFF;
  overflow: hidden;
}

body {
  position: relative;
}

canvas#canvas {
  display: block;
  background: #000;
}
</style>

</head>
<body translate="no">
<canvas id="canvas">Canvas not supported.</canvas>

<script>
/*
* File Name / sparkler.js
* Created Date / Aug 16, 2020
* Aurhor / Toshiya Marukubo
* Twitter / https://twitter.com/toshiyamarukubo
*/

(function () {
  'use strict';
  window.addEventListener('load', function () {
    var canvas = document.getElementById('canvas');

    if (!canvas || !canvas.getContext) {
      return false;
    }

    /********************
        Random Number
      ********************/

    function rand(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min);
    }

    /********************
        Var
      ********************/

    var ctx = canvas.getContext('2d');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var mouseX = null;
    var mouseY = null;
    // spark
    var rad = Math.PI * 2 / 8;
    var sparks = [];
    var sparkNum = 800;
    var time = 0;
    var color = 'hsl(' + Math.sin(time * Math.PI / 180) * 360 + ', 80%, 60%)';

    /********************
                                                                                 Animation
                                                                               ********************/

    window.requestAnimationFrame =
    window.requestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function (cb) {
      setTimeout(cb, 17);
    };

    /********************
         Stick
       ********************/

    function stickDraw() {
      ctx.save();
      ctx.lineWidth = 4;
      ctx.strokeStyle = color;
      ctx.beginPath();
      ctx.moveTo(X / 2, 0);
      ctx.lineTo(X / 2, Y / 2);
      ctx.stroke();
      ctx.restore();
    }

    /********************
        Fire Ball
      ********************/

    var fireBallNum = 1;
    var fireBalls = [];

    function FireBall(ctx, x, y) {
      this.ctx = ctx;
      this.init(x, y);
    }

    FireBall.prototype.init = function (x, y) {
      this.x = x;
      this.y = y;
      this.r = 10;
      this.ga = Math.random();
      this.flg = false;
      this.v = {
        y: 1 };

    };

    FireBall.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.globalAlpha = this.ga;
      ctx.globalCompositeOperation = 'lighter';
      ctx.fillStyle = color;
      for (var i = 0; i < 10; i++) {
        ctx.beginPath();
        ctx.arc(this.x, this.y, rand(5, 10), 0, Math.PI * 2, false);
        ctx.fill();
      }
      ctx.restore();
    };

    FireBall.prototype.fall = function (i) {
      if (this.y > Y - this.r) {
        this.y = Y - this.r;
        this.v.y *= -1.1;
        this.flg = true;
      } else {
        this.v.y += 0.05;
        this.y += this.v.y;
      }
      if (this.y < Y / 2 && this.flg === true) {
        this.y = Y / 2;
    .........完整代码请登录后点击上方下载按钮下载查看

网友评论0