xxxxxxxxxx
141
let img;
function preload() {
img = loadImage("lake-michigan.jpg");
}
function setup() {
createCanvas(img.width,img.height);
pixelDensity(1);
image(img,0,0);
// drawShadow(4,null);
loadPixels();
let w = 3;
// noStroke();
let t = false;
dither(null);
stroke(color(20,20,20,20));
for (let y = w; y < height-w; y+=w) {
for (let x = w; x < width-w; x+=w) {
let c = color(get(x, y));
c._array[3] = random(0.6,0.8);
fill(c);
square(x,y,random(w-1,w+1));
}
}
// dither(null);
noLoop();
}
function draw() {
// background(220);
}
function drawShadow(b, g) {
if (g == null) {
drawingContext.shadowOffsetX = 2;
drawingContext.shadowOffsetY = 2;
drawingContext.shadowBlur = b;
// if (bg._array[0] == 0 && bg._array[1] == 0 && bg._array[2] == 0)
// drawingContext.shadowColor = color(50);
//"black";
// else
drawingContext.shadowColor = color(0); //"black";
} else {
if (b == 0) {
g.drawingContext.shadowOffsetX = 0;
g.drawingContext.shadowOffsetY = 0;
g.drawingContext.shadowBlur = 0;
g.drawingContext.shadowColor = color(0); //"black";
} else {
g.drawingContext.shadowOffsetX = 3;
g.drawingContext.shadowOffsetY = 3;
g.drawingContext.shadowBlur = b;
g.drawingContext.shadowColor = color(0); //"black";
}
}
}
function index(g, x, y) {
if (g == null) return (x + y * width) * 4;
else return (x + y * g.width) * 4;
}
function dither(g) {
if (g != null) {
g.loadPixels();
for (let y = 0; y < height - 1; y++) {
for (let x = 1; x < width - 1; x++) {
let oldr = g.pixels[index(g, x, y)];
let oldg = g.pixels[index(g, x, y) + 1];
let oldb = g.pixels[index(g, x, y) + 2];
let factor = 1.0;
let newr = round((factor * oldr) / 255) * (255 / factor);
let newg = round((factor * oldg) / 255) * (255 / factor);
let newb = round((factor * oldb) / 255) * (255 / factor);
g.pixels[index(g, x, y)] = newr;
g.pixels[index(g, x, y) + 1] = newg;
g.pixels[index(g, x, y) + 2] = newb;
g.pixels[index(g, x + 1, y)] += ((oldr - newr) * 7) / 16.0;
g.pixels[index(g, x + 1, y) + 1] += ((oldr - newr) * 7) / 16.0;
g.pixels[index(g, x + 1, y) + 2] += ((oldr - newr) * 7) / 16.0;
g.pixels[index(g, x - 1, y + 1)] += ((oldr - newr) * 3) / 16.0;
g.pixels[index(g, x - 1, y + 1) + 1] += ((oldr - newr) * 3) / 16.0;
g.pixels[index(g, x - 1, y + 1) + 2] += ((oldr - newr) * 3) / 16.0;
g.pixels[index(g, x, y + 1)] += ((oldr - newr) * 5) / 16.0;
g.pixels[index(g, x, y + 1) + 1] += ((oldr - newr) * 5) / 16.0;
g.pixels[index(g, x, y + 1) + 2] += ((oldr - newr) * 5) / 16.0;
g.pixels[index(g, x + 1, y + 1)] += ((oldr - newr) * 1) / 16.0;
g.pixels[index(g, x + 1, y + 1) + 1] += ((oldr - newr) * 1) / 16.0;
g.pixels[index(g, x + 1, y + 1) + 2] += ((oldr - newr) * 1) / 16.0;
}
}
g.updatePixels();
} else {
g = null;
loadPixels();
for (let y = 0; y < height - 1; y++) {
for (let x = 1; x < width - 1; x++) {
let oldr = pixels[index(g, x, y)];
let oldg = pixels[index(g, x, y) + 1];
let oldb = pixels[index(g, x, y) + 2];
let factor = 1.0;
let newr = round((factor * oldr) / 255) * (255 / factor);
let newg = round((factor * oldg) / 255) * (255 / factor);
let newb = round((factor * oldb) / 255) * (255 / factor);
pixels[index(g, x, y)] = newr;
pixels[index(g, x, y) + 1] = newg;
pixels[index(g, x, y) + 2] = newb;
pixels[index(g, x + 1, y)] += ((oldr - newr) * 7) / 16.0;
pixels[index(g, x + 1, y) + 1] += ((oldr - newr) * 7) / 16.0;
pixels[index(g, x + 1, y) + 2] += ((oldr - newr) * 7) / 16.0;
pixels[index(g, x - 1, y + 1)] += ((oldr - newr) * 3) / 16.0;
pixels[index(g, x - 1, y + 1) + 1] += ((oldr - newr) * 3) / 16.0;
pixels[index(g, x - 1, y + 1) + 2] += ((oldr - newr) * 3) / 16.0;
pixels[index(g, x, y + 1)] += ((oldr - newr) * 5) / 16.0;
pixels[index(g, x, y + 1) + 1] += ((oldr - newr) * 5) / 16.0;
pixels[index(g, x, y + 1) + 2] += ((oldr - newr) * 5) / 16.0;
pixels[index(g, x + 1, y + 1)] += ((oldr - newr) * 1) / 16.0;
pixels[index(g, x + 1, y + 1) + 1] += ((oldr - newr) * 1) / 16.0;
pixels[index(g, x + 1, y + 1) + 2] += ((oldr - newr) * 1) / 16.0;
}
}
updatePixels();
}
}