diff --git a/packages/core/src/app.ts b/packages/core/src/app.ts index 669a1250..e1a45398 100644 --- a/packages/core/src/app.ts +++ b/packages/core/src/app.ts @@ -3,6 +3,7 @@ import Konva from 'konva'; import { MouseService } from './services/mouse'; import { ChildType, EventArgs, Plugin, Tool } from './types'; +import { Point } from './utils'; export class App extends BaseService { public stage: Konva.Stage; @@ -41,8 +42,9 @@ export class App extends BaseService { this.mouse = new MouseService(this); } - public get pointer(): Konva.Vector2d { - return this.stage.getPointerPosition() ?? { x: 0, y: 0 }; + public get pointer(): Point { + const { x, y } = this.stage.getPointerPosition() ?? { x: 0, y: 0 }; + return new Point(x, y); } public mount(element: HTMLElement) { diff --git a/packages/core/src/tools/ellipse-tool.ts b/packages/core/src/tools/ellipse-tool.ts index a61285ea..93881de6 100644 --- a/packages/core/src/tools/ellipse-tool.ts +++ b/packages/core/src/tools/ellipse-tool.ts @@ -1,14 +1,13 @@ -import Konva from 'konva'; - import App from '../app'; import { Ellipse } from '../customs/ellipse'; import { AppMouseEvent, Tool } from '../types'; +import { Point } from '../utils'; import { selectTool } from './select-tool'; class EllipseTool implements Tool { public name: string = 'ellipseTool'; - private startPointer: Konva.Vector2d = { x: 0, y: 0 }; + private startPointer: Point = new Point(0, 0); private ellipse: Ellipse | null = null; public onActive(app: App): void { @@ -17,7 +16,7 @@ class EllipseTool implements Tool { public onInactive(): void { this.ellipse = null; - this.startPointer = { x: 0, y: 0 }; + this.startPointer.setXY(0, 0); } public onMouseDown({ app }: AppMouseEvent): void { diff --git a/packages/core/src/tools/line-tool.ts b/packages/core/src/tools/line-tool.ts index 1d4c53b7..ba150f88 100644 --- a/packages/core/src/tools/line-tool.ts +++ b/packages/core/src/tools/line-tool.ts @@ -1,12 +1,11 @@ -import Konva from 'konva'; - import App from '../app'; import { Line } from '../customs/line'; import { AppMouseEvent, Tool } from '../types'; +import { Point } from '../utils'; class LineTool implements Tool { public name: string = 'polylineTool'; - private points: Konva.Vector2d[] = []; + private points: Point[] = []; private line: Line | null = null; public onActive(app: App): void { diff --git a/packages/core/src/tools/rect-tool.ts b/packages/core/src/tools/rect-tool.ts index f8a31385..e256842f 100644 --- a/packages/core/src/tools/rect-tool.ts +++ b/packages/core/src/tools/rect-tool.ts @@ -1,24 +1,22 @@ -import Konva from 'konva'; - import App from '../app'; import { Rect } from '../customs/rect'; import { AppMouseEvent, Tool } from '../types'; +import { Point } from '../utils'; import { selectTool } from './select-tool'; class RectTool implements Tool { public name: string = 'rectTool'; - private startPointer: Konva.Vector2d = { x: 0, y: 0 }; + private startPointer: Point = new Point(0, 0); private rectangle: Rect | null = null; public onActive(app: App): void { app.select(); - this.startPointer = { x: 0, y: 0 }; } public onInactive(): void { this.rectangle = null; - this.startPointer = { x: 0, y: 0 }; + this.startPointer.setXY(0, 0); } public onMouseDown({ app }: AppMouseEvent): void { diff --git a/packages/core/src/tools/triangle-tool.ts b/packages/core/src/tools/triangle-tool.ts index d488d105..4f648947 100644 --- a/packages/core/src/tools/triangle-tool.ts +++ b/packages/core/src/tools/triangle-tool.ts @@ -1,11 +1,10 @@ -import Konva from 'konva'; - import { Triangle } from '../customs/triangle'; import { Tool } from '../types'; +import { Point } from '../utils'; class TriangleTool implements Tool { public name: string = 'triangleTool'; - private startPointer: Konva.Vector2d = { x: 0, y: 0 }; + private startPointer: Point = new Point(0, 0); private triangle: Triangle | null = null; public onActive(): void {} diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 8dc74ae7..871a9637 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -1,5 +1,7 @@ import { AppConfig } from '../types'; +export * from './math'; + export const DEFAULT_APP_CONFIG: AppConfig = { backgroundColor: '#ffffff', }; diff --git a/packages/core/src/utils/math.ts b/packages/core/src/utils/math.ts new file mode 100644 index 00000000..5beb161d --- /dev/null +++ b/packages/core/src/utils/math.ts @@ -0,0 +1,24 @@ +import Konva from 'konva'; + +export class Point implements Konva.Vector2d { + public x: number; + public y: number; + + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + + public setX(x: number) { + this.x = x; + } + + public setY(y: number) { + this.y = y; + } + + public setXY(x: number, y: number) { + this.x = x; + this.y = y; + } +}