登 录
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"; }
Ball.prototype.dispose = function () { this.disposing = true; for (var i in this) { if (i == "parent") continue; if (typeof this[i].dispose == "function" && !this[i].disposing) this[i].dispose(); if (typeof this[i].parentNode == "object") this[i].parentNode.removeChild(this[i]); delete this[i]; } }
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() { this.total++; 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(); }, 10); }
Firework.prototype.stop = function() { if (!this.active) return; this.active = false; clearTimeout(this.timer); for (var i = 0; i < this.balls.length; i++) this.balls[i].hide(); if (typeof this.onstop == "function") this.onstop(this); }
Firework.prototype.replay = function() { this.stop(); this.active = true; this.selected = -1; this.total = 0; this.tick(); if (typeof this.onreplay == "function") this.onreplay(this); }
var firework = new Firework(document.body, "●", 32, 15, 7.5); firework.alpha = 0.5 - Math.random(); firework.offset = Math.random() < 0.5 ? +1 : -1; firework.ontick = function () { if (this.total % 10 == 0) { var h = document.body.clientHeight || document.documentElement.clientHeight; var w = document.body.clientWidth || document.documentElement.clientWidth; this.selected = (this.selected + 1) % this.balls.length; var alpha = this.alpha + 1 / this.balls.length * this.offset; if (alpha < -0.5 || alpha > 0.5) this.offset = -this.offset; this.alpha = alpha; this.balls[this.selected].update( typeof this.color == "undefined" ? Math.random() * 360 : this.color, w / 2, h, this.size, Math.PI * 1.5 + this.alpha * 2, this.speed, 0.5 ); this.balls[this.selected].show(); } } firework.replay();
var firework2 = new Firework(document.body, "●", 15, 20, 12.5); firework2.ontick = function () { if (this.total % 50 == 0) { var h = document.body.clientHeight || document.documentElement.clientHeight; var w = document.body.clientWidth || document.documentElement.clientWidth; var ball = firework.balls[Math.floor(firework.selected + firework.balls.length / 2) % firework.balls.length]; if (!ball || !ball.visible) return; ball.hide(); for (var i = 0; i < this.balls.length; i++) { this.balls[i].update( ball.color, ball.x, ball.y, this.size, 2 * Math.PI * Math.random(), Math.random() * this.speed, 1.5); this.balls[i].show(); } } } firework2.replay(); </script> </html>
抱歉!评论已关闭.