let buffer;
function setup() {
let canvas = createCanvas(windowWidth, windowHeight);
background(100);
buffer = createGraphics(width, height);
let saveButton = createButton("save");
saveButton.position(20, 20);
saveButton.mouseClicked(() => {
// Copy from canvas into buffer
buffer.copy(
// source
canvas,
// source x, y, w, h
0, 0, width, height,
// destination x, y, w, h
0, 0, buffer.width, buffer.height)
});
let restoreButton = createButton("restore");
restoreButton.position(80, 20);
restoreButton.mouseClicked(() => {
// Copy from buffer into the canvas
copy(buffer, 0, 0, buffer.width, buffer.height, 0, 0, width, height);
});
}
function draw() {
circle(mouseX, mouseY, 20);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>