MediaWiki:Feuerwerk.js: Unterschied zwischen den Versionen

Aus DarkfleetWiki
Wechseln zu: Navigation, Suche
(test! :D)
 
Zeile 1: Zeile 1:
−
////////////////////////////////////////////////////////////////////////
+
/**
−
// Fireworks-Script (c) 2014, Dominik Scholz / go4u.de Webdesign
+
  * You may use this code for free on any web page provided that
−
////////////////////////////////////////////////////////////////////////
+
  * these comment lines and the following credit remain in the code.
 +
  * Cross Browser Fireworks from http://www.javascript-fx.com
 +
  */
 +
/*************************************************/
 +
if(!window.JSFX) JSFX=new Object();
  
−
var fireworks = {
+
if(!JSFX.createLayer)
 +
{/*** Include Library Code ***/
  
−
///////////////////////////// configuration ////////////////////////////
+
var ns4 = document.layers;
−
+
var ie4 = document.all;
−
// random colors
+
JSFX.objNo=0;
−
_color: ['#D0D0D0', '#FF0000', '#FFFF00', '#22FF00', '#2040FF', '#00CCFF', '#FF00FF', '#A319D6'],
 
−
// gravity factor
 
−
_gravity: 0.07,
 
−
// air resistance factor
 
−
_resistance: 0.975,
 
−
// zIndex of particles
 
−
_zIndex: 20000,
 
−
// maximal age of particles (in msec)
 
−
_maxAge: 2000,
 
−
// interval of appearing explosions (in msec)
 
−
_interval: [500, 2500],
 
−
// amount of particles per explosion
 
−
_particlesPerExplosion: 40,
 
−
// maximal speed of particle at moment of explosion
 
−
_speed: 5,
 
  
−
+
JSFX.getObjId = function(){return "JSFX_obj" + JSFX.objNo++;};
−
 
−
///////////////////////////// private vars /////////////////////////////
 
−
 
−
_particles: [],
 
−
_bodyWidth: 0,
 
−
_bodyHeight: 0,
 
−
_count: 0,
 
−
_lastInterval: 0,
 
  
−
+
JSFX.createLayer = function(theHtml)
 +
{
 +
var layerId = JSFX.getObjId();
  
−
////////////////////////////// functions ///////////////////////////////
+
document.write(ns4 ? "<LAYER  NAME='"+layerId+"'>"+theHtml+"</LAYER>" :
 +
  "<DIV id='"+layerId+"' style='position:absolute'>"+theHtml+"</DIV>" );
  
−
// init fireworks
+
var el = document.getElementById ? document.getElementById(layerId) :
−
init: function()
+
document.all ? document.all[layerId] :
−
{
+
  document.layers[layerId];
−
this._addEventListener(window, 'resize', function() { return fireworks.resize.apply(fireworks); });
+
 
−
this._addEventListener(window, 'load', function() { return fireworks.start.apply(fireworks); });
+
if(ns4)
−
},
+
el.style=el;
−
+
 
−
+
return el;
−
// add an event listener
+
}
−
_addEventListener: function(el, name, handler)
+
JSFX.fxLayer = function(theHtml)
−
{
+
{
−
if (el.addEventListener)
+
if(theHtml == null) return;
−
el.addEventListener(name, handler, false);  
+
this.el = JSFX.createLayer(theHtml);
−
else if (el.attachEvent)
+
}
−
el.attachEvent('on' + name, handler);
+
var proto = JSFX.fxLayer.prototype
−
},
+
 
−
+
proto.moveTo    = function(x,y){this.el.style.left = x;this.el.style.top=y;}
−
+
proto.setBgColor = function(color) { this.el.style.backgroundColor = color; }
−
// start fireworks
+
proto.clip      = function(x1,y1, x2,y2){ this.el.style.clip="rect("+y1+" "+x2+" "+y2+" "+x1+")"; }
−
start: function()
+
if(ns4){
−
{
+
proto.clip = function(x1,y1, x2,y2){
−
// init window size
+
this.el.style.clip.top =y1;this.el.style.clip.left =x1;
−
this.resize();
+
this.el.style.clip.bottom=y2;this.el.style.clip.right =x2;
 +
}
 +
proto.setBgColor=function(color) { this.el.bgColor = color; }
 +
}
 +
if(window.opera)
 +
proto.setBgColor = function(color) { this.el.style.color = color==null?'transparent':color; }
 +
 
 +
if(window.innerWidth)
 +
{
 +
gX=function(){return innerWidth;};
 +
gY=function(){return innerHeight;};
 +
}
 +
else
 +
{
 +
gX=function(){return document.body.clientWidth;};
 +
gY=function(){return document.body.clientHeight;};
 +
}
 +
 
 +
/*** Example extend class ***/
 +
JSFX.fxLayer2 = function(theHtml)
 +
{
 +
this.superC = JSFX.fxLayer;
 +
this.superC(theHtml + "C");
 +
}
 +
JSFX.fxLayer2.prototype = new JSFX.fxLayer;
 +
}/*** End Library Code ***/
 +
 
 +
/*************************************************/
 +
 
 +
/*** Class Firework extends FxLayer ***/
 +
JSFX.Firework = function(fwImages)
 +
{
 +
window[ this.id = JSFX.getObjId() ] = this;
 +
this.imgId = "i" + this.id;
 +
this.fwImages  = fwImages;
 +
this.numImages = fwImages.length;
 +
this.superC = JSFX.fxLayer;
 +
this.superC("<img src='"+fwImages[0].src+"' name='"+this.imgId+"'>");
  
−
// start to move particles
+
this.img = document.layers ? this.el.document.images[0] : document.images[this.imgId];
−
this._animFn = function() {fireworks._move();};
+
this.step = 0;
−
this._lastInterval = this._time();
+
this.timerId = -1;
−
requestAnimFrame(this._animFn);
+
this.x = 0;
−
+
this.y = 0;
−
this._addExplosion();
+
this.dx = 0;
−
},
+
this.dy = 0;
−
+
this.ay = 0.2;
−
+
this.state = "OFF";
−
// get current time
+
}
−
_time: function()
+
JSFX.Firework.prototype = new JSFX.fxLayer;
−
{
 
−
return +new Date();
 
−
},
 
−
 
  
−
// return a random integer
+
JSFX.Firework.prototype.getMaxDy = function()
−
_random: function(value)
+
{
 +
var ydiff = gY() - 130;
 +
var dy    = 1;
 +
var dist  = 0;
 +
var ay    = this.ay;
 +
while(dist<ydiff)
 
{
 
{
−
return Math.random() * value;
+
dist += dy;
−
},
+
dy+=ay;
−
+
}
 +
return -dy;
 +
}
 +
JSFX.Firework.prototype.setFrame = function()
 +
{
 +
// this.img.src=this.fwName+"/"+this.step+".gif";
 +
this.img.src=this.fwImages[ this.step ].src;
 +
}
 +
JSFX.Firework.prototype.animate = function()
 +
{
  
−
// return a random array element
+
if(this.state=="OFF")
−
_randomArray: function(arr)
 
 
{
 
{
−
return arr[
 
−
Math.floor(
 
−
Math.random() * (arr.length)
 
−
)
 
−
];
 
−
},
 
−
 
−
 
−
// add a new explosion
 
−
_addExplosion: function()
 
−
{
 
−
var x = Math.floor(this._random(this._bodyWidth)),
 
−
y = Math.floor((this._random(.5) + .1) * this._bodyHeight),
 
−
dx = this._random(10) - 5,
 
−
dy = this._random(-2) - 1,
 
−
c1 = this._randomArray(this._color),
 
−
c2 = this._randomArray(this._color);
 
 
 
−
for (var i = 0; i < this._particlesPerExplosion; i++)
+
this.step = 0;
 +
this.x = gX()/2-20;
 +
this.y = gY()-100;
 +
this.moveTo(this.x, this.y);
 +
this.setFrame();
 +
if(Math.random() > .95)
 
{
 
{
−
this._createParticle(
+
this.dy = this.getMaxDy();
−
x,
+
this.dx = Math.random()*-8 + 4;
−
y,
+
this.dy += Math.random()*3;
−
dx,
+
this.state = "TRAVEL";
−
dy,
 
−
i / (this._particlesPerExplosion - 1) * 180 * Math.PI,
 
−
this._random(this._speed),
 
−
this._random(1) > .5 ? c1 : c2
 
−
);
 
 
}
 
}
−
+
}
−
window.setTimeout(
+
else if(this.state=="TRAVEL")
−
function() { return fireworks._addExplosion.apply(fireworks);},
+
{
−
this._random(this._interval[1] - this._interval[0]) + this._interval[0]
+
this.x += this.dx;
−
);
+
this.y += this.dy;
−
},
+
this.dy += this.ay;
−
+
this.moveTo(this.x,this.y);
−
+
if(this.dy > 1)
−
// creates a new particle
+
this.state="EXPLODE"
−
_createParticle: function(x, y, dx, dy, rot, speed, color)
+
}
 +
else if(this.state == "EXPLODE")
 
{
 
{
−
var el = null,
+
this.step++;
−
foundEl = false,
+
if(this.step < this.numImages)
−
p = this._particles;
+
this.setFrame();
 +
else
 +
this.state="OFF";
 +
}
 +
}
 +
/*** END Class Firework***/
  
−
// look for old particle
+
/*** Class FireworkDisplay extends Object ***/
−
for (var i = 0, l = p.length; i < l; i++)
+
JSFX.FireworkDisplay = function(n, fwImages, numImages)
−
if (p[i].data.age > 1)
+
{
−
{
+
window[ this.id = JSFX.getObjId() ] = this;
−
el = p[i];
+
this.timerId = -1;
−
foundEl = true;
+
this.fireworks = new Array();
−
break;
+
this.imgArray = new Array();
−
}
+
this.loadCount=0;
−
+
this.loadImages(fwImages, numImages);
−
// create span child for particles
 
−
if (!foundEl)
 
−
{
 
−
el = document.createElement('div');
 
−
el.className      = 'particle';
 
−
el.style.position = 'absolute';
 
−
el.style.fontSize = '20px';
 
−
el.style.zIndex  = this._zIndex;
 
−
el.style.width    = '10px';
 
−
el.style.overflow = 'hidden';
 
−
el.innerHTML      = '&#x25cf;';
 
−
}
 
−
 
 
−
el.style.left    = x + 'px';
 
−
el.style.top      = y + 'px';
 
−
el.style.color    = color;
 
−
el.data = {
 
−
x: x,
 
−
y: y,
 
−
dx: Math.cos(rot) * speed + dx,
 
−
dy: Math.sin(rot) * speed + dy,
 
−
color: color,
 
−
age: Math.random() * .25
 
−
};
 
−
 
−
if (!foundEl)
 
−
{
 
−
document.getElementsByTagName('body')[0].appendChild(el);
 
−
this._particles.push(el);
 
−
}
 
−
},
 
  
−
+
for(var i=0 ; i<n ; i++)
−
// move existing particles
+
this.fireworks[this.fireworks.length] = new JSFX.Firework(this.imgArray);
−
_move: function()
+
}
 +
JSFX.FireworkDisplay.prototype.loadImages = function(fwName, numImages)
 +
{
 +
for(var i=0 ; i<numImages ; i++)
 
{
 
{
−
requestAnimFrame(this._animFn);
+
this.imgArray[i] = new Image();
−
+
this.imgArray[i].obj = this;
−
// calculate movement factor
+
this.imgArray[i].onload = window[this.id].imageLoaded;
−
var dif = this._time() - this._lastInterval;
+
this.imgArray[i].src = fwName+"/"+i+".gif";
−
this._lastInterval = this._time();
+
}
−
+
}
−
var delta = dif / 20,
+
JSFX.FireworkDisplay.prototype.imageLoaded = function()
−
el,
+
{
−
d,
+
this.obj.loadCount++;
−
p = this._particles,
+
}
−
r = Math.pow(this._resistance, delta),
 
−
g = this._gravity * delta,
 
−
a = dif / this._maxAge;
 
−
 
−
for (var i = 0, l = p.length; i < l; i++)
 
−
{
 
−
el = p[i];
 
−
d = el.data;
 
−
 
−
if (d.age > 1)
 
−
continue;
 
−
 
−
d.age += a;
 
−
d.dy += g;
 
−
d.dx *= r;
 
−
d.dy *= r;
 
−
d.x += d.dx * delta;
 
−
d.y += d.dy * delta;
 
−
 
−
if (d.x < 0)
 
−
{
 
−
d.dx *= -1;
 
−
d.x = 0;
 
−
}
 
−
if (d.x > this._bodyWidth)
 
−
{
 
−
d.dx *= -1;
 
−
d.x = this._bodyWidth;
 
−
}
 
−
if (d.y < 0)
 
−
{
 
−
d.dy *= -1;
 
−
d.y = 0;
 
−
}
 
−
if (d.y > this._bodyHeight)
 
−
{
 
−
d.dy *= -1;
 
−
d.y = this._bodyHeight;
 
−
}
 
−
 
−
if (d.age > 1)
 
−
d.x = d.y = 0;
 
−
 
−
el.style.left = d.x + 'px';
 
−
el.style.top = d.y + 'px';
 
−
el.style.color = (Math.random() * .5 + d.age >= 1) ? 'transparent' : d.color;
 
−
el.style.fontSize = (1 - d.age) * 5 + 5 + 'px';
 
−
}
 
−
},
 
−
 
  
−
// calculate new positions for all particles
+
JSFX.FireworkDisplay.prototype.animate = function()
−
resize: function()
+
{
−
{
+
status = this.loadCount;
−
// get new width and height
+
if(this.loadCount < this.imgArray.length)
−
this._bodyWidth = this._getWindowWidth() - 10;
+
return;
−
this._bodyHeight = this._getWindowHeight() - 20;
 
−
},
 
  
−
+
for(var i=0 ; i<this.fireworks.length ; i++)
−
// get window width
+
this.fireworks[i].animate();
−
_getWindowWidth: function()
+
}
 +
JSFX.FireworkDisplay.prototype.start = function()
 +
{
 +
if(this.timerId == -1)
 
{
 
{
−
return document.getElementsByTagName('body')[0].offsetWidth;
+
this.state = "OFF";
−
},
+
this.timerId = setInterval("window."+this.id+".animate()", 40);
 +
}
  
−
+
}
−
// get window height
+
JSFX.FireworkDisplay.prototype.stop = function()
−
_getWindowHeight: function()
+
{
 +
if(this.timerId != -1)
 
{
 
{
−
var h = Math.max(self.innerHeight || 0, window.innerHeight || 0);
+
clearInterval(this.timerId);
−
+
this.timerId = -1;
−
if (document.documentElement)
+
for(var i=0 ; i<this.fireworks.length ; i++)
−
h = Math.max(h, document.documentElement.clientHeight || 0);
 
−
if (document.body)
 
 
{
 
{
−
h = Math.max(h, document.body.clientHeight || 0);
+
this.fireworks[i].moveTo(-100, -100);
−
h = Math.max(h, document.body.scrollHeight || 0);
+
this.fireworks[i].step = 0;;
−
h = Math.max(h, document.body.offsetHeight || 0);
+
this.fireworks[i].state = "OFF";
−
}
+
}
−
 
−
return h;
 
 
}
 
}
 +
}
 +
/*** END Class FireworkDisplay***/
  
−
};
+
JSFX.FWStart = function()
−
 
+
{
−
// shim layer with setTimeout fallback
+
if(JSFX.FWLoad)JSFX.FWLoad();
−
window.requestAnimFrame = (function(){
+
myFW.start();
−
  return  window.requestAnimationFrame      ||
+
}
−
  window.webkitRequestAnimationFrame ||
+
myFW = new JSFX.FireworkDisplay(20, "fw07", 27);
−
  window.mozRequestAnimationFrame    ||
+
JSFX.FWLoad=window.onload;
−
  window.oRequestAnimationFrame      ||
+
window.onload=JSFX.FWStart;
−
  window.msRequestAnimationFrame    ||
 
−
  function (cb){
 
−
window.setTimeout(cb, 1000 / 60);
 
−
  };
 
−
})();
 
−
 
 
−
fireworks.init();
 

Version vom 3. Dezember 2015, 03:30 Uhr

/**
  * You may use this code for free on any web page provided that 
  * these comment lines and the following credit remain in the code.
  * Cross Browser Fireworks from http://www.javascript-fx.com
  */
/*************************************************/
if(!window.JSFX) JSFX=new Object();

if(!JSFX.createLayer)
{/*** Include Library Code ***/

var ns4 = document.layers;
var ie4 = document.all;
JSFX.objNo=0;

JSFX.getObjId = function(){return "JSFX_obj" + JSFX.objNo++;};

JSFX.createLayer = function(theHtml)
{
	var layerId = JSFX.getObjId();

	document.write(ns4 ? "<LAYER  NAME='"+layerId+"'>"+theHtml+"</LAYER>" : 
				   "<DIV id='"+layerId+"' style='position:absolute'>"+theHtml+"</DIV>" );

	var el = 	document.getElementById	? document.getElementById(layerId) :
			document.all 		? document.all[layerId] :
							  document.layers[layerId];

	if(ns4)
		el.style=el;

	return el;
}
JSFX.fxLayer = function(theHtml)
{
	if(theHtml == null) return;
	this.el = JSFX.createLayer(theHtml);
}
var proto = JSFX.fxLayer.prototype

proto.moveTo     = function(x,y){this.el.style.left = x;this.el.style.top=y;}
proto.setBgColor = function(color) { this.el.style.backgroundColor = color; } 
proto.clip       = function(x1,y1, x2,y2){ this.el.style.clip="rect("+y1+" "+x2+" "+y2+" "+x1+")"; }
if(ns4){
	proto.clip = function(x1,y1, x2,y2){
		this.el.style.clip.top	 =y1;this.el.style.clip.left	=x1;
		this.el.style.clip.bottom=y2;this.el.style.clip.right	=x2;
	}
	proto.setBgColor=function(color) { this.el.bgColor = color; }
}
if(window.opera)
	proto.setBgColor = function(color) { this.el.style.color = color==null?'transparent':color; }

if(window.innerWidth)
{
	gX=function(){return innerWidth;};
	gY=function(){return innerHeight;};
}
else
{
	gX=function(){return document.body.clientWidth;};
	gY=function(){return document.body.clientHeight;};
}

/*** Example extend class ***/
JSFX.fxLayer2 = function(theHtml)
{
	this.superC = JSFX.fxLayer;
	this.superC(theHtml + "C");
}
JSFX.fxLayer2.prototype = new JSFX.fxLayer;
}/*** End Library Code ***/

/*************************************************/

/*** Class Firework extends FxLayer ***/
JSFX.Firework = function(fwImages)
{
	window[ this.id = JSFX.getObjId() ] = this;
	this.imgId = "i" + this.id;
	this.fwImages  = fwImages;
	this.numImages = fwImages.length;
	this.superC = JSFX.fxLayer;
	this.superC("<img src='"+fwImages[0].src+"' name='"+this.imgId+"'>");

	this.img = document.layers ? this.el.document.images[0] : document.images[this.imgId];
	this.step = 0;
	this.timerId = -1;
	this.x = 0;
	this.y = 0;
	this.dx = 0;
	this.dy = 0;
	this.ay = 0.2;
	this.state = "OFF";
}
JSFX.Firework.prototype = new JSFX.fxLayer;

JSFX.Firework.prototype.getMaxDy = function()
{
	var ydiff = gY() - 130;
	var dy    = 1;
	var dist  = 0;
	var ay    = this.ay;
	while(dist<ydiff)
	{
		dist += dy;
		dy+=ay;
	}
	return -dy;
}
JSFX.Firework.prototype.setFrame = function()
{
//	this.img.src=this.fwName+"/"+this.step+".gif";
	this.img.src=this.fwImages[ this.step ].src;
}
JSFX.Firework.prototype.animate = function()
{

	if(this.state=="OFF")
	{
		
		this.step = 0;
		this.x = gX()/2-20;
		this.y = gY()-100;
		this.moveTo(this.x, this.y);
		this.setFrame();
		if(Math.random() > .95)
		{
			this.dy = this.getMaxDy();
			this.dx = Math.random()*-8 + 4;
			this.dy += Math.random()*3;
			this.state = "TRAVEL";
		}
	}
	else if(this.state=="TRAVEL")
	{
		this.x += this.dx;
		this.y += this.dy;
		this.dy += this.ay;
		this.moveTo(this.x,this.y);
		if(this.dy > 1)
			this.state="EXPLODE"
	}
	else if(this.state == "EXPLODE")
	{
		this.step++;
		if(this.step < this.numImages)
			this.setFrame();
		else
			this.state="OFF";
	}
}
/*** END Class Firework***/

/*** Class FireworkDisplay extends Object ***/
JSFX.FireworkDisplay = function(n, fwImages, numImages)
{
	window[ this.id = JSFX.getObjId() ] = this;
	this.timerId = -1;
	this.fireworks = new Array();
	this.imgArray = new Array();
	this.loadCount=0;
	this.loadImages(fwImages, numImages);

	for(var i=0 ; i<n ; i++)
		this.fireworks[this.fireworks.length] = new JSFX.Firework(this.imgArray);
}
JSFX.FireworkDisplay.prototype.loadImages = function(fwName, numImages)
{
	for(var i=0 ; i<numImages ; i++)
	{
		this.imgArray[i] = new Image();
		this.imgArray[i].obj = this;
		this.imgArray[i].onload = window[this.id].imageLoaded;
		this.imgArray[i].src = fwName+"/"+i+".gif";
	}
}
JSFX.FireworkDisplay.prototype.imageLoaded = function()
{
	this.obj.loadCount++;
}

JSFX.FireworkDisplay.prototype.animate = function()
{
status = this.loadCount;
	if(this.loadCount < this.imgArray.length)
		return;

	for(var i=0 ; i<this.fireworks.length ; i++)
		this.fireworks[i].animate();
}
JSFX.FireworkDisplay.prototype.start = function()
{
	if(this.timerId == -1)
	{
		this.state = "OFF";
		this.timerId = setInterval("window."+this.id+".animate()", 40);
	}

}
JSFX.FireworkDisplay.prototype.stop = function()
{
	if(this.timerId != -1)
	{
		clearInterval(this.timerId);
		this.timerId = -1;
		for(var i=0 ; i<this.fireworks.length ; i++)
		{
			this.fireworks[i].moveTo(-100, -100);
			this.fireworks[i].step = 0;;
			this.fireworks[i].state = "OFF";
		}	
	}
}
/*** END Class FireworkDisplay***/

JSFX.FWStart = function()
{
	if(JSFX.FWLoad)JSFX.FWLoad();
	myFW.start();
}
myFW = new JSFX.FireworkDisplay(20, "fw07", 27);
JSFX.FWLoad=window.onload;
window.onload=JSFX.FWStart;