👔Size

an obect that has width and height.

👉 custom objects

// 2023.01.21 - 12:57 (+) .vector
// 2023.01.18 - 14:23 (+) size()
// 2023.01.17 - 21:09 (•) first draft
// -----------------------------------------------

// ⭐️ import
import { vec } from './Vector.js';       // Vector

// ⭐️ Size
// -----------------------------------------------
// - new Size(w, h)
// - new Size({width, height})
// - size(...args)
// -----------------------------------------------
// 🔸 .width
// 🔸 .height
// 🔸 .vector
// -----------------------------------------------
// 🔹 .toString()
//
class Size {

    // constructor
    constructor(...args) {
        
        const argLen = args.length;
        let width, height;
        
        switch (argLen) {
            // treat as (x, y)
            case 2: [width, height] = args; break;
            // treat as {x, y}
            case 1: [width, height] = [args[0].width, args[0].height]; break;
            // throw error otherwise
            default: 
                throw new Error(`expecting 1 or 2 arguments, but got ${argLen}`)
        }

        this.width = width;
        this.height = height;
    }

    // 🔸 .vector
    get vector() {
        return vec(this.width, this.height);
    }

    // 🔹 .toString()
    toString() {
        return `(${this.width}, ${this.height})`;
    }
}

// convenience factory functions
function size(...args) {
    return new Size(...args);
}

// ⭐️ exprt
// -----------------------------------------------
export {Size, size};    // ES module

History

0: (•)(+) size()

Last updated