User:MonkeysHK/common.js

window.HalloweenEffects = that = Object.assign(this, {	N: 25, // number of sprites	H: 80, // width and height of a sprite in pixels	MINSEP: 0.5, // images are no closer to each other or with the edge than MINSEP times the image size (≥ 0.5 recommended)	MINWAIT: 5000, // minimum wait time in ms	MAXWAIT: 10000, // maximum wait time in ms	MAXTRIES: 15, // maximum tries given to the random function before discarding (and not showing element); affects performance	pause: false,	$document: $(document),	init: function {		this.loaded = true;		this.x = Array.apply(null, Array(this.N)); // empty array		this.y = Array.apply(null, Array(this.N)); // empty array		this.invalid = Array.apply(null, Array(this.N)); // empty array		this.sprites = Array.apply(null, Array(this.N)).map(function  { return $(" "); });		this.canvas = (function { var div = $(" "); return div.append(this.sprites); });		$("body").prepend(this.canvas);		for (var i in this.x)			calculate(i);	},	calculate: function (index) {		var info = [			this.canvas.width, // info[0]: width, taking care of resizing			this.canvas.height, // info[1]: height, taking care of resizing		];		var boundSep = this.H * (MINSEP); // minimun separation to screen height boundaries		var centerSep = this.H * (MINSEP + 1); // minimun separation between sprites, assuming all sprites are round with radius of image size		// $(".bound").css({ //	 width: Math.floor(info[0] - boundSep*2), //	 height: Math.floor(info[1] - boundSep*2), //	 top: boundSep, //	 left: boundSep, // })

var pass = true, centerX = -1, centerY = -1, i, stopper = 0; do { if ((++stopper) > MAXTRIES) { pass = false; break; // out of do..while }			centerX = Math.floor(Math.random * (info[0] - boundSep * 2) + boundSep), centerY = Math.floor(Math.random * (info[1] - boundSep * 2) + boundSep); for (i in this.x) { if (i != index && typeof this.x[i] !== "undefined" && typeof this.y[i] !== "undefined" &&					(distanceFormula(centerX, centerY, this.x[i], this.y[i]) < centerSep)) { pass = false; break; // out of for }			}		} while (!pass);

if (pass) this.x[index] = centerX, this.y[index] = centerY, this.invalid[index] = false; else this.x[index] = this.y[index] = -this.H, this.invalid[index] = true; // send it out of screen

this.set(index, this.x[index] - this.H * 0.5, this.y[index] - this.H * 0.5); },	distanceFormula: function (x1, y1, x2, y2) { return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); },	set: function (index, x, y) { var delay = Math.floor(Math.random * MAXWAIT + MINWAIT); jQuery.fx.interval = 100; if (this.invalid[index]) var halloweenTimer = setTimeout(function {				clearTimeout(halloweenTimer);				if (!that.pause) that.calculate(index); // reset positions			}, delay); else this.sprites[index] .hide .delay(delay) .animate({				opacity: 1,			}, 10, function {				$(this).css({ left: x,					top: y + that.$document.scrollTop, }).show;			}) .animate({				opacity: 0,				top: "-=" + this.H * (Math.random * 0.75 + 0.25)			}, 2000, function {				if (!that.pause)					that.calculate(index); // reset positions			}); },	pauseAll: function { this.pause = true; },	startAll: function { this.pause = false; }, });

(function (mw) {	if (mw.config.get("wgAction") !== "view" || HalloweenEffects.loaded) return;	HalloweenEffects.init; })(mediaWiki);