Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active December 1, 2017 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/292d1b51db7d6b15fb509648e8448ade to your computer and use it in GitHub Desktop.
Save KrabCode/292d1b51db7d6b15fb509648e8448ade to your computer and use it in GitHub Desktop.
pixelated screensaver sand hourglass sketch you can control
import java.awt.*;
private PImage orig;
private int effScl = 3; //effect scale used heaping pixels into groups to ease processor's burden
private float yForce = 0.95f; //values from 0 to 1
private float xForce = 0.95f; //values from 0 to 1
public void settings()
{
fullScreen();
orig = screenshot();
}
public void setup(){
image(orig, 0, 0,width, height);
frameRate(30);
}
public void draw()
{
checkInput();
modifyPixels();
//println(frameRate);
}
private void modifyPixels(){
loadPixels();
for(int x = 0; x < width/ effScl; x++) {
for (int y = 0; y < height/ effScl; y++) {
int force = getForce();
//for each virtual square on my screen with side length "effScl"
for(int xOff = 0; xOff < effScl; xOff++){
for(int yOff = 0; yOff < effScl; yOff++){
if(force == 0) shiftU(x*effScl + xOff, y*effScl + yOff);
if(force == 1) shiftD(x*effScl + xOff, y*effScl + yOff);
if(force == 2) shiftL(x*effScl + xOff, y*effScl + yOff);
if(force == 3) shiftR(x*effScl + xOff, y*effScl + yOff);
}
}
}
}
updatePixels();
}
int getForce(){
int force;
if(coinFlip(0.5f)){
if(coinFlip(yForce)) force = 0;
else force = 1;
}else{
if(coinFlip(xForce)) force = 2;
else force = 3;
}
return force;
}
public void checkInput(){
xForce = map(mouseX,0, width, 0.01f, .99f);
yForce = map(mouseY,0, height, 0.01f, .99f);
}
public void keyPressed(){
if(key == 'm') effScl++;
if(key == 'n' && effScl > 1) effScl--;
}
private void shiftU(int x, int y){
if(getPixel(x, y) > getPixel(x, y+1)){
swapPixels(x, y, x, y+1);
}
}
private void shiftD(int x, int y){
if(getPixel(x, y) > getPixel(x, y-1)){
swapPixels(x, y, x, y-1);
}
}
private void shiftR(int x, int y){
if(getPixel(x, y) > getPixel(x-1, y)){
swapPixels(x, y, x-1, y);
}
}
private void shiftL(int x, int y){
if(getPixel(x, y) > getPixel(x+1, y)){
swapPixels(x, y, x+1, y);
}
}
private boolean coinFlip(float odds){
return random(1) < odds;
}
private void swapPixels(int x0, int y0, int x1, int y1){
//boundary check
if(x0 < width && x0 >= 0 && y0 < height && y0 >= 0
&& x1 < width && x1 >= 0 && y1 < height && y1 >= 0){
int rem = getPixel(x0, y0); // remember 0
setPixel(x0, y0, getPixel(x1, y1)); // overwrite 0
setPixel(x1, y1, rem); // overwrite 1
}
}
private int getPixel(int x, int y){
int index = x + y * width;
if(index < pixels.length && index >= 0){
return pixels[index];
}
return 0;
}
private void setPixel(int x, int y, int clr){
int index = x + y * width;
if(index < pixels.length && index > 0){
pixels[x + y * width] = clr;
}
}
private PImage screenshot() {
PImage screenshot = null;
try {
screenshot = new PImage(new Robot().createScreenCapture(
new Rectangle(0, 0, displayWidth, displayHeight)));
} catch (AWTException e) {
e.printStackTrace();
}
return screenshot;
}
@KrabCode
Copy link
Author

KrabCode commented Dec 1, 2017

twk2ndk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment