Skip to content

Commit

Permalink
feat: 新增圆形绘制
Browse files Browse the repository at this point in the history
  • Loading branch information
JessYan0913 committed Jul 24, 2023
1 parent 765a7e1 commit 763c456
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
3 changes: 2 additions & 1 deletion main/src/view/canvas/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { App, rectTool, selectTool } from '@pictode/core';
import { App, circleTool, rectTool, selectTool } from '@pictode/core';
const containerRef = ref<HTMLDivElement>();
Expand All @@ -18,6 +18,7 @@ onMounted(() => {
<button @click="app.setTool(selectTool)">选择🖱️</button>
<!-- <button @click="app.setModel('drawing')">铅笔✏️</button> -->
<button @click="app.setTool(rectTool)">矩形⬜️</button>
<button @click="app.setTool(circleTool)">圆形⭕️</button>
</div>
<div ref="containerRef" class="container"></div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/customs/circle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fabric } from 'fabric';

export class Circle extends fabric.Ellipse {
public render(ctx: CanvasRenderingContext2D): void {
super.render(ctx);
}
}

export default Circle;
4 changes: 2 additions & 2 deletions packages/core/src/customs/rect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fabric } from 'fabric';

export class Rect extends fabric.Rect {
public _render(ctx: CanvasRenderingContext2D): void {
super._render(ctx);
public render(ctx: CanvasRenderingContext2D): void {
super.render(ctx);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './types';
export * from './decorators/expand-app';
export * from './tools/rect-tool';
export * from './tools/select-tool';
export * from './tools/circle-tool';
61 changes: 61 additions & 0 deletions packages/core/src/tools/circle-tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { fabric } from 'fabric';

import { Circle } from '../customs/circle';
import { AppMouseEvent, ToolStrategy } from '../types';

import { selectTool } from './select-tool';

class CircleTool implements ToolStrategy {
private startPointer: fabric.Point = new fabric.Point(0, 0);
private circle: Circle | null = null;

public onMouseDown({ app }: AppMouseEvent): void {
app.canvas.selection = false;
this.startPointer = app.pointer;
this.circle = new Circle({
left: this.startPointer.x,
top: this.startPointer.y,
rx: 0,
ry: 0,
fill: 'transparent',
stroke: 'black',
strokeWidth: 2,
});
app.canvas.add(this.circle);
}

public onMouseMove({ app }: AppMouseEvent): void {
if (!this.circle) {
return;
}
app.canvas.discardActiveObject();
app.render();

// 计算起点和当前鼠标位置之间的距离
const dx = app.pointer.x - this.startPointer.x;
const dy = app.pointer.y - this.startPointer.y;

// 计算椭圆的宽度和高度的绝对值
const radiusX = Math.abs(dx) / 2;
const radiusY = Math.abs(dy) / 2;

// 根据起点和鼠标位置计算椭圆的中心位置
const centerX = this.startPointer.x + dx / 2;
const centerY = this.startPointer.y + dy / 2;

this.circle.set({ rx: radiusX, ry: radiusY, left: centerX - radiusX, top: centerY - radiusY });
app.render();
}

public onMouseUp({ app }: AppMouseEvent): void {
app.setTool(selectTool);
this.startPointer.setXY(0, 0);
this.circle && app.canvas.setActiveObject(this.circle);
this.circle = null;
app.render(true);
}
}

export const circleTool = new CircleTool();

export default circleTool;
2 changes: 1 addition & 1 deletion packages/core/src/tools/rect-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class RectTool implements ToolStrategy {
app.setTool(selectTool);
this.startPointer.setXY(0, 0);
this.rectangle && app.canvas.setActiveObject(this.rectangle);
app.render();
this.rectangle = null;
app.render(true);
}
}

Expand Down

0 comments on commit 763c456

Please sign in to comment.