SVG Importerfor Processing This library is primarily for importing SVG paths into your processing sketch. For now, you can only import paths (no lines, rects, ellipses). For the lack of a better name at the moment, it is named SVG Importer. However, future plans do include supporting features like layers, lines, fills, and more. For an example of what you can do with SVG (Adobe's Scalar Vector Graphics), check out what I used it for in a previous app I did titled Manifest. If you liked this library or have questions about its usage, you can email me at mflux [@] ucla [.] edu. However, please don't ask me to debug your code... :[ You can ask plenty questions on the discourse. Much thanks to Casey Reas, Ben Fry, Aaron Koblin, Julien Gachodoat, and Zai. --Page updated 11/30/2005 Version History Download SVG Importer 1.0 Library Download complete SVG example set Links InstructionsFirst, unzip the library into your processing-xxx/libraries. When writing a processing sketch, click sketch -> import library -> svgimport. IMPORTANT:
For now, only curves/paths will get imported to processing. At the moment, lines, rects, ellipses, fills, gradients, and a lot of stuff are ignored. Perhaps in future versions I will support these, depending on time/demand. Examples(note: Vincent the cat is a 3D model rendered in maya vector as an svg, and now... loaded into processing!). How to load an SVG
Using SVGDetail
//This example shows you how to change the detail level of an svg
//Remember, as of Processing 93 curve and bezier detail levels only work
// in P3D and OGL
//svgDetail() can save you buttloads of CPU load, since it gives you control
// of how fine you want your SVGs to be rendered.
import svgimport.*;
svgpath mysvg;
void setup()
{
size(350,350,P3D); //<---svgDetail only works in P3D and opengGL
mysvg=new svgpath("svgcat.svg",this);
}
void draw()
{
background(255);
//First, set the detail before you draw.
//Here, we set it to mouseX/10 so it animates. This is easier to show you
// the effects of detail levels.
//Note that it's actually altering bezierDetail, although each detail level
// is saved for every SVG you have.
mysvg.setDetail(mouseX/10);
//Now draw your svg
mysvg.draw(LINE_STRIP,35,120);
}
Using Color Override
//This example shows the color override function of svg import.
import svgimport.*;
svgpath mysvg;
void setup()
{
size(350,350);
mysvg=new svgpath("moogle.svg",this);
smooth();
}
float colorShift=0;
void draw()
{
background(255);
//Usually when you draw svgs to screen, it uses the color specified in
// the SVG file.
mysvg.colorOverride(false);
mysvg.draw(LINE_STRIP,40,120);
//However, if you want to use your own colors, or animate the colors
// in this case, you can use the colorOverride() function.
mysvg.colorOverride(true);
stroke(colorShift);
mysvg.draw(LINE_STRIP,190,120);
colorShift+=2;
if(colorShift>255)
colorShift=0;
}
|
Referencenew svgPath(filename, this); This is the constructor for your SVG object. You can load in the filename through this constructor.filename - Any string for filename of your svg located in your data folder. this - A reference to your current applet. Just leave it as this and everything will be okay... (see reference). eg
svg.draw(style,cornerX,cornerY); This is the drawing function for your SVG objects. You use this to display SVGs to the drawing space. style - Any drawing style found in processing for beginShape(). Examples include LINE_LOOP, POLYGON, TRIANGLES, etc. More examples can be found in the Processing reference for beginShape(). cornerX - X coordinate of the corner of your SVG to display. Takes floats or ints. cornerY - Y coordinate of the corner of your SVG to display. Takes floats or ints. eg
svg.setDetail(detailLevel); This handy function sets what resolution the svg object in question will be drawn at. detailLevel - Any int (or float that gets rounded) for the resolution of your svg. This is then stored into the svg object itself, so you needn't do this multiple times. eg
svg.colorOverride(state) This function allows you to override the svg's stored color information. You can then use stroke() or fill() to color your SVGs instead. state - A boolean either true or false. True, and stroke()s and fill()s will be applied (or as the case may be, noStroke()s or noFill()s). False will make the svg be drawn in the color in which it was created. eg noFill(); |