Skip to content

Commit bbc3def

Browse files
committed
Add SVG export examples for various scenarios
1 parent 5dc52be commit bbc3def

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* SVG Export (No Screen Display)
3+
*
4+
* This example draws a single frame to a SVG file and quits.
5+
* (Note that no display window will open; this helps when you're
6+
* trying to create massive SVG images that are far larger than
7+
* the screen size.)
8+
*/
9+
10+
import processing.svg.*;
11+
12+
void setup() {
13+
size(400, 400, SVG, "filename.svg");
14+
}
15+
16+
void draw() {
17+
// Draw something good here
18+
line(0, 0, width/2, height);
19+
20+
// Exit the program
21+
println("Finished.");
22+
exit();
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Single Frame from an Animation (With Screen Display)
3+
*
4+
* It's also possible to save one frame from a program with
5+
* moving elements. Create a boolean variable to turn the SVG
6+
* recording process on and off.
7+
*/
8+
9+
import processing.svg.*;
10+
11+
boolean record;
12+
13+
void setup() {
14+
size(400, 400);
15+
}
16+
17+
void draw() {
18+
if (record) {
19+
// Note that #### will be replaced with the frame number. Fancy!
20+
beginRecord(SVG, "frame-####.svg");
21+
}
22+
23+
// Draw something good here
24+
background(255);
25+
line(mouseX, mouseY, width/2, height/2);
26+
27+
if (record) {
28+
endRecord();
29+
record = false;
30+
}
31+
}
32+
33+
// Use a mouse press so thousands of files aren't created
34+
void mousePressed() {
35+
record = true;
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* SVG Files from 3D Geometry (With Screen Display)
3+
*
4+
* To create vectors from 3D data, use the beginRaw() and
5+
* endRaw() commands. These commands will grab the shape data
6+
* just before it is rendered to the screen. At this stage,
7+
* your entire scene is nothing but a long list of lines and
8+
* triangles. This means that a shape created with sphere()
9+
* method will be made up of hundreds of triangles, rather
10+
* than a single object.
11+
*
12+
* When using beginRaw() and endRaw(), it's possible to write
13+
* to either a 2D or 3D renderer. For instance, beginRaw()
14+
* with the SVG library will write the geometry as flattened
15+
* triangles and lines.
16+
*/
17+
18+
import processing.svg.*;
19+
20+
boolean record;
21+
22+
void setup() {
23+
size(500, 500, P3D);
24+
}
25+
26+
void draw() {
27+
if (record) {
28+
beginRaw(SVG, "output.svg");
29+
}
30+
31+
// Do all your drawing here
32+
background(204);
33+
translate(width/2, height/2, -200);
34+
rotateZ(0.2);
35+
rotateY(mouseX/500.0);
36+
box(200);
37+
38+
if (record) {
39+
endRaw();
40+
record = false;
41+
}
42+
}
43+
44+
// Hit 'r' to record a single frame
45+
void keyPressed() {
46+
if (key == 'r') {
47+
record = true;
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Using createGraphics() to Create an SVG File
3+
*
4+
* To write a SVG file using only the createGraphics() command,
5+
* rather than as part of a sketch, it's necessary to call
6+
* dispose() on the PGraphicsSVG object. This is the same as
7+
* calling exit(), but it won't quit the sketch.
8+
*/
9+
10+
import processing.svg.*;
11+
12+
PGraphics svg = createGraphics(300, 300, SVG, "output.svg");
13+
svg.beginDraw();
14+
svg.background(128, 0, 0);
15+
svg.line(50, 50, 250, 250);
16+
svg.dispose();
17+
svg.endDraw();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* SVG Export (With Screen Display)
3+
*
4+
* To draw to the screen while also saving an SVG, use the
5+
* beginRecord() and endRecord() functions. Unlike the PDF
6+
* renderer, the SVG renderer will only save the final frame
7+
* of a sequence. This is slower, but is useful when you need
8+
* to see what you're working on as it saves.
9+
*/
10+
11+
import processing.svg.*;
12+
13+
void setup() {
14+
size(400, 400);
15+
noLoop();
16+
beginRecord(SVG, "filename.svg");
17+
}
18+
19+
void draw() {
20+
// Draw something good here
21+
line(0, 0, width/2, height);
22+
23+
endRecord();
24+
}

0 commit comments

Comments
 (0)