UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

220 lines (217 loc) 5.37 kB
(() => { var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/vector/index.js var index_exports = {}; __export(index_exports, { DOWN: () => DOWN, LEFT: () => LEFT, ONE: () => ONE, RIGHT: () => RIGHT, UP: () => UP, Vector: () => Vector, ZERO: () => ZERO, vec: () => vec, vecAbs: () => vecAbs, vecAdd: () => vecAdd, vecAngle: () => vecAngle, vecAngleBetween: () => vecAngleBetween, vecCeil: () => vecCeil, vecClamp: () => vecClamp, vecCross: () => vecCross, vecDist: () => vecDist, vecDist2: () => vecDist2, vecDiv: () => vecDiv, vecDot: () => vecDot, vecEq: () => vecEq, vecFloor: () => vecFloor, vecIsZero: () => vecIsZero, vecLerp: () => vecLerp, vecLimit: () => vecLimit, vecMag: () => vecMag, vecMag2: () => vecMag2, vecMove: () => vecMove, vecMult: () => vecMult, vecNorm: () => vecNorm, vecRand: () => vecRand, vecReflect: () => vecReflect, vecRotate: () => vecRotate, vecRound: () => vecRound, vecSet: () => vecSet, vecSetMag: () => vecSetMag, vecSub: () => vecSub }); var sqrt = Math.sqrt; var cos = Math.cos; var sin = Math.sin; 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); }; var vecEq = (v, x, y = x) => { if (isVector(x)) { return vecEq(v, x.x, x.y); } return v.x === x && v.y === y; }; var vecSet = (v, x, y = x) => { if (isVector(x)) { vecSet(v, x.x, x.y); } else { v.x = x; v.y = y; } return v; }; var vecAdd = (v, x, y = x) => { if (isVector(x)) { return vecAdd(v, x.x, x.y); } v.x += x; v.y += y; return v; }; var vecSub = (v, x, y = x) => { if (isVector(x)) { return vecSub(v, x.x, x.y); } v.x -= x; v.y -= y; return v; }; var vecMult = (v, x, y = x) => { if (isVector(x)) { return vecMult(v, x.x, x.y); } v.x *= x; v.y *= y; return v; }; var vecDiv = (v, x, y = x) => { if (isVector(x)) { return vecDiv(v, x.x, x.y); } v.x /= x || 1; v.y /= y || 1; return v; }; var vecRotate = (v, radians) => { const c = cos(radians), s = sin(radians); v.x = c * v.x - s * v.y; v.y = s * v.x + c * v.y; return v; }; var vecReflect = (v, normal) => { const normalCopy = vecNorm(vec(normal)); return vecSub(v, vecMult(normalCopy, 2 * vecDot(v, normalCopy))); }; var vecSetMag = (v, value) => { vecNorm(v); vecMult(v, value); return v; }; var vecMag = (v) => Math.hypot(v.x, v.y); var vecMag2 = (v) => v.x * v.x + v.y * v.y; var vecNorm = (v) => { const length = vecMag(v); if (length > 0) { vecDiv(v, length); } return v; }; var vecLimit = (v, max = 1) => { const sq = vecMag2(v); if (sq > max * max) { vecDiv(v, sqrt(sq)); vecMult(v, max); } return v; }; var vecDist = (a, b) => { return Math.hypot(b.x - a.x, b.y - a.y); }; var vecDist2 = (a, b) => { const dx = a.x - b.x; const dy = a.y - b.y; return dx * dx + dy * dy; }; var vecAngle = (v) => Math.atan2(v.y, v.x); var vecAngleBetween = (v1, v2) => Math.atan2(v2.y - v1.y, v2.x - v1.x); var vecDot = (a, b) => a.x * b.x + a.y * b.y; var vecCross = (a, b) => a.x * b.y - a.y * b.x; var vecLerp = (a, b, t) => { a.x += (b.x - a.x) * t || 0; a.y += (b.y - a.y) * t || 0; return a; }; var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => { const angle = rng() * PI2; const radius = rng() * (maxlength - minlength) + minlength; return vec(cos(angle) * radius, sin(angle) * radius); }; var vecAbs = (v) => { v.x = Math.abs(v.x); v.y = Math.abs(v.y); return v; }; var vecCeil = (v) => { v.x = Math.ceil(v.x); v.y = Math.ceil(v.y); return v; }; var vecFloor = (v) => { v.x = Math.floor(v.x); v.y = Math.floor(v.y); return v; }; var vecRound = (v) => { v.x = Math.round(v.x); v.y = Math.round(v.y); return v; }; var vecClamp = (v, min, max) => { if (v.x < min.x) v.x = min.x; if (v.x > max.x) v.x = max.x; if (v.y < min.y) v.y = min.y; if (v.y > max.y) v.y = max.y; return v; }; var vecMove = (from, to, delta = 1) => vecAdd(from, to.x * delta, to.y * delta); var vecIsZero = (v) => vecEq(v, ZERO); var ZERO = /* @__PURE__ */ vec(0, 0); var ONE = /* @__PURE__ */ vec(1, 1); var UP = /* @__PURE__ */ vec(0, -1); var RIGHT = /* @__PURE__ */ vec(1, 0); var DOWN = /* @__PURE__ */ vec(0, 1); var LEFT = /* @__PURE__ */ vec(-1, 0); // src/vector/_web.js globalThis.utils = Object.assign(globalThis.utils || {}, index_exports); })();