// PGraphics save sample PGraphics pg; PImage img; PImage bg; int scrw; int scrh; float ang = 0; boolean exitfg = false; boolean saveframe_enable; int sframecount; void setup() { size(512, 288, P2D); scrw = width; scrh = height; println("size=" + scrw + "," + scrh); frameRate(24); img = loadImage("./chara.png"); bg = loadImage("./bg.png"); pg= createGraphics(scrw, scrh, P2D); exitfg = false; saveframe_enable = false; sframecount = 0; } void draw() { background(40, 80, 160); //draw to PGraphics pg.beginDraw(); pg.clear(); pg.push(); pg.imageMode(CENTER); float x =(scrw / 2) * 0.9 * sin(radians(ang * 2.0)) +(scrw / 2); float y =(scrh / 2) * 0.9 * sin(radians(ang * 5.0)) +(scrh / 2); pg.translate(x, y); pg.rotate(radians(ang)); pg.image(img, 0, 0); pg.pop(); pg.endDraw(); // draw Background image(bg, 0, 0); // draw PGraphics to canvas image(pg, 0, 0); if (saveframe_enable) { String fn = "frames/" + get_seq(sframecount, 8) + ".png"; noStroke(); fill(255); textSize(24); text("capture " + sframecount + " frame", 8, 24); text("save " + fn, 8, 48); pg.save(fn); sframecount++; } ang += 1.0; if (exitfg) { exit(); } } void keyTyped() { if (key == 's') { // frame save enable / disable saveframe_enable = !saveframe_enable; if (saveframe_enable) sframecount = 0; } else if (key == 'q') { // set exit exitfg = true; } } String get_seq(int n, int k) { String s = "000000000000" + n; return s.substring(s.length() - k); }