-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yan Heng
committed
Jul 25, 2023
1 parent
6881ab6
commit e3e8c58
Showing
5 changed files
with
58 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/core/src/customs/line.ts → packages/core/src/customs/polyline.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { fabric } from 'fabric'; | ||
|
||
export class Line extends fabric.Line { | ||
export class Polyline extends fabric.Polyline { | ||
public render(ctx: CanvasRenderingContext2D): void { | ||
super.render(ctx); | ||
} | ||
} | ||
|
||
export default Line; | ||
export default Polyline; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { fabric } from 'fabric'; | ||
|
||
import { Polyline } from '../customs/polyline'; | ||
import { AppMouseEvent, ToolStrategy } from '../types'; | ||
|
||
import { selectTool } from './select-tool'; | ||
|
||
class PolylineTool implements ToolStrategy { | ||
public drawable: boolean = true; | ||
private points: fabric.Point[] = []; | ||
private polyline: Polyline | null = null; | ||
|
||
public onMouseDown({ app }: AppMouseEvent): void { | ||
app.canvas.selection = false; | ||
this.points.push(app.pointer); | ||
this.polyline = new Polyline(this.points, { | ||
fill: 'transparent', | ||
stroke: 'black', | ||
strokeWidth: 2, | ||
}); | ||
app.canvas.add(this.polyline); | ||
} | ||
|
||
public onMouseMove({ app }: AppMouseEvent): void { | ||
if (!this.polyline) { | ||
return; | ||
} | ||
app.canvas.discardActiveObject(); | ||
app.render(); | ||
const pointer = app.pointer; | ||
const points = this.points.concat(pointer); | ||
this.polyline.set({ points }); | ||
app.render(); // Call render after updating the polyline | ||
} | ||
|
||
public onMouseUp({ app, event }: AppMouseEvent): void { | ||
app.setTool(selectTool); | ||
if (this.polyline) { | ||
// Use setTimeout to delay the selection after the fabric.js selection process is done | ||
setTimeout(() => { | ||
app.canvas.setActiveObject(this.polyline!, event.e); | ||
}, 10); | ||
} | ||
|
||
this.points = []; | ||
this.polyline = null; | ||
app.render(true); | ||
} | ||
} | ||
|
||
export const polylineTool = new PolylineTool(); | ||
|
||
export default polylineTool; |