html做了一个烟花的效果,初级阶段,做的不好
直接放代码,有兴趣的可以随便改
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>newfirework</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<canvas id="firework">换个浏览器</canvas>
<script>
window.requestAnimFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) {
window.setTimeout(callback, 1000 / 60)
}
})();
function init() {
const firework = document.getElementById("firework")
const ctx = firework.getContext("2d")
const width = document.documentElement.clientWidth
const height = document.documentElement.clientHeight
firework.width = width
firework.height = height
firework.style.background = "#000000"
ctx.beginPath()
ctx.fillRect(0,0,width,height)
const all_num = 20 // 出现的烟花数量
const radius = 3 // 烟花半径
const max_length = 16 // 烟花最大长度
var fireArr = new Array()
for( var i = 0; i < all_num; i++){
fireArr.push(new FireworkObj(randomX(10, width), randomY(height, 300), radius,
Math.floor(Math.random() * 5 + 5), Math.floor(Math.random() * (height / 2)),
ctx, randomColor(), max_length))
}
console.log(fireArr[0])
var loop = function(){
ctx.fillStyle = "#000000"
ctx.fillRect(0,0,width,height)
for( var i = 0; i < all_num; i++){
if(fireArr[i].y <= fireArr[i].height || fireArr[i].speed === 0){
fireArr[i].judgeSelf(fireArr[i])
if(fireArr[i].fire.length ===0){
fireArr[i] = new FireworkObj(randomX(10, width), randomY(height, 10), radius,
Math.floor(Math.random() * 5 + 5), Math.floor(Math.random() * (height / 2)),
ctx, randomColor(), max_length)
}
}else{
fireArr[i].drawSelf()
fireArr[i].updateSelf(fireArr[i])
}
}
requestAnimationFrame(loop)
}
loop()
}
init()
// 烟花对象
function FireworkObj(x, y, r, speed, height, ctx, color, maxLength) {
this.x = x // 烟花初始位置x
this.y = y // 烟花初始位置y
this.r = r // 烟花初始半径
this.speed = speed // 烟花初始速度
this.height = height //烟花上升的最高位置
this.ctx = ctx
this.color = color
this.fire = new Array()
for(var num = 0; num < Math.floor(Math.random()*20 + 10); num++){
this.fire.push(new FireObj(this.x, this.height, 2, this.color, Math.round(Math.random()) === 1 ? Math.floor(Math.random()*5 + 1) : 0 - Math.floor(Math.random()*5 + 1), -Math.floor(Math.random()*10), this.ctx))
}
this.maxLength = maxLength
this.drawSelf = function() {
this.ctx.fillStyle = this.color
for(var j = 0;j < Math.floor(this.maxLength);j++){
this.ctx.beginPath()
this.ctx.arc(this.x, this.y + 5 * j, this.r - j * 0.2, 0, 2 * Math.PI)
this.ctx.fill()
}
}
this.updateSelf = function(obj){
obj.speed = obj.speed - 0.2 > 10 ? obj.speed - 0.2 : 10
obj.y = obj.y - obj.speed < obj.height ? obj.height : obj.y - obj.speed
obj.maxLength = obj.maxLength - 0.35 > 1 ? obj.maxLength - 0.35 : 1
}
this.judgeSelf = function(obj) {
for(var fireNum = 0; fireNum < obj.fire.length; fireNum++){
obj.fire[fireNum].updateFire(obj.fire[fireNum].x, obj.fire[fireNum].y)
if(obj.fire[fireNum].speedX > 0){
obj.fire[fireNum].speedX = obj.fire[fireNum].speedX - 0.04 > 0 ? obj.fire[fireNum].speedX - 0.04 : 0
}else if(obj.fire[fireNum].speedX < 0){
obj.fire[fireNum].speedX = obj.fire[fireNum].speedX + 0.04 < 0 ? obj.fire[fireNum].speedX + 0.04 :0
}
obj.fire[fireNum].speedY += 0.7
obj.fire[fireNum].x += obj.fire[fireNum].speedX
obj.fire[fireNum].y += obj.fire[fireNum].speedY
obj.fire[fireNum].drawFire()
if(obj.fire[fireNum].speedX === 0){
obj.fire.splice(fireNum)
}
}
}
}
// 爆炸效果对象
function FireObj(x, y, r, color, speedX, speedY,ctx) {
this.x = x
this.y = y
this.r = r
this.color = color
this.speedX = speedX
this.speedY = speedY
this.ctx = ctx
this.movePosition = new Array()
this.movePosition.push([this.x, this.y])
this.drawFire = function(){
this.ctx.strokeStyle = this.color
for(var removeNum = 0; removeNum < 30; removeNum++){
this.ctx.beginPath()
this.ctx.lineWidth=1;
this.ctx.moveTo(this.movePosition[this.movePosition.length -1][0], this.movePosition[this.movePosition.length -1][1]);
this.ctx.lineTo(this.x, this.y);
this.ctx.stroke()
}
}
this.updateFire = function(positionX, positionY){
if(this.speedX !== 0 && this.speedY !== 0){
this.movePosition.pop()
this.movePosition.unshift([positionX, positionY])
}
}
}
function randomX(min, max){
return Math.floor((Math.random() * max) + min)
}
function randomY(min, max){
return Math.floor((Math.random() * max) + min)
}
function randomColor(){
return "rgb(" + Math.floor((Math.random() * 255)) + ","
+ Math.floor((Math.random() * 255)) + ","
+ Math.floor((Math.random() * 255)) + ")"
}
</script>
</body>
</html>