ホームページ  >  記事  >  ウェブフロントエンド  >  見事な Web ページの花火アニメーション効果を実現する JS メソッド_JavaScript スキル

見事な Web ページの花火アニメーション効果を実現する JS メソッド_JavaScript スキル

WBOY
WBOYオリジナル
2016-05-16 16:11:411326ブラウズ

この記事の例では、JS を使用して Web ページの見事な花火アニメーション効果を実現する方法を説明します。皆さんの参考に共有してください。具体的な分析は次のとおりです。

JS を使用して実装された非常にクールな Web ページの花火アニメーション効果。このようなアニメーションを作成するために JS に適応させることができます

コードをコピー コードは次のとおりです:


<頭>
見事なウェブページの花火効果




<スクリプトタイプ="text/javascript">
var Fire = function(r, color) {
this.radius = r ||
this.color = color;
this.xpos = 0;
this.ypos = 0;
this.zpos = 0;
this.vx = 0;
this.vy = 0;
this.vz = 0;
this.mass = 1;
this.x =0;
this.y=0;
this.p = document.createElement("span");
this.p.className = "火";
this.p.innerHTML = "*";
this.p.style.fontSize = this.radius "px";
this.p.style.color = "#" this.color;
}
Fire.prototype = {
追加: 関数(親) {
parent.appendChild(this.p);
}、
setSize: function(scale) {
this.p.style.fontSize = this.radius * スケール "px";
}、
setPosition:function(x, y) {
this.p.style.left = x "px";
this.p.style.top = y "px";
}、
setVisible: function(b) {
this.p.style.display = b ? "ブロック" : "なし";
}
}
var 花火 = function() {
var fires = new Array();
変数数 = 150;
var fl = 250;
var vpx = 500;
var vpy = 300;
var 重力 = .5;
var フロア = 200;
var バウンス = -.8;
var タイマー;
var Wind = ((Math.floor(Math.random()*3) 3)/10)*(Math.random()*2 - 1 > 0 ? 1 : -1)*.25;
var wpos = 0;
var wcount;
戻り値 {
init: function() {
wcount = 50 Math.floor(Math.random() * 100);
for (var i=0; i var color = 0xFF0000;
color = (Math.random() * 0xFFFFFF).toString(16).toString().split(".")[0];
while(color.length color = "0" color;
}
var fire = new Fire(12, color);
fires.push(fire);
fire.ypos = -100;
fire.vz = Math.random() * 6 - 3;
fire.vx = (Math.random()*2 - 1)*2 ;
fire.vy = Math.random()*-15 - 15;
fire.x = 500
fire.y = 600;
fire.append(document.body);
}
var that = this;
タイマー = setInterval(function() {
wpos ;
if (wpos >= wcount) {
wpos = 0;
wcount = 50 Math.floor(Math.random() * 100);
風 = ((Math.floor(Math.random()*3) 3)/10)*(Math.random()*2 - 1 > 0 ? 1 : -1)*.25;
}
for (var i=0;i that.move(fires[i]);
}
}, 30);
}、
move: function(fire) {
fire.vy = 重力;
fire.x = fire.vx;
fire.y = fire.vy;
fire.vx = 風;
fire.setPosition(fire.x, fire.y);
if (fire.x 1000 || fire.y 600) {
fire.vx = (Math.random()*2 - 1)*2;
fire.vy = Math.random()*-15 - 15;
fire.x = 500;
fire.y = 600;
fire.setPosition(fire.x, fire.y);
}
}
}
}
花火().init();

この記事が皆様の JavaScript プログラミング設計に役立つことを願っています。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。