Skip to content

Commit

Permalink
Add eraser tool with Lyra
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Apr 9, 2022
1 parent dcae47c commit a85fb26
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.cache
dist
public
public
.DS_Store
4 changes: 4 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Artboard,
ArtboardRef,
useShadingBrush,
useEraser,
useWatercolor,
ToolHandlers,
} from "../src/";
Expand All @@ -14,6 +15,7 @@ import {
FaPencilAlt,
FaPaintBrush,
FaMarker,
FaEraser,
FaSprayCan,
FaDownload,
FaTrash,
Expand Down Expand Up @@ -41,6 +43,7 @@ export function App(): JSX.Element {
const marker = useMarker({ color, strokeWidth });
const watercolor = useWatercolor({ color, strokeWidth });
const airbrush = useAirbrush({ color, strokeWidth });
const eraser = useEraser({ strokeWidth });
const shading = useShadingBrush({
color,
spreadFactor: (1 / 45) * strokeWidth,
Expand All @@ -52,6 +55,7 @@ export function App(): JSX.Element {
[brush, FaPaintBrush],
[marker, FaMarker],
[airbrush, FaSprayCan],
[eraser, FaEraser],
];
const [currentTool, setCurrentTool] = useState(0);

Expand Down
5 changes: 4 additions & 1 deletion example/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ label span {
display: inline-flex;
cursor: pointer;
border: none;
place-items: center;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
margin: 3px;
}



@media screen and (max-width: 500px) {
main {
grid-template-columns: 1fr 1fr;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./tools/marker/useMarker";
export * from "./tools/airbrush/useAirbrush";
export * from "./tools/shading/useShadingBrush";
export * from "./tools/watercolor/useWatercolor";
export * from "./tools/eraser/useEraser";
export * from "./components/Artboard";
export * from "./utils/pointUtils";
export * from "./history";
Expand Down
35 changes: 35 additions & 0 deletions src/tools/eraser/useEraser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useCallback } from "react";
import { ToolHandlers } from "../../components/Artboard";
import { Point } from "../../utils/pointUtils";
import { circleCursor } from "../../utils/cursors";
export interface UseEraserProps {
color?: string;
strokeWidth?: number;
}

export function useEraser({ strokeWidth = 25 }: UseEraserProps): ToolHandlers {
const startStroke = useCallback(
(point: Point, context: CanvasRenderingContext2D) => {
context.globalCompositeOperation = "source-over";

context.lineWidth = strokeWidth;
context.strokeStyle = "#ffffff";
context.lineJoin = context.lineCap = "round";
context.moveTo(point[0], point[1]);
context.beginPath();
},
[strokeWidth]
);

const continueStroke = useCallback(
(point: Point, context: CanvasRenderingContext2D) => {
context.lineTo(point[0], point[1]);
context.stroke();
},
[]
);

const cursor = circleCursor(strokeWidth);

return { name: "Eraser", startStroke, continueStroke, cursor };
}

0 comments on commit a85fb26

Please sign in to comment.