๐Ÿ‘”Size

an obect that has width and height.

๐Ÿ‘‰ custom

// 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