März 5, 2012
As in loadPixel - 2nd example the picture is modified by the horizontal and vertical mouse movements. Additionally, an alpha parameter in the color allows color mixing/overlaying which creates a painting-like effect. The picture can be reset using the space bar.PImage img; int res =5; int res2 = res; int rectSize = 4; boolean flag = true; long timeStamp = 0; boolean onOff = false; int pause = 5; void setup() { background(255); rectMode(CENTER); size(640, 480); img = loadImage("ducks.jpg"); noStroke(); smooth(); } void draw() { loadPixels(); img.loadPixels(); //map resolution bewtween 1..80 from mouse res = (int)map(mouseX, 0, width, 1, 80); res2 = (int)map(mouseY, 0, height, 1, 80); if (millis() - timeStamp > pause) { // We must also call loadPixels() on the PImage since we are going to read its pixels. img.loadPixels(); for (int x = 0; x < img.width; x+=res2) { for (int y = 0; y < img.height; y+=res2) { // Calculate the 1D pixel location int loc = x + y*img.width; // Get the R,G,B values from image float r = red(img.pixels[loc]); float g = green(img.pixels[loc]); float b = blue(img.pixels[loc]); fill(r, g, b, 10); //draw a growing shape (size grows from 1 to 4 to 1) if (flag) rectSize++; if (!flag) rectSize--; if (rectSize>=res) flag = false; if (rectSize<=0) flag = true; rect(x, y, rectSize, rectSize); } } timeStamp = millis(); } } //reset with space bar void keyPressed() { if (key==32) { background(255); } }