首页 > 编程语言 > js实现烟花特效
2020
09-24

js实现烟花特效

本文实例为大家分享了js实现烟花特效的具体代码,供大家参考,具体内容如下

1.概述

在网页背景中实现鼠标点击出现模拟烟花爆炸的特效

2.思路

1.获取鼠标点击位置,底端创建烟花节点。
2.为烟花添加css属性,烟花节点从下至上运动。
3.运动至鼠标位置时移除烟花节点,同时生成多个烟花碎片。
4.为不同的烟花碎片随机生成不同的颜色、运动速度、运动方向。
5.烟花碎片超出屏幕显示部分时移除。

3.代码部分

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
 * {
  padding: 0;
  margin: 0;
 }
 
 html,
 body {
  width: 100%;
  height: 100%;
  background-color: black;
  overflow: hidden;
 }
 </style>
</head>
 
<body>
 <script src="move.js"></script>
 <script>
 
 class Firework {
  constructor(x, y) {//x,y鼠标的位置
  this.x = x;//将水平位置赋值给this.x属性。
  this.y = y;//将垂直位置赋值给this.y属性。
  this.ch = document.documentElement.clientHeight;//可视区的高度
  }
  init() {
  //1.创建烟花节点。
  this.firebox = document.createElement('div');
  this.firebox.style.cssText = `width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${this.ch}px;`;
  document.body.appendChild(this.firebox);
  this.firemove();//创建完成,直接运动。
  }
  //2.烟花节点运动
  firemove() {
  bufferMove(this.firebox, { top: this.y }, () => {
   document.body.removeChild(this.firebox);
   //当烟花节点消失的时候,创建烟花碎片
   this.createfires()
  });
  }
  //3.当前鼠标点击的位置,随机产生30-60个盒子。(随机颜色)
  createfires() {
  for (let i = 1; i <= this.rannum(30, 60); i++) {
   this.fires = document.createElement('div');
   this.fires.style.cssText = `width:5px;height:5px;background:rgb(${this.rannum(0, 255)},${this.rannum(0, 255)},${this.rannum(0, 255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
   document.body.appendChild(this.fires);
   this.fireboom(this.fires);//设计成一个一个运动,等到循环结束,出现整体结果。
  }
  }
  //4.烟花碎片运动。
  fireboom(obj) {
  //存储当前obj的初始值。
  let initx = this.x;
  let inity = this.y;
 
  //随机产生速度(水平和垂直方向都是随机的,符号也是随机的)。
  let speedx = parseInt((Math.random() > 0.5 ? '-' : '') + this.rannum(1, 15));
  let speedy = parseInt((Math.random() > 0.5 ? '-' : '') + this.rannum(1, 15));
 
  obj.timer = setInterval(() => {
   initx += speedx;
   inity += speedy++; //模拟重力加速度(垂直方向比水平方向快一些)
   if (inity >= this.ch) {
   document.body.removeChild(obj);
   clearInterval(obj.timer);
   }
   obj.style.left = initx + 'px';
   obj.style.top = inity + 'px';
  }, 1000 / 60);
 
  }
  //随机区间数
  rannum(min, max) {
  return Math.round(Math.random() * (max - min) + min);
  }
 }
 
 
 document.onclick = function (ev) {
  var ev = ev || window.event;
  //ev.clientX,ev.clientY//获取的鼠标的位置
  new Firework(ev.clientX, ev.clientY).init();
 }
 </script>
</body>
 
</html>

4.Move.js

function getStyle(obj, attr) {
 if (window.getComputedStyle) {
 return window.getComputedStyle(obj)[attr];
 } else {
 return obj.currentStyle[attr];
 }
}
function bufferMove(obj, json, fn) {
 let speed = 0;
 clearInterval(obj.timer);
 obj.timer = setInterval(function () {
 var flag = true;
 for (var attr in json) {
  var currentValue = null;
  if (attr === 'opacity') {
  currentValue = Math.round(getStyle(obj, attr) * 100);
  } else {
  currentValue = parseInt(getStyle(obj, attr));
  }
  speed = (json[attr] - currentValue) / 10;
  speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
 
  if (currentValue !== json[attr]) {
  if (attr === 'opacity') {
   obj.style.opacity = (currentValue + speed) / 100;
   obj.style.filter = 'alpha(opacity=' + (currentValue + speed) + ')';//IE
  } else {
   obj.style[attr] = currentValue + speed + 'px';
  }
  flag = false;
  }
 }
 if (flag) {
  clearInterval(obj.timer);
  fn && typeof fn === 'function' && fn();
 }
 }, 10);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧