Skip to content

Instantly share code, notes, and snippets.

@alexsanqp
Last active April 24, 2019 17:28
Show Gist options
  • Save alexsanqp/ef9918a4781a33a96a21826223af0b5a to your computer and use it in GitHub Desktop.
Save alexsanqp/ef9918a4781a33a96a21826223af0b5a to your computer and use it in GitHub Desktop.
The Dimension class encapsulates the width and height of a component (in integer precision) in a single object.
/**
* The Dimension class encapsulates the width and height
* of a component (in integer precision) in a single object.
*/
export class Dimension {
/**
* The width dimension; negative values can be used.
* @default 0
*/
protected width: number;
/**
* The height dimension; negative values can be used.
* @default 0
*/
protected height: number;
public constructor();
public constructor(dimension: Dimension);
public constructor(dimension: [number, number]);
public constructor(width: number, height: number);
public constructor(width?: any, height?: any) {
this.setSize(width, height);
}
/**
* Returns the width of this Dimension
*
* @return {number}
*/
public getWidth(): number {
return this.width;
}
/**
* Returns the height of this Dimension
*
* @return {number}
*/
public getHeight(): number {
return this.height;
}
/**
* Sets the size of this Dimension object or (int, int) or [int, int] or (size Dimension) to the specified size
*
* @param {undefined | Dimension | [number, number] | number} size
*/
public setSize(): void;
public setSize(size: Dimension): void;
public setSize(size: [number, number]): void;
public setSize(width: number, height: number): void;
public setSize(width?: any, height?: any): void {
if (typeof width === 'undefined') {
this.width = 0;
this.height = 0;
} else if (width && width instanceof Dimension) {
this.width = width.getWidth();
this.height = width.getHeight();
} else if(Array.isArray(width)) {
this.width = width[0];
this.height = width[1];
} else {
this.width = width;
this.height = height;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment