User:MonkeysHK/common.js

window.HalloweenSprites = 25; // number of sprites

window.HalloweenEffects = that = Object.assign(this, {   N: HalloweenSprites, // 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,    canvas: (function  { var div = $(" "); for (var i = 0; i < HalloweenSprites; i++) $(" ").appendTo(div); return div; }),   init: function  {        this.loaded = true;        $("body").prepend(this.canvas);        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        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; else this.x[index] = this.y[index] = -this.H; // 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) { $(".halloween-sprite").eq(index).css({               left: x,                top: y,            }) .hide .delay(Math.floor(Math.random * MAXWAIT + MINWAIT)) .animate({               opacity: 1,            }, 10, function  {                $(this).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);