xxxxxxxxxx
PImage gradient;
float x;
float y;
float px;
float py;
float easing = 0.01;
float diameter = 12;
boolean controlDown = false;
boolean shiftDown = false;
Undo undo;
void setup() {
fullScreen();
background (255);
undo = new Undo(10);
smooth();
textSize(20);
fill(0);
text("Ctrl+Z to undo", 12, 30);
gradient = loadImage("gradient.jpg");
gradient.resize(width,height);
}
void draw() {
if (mousePressed) {
fill(0, 0, 0);
float targetX = mouseX;
x += (targetX - x) * easing;
float targetY = mouseY;
y += (targetY - y) * easing;
float radius = dist(0.1 * x, 0.1 * y, 0.1 * px, 0.1 * py);
strokeWeight(2);
noFill();
color c = gradient.get(int(x), int(y));
stroke(c);
ellipse(x, y, radius/2, radius/2);
ellipse(width - x, y, radius/2, radius/2);
ellipse(x, height-y, radius/2, radius/2);
ellipse(width - x, height-y, radius/2, radius/2);
}
}
void mousePressed() {
println("drawing");
py=mouseY;
px=mouseX;
y=mouseY;
x=mouseX;
}
void mouseReleased() {
undo.takeSnapshot();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == CONTROL)
controlDown = true;
if (keyCode == SHIFT)
shiftDown = true;
return;
}
if (controlDown) {
if (keyCode == 'Z') {
if (shiftDown)
undo.redo();
else
undo.undo();
}
return;
}
if (key=='s') {
saveFrame("image####.png");
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == CONTROL)
controlDown = false;
if (keyCode == SHIFT)
shiftDown = false;
}
}
class Undo {
int undoSteps=0, redoSteps=0;
CircImgCollection images;
Undo(int levels) {
images = new CircImgCollection(levels);
}
public void takeSnapshot() {
undoSteps = min(undoSteps+1, images.amount-1);
redoSteps = 0;
images.next();
images.capture();
}
public void undo() {
if (undoSteps > 0) {
undoSteps--;
redoSteps++;
images.prev();
images.show();
}
}
public void redo() {
if (redoSteps > 0) {
undoSteps++;
redoSteps--;
images.next();
images.show();
}
}
}
class CircImgCollection {
int amount, current;
PImage[] img;
CircImgCollection(int amountOfImages) {
amount = amountOfImages;
// Initialize all images as copies of the current display
img = new PImage[amount];
for (int i=0; i<amount; i++) {
img[i] = createImage(width, height, RGB);
img[i] = get();
}
}
void next() {
current = (current + 1) % amount;
}
void prev() {
current = (current - 1 + amount) % amount;
}
void capture() {
img[current] = get();
}
void show() {
image(img[current], 0, 0);
}
}