UNPKG

brella-transition

Version:
68 lines (67 loc) 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TinierColor = void 0; const brella_1 = require("./brella"); const math_1 = require("./math"); const util_1 = require("./util"); var tinier_color_1 = require("./tinier-color"); Object.defineProperty(exports, "TinierColor", { enumerable: true, get: function () { return tinier_color_1.TinierColor; } }); class BrellaTransition { constructor(options = {}) { var _a, _b, _c, _d, _e, _f; this.active = false; this.brellas = []; this.brellaMax = options.brellaMax || 30; this.brellaRibs = options.brellaRibs || [6, 8]; this.brellaRetries = options.brellaRetries || 1000000; this.frameAttack = (_a = options.frameAttack) !== null && _a !== void 0 ? _a : 15; this.frameHold = (_b = options.frameHold) !== null && _b !== void 0 ? _b : 30; this.frameRotate = (_c = options.frameRotate) !== null && _c !== void 0 ? _c : 0.01; this.colorHue = (((_d = options.colorHue) === null || _d === void 0 ? void 0 : _d.map(x => x == 360 || x == -360 ? x : (x < 0 ? (x % 360) + 360 : x % 360)).sort()) || [0, 360]); this.colorSaturation = (((_e = options.colorSaturation) === null || _e === void 0 ? void 0 : _e.map(x => x < 0 ? 0 : (x > 100 ? 100 : x)).sort()) || [80, 100]); this.colorLightness = (((_f = options.colorLightness) === null || _f === void 0 ? void 0 : _f.map(x => x < 0 ? 0 : (x > 100 ? 100 : x)).sort()) || [50, 50]); this.estimatedFrames = this.brellaMax * 0.5 + this.frameAttack * 2 + this.frameHold - 1; } /** * Renders all Brellas for a frame. The frame counter is automatically incremented. * Does not clear the canvas * @param ctx Canvas rendering context of the canvas you want to render onto */ render(ctx) { if (!this.active) return; // Keep spawning if limit is not reached if (this.brellas.length < this.brellaMax) { let spawns = 2; // Special case: frameAttack is 0 if (this.frameAttack == 0) spawns = this.brellaMax; // Spawn 2 brellas every frame for (let ii = 0; ii < spawns; ii++) { var retries = this.brellaRetries; if (retries < 0) retries = Infinity; var pos; // Keep retrying if overlapped do { pos = new math_1.Vec2(ctx.canvas.width * Math.random(), ctx.canvas.height * Math.random()); } while (this.brellas.some(brella => brella.position.addVec(pos.inverse()).magnitudeSqr() < Math.pow(brella.size * 0.47, 2)) && retries--); this.brellas.push(new brella_1.Brella(pos, (0, util_1.randomBetween)(ctx.canvas.height * 0.4, ctx.canvas.height * 0.6), this.brellaRibs[Math.floor(Math.random() * this.brellaRibs.length)], this.colorHue, this.colorSaturation, this.colorLightness, this.frameAttack, this.frameHold, this.frameRotate)); } } // Draw all the brellas for (const brella of this.brellas) brella.render(ctx); // Check if we are done if (this.brellas.length >= this.brellaMax && this.brellas.every(brella => brella.ended)) this.active = false; } activate() { this.brellas = []; this.active = true; } isActive() { return this.active; } } exports.default = BrellaTransition;