H5点击网页彩色粒子爆炸特效 - 阿强爱分享

H5点击网页彩色粒子爆炸特效

作者: Aqiang

全网最全的网络资源分享网站

手机扫码查看

标签:

样式代码

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!邮箱:2377724272@qq.com

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>H5点击网页彩色粒子爆炸特效 - 站长素材</title>

<style>
* {
    margin: 0;
    padding: 0;
}
body {
  width: 100vw;
  min-height: 100vh;
  font-family: 'Inconsolata', monospace;
  overflow: hidden;
}
#canvas {
  display: block;
  cursor: pointer;
}
.hint {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  pointer-events: none;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  opacity: 0;
  visibility: hidden;
  -webkit-animation: fade-in 1s ease-out 1s forwards, fade-out 1s ease-out 5s forwards;
          animation: fade-in 1s ease-out 1s forwards, fade-out 1s ease-out 5s forwards;
}
.hint > h1 {
  font-size: 32px;
  color: #fff;
}
@-webkit-keyframes fade-in {
  0% {
    opacity: 0;
    visibility: hidden;
  }
  100% {
    opacity: 1;
    visibility: visible;
  }
}
@keyframes fade-in {
  0% {
    opacity: 0;
    visibility: hidden;
  }
  100% {
    opacity: 1;
    visibility: visible;
  }
}
@-webkit-keyframes fade-out {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
  }
}
@keyframes fade-out {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
  }
}
</style>

</head>
<body>

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

<div class="hint" id="hint">
  <h1>Click.</h1>
</div>

<script>
var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}var getRandom = function getRandom(min, max) {
  return Math.random() * (max - min) + min;
};

var getRandomInt = function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
};

var getRandomColor = function getRandomColor() {
  var colors = [
  'rgba(231, 76, 60, 1)', // red
  'rgba(241, 196, 15, 1)', // yellow
  'rgba(46, 204, 113, 1)', // green
  'rgba(52, 152, 219, 1)', // blue
  'rgba(155, 89, 182, 1)' // purple
  ];

  return colors[getRandomInt(0, colors.length)];
};

// Particle
var
Particle = function () {

  function Particle(system, x, y) {_classCallCheck(this, Particle);
    this.system = system;
    this.universe = this.system.world.universe;
    this.x = x;
    this.y = y;
    this.color = getRandomColor();
    this.life = 1;
    this.aging = getRandom(0.99, 0.999); // 0.99, 0.999 || 0.999, 0.9999

    this.r = getRandomInt(8, 16);
    this.speed = getRandom(3, 8);
    this.velocity = [
    getRandom(-this.speed, this.speed),
    getRandom(-this.speed, this.speed)];

  }_createClass(Particle, [{ key: 'update', value: function update(

    dt) {
      this.life *= this.aging;

      if (
      this.r < 0.1 ||
      this.life === 0 ||
      this.x + this.r < 0 ||
      this.x - this.r > this.universe.width ||
      this.y + this.r < 0 ||
      this.y - this.r > this.universe.height)
      {
        this.system.removeObject(this);
      }

      this.r *= this.life;
      this.x += this.velocity[0];
      this.y += this.velocity[1];
    } }, { key: 'render', value: function render(

    ctx) {
      // Main circle

      ctx.fillStyle = this.color;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
      ctx.fill();
      ctx.closePath();

      var r = this.color.match(/([0-9]+)/g)[0];
      var g = this.color.match(/([0-9]+)/g)[1];
      var b = this.color.match(/([0-9]+)/g)[2];

      // Gradient

      var spread = 1.5;
      var gradient = ctx.createRadialGradient(
      this.x, this.y, this.r,
      this.x, this.y, this.r * spread);

      gradient.addColorStop(0, 'rgba(' + r + ', ' + g + ', ' + b + ', 0.3)');
      gradient.addColorStop(1, 'rgba(' + r + ', ' + g + ', ' + b + ', 0)');

      ctx.globalCompositeOperation = 'lighter';
      ctx.fillStyle = gradient;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r * spread, 0, 2 * Math.PI, false);
      ctx.fill();
      ctx.closePath();
      ctx.globalCompositeOperation = 'source-over';

      // Aberration

      var offset = this.r * 0.5;
      var color = 'rgba(' + g + ', ' + b + ', ' + r + ', 0.3)';

      ctx.globalCompositeOperation = 'lighter';
      ctx.fillStyle = color;
      ctx.beginPath();
      ctx.arc(this.x + offset, this.y + offset, this.r, 0, 2 * Math.PI, false);
      ctx.fill();
      ctx.closePath();
      ctx.globalCompositeOperation = 'source-over';
    } }]);return Particle;}();



// Crown
var
Crown = function () {

  function Crown(system, x, y) {_classCallCheck(this, Crown);
    this.system = system;
    this.x = x;
    this.y = y;
    this.r = getRandomInt(15, 20); // 5, 20
    this.mod = 1.1;
    this.life = 1;
    this.aging = getRandom(0.83, 0.88);
    this.speed = getRandom(4, 5);

    this.color = {
      r: getRandomInt(236, 242),
      g: getRandomInt(196, 241),
      b: getRandomInt(195, 242) };


    this.angle1 = Math.PI * getRandom(0, 2);
    this.angle2 = this.angle1 + Math.PI * getRandom(0.1, 0.5);
  }_createClass(Crown, [{ key: 'update', value: function update(

    dt) {
      this.life *= this.aging;

      if (this.life <= 0.0001) this.system.removeObject(this);

      this.r += Math.abs(1 - this.life) * this.speed;

      this.x1 = this.x + this.r * Math.cos(this.angle1);
      this.y1 = this.y + this.r * Math.sin(this.angle1);

      this.angle3 = this.angle1 + (this.angle2 - this.angle1) / 2;
      this.x2 = this.x + this.r * this.mod * Math.cos(this.angle3);
      this.y2 = this.y + this.r * this.mod * Math.sin(this.angle3);
    } }, { key: 'render', value: function render(

    ctx) {
      var gradient = ctx.createRadialGradient(
      this.x, this.y, this.r * 0.9,
      this.x, this.y, this.r);

      gradient.addColorStop(0, 'rgba(' + this.color.r + ', ' + this.color.g + ', ' + this.color.b + ', ' + this.life + ')');
      gradient.addColorStop(1, 'rgba(' + this.color.r + ', ' + this.color.g + ', ' + this.color.b + ', ' + this.life * 0.5 + ')');

      ctx.fillStyle = gradient;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r, this.angle1, this.angle2, false);
      ctx.quadraticCurveTo(this.x2, this.y2, this.x1, this.y1);
      ctx.fill();
      ctx.closePath();
    } }]);return Crown;}();



// Explosion
var
Explosion = function () {

  function Explosion(world, x, y) {_classCallCheck(this, Explosion);
    this.world = world;
    this.x = x;
    this.y = y;
    this.objects = [];

    var particles = getRandomInt(30, 80); // 10, 30 amount of particles
    var crowns = particles * getRandom(0.3, 0.5);

    while (crowns-- > 0) {this.addCrown();}
    while (particles-- > 0) {this.addParticle();}
  }_createClass(Explosion, [{ key: 'update', value: function update(

    dt) {
      this.objects.forEach(function (obj) {
        if (obj) obj.update(dt);
      });

      if (this.objects.length <= 0) {
        this.world.clearExplosion(this);
      }
    } }, { key: 'render', value: function render(

    ctx) {
      this.objects.forEach(function (obj) {
        if (obj) obj.render(ctx);
      });
    } }, { key: 'addCrown', value: function addCrown()

    {
      this.objects.push(new Crown(this, this.x, this.y));
    } }, { key: 'addParticle', value: function addParticle()

    {
      this.objects.push(new Particle(this, this.x, this.y));
    } }, { key: 'removeObject', value: function removeObject(

    obj) {
      var index = this.objects.indexOf(obj);

      if (index !== -1) {
        this.objects.splice(index, 1);
      }
    } }]);return Explosion;}();



// World
var
ConfettiWorld = function () {function ConfettiWorld() {_classCallCheck(this, ConfettiWorld);}_createClass(ConfettiWorld, [{ key: 'init', value: function init()

    {
      this.objects = [];
      window.addEventListener('click', this.explode.bind(this));

      // Initial explosion
      var counter = 3;
      while (counter-- > 0) {
        this.explode({
          clientX: this.universe.width / 2,
          clientY: this.universe.height / 2 });

      }
    } }, { key: 'update', value: function update(

    dt) {
      this.objects.forEach(function (obj) {
        if (obj) obj.update(dt);
      });

      var amount = this.objects.reduce(function (sum, explosion) {
        return sum += explosion.objects.length;
      }, 0);
    } }, { key: 'render', value: function render(

    ctx) {
      this.objects.forEach(function (obj) {
        if (obj) obj.render(ctx);
      });
    } }, { key: 'explode', value: function explode(

    event) {
      var x = event.clientX;
      var y = event.clientY;

      this.objects.push(new Explosion(this, x, y));
    } }, { key: 'clearExplosion', value: function clearExplosion(

    explosion) {
      var index = this.objects.indexOf(explosion);

      if (index !== -1) {
        this.objects.splice(index, 1);
      }
    } }]);return ConfettiWorld;}();



// Time
var
Time = function () {

  function Time() {_classCallCheck(this, Time);
    this.now = 0; // current tick time
    this.prev = 0; // prev tick time
    this.elapsed = 0; // elapsed time from last tick
    this.delta = 0; // time from last update
    this.fps = 60; // desired fps
    this.step = 1 / 60; // step duration
  }_createClass(Time, [{ key: 'update', value: function update(

    time) {
      this.now = time;
      this.elapsed = (this.now - this.prev) / 1000;
      this.prev = thi
分享到:
打赏

公告提示:本站已开放注册,欢迎投稿。
头像
站长签名: 专注分享各类源码-全网资源免费分享平台好货不私藏

文章作者信息版权声明

阅读时间:  发布于:2019-10-23

本文标题: H5点击网页彩色粒子爆炸特效

本文链接: https://iooqp.cn/?post=538

版权声明:文章为《 Aqiang》原创,转载请保留出处! 本文共有 7702 个字!

分享到:

评论

未显示?请点击刷新
切换注册

登录

您也可以使用第三方帐号快捷登录

切换登录

注册

© 2019 阿强爱分享   京ICP备19031919号-2

粤公网安备 44200002443934号

免责声明:本站所发布的资源信息及软件文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,本站信息来自网络,版权争议与本站无关。

H5点击网页彩色粒子爆炸特效

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏