VOOZH about

URL: https://bjango.com/articles/processingsvg/

⇱ Creating SVGs with Processing


Apps Articles Help Contact

Creating SVGs with Processing

I’ve been experimenting with creating SVGs programmatically, which opens up a world of possibilities. It allows patterns, shapes, and compositions that would not be easy or possible by hand. The results can then be imported into Illustrator, Sketch or your design tool of choice, augmenting their features.

Processing is a programming environment specifically tailored for code generated visuals, which makes it an ideal choice to create SVGs. It even comes with a built-in library for that exact purpose. This article is intended to be an introduction, and a companion to several open source examples I’ve created: Processing SVG experiments.

Feel free to reuse and remix the code as you see fit.

Getting started #

In addition to Processing’s standard boilerplate setup() and draw() functions, a few mandatory lines of code are required to export an SVG — importing the Processing SVG library, starting the SVG file recording with beginRecord(), drawing some shapes, then closing the SVG with endRecord(). Everything drawn after beginRecord() and before endRecord() will be sent to the canvas on screen, and to your SVG.

Make sure styling and transforms are done after beginRecord() has been called — if you don’t, styling and transforms will apply to your canvas, but not your SVG. If this is happening, you’ll probably notice Processing’s default styles being applied unexpectedly. A 1px black stroke or white fill is a sign that you’ve set your styles before beginRecord().

Oh, and while we’re talking about caveats, using pixelDensity(2) for a Retina or high DPI canvas will result in your SVG being 2× the intended size. Either don’t call pixelDensity() at all, or use pixelDensity(1).

Circle and rectangle #

The example below draws a purple circle and a blue rectangle on a transparent background. It’s not particularly exciting, but it shows the minimum needed to successfully render an SVG from Processing, and may serve as a good starting template.

👁 A blue circle and a purple rectangle

The noLoop() function tells Processing to not run the code in draw() every frame, which means it’ll just create a single file, rather than trying to create many SVGs per second. There’s a few ways you can ask Processing to only render the SVG once:

  • Adding noLoop() inside setup().
  • Calling exit() when you’ve finished drawing.
  • Triggering the drawing some other way outside draw(), like via a keyboard event.

Calling exit() will close the running sketch, so you won’t see the result. Using noLoop() is probably the best way to go in most cases.

SVG markup #

The resulting SVG isn’t as clean as it could be. But, it gets the job done, especially if you’re importing it into a design tool rather than using it as final production code.

👁 The SVG code for a blue circle and a purple rectangle

Hex spin #

Using Processing to generate SVGs really shines when creating many objects. It can be used as a super step-and-repeat, allowing for subtle variations to size, stroke weight, rotation angle and other attributes. The example below would be reasonably difficult to create quickly and accurately in most design tools.

👁 A hexagon repeating pattern

Random shapes and random arcs #

It can also be handy to randomly vary some properties. The Random Shapes and Random Arcs examples shuffle what’s drawn when space is pressed, and write an SVG when return is pressed.

👁 Some random shapes, with random bright colours

Sunflower pattern #

The sunflower pattern below consists of 2,500 circles of varying size and colour.

👁 A blue and purple sunflower pattern

It can take a while to set up, but once you have the code written to generate a pattern like this, it opens up even more experimentation — variables can be animated!

👁 Image

Pocket Casts #

I had the honour of helping the Pocket Casts team create an image using similar techniques. We used an audio file as input to alter the height of lines sweeping around a circle. The result was an SVG that could be imported into a design tool to be combined with the rest of the layout.

👁 Image

If you have any questions about this article, please get in touch. I’ve also created other Processing experiments you may be interested in.

Published 24 January 2019.

Featured articles

The vocal effects of Daft Punk

Matching drop shadows across CSS, Android, iOS, Figma, and Sketch

Two decades of Bjango

Design systems need a colour space

Blur radius comparison

Design tool canvas handles

Design tool memory usage

Shape builder vs pathfinder

Formulas for optical adjustments

Smaller Mac app icons

Icon speedrun videos

Fingerprint icon speedrun

Fountain pen icon speedrun

Pushpin icon speedrun

Flag icon speedrun

Designing macOS menu bar extras

SVG passthrough precision

Ideal SVG exports

Mac external displays for designers and developers, part 2

Camera iris icon speedrun

Opacity precision

Design tool performance signatures

Diagnosing common colour management issues

macOS prefs tab icons are 27×27pt

What is pass-through blending?

Colour management, part 4

Magnet icon speedrun

Soccer ball icon speedrun

Puzzle icon speedrun

Using SVGs in asset catalogs

My Illustrator snapping settings

Perfect loops in Processing

Testing for wide gamut

Creating SVGs with Processing

Colour management, part 3

Colour management, part 2

Colour management, part 1

Vector icon speedruns

My Mac app icon design workflow

Color Creator Templates

When two colours can be one

Mac external displays for designers and developers

Greyprint

Batch processing with Generator

Using blend if for masking

Design debugging

Interaction density

1px is not enough

My icon design workflow

Why Skala won't have artboards

Why I don't use PDFs for iOS assets

Blend if

Gradient maps

About the author

Marc Edwards is the founder and designer at Bjango. Subscribe via RSS to be notified of new articles. Marc on Mastodon. Marc on Bluesky.

We’re building a design tool! Learn more about Skala.