登 录
function Ball(parent, text) { this.parent = parent; this.text = text; this.visible = true; this.span = document.createElement("span"); this.span.innerHTML = this.text; this.span.style.position = "absolute"; }
Ball.prototype.update = function(color, x, y, size, alpha, speed, fire) { this.fire = fire; this.alpha = alpha; this.speed = speed; this.gravity = 0; this.x = x; this.y = y; this.color = color; this.hsl = {h: color, s: 100, l: 80}; this.size = size; this.doChange(); }
Ball.prototype.doChange = function() { with (this.span.style) { fontSize = this.size + "px"; left = this.x + "px"; top = this.y + "px"; color = hsl2color(this.hsl); } if (!this.span.parent) this.parent.insertBefore(this.span, this.parent.firstChild); }
Ball.prototype.move = function() { this.x = Math.cos(this.alpha) * this.speed + this.x; this.y = Math.sin(this.alpha) * this.speed + this.y; this.y = this.gravity + this.y; this.gravity += 0.05; this.hsl.l -= this.fire; this.doChange(); }
Ball.prototype.hide = function() { if (!this.visible) return; this.visible = false; this.span.style.display = "none"; }
Ball.prototype.show = function() { if (this.visible) return; this.visible = true; this.span.style.display = "block"; }
function Firework(parent, text, size, count, speed) { this.parent = parent; this.speed = speed; this.size = size; this.total = 0; this.active = false; this.balls = new Array(count); for (var i = 0; i < this.balls.length; i++) { this.balls[i] = new Ball(parent, text); this.balls[i].firework = this; this.balls[i].index = i; this.balls[i].hide(); } }
Firework.prototype.tick = function() { if (!this.active) return; if (typeof this.ontick == "function") this.ontick(this); for (var i = 0; i < this.balls.length; i++) { if (!this.balls[i].visible) continue; if (typeof this.onballmove == "function") this.onballmove(this.balls[i]); else this.balls[i].move(); } var self = this; this.timer = setTimeout(function() { self.tick(); }, 5); }
Firework.prototype.replay = function() { this.active = true; this.tick(); if (typeof this.onreplay == "function") this.onreplay(this); }
var firework = new Firework(document.body, "●", 12, 105, 0); firework.ontick = function () { if (this.total % 80 == 0) { var h = document.body.clientHeight || document.documentElement.clientHeight; var w = document.body.clientWidth || document.documentElement.clientWidth; for (var i = 0; i < 5; i++) { var color; var speed = 9.3; var alpha = Math.PI * 1.5; switch (i) { case 0: color = 240; speed = 12.7; alpha -= 0.43; break; // 蓝色 case 1: color = 30; alpha -= 0.27; break; // 黄色 case 2: speed = 11.5; break; // 黑色 case 3: color = 120; alpha += 0.27; break; // 绿色 case 4: color = 0; speed = 12.7; alpha += 0.43; break; // 红色 } this.balls[i].update(color, w / 2, h, 32, alpha, speed, 0.5); this.balls[i].hsl.l = 50; if (i == 2) { this.balls[i].hsl.h = 0; this.balls[i].hsl.s = 0; } this.balls[i].show(); } } if (this.total % 80 == 45) { var count = 5; for (var i = 0; i < 5; i++) { this.balls[i].hide(); for (var j = 0; j < 20; j++) { this.balls[count].update(this.balls[i].color, this.balls[i].x, this.balls[i].y, 24, j / 20 * 2 * Math.PI, 7, 3.9); if (i == 2) { this.balls[count].hsl.h = 0; this.balls[count].hsl.s = 0; } this.balls[count].show(); count++; } } } this.total++; } firework.total = 0; firework.replay(); </script> </html>
抱歉!评论已关闭.