React Sketch Canvas
react-sketch-canvas is a React component designed for freehand vector drawing, leveraging SVG as its rendering canvas. It provides a declarative interface for integrating drawing capabilities into React applications, supporting actions like drawing, erasing, undoing, and redoing strokes. The library allows for customization of stroke properties and offers functionality to export the drawn content as SVG paths or various image formats (PNG, JPEG). The current stable version is 6.2.0, with active development on a 7.0.0-next branch, indicating ongoing maintenance and feature additions. Its primary differentiators include its SVG-based output, which ensures resolution independence, a straightforward React component API, and built-in essential drawing features without relying on pixel-based HTML Canvas APIs.
Common errors
-
TypeError: (0 , react_sketch_canvas__WEBPACK_IMPORTED_MODULE_2__.ReactSketchCanvas) is not a function
cause Attempting to import `ReactSketchCanvas` as a default import when it is a named export.fixChange your import statement from `import ReactSketchCanvas from 'react-sketch-canvas';` to `import { ReactSketchCanvas } from 'react-sketch-canvas';` -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)...Check the render method of `YourComponent`.
cause Incorrect import of `ReactSketchCanvas` or React environment issues (e.g., mismatched React versions or incorrect setup).fixVerify that `import { ReactSketchCanvas } from 'react-sketch-canvas';` is correct. Ensure your React and ReactDOM versions are compatible and meet the peer dependency requirement (>=16.8). -
Canvas appears blank or with unexpected white space, despite being rendered.
cause This could be due to CSS conflicting with the canvas or, in older versions, specific rendering bugs (e.g., #103 fixed in `7.0.0-next.2`). Also check container dimensions.fixEnsure the `ReactSketchCanvas` component and its parent containers have explicit `width` and `height` CSS properties. If using an older version, consider upgrading to at least `6.2.0` or `7.0.0-next.2` if the issue persists. -
Warning: Each child in a list should have a unique 'key' prop.
cause While `react-sketch-canvas` handles internal rendering, if you are attempting to map or render multiple instances of `ReactSketchCanvas` or its internal path data, React requires a unique `key` prop for each item in a list.fixIf you are iterating over an array to render multiple `ReactSketchCanvas` components, ensure each instance receives a unique `key` prop, e.g., `<ReactSketchCanvas key={item.id} ... />`.
Warnings
- breaking Version 7.0.0 is under active development (as of next releases). While not yet stable, it introduces new features and potential API changes. Users should review the changelog carefully before upgrading to the stable 7.x release to identify any breaking changes.
- gotcha Older versions (pre-6.1.1) might have included coverage instrumentation in their release builds, potentially leading to increased bundle size or minor performance overhead. This was resolved in version 6.1.1.
- gotcha When using `ReactSketchCanvas` in environments with strict CSP (Content Security Policy) rules, ensure that `style-src 'unsafe-inline'` is permitted, as the component uses inline styles for certain SVG elements.
Install
-
npm install react-sketch-canvas -
yarn add react-sketch-canvas -
pnpm add react-sketch-canvas
Imports
- ReactSketchCanvas
import ReactSketchCanvas from 'react-sketch-canvas'; const ReactSketchCanvas = require('react-sketch-canvas');import { ReactSketchCanvas } from 'react-sketch-canvas'; - CanvasPath
import type { CanvasPath } from 'react-sketch-canvas'; - ReactSketchCanvasRef
import type { ReactSketchCanvasRef } from 'react-sketch-canvas';
Quickstart
import React, { useRef, useState } from 'react';
import { ReactSketchCanvas, ReactSketchCanvasRef } from 'react-sketch-canvas';
const DrawingApp: React.FC = () => {
const canvasRef = useRef<ReactSketchCanvasRef>(null);
const [strokeColor, setStrokeColor] = useState('#000000');
const [strokeWidth, setStrokeWidth] = useState(4);
const handleClear = () => {
if (canvasRef.current) {
canvasRef.current.clearCanvas();
}
};
const handleUndo = () => {
if (canvasRef.current) {
canvasRef.current.undo();
}
};
const handleRedo = () => {
if (canvasRef.current) {
canvasRef.current.redo();
}
};
const handleExport = async () => {
if (canvasRef.current) {
const image = await canvasRef.current.exportImage('png');
// For demonstration, log the base64 image or trigger a download
console.log('Exported PNG (base64):', image);
// To trigger a download:
// const a = document.createElement('a');
// a.href = image;
// a.download = 'sketch.png';
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a);
}
};
return (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
<h1>My Sketchpad</h1>
<div style={{ marginBottom: '10px' }}>
<label htmlFor="strokeColor">Color: </label>
<input
id="strokeColor"
type="color"
value={strokeColor}
onChange={(e) => setStrokeColor(e.target.value)}
/>
<label htmlFor="strokeWidth" style={{ marginLeft: '15px' }}>Width: </label>
<input
id="strokeWidth"
type="range"
min="1"
max="20"
value={strokeWidth}
onChange={(e) => setStrokeWidth(Number(e.target.value))}
/>
</div>
<ReactSketchCanvas
ref={canvasRef}
strokeWidth={strokeWidth}
strokeColor={strokeColor}
canvasColor="#FFFFFF"
width="600px"
height="400px"
style={{ border: '1px solid #ccc', borderRadius: '5px' }}
/>
<div style={{ marginTop: '10px' }}>
<button onClick={handleClear} style={{ marginRight: '5px' }}>Clear</button>
<button onClick={handleUndo} style={{ marginRight: '5px' }}>Undo</button>
<button onClick={handleRedo} style={{ marginRight: '5px' }}>Redo</button>
<button onClick={handleExport}>Export PNG</button>
</div>
</div>
);
};
export default DrawingApp;