Skip to content

Commit

Permalink
feat: 使用Point替换Vector2d
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 29, 2023
1 parent 7833336 commit dfaf3c2
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventArgs> {
public stage: Konva.Stage;
Expand Down Expand Up @@ -41,8 +42,9 @@ export class App extends BaseService<EventArgs> {
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) {
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/tools/ellipse-tool.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/tools/line-tool.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/tools/rect-tool.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/tools/triangle-tool.ts
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AppConfig } from '../types';

export * from './math';

export const DEFAULT_APP_CONFIG: AppConfig = {
backgroundColor: '#ffffff',
};
24 changes: 24 additions & 0 deletions packages/core/src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit dfaf3c2

Please sign in to comment.