Skip to content

Commit e3e8c58

Browse files
author
Yan Heng
committed
feat: 更新line-tool为polyline-tool
1 parent 6881ab6 commit e3e8c58

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

main/src/view/canvas/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { onMounted, ref } from 'vue';
3-
import { App, ellipseTool, lineTool, rectTool, selectTool, triangleTool } from '@pictode/core';
3+
import { App, ellipseTool, polylineTool, rectTool, selectTool, triangleTool } from '@pictode/core';
44
55
const containerRef = ref<HTMLDivElement>();
66
@@ -20,7 +20,7 @@ onMounted(() => {
2020
<button @click="app.setTool(rectTool)">矩形⬜️</button>
2121
<button @click="app.setTool(ellipseTool)">圆形⭕️</button>
2222
<button @click="app.setTool(triangleTool)">三角形🔺</button>
23-
<button @click="app.setTool(lineTool)">线条📉</button>
23+
<button @click="app.setTool(polylineTool)">线条📉</button>
2424
</div>
2525
<div ref="containerRef" class="container"></div>
2626
</div>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { fabric } from 'fabric';
22

3-
export class Line extends fabric.Line {
3+
export class Polyline extends fabric.Polyline {
44
public render(ctx: CanvasRenderingContext2D): void {
55
super.render(ctx);
66
}
77
}
88

9-
export default Line;
9+
export default Polyline;

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export * from './tools/rect-tool';
55
export * from './tools/select-tool';
66
export * from './tools/ellipse-tool';
77
export * from './tools/triangle-tool';
8-
export * from './tools/line-tool';
8+
export * from './tools/polyline-tool';

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

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { fabric } from 'fabric';
2+
3+
import { Polyline } from '../customs/polyline';
4+
import { AppMouseEvent, ToolStrategy } from '../types';
5+
6+
import { selectTool } from './select-tool';
7+
8+
class PolylineTool implements ToolStrategy {
9+
public drawable: boolean = true;
10+
private points: fabric.Point[] = [];
11+
private polyline: Polyline | null = null;
12+
13+
public onMouseDown({ app }: AppMouseEvent): void {
14+
app.canvas.selection = false;
15+
this.points.push(app.pointer);
16+
this.polyline = new Polyline(this.points, {
17+
fill: 'transparent',
18+
stroke: 'black',
19+
strokeWidth: 2,
20+
});
21+
app.canvas.add(this.polyline);
22+
}
23+
24+
public onMouseMove({ app }: AppMouseEvent): void {
25+
if (!this.polyline) {
26+
return;
27+
}
28+
app.canvas.discardActiveObject();
29+
app.render();
30+
const pointer = app.pointer;
31+
const points = this.points.concat(pointer);
32+
this.polyline.set({ points });
33+
app.render(); // Call render after updating the polyline
34+
}
35+
36+
public onMouseUp({ app, event }: AppMouseEvent): void {
37+
app.setTool(selectTool);
38+
if (this.polyline) {
39+
// Use setTimeout to delay the selection after the fabric.js selection process is done
40+
setTimeout(() => {
41+
app.canvas.setActiveObject(this.polyline!, event.e);
42+
}, 10);
43+
}
44+
45+
this.points = [];
46+
this.polyline = null;
47+
app.render(true);
48+
}
49+
}
50+
51+
export const polylineTool = new PolylineTool();
52+
53+
export default polylineTool;

0 commit comments

Comments
 (0)