-
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
1 parent
d713760
commit 6881ab6
Showing
4 changed files
with
57 additions
and
1 deletion.
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
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,9 @@ | ||
import { fabric } from 'fabric'; | ||
|
||
export class Line extends fabric.Line { | ||
public render(ctx: CanvasRenderingContext2D): void { | ||
super.render(ctx); | ||
} | ||
} | ||
|
||
export default Line; |
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 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,45 @@ | ||
import { fabric } from 'fabric'; | ||
|
||
import { Line } from '../customs/line'; | ||
import { AppMouseEvent, ToolStrategy } from '../types'; | ||
|
||
import { selectTool } from './select-tool'; | ||
|
||
class LineTool implements ToolStrategy { | ||
public drawable: boolean = true; | ||
private startPointer: fabric.Point = new fabric.Point(0, 0); | ||
private line: Line | null = null; | ||
|
||
public onMouseDown({ app }: AppMouseEvent): void { | ||
app.canvas.selection = false; | ||
this.startPointer = app.pointer; | ||
this.line = new Line([this.startPointer.x, this.startPointer.y, this.startPointer.x, this.startPointer.y], { | ||
fill: 'transparent', | ||
stroke: 'black', | ||
strokeWidth: 2, | ||
}); | ||
app.canvas.add(this.line); | ||
} | ||
|
||
public onMouseMove({ app }: AppMouseEvent): void { | ||
if (!this.line) { | ||
return; | ||
} | ||
const endX = app.pointer.x; | ||
const endY = app.pointer.y; | ||
this.line.set({ x2: endX, y2: endY }); | ||
app.render(); // Call render after updating the line | ||
} | ||
|
||
public onMouseUp({ app }: AppMouseEvent): void { | ||
app.setTool(selectTool); | ||
this.startPointer.setXY(0, 0); | ||
this.line && app.canvas.setActiveObject(this.line); | ||
this.line = null; | ||
app.render(true); | ||
} | ||
} | ||
|
||
export const lineTool = new LineTool(); | ||
|
||
export default lineTool; |