Skip to content

Commit

Permalink
fix: 修复矩形绘制重复添加逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 31, 2023
1 parent 7d2ea34 commit 7509af2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ export class App extends BaseService<EventArgs> {

public select(...children: ChildType[]): void {
this.selected.forEach((child) => child.draggable(false));
this.selected = [];
this.selector.nodes([]);
this.selected = children.map((child) => {
children.forEach((child) => {
child.draggable(true);
return child;
this.selected.push(child);
});
this.selector.nodes(children);
this.selector.moveToTop();
Expand Down
23 changes: 12 additions & 11 deletions packages/core/src/tools/rect-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ import { Point } from '../utils';
import { selectTool } from './select-tool';

export const rectTool = (): Tool => {
let startPointer: Point = new Point(0, 0);
let rectangle: Rect | null = null;
const startPointer: Point = new Point(0, 0);
const rectangle: Rect = new Rect({
fill: 'transparent',
stroke: 'black',
strokeWidth: 2,
});

return {
name: 'rectTool',
onActive(app) {
app.select();
},
onInactive() {
rectangle = null;
startPointer.setXY(0, 0);
},
onMouseDown({ app }: AppMouseEvent): void {
startPointer = app.pointer;
rectangle = new Rect({
x: startPointer.x,
y: startPointer.y,
fill: 'transparent',
stroke: 'black',
strokeWidth: 2,
});
startPointer.clone(app.pointer);
rectangle.setPosition(
new Point(Math.min(startPointer.x, app.pointer.x), Math.min(startPointer.y, app.pointer.y))
);
rectangle.width(0);
rectangle.height(0);
app.add(rectangle);
},
onMouseMove({ app }: AppMouseEvent): void {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/tools/select-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export const selectTool = (...shapes: ChildType[]): Tool => {
listening: false,
});
let isRubberSelector: boolean = false;

return {
name: 'selectTool',
onActive(app) {
app.select(...shapes);
app.add(rubberRect);
},
onInactive() {
rubberRect.destroy();
startPointer.setXY(0, 0);
},
onMouseDown({ app }) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ export class Point implements Konva.Vector2d {
public toArray(): number[] {
return [this.x, this.y];
}

public clone(point: Point): void {
this.setXY(point.x, point.y);
}
}

0 comments on commit 7509af2

Please sign in to comment.