Last active
November 26, 2017 12:06
-
-
Save KrabCode/1e6a9f4892c71909c6ae243ec09c452f to your computer and use it in GitHub Desktop.
sketch_christmas_screensaver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import processing.core.PApplet; | |
| import processing.core.PImage; | |
| import java.awt.*; | |
| PImage orig; | |
| int scaledown = 1; //only used for huge pictures | |
| int effectScale = 2; | |
| float gravity = 0.5f; //values from 0 to 1 | |
| int screenWidth = 1920; | |
| int screenHeight = 1080; | |
| public void settings() | |
| { | |
| orig = screenshot(); | |
| //orig = loadImage("C:\\Projects\\Sand\\OyL9Ipc.jpg"); // remember to set the scaledown if uncommented | |
| if(orig.width == screenWidth && orig.height == screenHeight){ | |
| fullScreen(); | |
| scaledown = 1; | |
| }else{ | |
| size(orig.width/ scaledown, orig.height/ scaledown); | |
| } | |
| } | |
| public void setup(){ | |
| image(orig, 0, 0,orig.width/scaledown, orig.height/scaledown); | |
| } | |
| public void draw() | |
| { | |
| loadPixels(); | |
| modifyPixels(); | |
| updatePixels(); | |
| checkInput(); | |
| } | |
| private void modifyPixels(){ | |
| for(int x = 0; x < width/effectScale; x++) { | |
| for (int y = 0; y < height/effectScale; y++) { | |
| //for each virtual square on my screen with side length "pixelSize" | |
| if(coinFlip()){ | |
| for(int xoff = 0; xoff < effectScale; xoff++){ | |
| for(int yoff = 0; yoff < effectScale; yoff++){ | |
| shiftUpwards(x*effectScale+xoff, y*effectScale+yoff); | |
| } | |
| } | |
| }else{ | |
| for(int xoff = 0; xoff < effectScale; xoff++){ | |
| for(int yoff = 0; yoff < effectScale; yoff++){ | |
| shiftLeft(x*effectScale+xoff, y*effectScale+yoff); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| private void shiftUpwards(int x, int y){ | |
| if(getPixel(x, y) > getPixel(x+1, y)){ | |
| swapPixels(x, y, x, y+1); | |
| } | |
| } | |
| private void shiftLeft(int x, int y){ | |
| if(getPixel(x, y) > getPixel(x, y+1)){ | |
| swapPixels(x, y, x+1, y); | |
| } | |
| } | |
| private boolean coinFlip(){ | |
| return random(1)<gravity; | |
| } | |
| private int getPixel(int x, int y){ | |
| int index = x + y * width; | |
| if(index < pixels.length){ | |
| return pixels[index]; | |
| } | |
| return color(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 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 a = getPixel(x0, y0); | |
| int b = getPixel(x1, y1); | |
| setPixel(x0, y0, b); | |
| setPixel(x1, y1, a); | |
| } | |
| } | |
| 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; | |
| } | |
| public void checkInput(){ | |
| frame.requestFocus(); | |
| if(keyPressed){ | |
| exit(); | |
| } | |
| gravity = map(mouseX,0, width, 0.01f, .99f); | |
| //pixelSize = round(map(mouseY, 0, height, 2, 6)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment