UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

220 lines (214 loc) 5.32 kB
(() => { var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/_global.js globalThis.utils = globalThis.utils || {}; globalThis.utils.global = (overrides = true) => { for (const key in globalThis.utils) { if ("global" === key) continue; if (overrides || globalThis[key] === void 0) { globalThis[key] = globalThis.utils[key]; } } }; // src/actor/index.js var index_exports = {}; __export(index_exports, { ANCHOR_BOT_LEFT: () => ANCHOR_BOT_LEFT, ANCHOR_BOT_RIGHT: () => ANCHOR_BOT_RIGHT, ANCHOR_CENTER: () => ANCHOR_CENTER, ANCHOR_TOP_LEFT: () => ANCHOR_TOP_LEFT, ANCHOR_TOP_RIGHT: () => ANCHOR_TOP_RIGHT, Actor: () => Actor }); // src/vector/index.js var PI2 = 2 * Math.PI; var Vector = class { /** @type {number} */ x; /** @type {number} */ y; /** * @param {number} [x=0] * @param {number} [y] */ constructor(x = 0, y = x) { this.x = x; this.y = y; } /** * @returns {string} */ toString() { return `Vector (${this.x}, ${this.y})`; } }; var isVector = (v) => v instanceof Vector; var vec = (x = 0, y = x) => { if (isVector(x)) { y = x.y; x = x.x; } return new Vector(x, y); }; // src/actor/index.js var ANCHOR_CENTER = /* @__PURE__ */ vec(0.5, 0.5); var ANCHOR_TOP_LEFT = /* @__PURE__ */ vec(0, 0); var ANCHOR_TOP_RIGHT = /* @__PURE__ */ vec(1, 0); var ANCHOR_BOT_LEFT = /* @__PURE__ */ vec(0, 1); var ANCHOR_BOT_RIGHT = /* @__PURE__ */ vec(1, 1); var Actor = class { /** @type {Image|HTMLCanvasElement|OffscreenCanvas} */ sprite; /** @type {Vector} The actor position */ pos; /** @type {Vector} The actor anchor (origin) */ _o; /** @type {Vector} The actor scale */ _s; /** @type {boolean} */ flipX = false; /** @type {boolean} */ flipY = false; /** @type {number} The actor angle (in degrees) */ angle = 0; /** @type {number} The actor opacity */ opacity = 1; /** @type {boolean} If `true` the actor will not be drawn. */ hidden = false; /** * @param {Image|HTMLCanvasElement|OffscreenCanvas} sprite * @param {Vector} position * @param {Vector} anchor */ constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) { this.sprite = sprite; this.pos = position || vec(0); this._o = vec(anchor); this._s = vec(1, 1); } /** * @param {number} */ set x(value) { this.pos.x = value; } /** * @returns {number} */ get x() { return this.pos.x; } /** * @param {number} */ set y(value) { this.pos.y = value; } /** * @returns {number} */ get y() { return this.pos.y; } /** * @param {Vector} */ set anchor(vec2) { this._o.x = vec2.x; this._o.y = vec2.y; } /** * @returns {Vector} */ get anchor() { return this._o; } /** * @returns {number} */ get width() { return this.sprite.width * this._s.x; } /** * @returns {number} */ get height() { return this.sprite.height * this._s.y; } /** * @retuns {Vector} */ get scale() { return this._s; } /** * Sets the actor scale * * @param {number} x * @param {number} [y] */ scaleTo(x, y = x) { this._s.x = x; this._s.y = y; } /** * Multiplies the actor scale * * @param {number} x * @param {number} [y] */ scaleBy(x, y = x) { this._s.x *= x; this._s.y *= y; } /** * @returns {number[]} */ getBounds(scaled = true) { const w = this.sprite.width * (scaled ? this._s.x : 1); const h = this.sprite.height * (scaled ? this._s.y : 1); const x = this.pos.x - w * this.anchor.x; const y = this.pos.y - h * this.anchor.y; return [x, y, w, h]; } /** * Draw the actor * * @param {LitecanvasInstance} [litecanvas] */ draw(litecanvas = globalThis, saveContext = true) { if (this.hidden || this.opacity <= 0) return; if (saveContext) litecanvas.push(); this.transform(litecanvas); this.drawImage(litecanvas); if (saveContext) litecanvas.pop(); } /** * @param {LitecanvasInstance} litecanvas */ transform(litecanvas) { litecanvas.translate(this.pos.x, this.pos.y); litecanvas.rotate(litecanvas.deg2rad(this.angle)); litecanvas.scale( (this.flipX ? -1 : 1) * this._s.x, (this.flipY ? -1 : 1) * this._s.y ); } /** * @param {LitecanvasInstance} litecanvas */ drawImage(litecanvas, alpha = true) { const anchor = this.anchor; const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x); const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y); if (alpha) litecanvas.alpha(this.opacity); litecanvas.image(x, y, this.sprite); } }; // src/actor/_web.js globalThis.utils = Object.assign(globalThis.utils || {}, index_exports); })();