Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>Please wait while I get creative…</p>

              
            
!

CSS

              
                html, body {
	height: 100%;
}

html {
	background: #e0dee5;
}

body {
	margin: 0;
	display: grid;
	place-items: center;
	font-family: system-ui, sans-serif;
	color: rgb(23, 14, 12);
}

p {
	text-wrap: balance;
}

canvas {
	max-height: 90dvmin;
	max-width: 90dvmin;
}

              
            
!

JS

              
                window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;

// Configuration.
const WIDTH = 801;
const HEIGHT = 801;
const N_AGENTS = 30000;
const FILL_COLOR = [23, 14, 12];
const DIRECTION_WEIGHT = 8;
const FILLED_NEIGHBOR_WEIGHT = 20000;
const EMPTY_NEIGHBOR_WEIGHT = 1;

// Derived.
const nPx = WIDTH * HEIGHT;
const cX = Math.floor(WIDTH / 2);
const cY = Math.floor(HEIGHT / 2);
const filledPx = new Uint8Array(nPx);

// Utility functions.
function xy(x, y) {
	return x + WIDTH * y;
}
function normalize(vector) {
	const length = Math.hypot(...vector);
	if (length === 0) return vector;
	return vector.map((component) => component / length);
}
const sumArrays = (a, b) => a.map((x, i) => x + b[i]);
const multiplyArrays = (a, b) => a.map((x, i) => x * b[i]);
function weightedRandomIdx(weights) {
	const { length } = weights;
	const cumulativeWeights = new Array(length);
	const totalWeight = weights.reduce((acc, weight, i) => {
		const sum = acc + weight;
		cumulativeWeights[i] = sum;
		return sum;
	}, 0);

	const random = Math.random() * totalWeight;

	// Use binary search to find the index.
	let low = 0,
		high = length - 1;
	while (low < high) {
		const mid = Math.floor((low + high) / 2);
		if (cumulativeWeights[mid] < random) {
			low = mid + 1;
		} else {
			high = mid;
		}
	}
	return low;
}

// XY, 0 -> 2π
const directions = [
	[1, 0],
	[1, 1],
	[0, 1],
	[-1, 1],
	[-1, 0],
	[-1, -1],
	[0, -1],
	[1, -1]
];
const normalizedDirections = directions.map(normalize);

function placeAgent(x, y, agentDirection) {
	let age = 1;
	let prevLocation = 0;
	do {
		if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return false;
		if (!filledPx[xy(x, y)]) {
			filledPx[xy(x, y)] = 1;
			return true;
		}

		// Prefer moving in the agent’s own direction.
		const directionWeights = normalizedDirections.map(
			(direction) =>
				(Math.pow(Math.hypot(...sumArrays(direction, agentDirection)), 6) *
					DIRECTION_WEIGHT) /
				2
		);
		// Prefer moving to a filled pixel initially, but prefer open space over time.
		const neighborStates = directions.map(
			([dX, dY]) => filledPx[xy(x + dX, y + dY)]
		);
		const preferFilled =
			neighborStates.filter(Boolean).length >= 1 + Math.floor(age / 64);
		const neighborWeights = neighborStates.map((state) =>
			state == preferFilled ? FILLED_NEIGHBOR_WEIGHT : EMPTY_NEIGHBOR_WEIGHT
		);

		const idx = weightedRandomIdx(
			multiplyArrays(directionWeights, neighborWeights)
		);
		const nextStep = directions[idx];
		const nextLocation = [x + nextStep[0], y + nextStep[1]];
		const nextLocationXY = xy(...nextLocation);
		if (prevLocation !== nextLocationXY) {
			prevLocation = nextLocationXY;
			[x, y] = nextLocation;
		}
		++age;
	} while (true);
}

const maxAgentGroupSize = Math.ceil(N_AGENTS / 60);
let nPlacedAgents = 0;
function placeAgents() {
	const nAgents = Math.min(maxAgentGroupSize, N_AGENTS - nPlacedAgents);
	for (let i = 0; i < nAgents; ++i) {
		const angle = Math.random() * Math.PI * 2;
		const direction = [Math.cos(angle), Math.sin(angle)];
		placeAgent(cX, cY, direction);
	}
	nPlacedAgents += nAgents;
	if (nPlacedAgents < N_AGENTS) requestAnimationFrame(placeAgents);
	else drawImage();
}

function drawImage() {
	const canvas = document.createElement("canvas");
	const ctx = canvas.getContext("2d");
	canvas.width = WIDTH;
	canvas.height = HEIGHT;

	const imageData = ctx.createImageData(WIDTH, HEIGHT);
	const dataRGBA = imageData.data;

	for (let i = 0; i < nPx; ++i) {
		dataRGBA[i * 4 + 0] = FILL_COLOR[0];
		dataRGBA[i * 4 + 1] = FILL_COLOR[1];
		dataRGBA[i * 4 + 2] = FILL_COLOR[2];
		dataRGBA[i * 4 + 3] = filledPx[i] * 255;
	}

	ctx.putImageData(imageData, 0, 0);
	document.querySelector("p").remove();
	document.body.append(canvas);
}

placeAgents();

              
            
!
999px

Console