Skip to content

Commit d713760

Browse files
committed
feat: 新增triangleTool
1 parent c7c0d0d commit d713760

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

main/src/view/canvas/index.vue

Lines changed: 2 additions & 1 deletion
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, rectTool, selectTool } from '@pictode/core';
3+
import { App, ellipseTool, rectTool, selectTool, triangleTool } from '@pictode/core';
44
55
const containerRef = ref<HTMLDivElement>();
66
@@ -19,6 +19,7 @@ onMounted(() => {
1919
<!-- <button @click="app.setModel('drawing')">铅笔✏️</button> -->
2020
<button @click="app.setTool(rectTool)">矩形⬜️</button>
2121
<button @click="app.setTool(ellipseTool)">圆形⭕️</button>
22+
<button @click="app.setTool(triangleTool)">三角形🔺</button>
2223
</div>
2324
<div ref="containerRef" class="container"></div>
2425
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { fabric } from 'fabric';
2+
3+
export class Triangle extends fabric.Triangle {
4+
public render(ctx: CanvasRenderingContext2D): void {
5+
super.render(ctx);
6+
}
7+
}
8+
9+
export default Triangle;

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './decorators/expand-app';
44
export * from './tools/rect-tool';
55
export * from './tools/select-tool';
66
export * from './tools/ellipse-tool';
7+
export * from './tools/triangle-tool';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { fabric } from 'fabric';
2+
3+
import { Triangle } from '../customs/triangle';
4+
import { AppMouseEvent, ToolStrategy } from '../types';
5+
6+
import { selectTool } from './select-tool';
7+
8+
class TriangleTool implements ToolStrategy {
9+
public drawable: boolean = true;
10+
private startPointer: fabric.Point = new fabric.Point(0, 0);
11+
private triangle: Triangle | null = null;
12+
13+
public onMouseDown({ app }: AppMouseEvent): void {
14+
app.canvas.selection = false;
15+
this.startPointer = app.pointer;
16+
this.triangle = new Triangle({
17+
top: this.startPointer.y,
18+
left: this.startPointer.x,
19+
width: 0,
20+
height: 0,
21+
fill: 'transparent',
22+
stroke: 'black',
23+
strokeWidth: 2,
24+
});
25+
app.canvas.add(this.triangle);
26+
}
27+
28+
public onMouseMove({ app }: AppMouseEvent): void {
29+
if (!this.triangle) {
30+
return;
31+
}
32+
const width = Math.abs(app.pointer.x - this.startPointer.x);
33+
const height = Math.abs(app.pointer.y - this.startPointer.y);
34+
this.triangle.set({ width, height });
35+
app.render(); // Call render after updating the triangle
36+
}
37+
38+
public onMouseUp({ app }: AppMouseEvent): void {
39+
app.setTool(selectTool);
40+
this.startPointer.setXY(0, 0);
41+
this.triangle && app.canvas.setActiveObject(this.triangle);
42+
this.triangle = null;
43+
app.render(true);
44+
}
45+
}
46+
47+
export const triangleTool = new TriangleTool();
48+
49+
export default triangleTool;

0 commit comments

Comments
 (0)