xxxxxxxxxx
110
let bg = 0;
let stk = 255 - bg;
let a = 0;
let spacing = 100;
let xval;
let yval;
let points;
let hexes;
function setup() {
createCanvas(windowWidth, windowHeight);
// createCanvas(1080,1080)
spacing = width / 10;
a = TWO_PI / 6;
genPoints();
hexes = [];
for (let i = 0; i < points.length; i++) {
let h = new hex(points[i].x, points[i].y, spacing / 2, 2, random(255));
hexes.push(h);
}
}
function draw() {
background(bg);
for (let i = 0; i < hexes.length; i++) {
hexes[i].display();
}
sign();
}
class coord {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class hex {
constructor(x, y, r, n, c) {
this.x = x;
this.y = y;
this.r = r;
this.n = n;
this.c = c;
}
display() {
push();
stroke(this.c);
strokeWeight(this.r / 10);
noFill();
for (let i = 0; i < this.n; i++) {
hexagon(this.x, this.y, this.r - this.r / 2 / (this.n - i));
}
pop();
}
}
function hexagon(x, y, r) {
beginShape();
for (let i = PI / 2; i < (5 * PI) / 2; i += a) {
vertex(x + r * cos(i), y + r * sin(i));
}
endShape();
}
function genPoints() {
// clear x + y values
yval = [];
xval = [];
// generate x + y values
for (let iy = 0; iy < height + spacing; iy += spacing * sin(a)) {
yval.push(iy);
}
for (let ix = 0; ix < width + spacing; ix += spacing / 2) {
xval.push(ix);
}
points = [];
for (let yi = 0; yi < yval.length + 2; yi += 2) {
let y = 0;
for (let i = 0; i < xval.length + 2; i += 2) {
y = yval[yi];
let p = new coord(xval[i], y);
points.push(p);
}
for (let i = 1; i < xval.length + 2; i += 2) {
y = yval[yi + 1];
let p = new coord(xval[i], y);
points.push(p);
}
}
}
function mousePressed() {
if (mouseButton != RIGHT) {
for (let i = 0; i < hexes.length; i++) {
hexes[i].c = random(255);
}
}
}
function sign() {
push();
let t = "jesse toohey";
textSize(spacing / 4);
textAlign(RIGHT, BOTTOM);
noStroke();
fill(stk);
text(t, width - spacing / 8, height - spacing / 8);
pop();
}