Class PShapeSVG

java.lang.Object
processing.core.PShape
processing.core.PShapeSVG
All Implemented Interfaces:
PConstants
Direct Known Subclasses:
PShapeJava2D, PShapeSVG.Font, PShapeSVG.FontGlyph, PShapeSVG.Gradient, PShapeSVG.LineOfText, PShapeSVG.Text

public class PShapeSVG extends PShape
This class is not part of the Processing API and should not be used directly. Instead, use loadShape() and methods like it, which will make use of this class. Using this class directly will cause your code to break when combined with future versions of Processing.

SVG stands for Scalable Vector Graphics, a portable graphics format. It is a vector format so it allows for "infinite" resolution and relatively small file sizes. Most modern media software can view SVG files, including Adobe products, Firefox, etc. Illustrator and Inkscape can edit SVG files. View the SVG specification here.

We have no intention of turning this into a full-featured SVG library. The goal of this project is a basic shape importer that originally was small enough to be included with applets, meaning that its download size should be in the neighborhood of 25-30 Kb. Though we're far less limited nowadays on size constraints, we remain extremely limited in terms of time, and do not have volunteers who are available to maintain a larger SVG library.

For more sophisticated import/export, consider the Batik library from the Apache Software Foundation.

Batik is used in the SVG Export library in Processing 3, however using it for full SVG import is still a considerable amount of work. Wiring it to Java2D wouldn't be too bad, but using it with OpenGL, JavaFX, and features like begin/endRecord() and begin/endRaw() would be considerable effort.

Future improvements to this library may focus on this properly supporting a specific subset of SVG, for instance the simpler SVG profiles known as SVG Tiny or Basic, although we still would not support the interactivity options.


A minimal example program using SVG: (assuming a working moo.svg is in your data folder)

 PShape moo;

 void setup() {
   size(400, 400);
   moo = loadShape("moo.svg");
 }
 void draw() {
   background(255);
   shape(moo, mouseX, mouseY);
 }
 
  • Constructor Details

    • PShapeSVG

      public PShapeSVG(XML svg)
      Initializes a new SVG object from the given XML object.
  • Method Details

    • getChild

      public PShape getChild(String name)
      Get a particular element based on its SVG ID. When editing SVG by hand, this is the id="" tag on any SVG element. When editing from Illustrator, these IDs can be edited by expanding the layers palette. The names used in the layers palette, both for the layers or the shapes and groups beneath them can be used here.
       // This code grabs "Layer 3" and the shapes beneath it.
       PShape layer3 = svg.getChild("Layer 3");
       
      Overrides:
      getChild in class PShape
      Parameters:
      name - the name of the shape to get
    • print

      public void print()
      Prints out the SVG document. Useful for parsing.