svgpen offers HTML5 Canvas features but with SVG. It extends the functionality of SVGs by making them easily "programmable", wihout having to declare document.createElementNS everytime.
npm i @js_cipher/svgpenCreate a root element in your HTML.
<div id="root" width="400" height="300"></div>import { SVGRoot } from '@js_cipher/svgpen';
const svg = new SVGRoot(root, 300, 300); // root element, width, height, backgroundColor?
svg.fillRect(10, 10, 100, 50, 'red', 'black'); // x, y, w, h, fill, stroke
svg.fillEllipse(200, 50, 40, 20, '#3b82f6'); // cx, cy, rx, ry, fill
svg.fillText('Hello SVG', 10, 120, '#111', 16); // text, x, y, fill, fontSizeAll methods return the created SVG element so you can chain or modify it.
new SVG(root, w, h)SVGSVGElementCreate your<svg>nodefillRect(x, y, w, h, fill?, stroke?)Draws<rect>fillCircle(cx, cy, r, fill?, stroke?)Draws<ellipse>plotLine(x1, y1, x2, y2, stroke, strokeWidth?Draws<line>polygon(points, fill?, stroke?, strokeWidth?)points="x1,y1 x2,y2"Draws<polygon>path(d, fill?, stroke?, strokeWidth?)SVG pathd-> Raw SVG pathdattributerenderText(text, x, y, fill?, fontSize?)Draws<text>- Other methods: fillEllipse, polyline
fillandstrokedefault tononeandblackif you skip them.
The updateCanvas(element, attribute, newValue) method updates the SVG element, adding a layer of functionality on top of it, it can be used togther with window.requestAnimationFrame to render smooth animations.
const rect = svg.fillRect(0, 0, 20, 20, "#50FF50");
let dx = 0;
function animate() {
dx += 1;
svg.updateCanvas(rect, "x", dx);
window.requestAnimationFrame(animate);
}
window.requestAnimationFrame(animate);Unlike the HTML5 canvas that clears the canvas and redraws, svgpen updates the SVG element's attributes in real-time without needing a redraw.
- Canvas API feel: Less verbose than
createElementNSevery time. - Real DOM: You still get
<svg>you can style, animate with CSS, or export. - Tiny: Zero deps. Tree-shakeable ESM + UMD build included.
- Programmability: svgpen helps you abstract all the raw SVG with JavaScript, allowing you focus on the logic.
- More SVG elements and attributes
- Path builder helpers
- TypeScript types
PRs are welcome.
MIT
