User:MonkeysHK/common.js

window.HalloweenEffects = that = Object.assign(this, {	N: 10, // 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	pause: false,	$document: $(document),	init: function {		this.loaded = true;		$(":root").css("--halloween-sprite-size", this.H + "px");		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);	},	rand: function(min, max) {		return Math.floor(Math.random * Math.floor(max)) + Math.ceil(min);	},	calculate: function (index) {		if (document.visibilityState !== 'visible')			return waitUntilFocus.then(function { that.calculate(index); });

this.setbounds(index); var boundSep = this.H * (MINSEP); // minimun separation to screen height boundaries

var info = [ this.canvas.width - boundSep * 2, // info[0]: width, taking care of resizing this.canvas.height - boundSep * 2, // info[1]: height, taking care of resizing ];

console.log(boundSep, info[0], info[1], this.rand(boundSep, info[0]), this.rand(boundSep, info[1])); var centerX = -1, centerY = -1;

do { centerX = this.rand(boundSep, info[0]), centerY = this.rand(boundSep, info[1]); } while (this.withinBounds(centerX, centerY));

console.log(centerX, centerY); this.x[index] = centerX, this.y[index] = centerY, this.invalid[index] = false;

console.log(this.x[index], this.y[index]); 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			}); },	avoidElements: [".page"], setbounds: function(index) { this.bounds = []; for (var i in this.avoidElements) { var $el = $(this.avoidElements[i]); if ($el.length) { var offset = $el.offset, scrolltop = that.$document.scrollTop, top = Math.max(offset.left - scrolltop, 0), bottom = Math.min(offset.left + $el.innerHeight - scrolltop, this.canvas.height); this.bounds.push({					top: top,					left: offset.top,					width: $el.innerWidth,					height: bottom - top,				});

$(".bound").remove; this.canvas.append($(" ").css({ top: top, left: offset.top, width: $el.innerWidth, height: bottom - top, }));			}		}

var centerSep = this.H * (MINSEP + 1); // minimun separation between sprites for (i in this.x) { if (i != index && typeof this.x[i] !== "undefined" && typeof this.y[i] !== "undefined") { this.bounds.push({					left: this.x[i] - centerSep * 1,					top: this.y[i] - centerSep * 1,					height: centerSep * 1,					width: centerSep * 1,				}); }		}	},	withinBounds: function(x, y) { for (var i in this.bounds) { var xmin = this.bounds[i].left, xmax = this.bounds[i].left + this.bounds[i].width, ymin = this.bounds[i].top, ymax = this.bounds[i].top + this.bounds[i].height; if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) return true; }		return false; },	pauseAll: function { this.pause = true; },	startAll: function { this.pause = false; },	waitUntilFocus: function { var promise = $.Deferred;

if (document.visibilityState !== 'visible') document.addEventListener('visibilitychange', function {				promise.resolve;			}, { once: true }); else return promise.resolve;

return promise; }, });

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

// $(function { // 	HalloweenEffects.init; // });