xxxxxxxxxx
// By Roni Kaufman
// https://ronikaufman.github.io/
const N_DIVS = 48; // number of divisions in each hexagon side
const N = 6*N_DIVS; // total number of points per hexagon
let hexagons = [];
let hu = 0;
function setup() {
createCanvas(1112, 834);
colorMode(HSB, 255);
noFill();
strokeWeight(4);
strokeCap(SQUARE);
for (let r = 400; r > 0; r -= 15) {
hexagons.push(makeHexPoints(width/2, height/2, r));
}
}
function draw() {
background(255);
let off = floor(frameCount);
for (let i = 0; i < hexagons.length; i++) {
let hexa = hexagons[i];
stroke((hu + i/hexagons.length*255)%255, 255, 255);
traceLine(hexa, (i*N_DIVS), ((i+4)*N_DIVS), -off);
}
hu+=.3;
}
function makeHexPoints(x, y, r) {
let points = [];
for (let theta = 0; theta < TWO_PI; theta += PI/3) {
let x1 = x + r*cos(theta);
let y1 = y + r*sin(theta);
let x2 = x + r*cos(theta+PI/3);
let y2 = y + r*sin(theta+PI/3);
for (let t = 0; t < 1; t += 1/N_DIVS) {
points.push(
createVector(
x1*(1-t) + x2*t,
y1*(1-t) + y2*t
)
);
}
}
return points;
}
function traceLine(points, iMin, iMax, offset) {
beginShape();
for (let i = iMin; i < iMax; i++) {
let idx = (i + offset)%N;
if (idx < 0) {
idx += N;
}
let p = points[idx];
vertex(p.x, p.y);
}
endShape();
}
//save JPG
let lapse = 0; // mouse timer
function mousePressed(){
// prevents mouse press from registering twice
if (millis() - lapse > 400){
save("img_" + month() + '-' + day() + '_' + hour() + '-' + minute() + '-' + second() + ".jpg");
lapse = millis();
}
}