To implement undo/redo or SVG export, enable recordStrokes and use the strokerecorded event to save stroke data. You can then replay these strokes programmatically using beginStroke, draw, and endStroke.
// 1. Enable recording
atrament.recordStrokes = true;
atrament.addEventListener('strokerecorded', ({ stroke }) => {
// store `stroke` in an array for undo/redo
});
// 2. Replay a stroke
// Set settings to match the recorded stroke
atrament.mode = stroke.mode;
atrament.weight = stroke.weight;
// ... other settings
const segments = stroke.segments.slice();
const firstPoint = segments.shift().point;
// Start the path
atrament.beginStroke(firstPoint.x, firstPoint.y);
let prevPoint = firstPoint;
while (segments.length > 0) {
const segment = segments.shift();
// draw() returns the processed (smoothed) position
const { x, y } = atrament.draw(
segment.point.x,
segment.point.y,
prevPoint.x,
prevPoint.y,
segment.pressure
);
prevPoint = { x, y };
}
// Close the path
atrament.endStroke(prevPoint.x, prevPoint.y);