xxxxxxxxxx
101
// initialize arrays to hold vectors, movers, and rings
let vectors = [];
let movers = [];
let rings = [];
// set the scale for the grid
let scl = 20;
let rows, cols;
// initialize an offset for noise
let offset = 0;
function setup() {
createCanvas(1000, 600);
// calculate the number of rows and columns based on canvas size and scale
rows = floor(width / scl);
cols = floor(height / scl);
// create an array of mover objects
for (let i = 0; i < 6000; i++) {
movers[i] = new Mover(random(width), random(height), random(0.1, 0.15));
}
// set pixel density to 1 for consistency
pixelDensity(1);
// create an array of ring objects
for (let i = 0; i < 10; i++) {
rings[i] = new Rings(random(width), random(height));
}
}
function draw() {
background(10, 5);
// draw rings, movers, and surface
drawRings();
drawMovers();
drawSurface();
// increment the offset for noise to animate the scene
offset += 0.03;
}
function drawRings() {
// update and display each ring object
for (let ring of rings) {
ring.update();
ring.display();
}
}
function drawSurface() {
// create a noisy surface using Perlin noise
let y = 0;
loadPixels();
for (let i = 0; i < height; i++) {
let x = 0;
for (let j = 0; j < width; j += 4) {
let index = (j + i * width) * 4;
let r = noise(x, y, offset / 3) * 255;
// set pixel colors with some randomness
pixels[index + 0] = 242;
pixels[index + 1] = 191;
pixels[index + 2] = 9;
pixels[index + 3] = r;
x += 0.1;
}
y += 0.02;
}
updatePixels();
}
function drawMovers() {
// generate a grid of vectors influenced by Perlin noise
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let index = i + j * cols;
let angle = noise(i * 0.1, j * 0.1, offset / 4) * TWO_PI;
let v = p5.Vector.fromAngle(angle);
v.setMag(0.1);
vectors[index] = v;
}
}
// update, display, and apply edge checks for each mover object
for (let mover of movers) {
mover.update();
mover.display();
mover.checkEdges();
mover.follow(vectors);
}
}