1

My goal for this Processing sketch is to be able to export as svg or pdf so I can then open the file in inkscape to print with my Axidraw (as I've done in the past with other sketches). Given that you can't save multiple frames to one .svg file in draw(), I am attempting it in setup(). The problem I'm running into is as soon as I add beginRecord() and endRecord(), no graphics display in the sketch and the resulting svg file is blank and zero bytes in size.

I feel like this is a simple error, I just can't see it.

 // Declare 2D array int counter = 1000; int[] coordx = new int[counter]; int[] coordy = new int[counter]; int x; int y; int sz = 10; boolean tooClose; void setup() { size(500, 500); rectMode(CENTER); //start in middle of screen x = width/2; y = height/2; beginRecord(SVG, "output/filenameZ####.svg"); background(255); noStroke(); for (int i = 0; i < counter; i++) { runWalker(); checkOverlap(x, y, i); coordx[i]= x; coordy[i]= y; println(x, coordx[i], y, coordy[i], i); } endRecord(); } void draw() { } void checkOverlap(int xx, int yy, int jj) { for (int k = 0; k < counter; k++) { if (xx == coordx[k] && yy == coordy[k]) { tooClose = true; } else { tooClose = false; noStroke(); fill(0); ellipse(x, y, sz, sz); } } } void runWalker() { float move = int(random(0, 4)); if (move == 0) { x += sz; } else if ( move == 1) { y += sz; } else if (move == 2) { x += -sz; } else { y += -sz; } //prevent going off left or right if (x < 0) { x = width; } if (x > width) { x = 0; } //prevent going off top or bottom if (y < 0) { y = height; } if (y > height) { y = 0; } }``` 

1 Answer 1

1

The following source code uses an ArrayList of PVectors to save an SVG image with each key press. If you just use beginRecord() and endRecord() you will only see one dot at a time. You should be able to save multiple images with this technique, described here: https://discourse.processing.org/t/creating-svg-only-saves-a-single-frame/14509

import processing.svg.PGraphicsSVG; int counter = 1000; float x; float y; float sz = 5; ArrayList<PVector> pts = new ArrayList<PVector>(); void setup() { size(500, 500); rectMode(CENTER); x = width/2; //start in middle of screen y = height/2; beginRecord(SVG, "img####.svg"); background(255); noStroke(); } void draw() { for (int i = 0; i < counter; i++) { runWalker(); checkOverlap(x, y, i); } if (frameCount > 1) { for (int i = 1; i < pts.size(); i++ ) { circle(pts.get(i).x, pts.get(i).y, sz ); // draw all } } } void checkOverlap(float xx, float yy, int i) { if(xx != pts.get(i).x && yy != pts.get(i).y){ fill(0); circle(x, y, sz); } } void runWalker() { float move = int(random(0, 4)); if (move == 0) { x += sz; } else if ( move == 1) { y += sz; } else if (move == 2) { x += -sz; } else { y += -sz; } //prevent going off left or right if (x < 0) { x = width; } if (x > width) { x = 0; } //prevent going off top or bottom if (y < 0) { y = height; } if (y > height) { y = 0; } pts.add(new PVector(x, y)); } void keyPressed() { if (key == 'r') { // press 'r' to save endRecord(); beginRecord(SVG, "img####.svg"); println("image saved."); } } 

Addendum: Added technique to save ArrayList of PVectors to a timestamped text file containing comma separated values. The x,y coordinates are saved each time an SVG is recorded. Only unique x,y coordinates are saved to the ArrayList to avoid duplicates.

import processing.svg.PGraphicsSVG; import java.util.Date; import java.text.SimpleDateFormat; int counter = 1000; float x; float y; float sz = 5; ArrayList<PVector> pts = new ArrayList<PVector>(); String[] data; void setup() { size(500, 500); rectMode(CENTER); x = width/2; //start in middle of screen y = height/2; beginRecord(SVG, "img####.svg"); background(255); noStroke(); } void draw() { for (int i = 0; i < counter; i++) { runWalker(); checkOverlap(); } if (frameCount > 1) { for (int i = 1; i < pts.size(); i++ ) { circle(pts.get(i).x, pts.get(i).y, sz ); // draw all } } } void checkOverlap() { PVector newVector = new PVector(x, y); if (!pts.contains(newVector)) { pts.add(newVector); fill(0); circle(x, y, sz); } } void runWalker() { float move = int(random(0, 4)); if (move == 0) { x += sz; } else if ( move == 1) { y += sz; } else if (move == 2) { x += -sz; } else { y += -sz; } //prevent going off left or right if (x < 0) { x = width; } if (x > width) { x = 0; } //prevent going off top or bottom if (y < 0) { y = height; } if (y > height) { y = 0; } } void arrayListToStringArray() { data = new String[pts.size()]; for (int i = 0; i < pts.size(); i++ ) { data[i] = str(pts.get(i).x) + "," + str(pts.get(i).y); } } void keyPressed() { if (key == 'r') { // press 'r' to save endRecord(); arrayListToStringArray(); String timeStamp = new SimpleDateFormat("MM.dd.HH.mm.ss").format(new java.util.Date()); String fileNameStr = timeStamp + ".txt"; saveStrings(fileNameStr, data); beginRecord(SVG, "img-####.svg"); println("image saved."); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

This is working really well as far as exports go! I still need it to only save coordinates or draw a circle if those exact coordinates haven't already been saved before, though. This way the plotter won't draw multiple circles in one spot.
I updated my code to account for this but there's too many pending edits on stack overflow for me to post it here yet—will do that when I'm able.
See edited answer; added code to save ArrayList of PVectors as a text file with comma separated values. Only unique x,y coordinates are saved to the ArrayList to avoid duplicates.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.