From 5b98cd7e9ba70e0fae5139261ba2a85decc2b137 Mon Sep 17 00:00:00 2001 From: Kamil Socha Date: Wed, 15 Jan 2020 23:20:44 +0100 Subject: [PATCH] [lazy-brush] Add definitions (#41526) * Add lazy-brush definitions * Add instance properties --- types/lazy-brush/index.d.ts | 145 +++++++++++++++++++++++++++ types/lazy-brush/lazy-brush-tests.ts | 47 +++++++++ types/lazy-brush/tsconfig.json | 23 +++++ types/lazy-brush/tslint.json | 1 + 4 files changed, 216 insertions(+) create mode 100644 types/lazy-brush/index.d.ts create mode 100644 types/lazy-brush/lazy-brush-tests.ts create mode 100644 types/lazy-brush/tsconfig.json create mode 100644 types/lazy-brush/tslint.json diff --git a/types/lazy-brush/index.d.ts b/types/lazy-brush/index.d.ts new file mode 100644 index 0000000000..ab7c80f0be --- /dev/null +++ b/types/lazy-brush/index.d.ts @@ -0,0 +1,145 @@ +// Type definitions for lazy-brush 1.0 +// Project: https://github.com/dulnan/lazy-brush#readme +// Definitions by: Kamil Socha +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export interface Coordinates { + x: number; + + y: number; +} + +export class Point implements Coordinates { + x: number; + + y: number; + + constructor(x: number, y: number); +} + +export class LazyPoint extends Point { + /** + * Update the x and y values + */ + update(coordinates: Coordinates): void; + + /** + * Move the point to another position using an angle and distance + */ + moveByAngle(angle: number, distance: number): void; + + /** + * Get the difference for x and y axis to another point + */ + getDifferenceTo(point: Coordinates): Point; + + /** + * Calculate distance to another point + */ + getDistanceTo(point: Coordinates): number; + + /** + * Calculate the angle to another point + */ + getAngleTo(point: Coordinates): number; + + /** + * Check if this point is the same as another point + */ + equalsTo(point: Coordinates): boolean; + + /** + * Return a simple object with x and y properties + */ + toObject(): Coordinates; +} + +export interface Options { + radius?: number; + enabled?: boolean; + initialPoint?: Coordinates; +} + +export interface UpdateOptions { + both: boolean; +} + +export class LazyBrush { + radius: number; + + _isEnabled: boolean; + + pointer: LazyPoint; + + brush: LazyPoint; + + angle: number; + + distance: number; + + _hasMoved: boolean; + + constructor(options?: Options); + + /** + * Enable lazy brush calculations + */ + enable(): void; + + /** + * Disable lazy brush calculations + */ + disable(): void; + + isEnabled(): boolean; + + /** + * Update the radius + */ + setRadius(radius: number): void; + + /** + * Return the current radius + */ + getRadius(): number; + + /** + * Updates the pointer coordinates and calculates the new brush point + */ + update(coordinates: Coordinates, options?: UpdateOptions): boolean; + + /** + * Return the brush as a LazyPoint + */ + getBrush(): LazyPoint; + + /** + * Return the brush coordinates as a simple object + */ + getBrushCoordinates(): Coordinates; + + /** + * Return the pointer as a LazyPoint + */ + getPointer(): LazyPoint; + + /** + * Return the pointer coordinates as a simple object + */ + getPointerCoordinates(): Coordinates; + + /** + * Return the angle between pointer and brush + */ + getAngle(): number; + + /** + * Return the distance between pointer and brush + */ + getDistance(): number; + + /** + * Return if the previous update has moved the brush + */ + brushHasMoved(): boolean; +} diff --git a/types/lazy-brush/lazy-brush-tests.ts b/types/lazy-brush/lazy-brush-tests.ts new file mode 100644 index 0000000000..a3281fd00c --- /dev/null +++ b/types/lazy-brush/lazy-brush-tests.ts @@ -0,0 +1,47 @@ +import { LazyBrush, Point, LazyPoint, Coordinates } from 'lazy-brush'; + +const point = new Point(1, 2); +const pointX: number = point.x; +const pointY: number = point.y; + +const lazyPoint = new LazyPoint(1, 2); +const lazyPointX: number = lazyPoint.x; +const lazyPointY: number = lazyPoint.y; +const equalsToResult: boolean = lazyPoint.equalsTo({ x: 1, y: 4 }); +const getAngleToResult: number = lazyPoint.getAngleTo(point); +const getDifferenceToResult: Point = lazyPoint.getDifferenceTo(point); +const getDistanceToResult: number = lazyPoint.getDistanceTo(point); +lazyPoint.moveByAngle(45, 20); +const toObjectResult: Coordinates = lazyPoint.toObject(); +lazyPoint.update({ x: 1, y: 7 }); + +new LazyBrush(); +new LazyBrush({ radius: 20 }); +const lazy = new LazyBrush({ + radius: 30, + enabled: true, + initialPoint: { x: 0, y: 0 }, +}); + +const radiusValue: number = lazy.radius; +const _isEnabledValue: boolean = lazy._isEnabled; +const pointerValue: LazyPoint = lazy.pointer; +const brushValue: LazyPoint = lazy.brush; +const angleValue: number = lazy.angle; +const distanceValue: number = lazy.distance; +const _hasMovedValue: boolean = lazy._hasMoved; + +const updateResult: boolean = lazy.update({ x: 50, y: 0 }); +const updateResult2: boolean = lazy.update({ x: 50, y: 0 }, { both: true }); +const brushHasMovedResult: boolean = lazy.brushHasMoved(); +lazy.disable(); +lazy.enable(); +const isEnabledResult: boolean = lazy.isEnabled(); +const getAngleResult: number = lazy.getAngle(); +const getBrushResult: LazyPoint = lazy.getBrush(); +const getBrushCoordinatesResult: Coordinates = lazy.getBrushCoordinates(); +const getDistanceResult: number = lazy.getDistance(); +const getPointerResult: LazyPoint = lazy.getPointer(); +const getPointerCoordinatesResult: Coordinates = lazy.getPointerCoordinates(); +const getRadiusResult: number = lazy.getRadius(); +lazy.setRadius(12); diff --git a/types/lazy-brush/tsconfig.json b/types/lazy-brush/tsconfig.json new file mode 100644 index 0000000000..62038ad635 --- /dev/null +++ b/types/lazy-brush/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "lazy-brush-tests.ts" + ] +} diff --git a/types/lazy-brush/tslint.json b/types/lazy-brush/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/lazy-brush/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }