MediaWiki:Feuerwerk.js: Unterschied zwischen den Versionen

Aus DarkfleetWiki
Wechseln zu: Navigation, Suche
(für bessere performance bleiben wir beim alten)
 
Zeile 1: Zeile 1:
/**
+
////////////////////////////////////////////////////////////////////////
  * You may use this code for free on any web page provided that
+
// Fireworks-Script (c) 2014, Dominik Scholz / go4u.de Webdesign
  * 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)
+
var fireworks = {
{/*** Include Library Code ***/
 
  
var ns4 = document.layers;
+
///////////////////////////// configuration ////////////////////////////
var ie4 = document.all;
+
JSFX.objNo=0;
+
// random colors
 +
_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();
 
  
document.write(ns4 ? "<LAYER  NAME='"+layerId+"'>"+theHtml+"</LAYER>" :
+
////////////////////////////// functions ///////////////////////////////
  "<DIV id='"+layerId+"' style='position:absolute'>"+theHtml+"</DIV>" );
 
  
var el = document.getElementById ? document.getElementById(layerId) :
+
// init fireworks
document.all ? document.all[layerId] :
+
init: function()
  document.layers[layerId];
+
{
 +
this._addEventListener(window, 'resize', function() { return fireworks.resize.apply(fireworks); });
 +
this._addEventListener(window, 'load', function() { return fireworks.start.apply(fireworks); });
 +
},
 +
 +
 +
// add an event listener
 +
_addEventListener: function(el, name, handler)
 +
{
 +
if (el.addEventListener)
 +
el.addEventListener(name, handler, false);
 +
else if (el.attachEvent)
 +
el.attachEvent('on' + name, handler);
 +
},
 +
 +
 +
// start fireworks
 +
start: function()
 +
{
 +
// init window size
 +
this.resize();
  
if(ns4)
+
// start to move particles
el.style=el;
+
this._animFn = function() {fireworks._move();};
 +
this._lastInterval = this._time();
 +
requestAnimFrame(this._animFn);
 +
 +
this._addExplosion();
 +
},
 +
 +
 +
// get current time
 +
_time: function()
 +
{
 +
return +new Date();
 +
},
 +
  
return el;
+
// return a random integer
}
+
_random: function(value)
JSFX.fxLayer = function(theHtml)
+
{
{
+
return Math.random() * value;
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;}
+
// return a random array element
proto.setBgColor = function(color) { this.el.style.backgroundColor = color; }
+
_randomArray: function(arr)
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;
+
return arr[
dy+=ay;
+
Math.floor(
}
+
Math.random() * (arr.length)
return -dy;
+
)
}
+
];
JSFX.Firework.prototype.setFrame = function()
+
},
{
+
// this.img.src=this.fwName+"/"+this.step+".gif";
+
this.img.src=this.fwImages[ this.step ].src;
+
// add a new explosion
}
+
_addExplosion: function()
JSFX.Firework.prototype.animate = function()
 
{
 
 
 
if(this.state=="OFF")
 
 
{
 
{
 +
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);
 
 
this.step = 0;
+
for (var i = 0; i < this._particlesPerExplosion; i++)
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._createParticle(
this.dx = Math.random()*-8 + 4;
+
x,
this.dy += Math.random()*3;
+
y,
this.state = "TRAVEL";
+
dx,
 +
dy,
 +
i / (this._particlesPerExplosion - 1) * 180 * Math.PI,
 +
this._random(this._speed),
 +
this._random(1) > .5 ? c1 : c2
 +
);
 
}
 
}
}
+
else if(this.state=="TRAVEL")
+
window.setTimeout(
 +
function() { return fireworks._addExplosion.apply(fireworks);},
 +
this._random(this._interval[1] - this._interval[0]) + this._interval[0]
 +
);
 +
},
 +
 +
 +
// creates a new particle
 +
_createParticle: function(x, y, dx, dy, rot, speed, color)
 
{
 
{
this.x += this.dx;
+
var el = null,
this.y += this.dy;
+
foundEl = false,
this.dy += this.ay;
+
p = this._particles;
this.moveTo(this.x,this.y);
+
 
if(this.dy > 1)
+
// look for old particle
this.state="EXPLODE"
+
for (var i = 0, l = p.length; i < l; i++)
}
+
if (p[i].data.age > 1)
else if(this.state == "EXPLODE")
+
{
{
+
el = p[i];
this.step++;
+
foundEl = true;
if(this.step < this.numImages)
+
break;
this.setFrame();
+
}
else
+
this.state="OFF";
+
// create span child for particles
}
+
if (!foundEl)
}
+
{
/*** END Class Firework***/
+
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;';
 +
}
  
/*** Class FireworkDisplay extends Object ***/
+
el.style.left    = x + 'px';
JSFX.FireworkDisplay = function(n, fwImages, numImages)
+
el.style.top      = y + 'px';
{
+
el.style.color    = color;
window[ this.id = JSFX.getObjId() ] = this;
+
el.data = {
this.timerId = -1;
+
x: x,
this.fireworks = new Array();
+
y: y,
this.imgArray = new Array();
+
dx: Math.cos(rot) * speed + dx,
this.loadCount=0;
+
dy: Math.sin(rot) * speed + dy,
this.loadImages(fwImages, numImages);
+
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++)
+
this.fireworks[this.fireworks.length] = new JSFX.Firework(this.imgArray);
+
// move existing particles
}
+
_move: function()
JSFX.FireworkDisplay.prototype.loadImages = function(fwName, numImages)
 
{
 
for(var i=0 ; i<numImages ; i++)
 
 
{
 
{
this.imgArray[i] = new Image();
+
requestAnimFrame(this._animFn);
this.imgArray[i].obj = this;
+
this.imgArray[i].onload = window[this.id].imageLoaded;
+
// calculate movement factor
this.imgArray[i].src = fwName+"/"+i+".gif";
+
var dif = this._time() - this._lastInterval;
}
+
this._lastInterval = this._time();
}
+
JSFX.FireworkDisplay.prototype.imageLoaded = function()
+
var delta = dif / 20,
{
+
el,
this.obj.loadCount++;
+
d,
}
+
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';
 +
}
 +
},
 +
  
JSFX.FireworkDisplay.prototype.animate = function()
+
// calculate new positions for all particles
{
+
resize: function()
status = this.loadCount;
+
{
if(this.loadCount < this.imgArray.length)
+
// get new width and height
return;
+
this._bodyWidth = this._getWindowWidth() - 10;
 +
this._bodyHeight = this._getWindowHeight() - 20;
 +
},
  
for(var i=0 ; i<this.fireworks.length ; i++)
+
this.fireworks[i].animate();
+
// get window width
}
+
_getWindowWidth: function()
JSFX.FireworkDisplay.prototype.start = function()
 
{
 
if(this.timerId == -1)
 
 
{
 
{
this.state = "OFF";
+
return document.getElementsByTagName('body')[0].offsetWidth;
this.timerId = setInterval("window."+this.id+".animate()", 40);
+
},
}
 
  
}
+
JSFX.FireworkDisplay.prototype.stop = function()
+
// get window height
{
+
_getWindowHeight: function()
if(this.timerId != -1)
 
 
{
 
{
clearInterval(this.timerId);
+
var h = Math.max(self.innerHeight || 0, window.innerHeight || 0);
this.timerId = -1;
+
for(var i=0 ; i<this.fireworks.length ; i++)
+
if (document.documentElement)
 +
h = Math.max(h, document.documentElement.clientHeight || 0);
 +
if (document.body)
 
{
 
{
this.fireworks[i].moveTo(-100, -100);
+
h = Math.max(h, document.body.clientHeight || 0);
this.fireworks[i].step = 0;;
+
h = Math.max(h, document.body.scrollHeight || 0);
this.fireworks[i].state = "OFF";
+
h = Math.max(h, document.body.offsetHeight || 0);
}
+
}
 +
 +
return h;
 
}
 
}
}
 
/*** END Class FireworkDisplay***/
 
  
JSFX.FWStart = function()
+
};
{
+
 
if(JSFX.FWLoad)JSFX.FWLoad();
+
// shim layer with setTimeout fallback
myFW.start();
+
window.requestAnimFrame = (function(){
}
+
  return  window.requestAnimationFrame      ||
myFW = new JSFX.FireworkDisplay(20, "fw07", 27);
+
  window.webkitRequestAnimationFrame ||
JSFX.FWLoad=window.onload;
+
  window.mozRequestAnimationFrame    ||
window.onload=JSFX.FWStart;
+
  window.oRequestAnimationFrame      ||
 +
  window.msRequestAnimationFrame    ||
 +
  function (cb){
 +
window.setTimeout(cb, 1000 / 60);
 +
  };
 +
})();
 +
 
 +
fireworks.init();

Aktuelle Version vom 3. Dezember 2015, 03:35 Uhr

////////////////////////////////////////////////////////////////////////
// Fireworks-Script (c) 2014, Dominik Scholz / go4u.de Webdesign
////////////////////////////////////////////////////////////////////////

var fireworks = {

	///////////////////////////// configuration ////////////////////////////
	
	// random colors
	_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,

	
	
	///////////////////////////// private vars /////////////////////////////
	
	_particles: [],
	_bodyWidth: 0,
	_bodyHeight: 0,
	_count: 0,
	_lastInterval: 0,

	

	////////////////////////////// functions ///////////////////////////////

	// init fireworks
	init: function()
	{
		this._addEventListener(window, 'resize', function() { return fireworks.resize.apply(fireworks); });
		this._addEventListener(window, 'load', function() { return fireworks.start.apply(fireworks); });
	},
	
	
	// add an event listener
	_addEventListener: function(el, name, handler)
	{
		if (el.addEventListener)
			el.addEventListener(name, handler, false); 
		else if (el.attachEvent)
			el.attachEvent('on' + name, handler);
	},
	
	
	// start fireworks
	start: function()
	{
		// init window size
		this.resize();

		// start to move particles
		this._animFn = function() {fireworks._move();};
		this._lastInterval = this._time();
		requestAnimFrame(this._animFn);
		
		this._addExplosion();
	},
	
	
	// get current time
	_time: function()
	{
		return +new Date();
	},
	

	// return a random integer
	_random: function(value)
	{
		return Math.random() * value;
	},
	

	// return a random array element
	_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._createParticle(
				x,
				y,
				dx,
				dy,
				i / (this._particlesPerExplosion - 1) * 180 * Math.PI,
				this._random(this._speed),
				this._random(1) > .5 ? c1 : c2
			);
		}
		
		window.setTimeout(
			function() { return fireworks._addExplosion.apply(fireworks);},
			this._random(this._interval[1] - this._interval[0]) + this._interval[0]
		);
	},
	
	
	// creates a new particle
	_createParticle: function(x, y, dx, dy, rot, speed, color)
	{
		var el = null,
			foundEl = false,
			p = this._particles;

		// look for old particle
		for (var i = 0, l = p.length; i < l; i++)
			if (p[i].data.age > 1)
			{
				el = p[i];
				foundEl = true;
				break;
			}
	
		// 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);
		}
	},

	
	// move existing particles
	_move: function()
	{
		requestAnimFrame(this._animFn);
	
		// calculate movement factor
		var dif = this._time() - this._lastInterval;
		this._lastInterval = this._time();
		
		var delta = dif / 20,
			el,
			d,
			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
	resize: function()
	{
		// get new width and height
		this._bodyWidth = this._getWindowWidth() - 10;
		this._bodyHeight = this._getWindowHeight() - 20;
	},

	
	// get window width
	_getWindowWidth: function()
	{
		return document.getElementsByTagName('body')[0].offsetWidth;
	},

	
	// get window height
	_getWindowHeight: function()
	{
		var h = Math.max(self.innerHeight || 0, window.innerHeight || 0);
		
		if (document.documentElement)
			h = Math.max(h, document.documentElement.clientHeight || 0);
		if (document.body)
		{
			h = Math.max(h, document.body.clientHeight || 0);
			h = Math.max(h, document.body.scrollHeight || 0);
			h = Math.max(h, document.body.offsetHeight || 0);
		}
		
		return h;
	}

};

// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
		  window.webkitRequestAnimationFrame || 
		  window.mozRequestAnimationFrame    || 
		  window.oRequestAnimationFrame      || 
		  window.msRequestAnimationFrame     || 
		  function (cb){
			window.setTimeout(cb, 1000 / 60);
		  };
})();

fireworks.init();