๐Size
an obect that has width and height.
๐ custom
replit โฉ various boxes , require -> Vector
// 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
๐พ replit๏ผRect
// code
History
0: (โข)(+) size()
// 2023.01.18 - 14:23 (+) size()
// 2023.01.17 - 21:09 (โข) first draft
// -----------------------------------------------
// โญ๏ธ Size
// -----------------------------------------------
// - new Size(w, h)
// - new Size({width, height})
// - size(...args)
// -----------------------------------------------
// ๐ธ .width
// ๐ธ .height
// -----------------------------------------------
// ๐น .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;
}
// ๐น .toString()
toString() {
return `(${this.width}, ${this.height})`;
}
}
// convenience factory functions
function size(...args) {
return new Size(...args);
}
// โญ๏ธ exprt
// -----------------------------------------------
export {Size, size}; // ES module
Last updated