Skip to content

Commit 3a96917

Browse files
author
Yan Heng
committed
feat: 实现折线绘制
1 parent dfaf3c2 commit 3a96917

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class EllipseTool implements Tool {
4848
// 根据起点和鼠标位置计算椭圆的中心位置
4949
const centerX = this.startPointer.x + dx / 2;
5050
const centerY = this.startPointer.y + dy / 2;
51-
this.ellipse.x(centerX);
52-
this.ellipse.y(centerY);
51+
this.ellipse.setPosition(new Point(centerX, centerY));
5352
this.ellipse.radiusX(radiusX);
5453
this.ellipse.radiusY(radiusY);
5554
app.render();

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Line } from '../customs/line';
33
import { AppMouseEvent, Tool } from '../types';
44
import { Point } from '../utils';
55

6+
import { selectTool } from './select-tool';
7+
68
class LineTool implements Tool {
79
public name: string = 'polylineTool';
810
private points: Point[] = [];
@@ -18,12 +20,42 @@ class LineTool implements Tool {
1820
}
1921

2022
public onMouseDown({ app }: AppMouseEvent): void {
21-
console.log('===>', app);
23+
const lastPoint = this.points.at(-1);
24+
if (!lastPoint || !lastPoint.eq(app.pointer)) {
25+
this.points.push(app.pointer);
26+
}
27+
if (this.line) {
28+
this.line.points(this.flatPoints());
29+
} else {
30+
this.line = new Line({
31+
points: this.flatPoints(),
32+
fill: 'transparent',
33+
stroke: 'black',
34+
strokeWidth: 2,
35+
});
36+
app.add(this.line);
37+
}
2238
}
2339

24-
public onMouseMove(): void {}
40+
public onMouseMove({ app }: AppMouseEvent): void {
41+
if (!this.line) {
42+
return;
43+
}
44+
this.line.points(this.flatPoints().concat(app.pointer.x, app.pointer.y));
45+
app.render();
46+
}
2547

26-
public onMouseDoubleClick() {}
48+
public onMouseDoubleClick({ app }: AppMouseEvent): void {
49+
if (!this.line) {
50+
return;
51+
}
52+
app.select(this.line);
53+
app.setTool(selectTool);
54+
}
55+
56+
private flatPoints(): number[] {
57+
return this.points.reduce<number[]>((points, point) => [...points, ...point.toArray()], []);
58+
}
2759
}
2860

2961
export const lineTool = new LineTool();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ class RectTool implements Tool {
3535
if (!this.rectangle) {
3636
return;
3737
}
38-
this.rectangle.x(Math.min(this.startPointer.x, app.pointer.x));
39-
this.rectangle.y(Math.min(this.startPointer.y, app.pointer.y));
38+
this.rectangle.setPosition(
39+
new Point(Math.min(this.startPointer.x, app.pointer.x), Math.min(this.startPointer.y, app.pointer.y))
40+
);
4041
this.rectangle.width(Math.abs(app.pointer.x - this.startPointer.x));
4142
this.rectangle.height(Math.abs(app.pointer.y - this.startPointer.y));
4243
app.render();

packages/core/src/utils/math.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ export class Point implements Konva.Vector2d {
2121
this.x = x;
2222
this.y = y;
2323
}
24+
25+
public eq(point: Point): boolean {
26+
return this.x === point.x && this.y === point.y;
27+
}
28+
29+
public toArray(): number[] {
30+
return [this.x, this.y];
31+
}
2432
}

0 commit comments

Comments
 (0)