Skip to content

Commit f2f3238

Browse files
author
Yan Heng
committed
feat: 调整工具
1 parent b2fafcc commit f2f3238

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

packages/core/src/tools/rect-tool.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1+
import { fabric } from 'fabric';
2+
13
import { Rect } from '../customs/rect';
24
import { AppMouseEvent, ToolStrategy } from '../types';
35

46
export class RectTool implements ToolStrategy {
5-
private isDrawing: boolean = false;
6-
private rect: Rect | null = null;
7+
private startPointer: fabric.Point = new fabric.Point(0, 0);
8+
private rectangle: Rect | null = null;
79

810
public onMouseDown({ app }: AppMouseEvent): void {
9-
this.isDrawing = true;
10-
const pointer = app.pointer;
11-
this.rect = new Rect({
12-
left: pointer.x,
13-
top: pointer.y,
14-
width: 0,
15-
height: 0,
11+
this.startPointer = app.pointer;
12+
this.rectangle = new Rect({
13+
left: this.startPointer.x,
14+
top: this.startPointer.y,
15+
width: 10,
16+
height: 10,
1617
fill: 'transparent',
1718
stroke: 'black',
1819
strokeWidth: 2,
1920
});
20-
app.canvas.add(this.rect);
21+
app.canvas.add(this.rectangle);
2122
}
2223

2324
public onMouseMove({ app }: AppMouseEvent): void {
24-
if (!this.isDrawing || !this.rect) {
25+
if (!this.rectangle) {
2526
return;
2627
}
27-
const pointer = app.pointer;
28-
this.rect.set({
29-
width: pointer.x - (this.rect.left ?? 0),
30-
height: pointer.y - (this.rect.top ?? 0),
31-
});
28+
app.canvas.discardActiveObject();
29+
app.render();
30+
const width = app.pointer.x - this.startPointer.x;
31+
const height = app.pointer.y - this.startPointer.y;
32+
this.rectangle.set({ width, height });
3233
app.render();
3334
}
3435

3536
public onMouseUp(): void {
36-
this.isDrawing = false;
37-
this.rect = null;
37+
this.rectangle = null;
3838
}
3939
}
4040

packages/core/src/tools/select-tool.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ export class SelectTool implements ToolStrategy {
1111

1212
public onMouseMove(): void {}
1313

14-
public onMouseUp({ app }: AppMouseEvent): void {
15-
app.canvas.selection = false;
16-
}
14+
public onMouseUp(): void {}
1715
}
1816

1917
export default SelectTool;

0 commit comments

Comments
 (0)