xxxxxxxxxx
PImage gradient;
float x;
float y;
float px;
float py;
float c;
float easing = 0.01;
float diameter = 12;
boolean controlDown = false;
boolean shiftDown = false;
Undo undo;
void setup() {
fullScreen();
background (#f2f6f9);
gradient = loadImage("gradient.jpg");
gradient.resize(0, 1000);
undo = new Undo(10);
smooth();
fill(0);
textSize(20);
text("Ctrl Z to undo", 10, 30);
}
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 (x,y,px,py);
strokeWeight(2);
noFill();
color c = gradient.get(int(x), int(y));
stroke(c);
ellipse(x,y,radius+abs(px-x),radius+abs(py-y));
ellipse(width-x,y,radius+abs(px-x),radius+abs(py-y));
ellipse(x,height-y,radius+abs(px-x),radius+abs(py-y));
ellipse(width-x,height-y,radius+abs(px-x),radius+abs(py-y));
}
}
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);
}
}