@adamlockhart/draw-view
v1.0.1
Published
Real-time stroke smoothing and curve-fitting engine for canvas drawing
Readme
@adamlockhart/draw-view
Real-time stroke-smoothing and curve-fitting engine for HTML5 Canvas drawing.
Install
npm install @adamlockhart/draw-viewUsage
import { DrawEngine } from '@adamlockhart/draw-view';
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const engine = new DrawEngine({ ctx });
// On pointer down:
engine.strokeStart(x, y);
// On pointer move:
engine.strokeMove(x, y);
// On pointer up:
const stroke = engine.strokeEnd();API
new DrawEngine(options)
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| ctx | CanvasRenderingContext2D | required | Canvas context to draw on |
| strokeRadius | number | 3 | Radius of the stroke |
| maxError | number | 5 | Error tolerance for knot removal |
| color | { r, g, b } | { r: 0, g: 0, b: 0 } | Stroke color |
Methods
strokeStart(x, y)— begin a strokestrokeMove(x, y)— continue a strokestrokeEnd()— finish a stroke, returns the completed stroke objectstrokeTap(x, y)— draw a single dotsetColor(r, g, b)— change stroke colorredraw()— clear and re-render all strokesclear()— clear canvas and reset documentgetDocument()— get serializable documentsetDocument(doc)— load and render a documentpopStroke()— remove last stroke (undo)pushStroke(stroke)— add a stroke back (redo)
License
MIT
