Skip to content

Commit 7e4480d

Browse files
authored
feat(eraser): select eraser if a pens eraser is used (#101)
1 parent fd07a3c commit 7e4480d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Canvas/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function getCanvasWithViewBox(canvas: HTMLDivElement) {
3232
export interface CanvasProps {
3333
paths: CanvasPath[];
3434
isDrawing: boolean;
35-
onPointerDown: (point: Point) => void;
35+
onPointerDown: (point: Point, isEraser: boolean) => void;
3636
onPointerMove: (point: Point) => void;
3737
onPointerUp: () => void;
3838
className?: string;
@@ -115,9 +115,10 @@ export const Canvas = React.forwardRef<CanvasRef, CanvasProps>((props, ref) => {
115115

116116
if (event.pointerType === 'mouse' && event.button !== 0) return;
117117

118+
const isEraser = event.pointerType === 'pen' && ((event.buttons & 32) === 32);
118119
const point = getCoordinates(event);
119120

120-
onPointerDown(point);
121+
onPointerDown(point, isEraser);
121122
};
122123

123124
const handlePointerMove = (

src/ReactSketchCanvas/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,16 @@ export const ReactSketchCanvas = React.forwardRef<
189189
},
190190
}));
191191

192-
const handlePointerDown = (point: Point): void => {
192+
const handlePointerDown = (point: Point, isEraser: boolean): void => {
193193
setIsDrawing(true);
194194
setUndoStack([]);
195195

196+
const isDraw = !isEraser && drawMode;
197+
196198
let stroke: CanvasPath = {
197-
drawMode: drawMode,
198-
strokeColor: drawMode ? strokeColor : '#000000', // Eraser using mask
199-
strokeWidth: drawMode ? strokeWidth : eraserWidth,
199+
drawMode: isDraw,
200+
strokeColor: isDraw ? strokeColor : '#000000', // Eraser using mask
201+
strokeWidth: isDraw ? strokeWidth : eraserWidth,
200202
paths: [point],
201203
};
202204

0 commit comments

Comments
 (0)