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."); } }