Drawing & Erasing
Switching modes
import { ReactSketchCanvas, type ReactSketchCanvasRef, } from "react-sketch-canvas"; import { type ChangeEvent, useRef, useState } from "react"; export default function App() { const canvasRef = useRef<ReactSketchCanvasRef>(null); const [eraseMode, setEraseMode] = useState(false); const [strokeWidth, setStrokeWidth] = useState(5); const [eraserWidth, setEraserWidth] = useState(10); const handleEraserClick = () => { setEraseMode(true); canvasRef.current?.eraseMode(true); }; const handlePenClick = () => { setEraseMode(false); canvasRef.current?.eraseMode(false); }; const handleStrokeWidthChange = (event: ChangeEvent<HTMLInputElement>) => { setStrokeWidth(+event.target.value); }; const handleEraserWidthChange = (event: ChangeEvent<HTMLInputElement>) => { setEraserWidth(+event.target.value); }; return ( <div className="d-flex flex-column gap-2 p-2"> <h1>Tools</h1> <div className="d-flex gap-2 align-items-center "> <button type="button" className="btn btn-sm btn-outline-primary" disabled={!eraseMode} onClick={handlePenClick} > Pen </button> <button type="button" className="btn btn-sm btn-outline-primary" disabled={eraseMode} onClick={handleEraserClick} > Eraser </button> <label htmlFor="strokeWidth" className="form-label"> Stroke width </label> <input disabled={eraseMode} type="range" className="form-range" min="1" max="20" step="1" id="strokeWidth" value={strokeWidth} onChange={handleStrokeWidthChange} /> <label htmlFor="eraserWidth" className="form-label"> Eraser width </label> <input disabled={!eraseMode} type="range" className="form-range" min="1" max="20" step="1" id="eraserWidth" value={eraserWidth} onChange={handleEraserWidthChange} /> </div> <h1>Canvas</h1> <ReactSketchCanvas ref={canvasRef} strokeWidth={strokeWidth} eraserWidth={eraserWidth} /> </div> ); }
Color selection
import { ReactSketchCanvas, type ReactSketchCanvasRef, } from "react-sketch-canvas"; import { type ChangeEvent, useRef, useState } from "react"; export default function App() { const canvasRef = useRef<ReactSketchCanvasRef>(null); const [strokeColor, setStrokeColor] = useState("#000000"); const [canvasColor, setCanvasColor] = useState("#ffffff"); const handleStrokeColorChange = (event: ChangeEvent<HTMLInputElement>) => { setStrokeColor(event.target.value); }; const handleCanvasColorChange = (event: ChangeEvent<HTMLInputElement>) => { setCanvasColor(event.target.value); }; return ( <div className="d-flex flex-column gap-2 p-2"> <h1>Tools</h1> <div className="d-flex gap-2 align-items-center "> <label htmlFor="color">Stroke color</label> <input type="color" value={strokeColor} onChange={handleStrokeColorChange} /> <label htmlFor="color">Canvas color</label> <input type="color" value={canvasColor} onChange={handleCanvasColorChange} /> </div> <h1>Canvas</h1> <ReactSketchCanvas ref={canvasRef} strokeColor={strokeColor} canvasColor={canvasColor} /> </div> ); }
Read-only mode
import { ReactSketchCanvas } from "react-sketch-canvas"; import { type ChangeEvent, useState } from "react"; export default function App() { const [readOnly, setReadOnly] = useState(false); const handleReadOnlyChange = (event: ChangeEvent<HTMLInputElement>) => { setReadOnly(event.target.checked); }; return ( <div className="d-flex flex-column gap-2 p-2"> <h1>Tools</h1> <div className="d-flex gap-2 align-items-center "> <div className="form-check form-switch"> <input className="form-check-input" type="checkbox" role="switch" id="readOnly" checked={readOnly} onChange={handleReadOnlyChange} /> <label className="form-check-label" htmlFor="readOnly"> readOnly - Disables drawing </label> </div> </div> <h1>Canvas</h1> <ReactSketchCanvas readOnly={readOnly} /> </div> ); }