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 delay time in ms	MAXWAIT: 10000, // maximum delay time in ms	avoidElements: [".wds-global-footer__main"], // selectors for elements to avoid setting sprites onto	pause: false,	DEBUG: true,	$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.regions = Array.apply(null, Array(this.N)); // empty array		this.sprites = Array.apply(null, Array(this.N)).map(function  { return $(" "); });		this.canvas = $(" ").append(this.sprites);		$("body").prepend(this.canvas);		for (var i in this.x)			calculate(i);	},	calculate: function (index) {		if (document.visibilityState !== "visible")			return waitUntilFocus.then(function { that.calculate(index); });

this.setbounds; var boundSep = this.H * (this.MINSEP); // minimun separation to screen height boundaries var centerSep = this.H * (this.MINSEP + 1); // minimun separation between sprites

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 ];

var centerX = -1, centerY = -1;

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

this.x[index] = centerX, this.y[index] = centerY;

this.regions[index] = { left: this.x[index] - centerSep * 1, top: this.y[index] - centerSep * 1, height: centerSep * 1, width: centerSep * 1, };		this.set(index, this.x[index] - this.H * 0.5, this.y[index] - this.H * 0.5); },	set: function (index, x, y) { var delay = Math.floor(Math.random * this.MAXWAIT + this.MINWAIT); jQuery.fx.interval = 100; 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			}); },	setbounds: function { this.bounds = []; $(".halloween-bound-debug-tool").remove; 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.top - scrolltop, 0), bottom = Math.min(offset.top + $el.innerHeight - scrolltop, this.canvas.height); this.bounds.push({					top: top,					left: offset.left,					width: $el.innerWidth,					height: bottom - top,				}); if (this.DEBUG) this.canvas.append($(" ").css({ top: top, left: offset.left, width: $el.innerWidth, height: bottom - top + scrolltop, }));			}		}	},	withinBounds: function (allBounds, x, y) { for (var i in allBounds) { if (typeof allBounds[i] !== "undefined") { var xmin = allBounds[i].left, xmax = allBounds[i].left + allBounds[i].width, ymin = allBounds[i].top, ymax = allBounds[i].top + allBounds[i].height; if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) return true; }		}		return false; },	rand: function (min, max) { return Math.floor(Math.random * Math.floor(max)) + Math.ceil(min); },	waitUntilFocus: function { var promise = $.Deferred;

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

return promise; },	pauseAll: function { this.pause = true; },	startAll: function { this.pause = false; }, });

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

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