--- import { createHash } from "crypto"; type Grid = number[][]; function hashStringToNumber(str: string): number { let hash = 0; for (let i = 0; i < str.length; i++) { const charCode = str.charCodeAt(i); hash = (hash << 5) - hash + charCode; hash |= 0; } return hash; } function seedRandom(seed: number): () => number { return function () { const x = Math.sin(seed++) * 10000; return x - Math.floor(x); }; } function createEmptyGrid(size: number): Grid { return Array(size) .fill(0) .map(() => Array(size).fill(0)); } function randomFill(grid: Grid, rand: () => number): Grid { for (let y = 0; y < grid.length; y++) { for (let x = 0; x < grid[y].length; x++) { if (rand() > 0.95) { grid[y][x] = 1; } } } return grid; } function drawUsingTrig( grid: Grid, frequency: number, amplitude: number, phase: number, trigFunction: (x: number) => number, ): Grid { const height = grid.length; const width = grid[0].length; for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const value = trigFunction((x / width) * frequency + phase) * amplitude; if (y / height < value + 0.5 && y / height > 0.5 - value) { grid[y][x] = 1; } } } return grid; } function randomSubtract(grid: Grid, rand: () => number): Grid { for (let y = 0; y < grid.length; y++) { for (let x = 0; x < grid[y].length; x++) { if (rand() < 0.2 && grid[y][x] !== 0) { grid[y][x] = 0; } } } return grid; } function rotateGrid(grid: Grid): Grid { const size = grid.length; const newGrid: Grid = []; for (let i = 0; i < size; i++) { newGrid.push([]); for (let j = 0; j < size; j++) { newGrid[i][j] = grid[size - j - 1][i]; } } return newGrid; } function getFragmentID(slug: string): { grid: Grid; size: number } { const seed = createHash("sha256").update(slug).digest("hex"); const size = 32; let grid = createEmptyGrid(size); const rand = seedRandom(hashStringToNumber(seed)); grid = randomFill(grid, rand); grid = drawUsingTrig(grid, 6, 0.3, 1.3, Math.cos); grid = rotateGrid(grid); grid = randomSubtract(grid, rand); return { grid, size }; } --- { Astro.props.slug && getFragmentID(Astro.props.slug).grid.flatMap((row, y) => row.map((color, x) => ( )), ) }