xxxxxxxxxx
202
let circles;
let g;
// dither a bunch of circles at full res and scale?
let gs;
function setup() {
createCanvas(1000, 1000);
frameRate(20);
circles = [];
circles.push({ r: 0, col: random(255) });
g = createGraphics(width, height);
g.noStroke();
g.fill(180);
let off = width * 0.05;
g.rect(0,0,width,off);
g.rect(0,0,off,height);
g.rect(0,height-off,width,height-off);
g.rect(width-off,0,off,height);
dither(g);
gs = [];
for (let i = 0; i < 10; i++) {
let _g = createGraphics(width,height);
_g.fill(random(255));
// _g.background(random(255));
_g.circle(width>>1,height>>1,width);
dither(_g);
gs.push({g:_g, r:random(1,width-1)});
}
gs[0].r = width-1;
}
function draw() {
background(220);
translate(width>>1,height>>1);
// translate(width / 2, height / 2);
for (let g of gs) {
image(g.g,-g.r>>1,-g.r>>1,g.r,g.r);
g.r += random(1,10);
if (g.r > width) g.r = 1;
}
// for (let c of circles) {
// fill(c.col);
// circle(0, 0, c.r);
// c.r += random(1, 3);
// if (c.r > width + width/2) c.r = 0;
// }
// if (random() > 0.99 && circles.length < 10) {
// circles.push({ r: 0, col: random(255) });
// }
image(g,-width>>1,-height>>1);
}
function index(g, x, y) {
if (g == null) return (x + y * width) * 4;
else return (x + y * g.width) * 4;
}
function DivideBy255(value) {
return (value + 1 + (value >> 8)) >> 8;
}
function dither(g) {
let referenceSize = 1000;
let hasMaxSize = false;
if (g == null) {
let _scale = Math.ceil(1, map(width, 0, referenceSize, 0, 1, hasMaxSize));
loadPixels();
for (let y = 0; y < height - _scale; y++) {
for (let x = _scale; x < width - _scale; 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 newr = (DivideBy255(oldr) * 255) | 0;
let newg = (DivideBy255(oldg) * 255) | 0;
let newb = (DivideBy255(oldb) * 255) | 0;
pixels[index(g, x, y)] = newr;
pixels[index(g, x, y) + 1] = newg;
pixels[index(g, x, y) + 2] = newb;
for (let _y = 1; _y <= _scale; _y++) {
for (let _x = 1; _x <= _scale; _x++) {
pixels[index(g, x + _x, y)] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x + _x, y) + 1] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x + _x, y) + 2] += ((oldr - newr) * 7) >> 4;
pixels[index(g, x - _x, y + _y)] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x - _x, y + _y) + 1] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x - _x, y + _y) + 2] += ((oldr - newr) * 3) >> 4;
pixels[index(g, x, y + _y)] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x, y + _y) + 1] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x, y + _y) + 2] += ((oldr - newr) * 5) >> 4;
pixels[index(g, x + _x, y + _y)] += ((oldr - newr) * 1) >> 4;
pixels[index(g, x + _x, y + _y) + 1] += ((oldr - newr) * 1) >> 4;
pixels[index(g, x + _x, y + _y) + 2] += ((oldr - newr) * 1) >> 4;
}
}
}
}
updatePixels();
} else {
let _scale = Math.ceil(1, map(g.width, 0, referenceSize, 0, 1, hasMaxSize));
g.loadPixels();
for (let y = 0; y < g.height - _scale; y++) {
for (let x = _scale; x < g.width - _scale; 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 newr = (DivideBy255(oldr) * 255) | 0;
let newg = (DivideBy255(oldg) * 255) | 0;
let newb = (DivideBy255(oldb) * 255) | 0;
g.pixels[index(g, x, y)] = newr;
g.pixels[index(g, x, y) + 1] = newg;
g.pixels[index(g, x, y) + 2] = newb;
for (let _y = 1; _y <= _scale; _y++) {
for (let _x = 1; _x <= _scale; _x++) {
g.pixels[index(g, x + _x, y)] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x + _x, y) + 1] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x + _x, y) + 2] += ((oldr - newr) * 7) >> 4;
g.pixels[index(g, x - _x, y + _y)] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x - _x, y + _y) + 1] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x - _x, y + _y) + 2] += ((oldr - newr) * 3) >> 4;
g.pixels[index(g, x, y + _y)] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x, y + _y) + 1] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x, y + _y) + 2] += ((oldr - newr) * 5) >> 4;
g.pixels[index(g, x + _x, y + _y)] += ((oldr - newr) * 1) >> 4;
g.pixels[index(g, x + _x, y + _y) + 1] += ((oldr - newr) * 1) >> 4;
g.pixels[index(g, x + _x, y + _y) + 2] += ((oldr - newr) * 1) >> 4;
}
}
}
}
g.updatePixels();
}
}
// p5
function setGradient(x, y, w, h, c1, c2, axis, g = null) {
if (g == null) {
noFill();
if (axis === Y_AXIS) {
// Top to bottom gradient
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
} else if (axis === X_AXIS) {
// Left to right gradient
for (let i = x; i <= x + w; i++) {
let inter = map(i, x, x + w, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(i, y, i, y + h);
}
}
} else {
g.noFill();
if (axis === Y_AXIS) {
// Top to bottom gradient
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
g.stroke(c);
g.line(x, i, x + w, i);
}
} else if (axis === X_AXIS) {
// Left to right gradient
for (let i = x; i <= x + w; i++) {
let inter = map(i, x, x + w, 0, 1);
let c = lerpColor(c1, c2, inter);
g.stroke(c);
g.line(i, y, i, y + h);
}
}
}
}