ICM

ICM

Reorganised an existing sketch for this week and cleaned it up.

image
let r;
let g;
let b;
let a;

// Constants
let BOKEH_COUNT = 500;
let BOKEH_MAX_SIZE = 150;
let MOVE_COUNT;
let MOUSE_MOVE_COUNT;

// Variables
let SWATCH;
let bgColor;
let backgroundPoints = [];
let chosenSwatch;

let toMove;
let toMouseMove;
let lastCheckpoint;

function getSwatches() {
    return [
        [color(255, 193, 94), random(240, 255), color(247, 176, 91, random(240, 255)), color(247, 147, 76, random(240, 255)), color(204, 88, 3, random(240, 255)), color(31, 19, 0, random(240, 255))],
    ]
}

function setup() {
    // Setup swatch
    SWATCH = getSwatches();

    lastCheckpoint = millis();
    MOVE_COUNT = round(BOKEH_COUNT * 0.25, 10);
    MOUSE_MOVE_COUNT = round(BOKEH_COUNT * 0.15, 10);

    // Global settings
    createCanvas(1280, 1080);
    noStroke();

    // Set variables
    bgColor = color(0, 0, 0, 40);
    chosenSwatch = random(SWATCH);

    // Generate points in the background randomly.
    backgroundPoints = _.times(BOKEH_COUNT).map((item => {
        return newPoint()
    }))

    background(0);
}

function newPoint() {
    return {
        x: random(width),
        px: random(1),
        dirX: 1,
        y: random(height),
        py: random(1),
        dirY: 1,
        size: random(0, Math.sqrt(random(BOKEH_MAX_SIZE))),
        color: random(chosenSwatch),
    }
}

function drawPoints() {
    backgroundPoints.forEach(point => {
        noStroke();
        fill(point.color);
        ellipse(point.x, point.y, point.size);
    })
}

function updatePoints() {
    backgroundPoints = backgroundPoints.map(point => {
        const flip = random(0, 100);
        // 10% chance of something moving
        if(flip <= 3) {
            return Object.assign({}, point, {
                x: (point.x + random(-15, 15)) % width,
                y: (point.y + random(-15, 15)) % height,
            })
        } else 
            return point
    })
}

function draw() {
    background(bgColor);

    updatePoints();

    drawPoints();
}

// For Debugging: Converts color to string
function colorToString(color) {
    return hex(color.levels.slice(0, 3), 2).join("");
}