闲来无事,做了个烟花效果,面向过程的JS原生写的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
#main {
width: 100%;
height: 100%;
/* border: 4px solid #ccc; */
background-color: #000;
/* margin: 20px auto 0px; */
position: relative;
overflow: hidden;
}
button {
border: none;
font-family: Georgia, 'Times New Roman', Times, serif;
background: rgb(106, 202, 247);
color: white;
display: block;
width: 100px;
height: 30px;
margin: 0px auto;
}
</style>
</head>
<body>
<div id="main">
<button>燃放烟花</button>
</div>
<script src="../封装函数/easing.js"></script>
<script src="../封装函数/move.js"></script>
<script>
//* 烟花具备的属性有: 1 颜色 2 元素 3 能动 4 能爆炸 5 起始位置 6 终点位置 宽 高
//这里是把烟花抽象成了一个对象,而不是把画板抽象成了一个对象。
function Fire(dom, color, startX, startY, endX, endY, width, height) {
// this.dom = document.createElement("div")
this.dom = dom;
this.color = color;
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.width = width;
this.height = height;
}
Fire.prototype.firstrender = function () {
this.dom.style.background = this.color;
this.dom.style.position = "absolute"
this.dom.style.left = this.startX + "px";
this.dom.style.top = this.startY + "px";
this.dom.style.width = this.width + "px";
this.dom.style.height = this.height + "px";
// this.dom.style.borderRadius = "50%";
// console.log(this.color, this.startX, this.startY, this.endX, this.endY, this.width, this.height)
}
Fire.prototype.render = function () {
this.dom.style.background = this.color;
this.dom.style.position = "absolute"
this.dom.style.left = this.startX + "px";
this.dom.style.top = this.startY + "px";
this.dom.style.width = this.width + "px";
this.dom.style.height = this.height + "px";
this.dom.style.borderRadius = "50%";
// console.log(this.color, this.startX, this.startY, this.endX, this.endY, this.width, this.height)
}
Fire.prototype.uptree = function (father) {
father.appendChild(this.dom)
}
Fire.prototype.downtree = function () {
this.dom.remove()
}
Fire.prototype.move = function (callback) {
var _this = this;
move(this.dom, { left: this.endX, top: this.endY }, 1000, callback, "easeInQuad")
}
Fire.prototype.boom = function () {
for (var i = 0; i < 50; i++) {
var dom = document.createElement("div")
var color = `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
var width = 8;
var height = width;
var startX = this.endX;
var startY = this.endY;
var endX = Math.floor(Math.random() * board.clientWidth);
var endY = Math.floor(Math.random() * board.clientHeight);
(function () {
var f = new Fire(dom, color, startX, startY, endX, endY, width, height);
f.render()
f.uptree(board)
f.move(function () {
f.downtree()
})
})()
}
}
var board = document.getElementById("main")
board.onclick = function (e) {
var color = `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
var dom = document.createElement("div")
var width = 5;
var height = 20;
var startX = e.offsetX - width / 2;
var startY = board.clientHeight;
var endX = e.offsetX - width / 2;
var endY = e.offsetY - height / 2;
var fire = new Fire(dom, color, startX, startY, endX, endY, width, height);
fire.firstrender()
fire.uptree(board)
fire.move(function () {
fire.boom()
fire.downtree()
})
}
var timer = null;
var lock = true;
var count = 0;
var button = document.getElementsByTagName("button")[0]
button.onclick = function (e) {
e.stopPropagation()
if(!lock){ //如果为假,把定时器关闭,打开锁,并且return
clearInterval(timer)
lock = true;
button.innerHTML = "燃放烟花"
return
}
timer = setInterval(function () {
var color = `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
var dom = document.createElement("div")
var width = 5;
var height = 20;
var startX = Math.floor(Math.random() * board.clientWidth);
var startY = board.clientHeight;
var endX = startX;
var endY = Math.floor(Math.random() * board.clientHeight/2);
// console.log(color, startX, startY, endX, endY, width, height)
var fire = new Fire(dom, color, startX, startY, endX, endY, width, height);
fire.render()
fire.uptree(board)
fire.move(function () {
fire.boom()
fire.downtree()
})
}, 1000)
lock= false;
button.innerHTML = "关闭烟花"
}
</script>
</body>
</html>
下边是引用的动画脚本和速度曲线脚本
var easing = {
easeInQuad: function (x, t, b, c, d) {
return c*(t/=d)*t + b;
},
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
},
easeInOutQuad: function (x, t, b, c, d) {
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
},
easeInCubic: function (x, t, b, c, d) {
return c*(t/=d)*t*t + b;
},
easeOutCubic: function (x, t, b, c, d) {
return c*((t=t/d-1)*t*t + 1) + b;
},
easeInOutCubic: function (x, t, b, c, d) {
if ((t/=d/2) < 1) return c/2*t*t*t + b;
return c/2*((t-=2)*t*t + 2) + b;
},
easeInQuart: function (x, t, b, c, d) {
return c*(t/=d)*t*t*t + b;
},
easeOutQuart: function (x, t, b, c, d) {
return -c * ((t=t/d-1)*t*t*t - 1) + b;
},
easeInOutQuart: function (x, t, b, c, d) {
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
return -c/2 * ((t-=2)*t*t*t - 2) + b;
},
easeInQuint: function (x, t, b, c, d) {
return c*(t/=d)*t*t*t*t + b;
},
easeOutQuint: function (x, t, b, c, d) {
return c*((t=t/d-1)*t*t*t*t + 1) + b;
},
easeInOutQuint: function (x, t, b, c, d) {
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
return c/2*((t-=2)*t*t*t*t + 2) + b;
},
easeInSine: function (x, t, b, c, d) {
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
},
easeOutSine: function (x, t, b, c, d) {
return c * Math.sin(t/d * (Math.PI/2)) + b;
},
easeInOutSine: function (x, t, b, c, d) {
return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
},
easeInExpo: function (x, t, b, c, d) {
return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
},
easeOutExpo: function (x, t, b, c, d) {
return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
},
easeInOutExpo: function (x, t, b, c, d) {
if (t==0) return b;
if (t==d) return b+c;
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
},
easeInCirc: function (x, t, b, c, d) {
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
},
easeOutCirc: function (x, t, b, c, d) {
return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
},
easeInOutCirc: function (x, t, b, c, d) {
if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
},
easeInElastic: function (x, t, b, c, d) {
var s=1.70158;var p=0;var a=c;
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
if (a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
},
easeOutElastic: function (x, t, b, c, d) {
var s=1.70158;var p=0;var a=c;
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
if (a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
},
easeInOutElastic: function (x, t, b, c, d) {
var s=1.70158;var p=0;var a=c;
if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
if (a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
},
easeInBack: function (x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c*(t/=d)*t*((s+1)*t - s) + b;
},
easeOutBack: function (x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
easeInOutBack: function (x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
},
easeInBounce: function (x, t, b, c, d) {
return c - this.easeOutBounce (x, d-t, 0, c, d) + b;
},
easeOutBounce: function (x, t, b, c, d) {
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
} else {
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
}
},
easeInOutBounce: function (x, t, b, c, d) {
if (t < d/2) return this.easeInBounce (x, t*2, 0, c, d) * .5 + b;
return this.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
}
}
function move(ele, target, time, callback, curve) {
curve = curve || "easeInQuad";
// 定义了一个对象 用来存储对应的初始值
var nowJSON = {};
// 循环target
for (var i in target) {
if (window.getComputedStyle) { // 能力检测
// 对象使用方括号语法读取对应的值
nowJSON[i] = parseInt(getComputedStyle(ele)[i])
}
}
// 定义一个对象 用来存储要发生变化的属性的变化量
var distanceJSON = {};
for (var i in target) {
distanceJSON[i] = (target[i] - nowJSON[i]);
}
// 定义执行频率
var interval = 20;
// 得到总次数
var allCount = time / interval;
// 定义计数器
var count = 0;
var timer = setInterval(function() {
// 累加
count++;
for (var i in target) {
// easing是引入的一个对象 对象的身上有许多的数学公式 它们可以实现不同的运动曲线
if (i.toLowerCase() === "opacity") {
ele.style[i] = easing[curve](0, count * interval, nowJSON[i], distanceJSON[i], time);
} else {
ele.style[i] = easing[curve](0, count * interval, nowJSON[i], distanceJSON[i], time) + "px";
}
}
if (count >= allCount) {
clearInterval(timer);
for (var i in target) {
ele.style[i] = target[i] + "px";
}
callback && callback();
}
}, interval);
}