let cellStages = ['in', 'wait', 'out']
createCanvas(windowWidth, windowHeight);
function windowResized() {
resizeCanvas(windowWidth, windowHeight)
m = min(width, height) * 0.9
function mouseClicked() {
if (!lastTime) lastTime = t
addProgress = min(1, addProgress + delta / DUR.enter)
let progressEase = ease[EASE_ENTER](addProgress)
addIndex = round(progressEase * (cells.length - 1))
cellSize = ceil(cellSize)
translate((width - m) / 2, (height - m) / 2)
for (let i = 0; i <= min(addIndex, cells.length - 1); i++) {
let x = cell.nx * cellSize
let y = cell.ny * cellSize
let s = cell.s * cellSize
if (cell.stage === 'pre') {
cell.stage = cellStages[(cellStages.indexOf(cell.stage) + 1) % 3]
if (cell.stage === 'in') {
let dur = DUR[cell.stage]
cell.p = min(1, cell.p + delta / dur)
if (cell.style.includes('line')) {
if (cell.stage === 'wait') {
} else if (cell.stage === 'in') {
p = ease[EASE_IN](cell.p)
} else if (cell.stage === 'out') {
p = ease[EASE_OUT](1 - cell.p)
cellTransform(x, y, s, cell.rot, p)
celldr[cell.style](s, p, cell.color)
function createPattern() {
m = min(width, height) * 0.9
palette = shuffle([...random(palettes)])
styleOpts = shuffle(Object.keys(celldr))
seed = floor(random(1000))
let cellMap = Array.from({ length: SIDES * SIDES }, () => null)
while (i < cellMap.length) {
if (cellMap[i] !== null) {
let {x, y} = indexToPoint(i, SIDES)
let cell = { nx: x, ny: y, s: 1, rot: 0, stage: 'pre' }
cells = cells.sort((a, b) => {
let diff = a.ny + a.nx - (b.ny + b.nx)
function updateCell(cell, t) {
if (noiseIndex >= SIDES * SIDES) {
let n = noise(cell.nx * FREQ_SHAPE, cell.ny * FREQ_SHAPE, noiseIndexGrid)
let ns = floor(n * styleOpts.length * 2)
cell.style = styleOpts[ns % styleOpts.length]
let rotOptions = [0, PI * 0.5, PI, PI * 1.5]
let nr = floor((n + random(0.3)) * 4)
cell.rot = rotOptions[nr % rotOptions.length]
let nColor = noise(cell.nx * FREQ_COLOR, cell.ny * FREQ_COLOR, noiseIndexGrid)
nColor = floor(nColor * palette.length * 2)
cell.color = palette[nColor % palette.length]
function cellTransform(x, y, s, rot, p) {
translate(x + s / 2,y + s / 2)
translate(-s / 2, -s / 2)