Skip to content

Commit

Permalink
fix: pathText和freeDraw元素绘制时设置唯一id (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchenguang1998 authored Jun 4, 2024
1 parent 21df79d commit 75f2f5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/core/plugin/FreeDrawPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fabric } from 'fabric';
import Editor from '../Editor';
import { v4 as uuid } from 'uuid';

type IEditor = Editor;

Expand All @@ -12,14 +13,28 @@ export default class FreeDrawPlugin {
static apis = ['startDraw', 'endDraw'];
constructor(public canvas: fabric.Canvas, public editor: IEditor) {}

_bindEvent() {
this.canvas.on('path:created', this._createdHandler);
}

_unbindEvent() {
this.canvas.off('path:created', this._createdHandler);
}

_createdHandler = (opt: any) => {
opt.path.set('id', uuid());
};

startDraw(options: DrawOptions) {
this.canvas.isDrawingMode = true;
this.canvas.freeDrawingBrush = new fabric.PencilBrush(this.canvas);
this.canvas.freeDrawingBrush.width = options.width;
this._bindEvent();
}
endDraw() {
if (this.canvas.isDrawingMode) {
this.canvas.isDrawingMode = false;
this._unbindEvent();
return;
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/plugin/PathTextPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fabric } from 'fabric';
import Editor from '../Editor';
import { v4 as uuid } from 'uuid';

type IEditor = Editor;
type DrawOptions = {
Expand Down Expand Up @@ -31,6 +32,7 @@ export default class PathTextPlugin {
left: path.left,
fill: this.options.color,
path: path,
id: uuid(),
});
this.canvas.add(textObject);
};
Expand Down

0 comments on commit 75f2f5c

Please sign in to comment.