npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

p5.plotsvg

v0.3.0

Published

A Plotter-Oriented SVG Exporter for p5.js

Readme

p5.plotSvg

A Plotter-Oriented SVG Exporter for p5.js

Plotter

p5.plotSvg is a p5.js library for exporting SVG files tailored for pen plotting. Version 0.3.0, June 22, 2026 • Initiated by Golan Levin (@golanlevin)

Downloads, Mirrors, and Documentation:

Contents:


About p5.plotSvg

The p5.plotSvg library allows the p5.js creative coding toolkit to generate SVG files specifically tailored for path-based vector output devices like the AxiDraw pen-plotter. Key advantages of p5.plotSvg are that it does not interfere with graphics performance during animation, and it is easy to add to projects.

Note that p5.plotSvg is not a general-purpose library for importing, exporting, optimizing, or rendering SVG files in p5.js: the p5.plotSvg library only considers the geometry of paths, and not how they are visually styled, with the expectation that the way a path eventually appears is a function of the type of physical tool you happen to have in your drawing machine. The p5.plotSvg library is known to be compatible with p5.js versions 1.4.2 through 1.11.13. Compatibility with p5.js version 2.x is now supported for basic 2D geometry export and remains under active development for newer p5.js v2 curve and spline features.

p5.plotSvg was initiated by Golan Levin in November 2024 as a resource for the Drawing with Machines course at CMU School of Art. It was created with encouragement and generous support from Bantam Tools, makers of the world's finest pen-plotting instruments.


Quickstart Installation Instructions

First, include p5.plotSvg.js in your project, alongside p5.js. You can do this by linking to an online copy of p5.plotSvg at unpkg.com or cdn.jsdelivr.net in your project's index.html file. Alternatively, you can link to a local copy of p5.plotSvg.js, which you can download from this GitHub repo, here.

p5.plotSvg works with both p5.js v1 and p5.js v2. It can be used as a classic script-tag library, through the p5plotSvg namespace, or through newer p5 add-on-style methods attached to p5 instances. The older global functions remain supported for backward compatibility.

Script tag usage

For p5.js v1 projects, the traditional script-tag form remains fully supported:

<!-- This is the index.html file -->
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/p5.plotsvg@latest/lib/p5.plotSvg.js"></script>
  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

For p5.js v2 projects, use p5.js v2 and the generated plotSvg script build:

<!-- This is the index.html file -->
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/p5.plotsvg@latest/dist/p5.plotSvg.js"></script>
  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

Next, create a p5.js file like the one below, called sketch.js, in the same directory as your index.html. When you run this example file in a web browser, you can press the s key to export an SVG file. In global mode, the newer add-on-style form is:

// This is the sketch.js file.
// Press 's' to export the SVG.
// Note that p5.js is used in 'global mode'. 

p5.disableFriendlyErrors = true; // keep warnings quiet
let bDoExportSvg = false; 

function setup(){
  // These canvas dimensions are 8.5"x11" at 96 dpi
  createCanvas(816, 1056); 
}

function keyPressed(){
  if (key == 's'){ 
    bDoExportSvg = true; 
  }
}

function draw(){
  background(255); 
  if (bDoExportSvg){
    beginRecordSvg("myOutput.svg");
  }

  // Draw stuff here, such as:
  line(0,0, mouseX, mouseY); 

  if (bDoExportSvg){
    endRecordSvg();
    bDoExportSvg = false;
  }
}

Older sketches that call beginRecordSvg(this, "myOutput.svg") are still supported.

One recommended workflow is to generate an SVG file using p5.js with p5.plotSvg, as described above, and then (if desired) optimize your SVG file for plotting using the excellent vpype command-line utility.

Instance mode

As an alternative to using p5.js global mode, the p5.plotSvg library can also be used in p5's instance mode. The add-on-style instance API is recommended for new instance-mode sketches:

// This is a minimal sketch.js file for instance mode. 

function sketch(context) {
  context.setup = function() {
    context.createCanvas(400, 400);    
    context.beginRecordSvg("output.svg");
    context.circle(200, 200, 200);
    context.endRecordSvg();
  };
};

new p5(sketch, document.getElementById("container"));

The namespace form also remains available:

p5plotSvg.beginRecordSvg(context, "output.svg");
// draw with context.line(), context.circle(), etc.
p5plotSvg.endRecordSvg();

ESM usage

Projects that use JavaScript modules can import p5.plotSvg from npm/CDN package entry points. With p5.js v2, register the exported add-on before creating a sketch:

import p5 from 'p5';
import { plotSvgAddon } from 'p5.plotsvg';

p5.registerAddon(plotSvgAddon);

new p5((sketch) => {
  sketch.setup = function() {
    sketch.createCanvas(400, 400);
    sketch.beginRecordSvg("output.svg");
    sketch.line(20, 20, 380, 380);
    sketch.endRecordSvg();
  };
});

The named plotSvgAddon export is the p5.js v2 add-on installer. The default export is the p5plotSvg namespace, which remains available for namespace-style calls.


What the p5.plotSvg library IS:

  • The p5.plotSvg library allows you to export a p5.js drawing as an SVG file that consists exclusively of scalable 2D vector paths, such as lines, arcs, shapes, polylines, and curves. We anticipate that you'll use the SVG files generated with this library to execute your drawings on a vector output device, such as a laser cutter, AxiDraw, or NextDraw pen-plotter.
  • The p5.plotSvg library is intended for use with p5.js, and is modeled after the way in which PDF exporting and SVG exporting are implemented in Processing (Java). To use p5.plotSvg, you are expected to manage the timing of a beginRecordSvg() and endRecordSvg() function in your code.
  • The p5.plotSvg library works by temporarily overriding the functionality of the p5.js drawing commands. At the precise moment when you export the SVG, p5 drawing commands like line() and ellipse() are temporarily redefined so that they not only draw onscreen, but also add their data to the SVG file. When the SVG is finished saving, the regular definitions of these functions are restored. This temporary override model is central to p5.plotSvg's export-on-demand workflow; the library restores original property descriptors after recording so p5.js and other libraries can continue normally.

What the p5.plotSvg library IS NOT:

  • The p5.plotSvg library is not a "general purpose" p5-to-SVG exporter; it is intended for the specific needs of plotter enthusiasts. Large parts of both the p5 and SVG specifications have been purposefully omitted, even where they are common to both. To ensure plotter compatibility, this library provides no support for exporting SVG files with graphics features that have no analogue in pen-plotting — such as pixel-based images, transparency, filters, shaders, blend modes, gradients, animation, or (even) fills and stroke weights. You might be able to render such things onscreen with p5.js, but they will not appear in the SVG vector files made with this library.
  • p5.plotSvg is not an SVG-based alternative renderer for the web browser. What you see onscreen is a standard p5.js canvas, not an SVG. If you want an SVG runtime in the browser as a substitute for p5.js graphics, consider using Zenozeng's p5.js-svg library instead.
  • This is not a library for loading, parsing, or displaying SVG files in p5.js. Zenozeng's p5.js-svg can do that as well.
  • This is not a library for optimizing the geometry of vector graphics for plotting or cutting. For example: no utilities are provided for sorting the order/direction of exported lines (using a TSP-like algorithm) to minimize your plotting time; for merging line segments with common endpoints into polylines (to eliminate unnecessary pen-up/pen-down movements); for de-duplicating multiple lines in precisely the same location; or for reordering closed shapes from innermost to outermost for optimal laser-cutting. For such functionality, consider optimizing your SVGs with Antoine Beyeler's vpype for plotting, and/or Deepnest for laser cutting.
  • p5.plotSvg is not a library for vectorizing pixel-based canvases rendered by p5.js. In other words, no utilities are provided for hatching or dithering that would "convert" the pixels on the screen into vector strokes. The only marks that get exported to SVG are the geometric paths you specify with vector-based p5.js drawing commands like line(), ellipse(), etc.
  • This is not a library for computational geometry in p5. For problems like computing offset curves or shape-shape intersections, clipping, and occlusions, consider using libraries like Paper.js, Shapely, or occult.

Example Programs

This collection of examples shows how to generate plotter-friendly SVGs from p5.js using p5.plotSvg. All examples are mirrored in collections at editor.p5js.org and openProcessing.org. A visual index of examples is here.

  1. plotSvg_smorgasbord: ⭐ Full demo of all p5.js drawing primitives exported to SVG. @editor@openProcessing
  2. plotSvg_hello_static: Simplest possible demo; all art in setup() only. @editor@openProcessing
  3. plotSvg_hello_animating: Simple demo; uses setup() & draw() and a keypress. @editor@openProcessing
  4. plotSvg_addon_example: Demonstrates using the p5.plotSvg add-on.
  5. plotSvg_generative: Simple "generative artwork"; press button to export. @editor • @openProcessing
  6. plotSvg_drawing_recorder: Records a series of marks drawn by the user. @editor@openProcessing
  7. plotSvg_particle_paths: Accumulates the traces of some particles over time. @editor@openProcessing
  8. plotSvg_hatched_shapes: A trick for exporting hatched ("filled") SVG shapes. @editor@openProcessing
  9. plotSvg_instancemode: Example of SVG export in p5's instance mode. @editor
  10. plotSvg_post_grouping: Merge groups of paths ("layers") computed at different times. @editor • @openProcessing
  11. plotSvg_svg_font_text: Use monoline SVG fonts in your exported designs. @editor@openProcessing
  12. plotSvg_face_flipbook: Exports a tiny flipbook recording from a face-tracker. @openProcessing

Usage Notes

Color

  • The p5.plotSvg library ignores p5.js fill() commands, and does not export SVG shapes with filled colors. To export filled shapes for a pen-plotter, consider implementing your own hatching method, as in this example.
  • SVGs produced with p5.plotSvg have a default stroke color, black, which can be altered with setSvgDefaultStrokeColor(). This library takes stroke commands with valid CSS color strings (e.g., 'red', '#ff0000', 'rgb(255,0,0)'). The p5 colorMode() command is not supported by p5.plotSvg, and calls to colorMode() may produce unpredictable results in your SVG.
  • If you use stroke colors, the working assumption of p5.plotSvg is that you are using so to label different logical entities — such as different color pens in a multi-pen plotter, different tools in a CNC machine, or different intensity settings in a laser cutter. For this reason, alpha (transparency) values are stripped out. I strongly recommend using just a small number of colors, and selecting easy-to-remember CSS color keyword names such as 'red', 'green', 'blue', etc.
  • When making complex and/or multi-color plots, the setSvgMergeNamedGroups() and setSvgGroupByStrokeColor() functions may be helpful in producing Inkscape-compatible "layers" and grouping paths you want plotted with the same pen.

Graphics Transforms

The p5.plotSvg library offers two different ways of encoding graphics transformation operations (such as rotate(), translate(), scale(), shearX() and shearY()) into your SVG file. You can select the option you prefer using one the following functions during setup():

  • setSvgFlattenTransforms (true): The current transformation matrix is encoded into each SVG element. This leads to potentially larger SVG files, but graphical elements will appear with exactly the same transformations as they do in the corresponding p5 sketch. You might want to use this mode if your design concatenates many transforms.
  • setSvgFlattenTransforms (false) (default option): Graphics transforms are encoded into a hierarchy of SVG groups, each containing an atomic transform operation. This may produce smaller SVG files, depending on your design, but there is a possibility that different SVG rendering engines may accumulate the transforms with slightly different results.
  • If no graphics transforms are used in a p5 sketch, none are encoded into the SVG file.

Numeric Precision

p5.plotSvg offers two convenience functions which control how many digits of decimal precision are exported to SVG files. These can have a significant impact on SVG file size:

  • setSvgCoordinatePrecision() – The default is 4 digits of precision for path coordinates, e.g. 3.1416
  • setSvgTransformPrecision() – The default is 6 digits of precision for matrix transform data, e.g. 3.141593

Experimental Extension Hooks

p5.plotSvg intentionally exposes a few low-level hooks for advanced experiments and companion add-ons. These hooks are not needed for ordinary SVG export, and they may change more readily than the public drawing/export API, but they are available for people who want to extend the library's SVG output.

During an active recording session, p5plotSvg._commands refers to the live internal command array that will be converted into SVG by endRecordSvg(). External code may inspect this array or append compatible command objects before export. This is an experimental interface: it only exists between beginRecordSvg() and endRecordSvg(), and it is cleared after export. The read-only p5plotSvg.SVG_COMMAND and p5plotSvg.SVG_SEGMENT constants document the command and segment type strings used by this interface.

Related generic hooks include injectSvgHeaderAttribute(), injectSvgDef(), custom attributes arrays on injected command objects, and setSvgExportPolylinesAsPaths(). These make it possible to add custom namespaces, <defs> entries, command-level SVG attributes, or downstream-tool-specific path output without adding those experiments directly to the core library.


Known Issues and Bugs:

  • p5.plotSvg does not yet convert TTF/OTF text to outlines (though, this is planned). Additionally, p5.plotSvg does not embed TTF/OTF font data into SVGs; it refers to fonts by name and assumes they are installed on your host computer. You may need to perform additional operations on your SVGs (e.g. in Inkscape) to achieve desired typography. As an alternative, for quick typography with plotters, consider using single stroke fonts.
  • Non-default vertical textAlign() settings are not yet fully supported; only BASELINE currently works correctly.
  • p5.js v2 curve and spline compatibility is still in progress. Basic v1-style curve export is supported, but newer p5.js v2 curve/spline APIs may not yet export with complete fidelity.
  • clip(), beginClip(), and endClip() are intentionally unsupported for SVG export. Correct plotter-safe clipping would require computational geometry and path splitting, which are outside the scope of this library.
  • This library is not intended to be used in WEBGL mode. There is currently no support for converting 3D graphics to 2D, though this may be added later.
  • Other bugs and issues as listed here.

Other Libraries and Related Work

The following projects may be of interest to creative coders working with SVG files for plotting:

  • p5.js-svg by Zenozeng allows for direct SVG rendering in p5.js sketches, offering an alternative SVG-based renderer for the web browser. Replacing the HTML canvas entirely, it supports a wide range of SVG elements but also aims for full compatibility with p5.js drawing functions.
  • Paper.js by Jürg Lehni & Jonathan Puckey is a powerful open-source vector graphics scripting framework that runs on top of the HTML5 Canvas. It supports SVG import and export and offers a wide range of vector graphics manipulation features, such as path simplification and shape-shape intersection.
  • vpype by Antoine Beyeler is a powerful command-line tool and Python library for preprocessing and optimizing SVG vector graphics for plotting. It provides utilities for sorting paths, simplifying curves, and optimizing plotting jobs for pen plotters.
  • Rune.js by Rune Madsen is a JavaScript library for creative coding, similar to p5.js. While it is not strictly a p5.js SVG exporter, it includes capabilities for working with vector graphics, including SVG import/export.
  • p5-svg-test by Rune Madsen is a simple test for SVG generation using p5.js. This repository provides a proof of concept for exporting p5.js graphics to SVG format but is not a fully-featured library.
  • PEmbroider by the Frank-Ratchye STUDIO for Creative Inquiry at Carnegie Mellon University is a library for computational embroidery using Processing (Java). It allows users to generate embroidery stitch paths from their Processing sketches, with support for various embroidery machine formats. It also supports SVG export.
  • canvas2svg by Gliffy provides a way to export HTML5 Canvas content to SVG using JavaScript. It works by implementing a virtual canvas that mimics the CanvasRenderingContext2D interface, capturing drawing commands as SVG elements.
  • Bob Cook's SVG Example provides an example demonstrating how to convert canvas-based drawings to SVG using a custom library in a jsFiddle example.

License and Code of Conduct

p5.plotSvg is released under the MIT License. The p5.plotSvg project adheres to a Code of Conduct adapted from the Contributor Covenant, version 1.1.0.


Keywords

Pen plotters, vector output, plotter art, p5.js, SVG, #plotterTwitter, #penplotter, creative coding, generative art, drawing machines, JavaScript library, EMSL AxiDraw, NextDraw, open-source software tools for the arts, #OSSTA.


Acknowledgments

This project was initiated by Golan Levin and made possible by support from the CMU School of Art, the Frank-Ratchye STUDIO for Creative Inquiry at Carnegie Mellon University, and Bantam Tools. Special thanks to the Processing Foundation's p5.js project, the students in my Drawing with Machines course at CMU, @lewi0622, @mariuswatz, @msurguy, @R4chel, @thrly, @Ucodia, @v3ga, @webholics, and everyone else in the community who has generously contributed by filing issues, thoughtful comments, pull requests, and other forms of support.

Processing logo p5.js logo SVG logo