Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

p5.js-svg

zenozeng1.6kMIT1.6.0TypeScript support: included

The main goal of p5.SVG is to provide a SVG runtime for p5.js, so that we can draw using p5's powerful API in <svg>, save things to svg file and manipulating existing SVG file without rasterization.

p5.js, svg

readme

p5.js-svg

The main goal of p5.SVG is to provide a SVG runtime for p5.js, so that we can draw using p5's powerful API in <svg>, save things to svg file and manipulating existing SVG file without rasterization.

Getting Started

Add this line in your projects index.html :

<script src="https://unpkg.com/p5.js-svg@1.6.0"></script>

(p5.js-svg v1.6.x is compatible with p5.js v1.11.x)

Open your sketch.js and edit it:

function setup() {
  createCanvas(100, 100, SVG);
  background(255);
  fill(150);
  stroke(150);
}

function draw() {
  var r = frameCount % 200 * Math.sqrt(2);
  background(255);
  ellipse(0, 0, r, r);
}

Then you can open your html file, and view the result. It's <svg>!

SVG Gettting Started

Examples

SVG Renderer vs Canvas2D Renderer

The major difference is that SVG Renderer is based on SVG Document Object Model while Canvas 2D Renderer is based on pixels. Therefore, the performance may not be as good as canvas, but SVG-format vector images can be rendered at any size without loss of quality.

Note that not all drawing results are exactly same in pixel-level.For example, the round rects below are almost same, but there are some pixels different.

round rect

As for filters, gray(), invert(), threshold(), opaque() did have same behavior as Canvas2D Renderer. But blur(), erode(), dilate() didn't.

To implement blur, feGaussianBlur was used, which is different from Processing's blur. blur

As for erode() and dilate(), they were implemnted using feOffset and feBlend. So, the result is not exactly same. erode

You can view all the pixels based diff on the online tests.

Browser Compatibility

p5.js-svg@1.x was tested and should work on:

  • Chromium 90 (Debian 11.0, LXQt 0.16)
  • Safari (iPadOS 14)

How it works

p5.RendererSVG is a class which extends p5.Renderer2D. A mocked <canvas> element and a CanvasRenderingContext2D api are provided using svgcanvas, which is JavaScript Object that syncs proprieties and draws on <svg> element.

Known issue

Too many child elements

Since SVG is XML-based, every call of the draw function will insert elements into it, and these elements keep existing even if they are not visible. So, long-time running will result in too many child elements. We recommend calling clear() in your draw function, which will trigger internal context.__clearCanvas() to remove elements.

function draw() {
  clear();
  // draw
}

See https://github.com/zenozeng/p5.js-svg/issues/32

blendMode is not implemented yet.

Building dist

To build dist files after cloning repo, you can run:

npm install
npm run build

Tests

p5.SVG was driven by tests. We use Karma and mocha. Most tests are based on pixel-diff. There are still some p5's methods not covered with unit tests. But Rendering and Shape API are already covered with tests and should work.

If you found a bug, feel free to open a issue or pull a request.

All tests can be found here: https://github.com/zenozeng/p5.js-svg/tree/master/test/unit

You can also run the online test yourself: https://zenozeng.github.io/p5.js-svg/test/

changelog

v1.6.0

v1.5.1

  • docs: examples/vite
  • refactor: p5svg -> p5SVG

v1.5.0

  • feat: TypeScript type declarations
  • refactor: rewrite using TypeScript
  • fix: clear after resizing should not have unwanted white background, fixes

    235

v1.4.0

v1.3.3

  • fix: add cjs output, fixes #217
  • docs: update examples/webpack
  • docs: add examples/vite

v1.3.2

  • update package.main to dist/p5.svg.js, fixes #213

v1.3.1

v1.3.0

  • feat: loadPixels() for https://github.com/zenozeng/p5.js-svg/issues/203
  • fix(graphics): call RendererSVG with this instead of pInst
  • fix: _pixelDensity for drawing image created by createGraphics
  • refactor: RendererTester
  • refactor: use Renderer2D.prototype.line instead of RendererSVG.prototype.line
  • refactor: use Proxy instead of _withPixelDensity

v1.2.1

v1.2.0

v1.1.1

  • fix: push/pop, fixes #191, fixes #192
  • test: add test for push

v1.1.0

v1.0.8

  • fix: set $this for RendererSVG.prototype.parent, fixes #187

v1.0.7

  • feat: sync element's width and height to context (svgcanvas@2.0.3), calling clear() in your draw function will now trigger internal context.__clearCanvas() to remove elements.
  • test: add test for loadFont, fixes #147

v1.0.6

  • fix: use encodeURIComponent when saving svg, fixes #176 (save() bug)

v1.0.5

v0.6.0-alpha.0

p5.svg@0.6.0-alpha.0 (tests with p5.js@0.4.21)

v0.5.2

p5.svg@0.5.2 (tests with p5.js@0.4.13)

v0.5.1

p5.svg@0.5.1 (requires p5.js@0.4.8)

  • Fix issues with unit tests

v0.5.0

p5.svg@0.5.0 (requires p5.js@0.4.8)

  • Fix loadSVG in p5.js@0.4.8

  • RendererSVG.prototype.appendChild(SVGElement)

  • Configure ESLint and make it happy

  • Allow save SVGElement in save() and saveSVG()

  • add p5.prototype.registerSVGFilter

  • add p5.prototype.getDataURL

v0.4.3

p5.svg@0.4.3 (requires p5.js@0.4.7 with patch#850)

v0.4.2

p5.svg@0.4.2 (requires p5.js@0.4.7, with patch#850)

p5.SVG

v0.4.1

p5.svg@0.4.1 (built on top of p5.js@0.4.7, with patch#850)

Fix some issues in IE 10 and Safari (iOS).

p5.SVG

svgcanvas

v0.4.0

p5.svg

p5.svg@0.4.0 (built on top of p5.js@0.4.7, with patch#850)

Filters

  • SVGFilters.apply(element, filter, arg)

  • filters chain using svg's in and result.

  • SVGElement.prototype.filter = function(filter, arg)

  • SVGElement.prototype.unfilter = function(filter, arg)

    Undo the filter applied.

  • SVGFilters.colorMatrix = function(inGraphics, resultGraphics, matrix)

  • SVGFilters.blur using feGaussianBlur

  • SVGFilters.gray using feColorMatrix (CIE luminance)

  • SVGFilters.invert using feColorMatrix

  • SVGFilters.threshold using feColorMatrix and feComponentTransfer (linear feFunc)

  • SVGFilters.opaque using feColorMatrix

  • SVGFilters._discreteTableValues

    Generate discrete table values based on the given color map function

  • SVGFilters.posterize using feComponentTransfer (discrete feFunc)

  • SVGFilters.erode using feOffset and feBlend

    Will create 4 offset layer and combine them with current layer using darken blend mode.

  • SVGFilters.dilate using feOffset and feBlend

    Will create 4 offset layer and combine them with current layer using lighten blend mode.

p5.SVGElement

  • add p5.prototype.querySVG (querySelectorAll and map to SVGElement objects)

  • Extends p5.Element

  • SVGElement.prototype.query (querySelectorAll and map to SVGElement objects)

  • SVGElement.prototype.attribute (setAttributeNS, setAttribute and getAttribute)

  • SVGElement.prototype.append

  • SVGElement.create = function(nodeName, attributes)

  • SVGElement.prototype.parentNode()

    Get parent node

  • SVGElement.prototype.parentNode(selector)

    Get parent node matching given selector

  • SVGElement.prototype.matches = function(selector)

    To tell whether a element matches certain selector

  • SVGElement.prototype._getDefs

    Get defs element, or create one if not exists

RendererSVG

  • image(SVGElement) now supported

IO

  • private method p5.prototype._svg_get that handles dataurl and http requests

  • force request to dataurl to be async so that it won't mess up preload

  • p5.prototype.loadSVG (will return a SVGElement Object)

Etc

  • RendererSVG's image() now uses <svg> directly instead of drawImage

  • use svgcanvas@0.8.0 & update tests, fixes #104

  • add testRender.lock and testRender.unlock

  • add testRender.setMaxDiff

  • add testRender.setMaxPixelDiff

svgcanvas

  • replace encodeURI with encodeURIComponent in toDataURL

  • use new XMLSerializer().serializeToString(this.svg)

  • remove the buggy Context.prototype.getSerializedSvg

v0.3.0

p5.svg@0.3.0 (built on top of p5.js@0.4.7)

CHANGELOG

p5.svg

  • add p5.RendererSVG (extends p5.Renderer2D)

  • use p5.js@0.4.7

  • add p5.svg constants

  • add loadGraphics

  • patched p5.Graphics for p5.RendererSVG

  • Switch to browserify

  • Fix karma

  • Canvas now will be cleared when resize called

  • add RendererSVG.prototype._applyDefaults, fixes #95 (lineWidth issue)

  • Rendering functions (createCanvas(), resizeCanvas(), noCanvas(), createGraphics()) now covered with unit tests

  • use createCanvas(w, h, SVG) instead of createSVG(w, h)

  • set viewBox attribute when resize, fixes scale issue

  • add RendererSVG.prototype._withPixelDensity (temporally set pixel density to 1), fixes window scaled issue (https://github.com/zenozeng/p5.js-svg/issues/35)

  • Fix issues in test-render

svgcanvas

v0.2.0

p5.svg@0.2.0 (built on top of p5.js@0.4.5)

  • save()
  • saveFrames()
  • saveSVG()

v0.1.1

Basic p5.js functions support.