cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              body
	background: #222

canvas
	position: absolute
	top: 50%
	left: 50%
	transform: translate3d(-50%, -50%, 0)
	border-radius: 30px
	box-shadow: 0 0 24px 8px rgba(0, 0, 0, 0.2)
	cursor: pointer

            
          
!
            
              const PX = 600;
const FLOORS = 8;
const ITERATIONS = 1;
const FG = [102, 199, 244];
const BG = [34, 34, 34];
const HOURS = 6;
const HOUR_OFFSET = 0.5;
const SPACING = 110;
const LINE_WIDTH = 40;

const DEBUG_SMALL = false;
const DEBUG_HOURS = [];
const DEBUG_NO_CURVES = false;

const {abs, cos, floor, PI, round, random, sign, sin, sqrt} = Math;
const R = SPACING * sqrt(3)/4;
const canvas = document.createElement('canvas');
canvas.height = canvas.width = PX;
canvas.style.height = canvas.style.width = `${PX / 2}px`;
canvas.style.background = `rgb(${BG})`;
document.body.appendChild(canvas);

const ctx = canvas.getContext('2d');
// Flip the y-axis.
ctx.transform(1, 0, 0, -1, 0, PX);
ctx.lineWidth = LINE_WIDTH;
ctx.fillStyle = `rgba(${BG}, ${1.4 / FLOORS})`;

function go () {
	ctx.clearRect(0, 0, PX, PX);

	if (DEBUG_SMALL === true) {
		ctx.strokeStyle = '#fff';
		ctx.rect(PX / 4, PX / 4, PX / 2, PX / 2);
		ctx.stroke();
	}

	for (let floor = 0; floor < FLOORS; ++floor) {
		ctx.fillRect(0, 0, PX, PX);
		console.log('fillin');
	
		for (let n = 0; n < ITERATIONS; ++n) {
			// Don't introduce PI etc to θ yet to avoid some precision hassles.
			let hour = rand(HOURS);
			let θ = (hour + HOUR_OFFSET) / HOURS * 2*PI;
			let opacity = 1;

			if (DEBUG_HOURS.length && DEBUG_HOURS.indexOf(hour) === -1) continue;
			const p = getPosition(hour, θ);

			do {
				ctx.strokeStyle = `rgb(${FG})`;

				ctx.beginPath();
				if (DEBUG_NO_CURVES || rand(10)) {
					// Draw a line straight ahead.
					DEBUG_SMALL
						? ctx.moveTo(p.x / 2 + PX / 4, p.y / 2 + PX / 4)
						: ctx.moveTo(p.x, p.y)
						;

					p.x += SPACING * cos(θ);
					p.y += SPACING * sin(θ);

					DEBUG_SMALL
						? ctx.lineTo(p.x / 2 + PX / 4, p.y / 2 + PX / 4)
						: ctx.lineTo(p.x, p.y)
						;

					ctx.stroke();
				} else {
					// Once in a while, turn.
					const Δ_hour = 2*rand(2) - 1; // +/- 1
					const θ_1 = θ - PI/2 * Δ_hour;

					// Add or subtract an hour and update the angle.
					hour = (hour + Δ_hour) % HOURS;
					θ = (hour + HOUR_OFFSET) / HOURS * 2*PI;
					const θ_2 = θ - PI/2 * Δ_hour;

					p.x += R * cos(θ_1 + PI);
					p.y += R * sin(θ_1 + PI);

					ctx.arc(
						p.x,
						p.y,
						R,
						θ_1,
						θ_2,
						Δ_hour < 0
					);

					p.x += R * cos(θ_2);
					p.y += R * sin(θ_2);
				}
				ctx.stroke();
			} while (
				p.x > -SPACING && p.x < PX + SPACING &&
				p.y > -SPACING && p.y < PX + SPACING &&
				opacity > 0
			);
		}
	}
}

// Set the position so that the lines come out of the appropriate
// wall (based on direction), in a random cell.
function getPosition (hour, θ) {
	const cos_θ = +cos(θ).toFixed(6);
	const sin_θ = +sin(θ).toFixed(6);
	const abs_cos_θ = +abs(cos_θ);
	const abs_sin_θ = +abs(sin_θ);

	// One of our axes will be latched to either the min or max value.
	const latched_axis = abs_cos_θ / (abs_sin_θ + abs_cos_θ) > random()
		? 'x'
		: 'y'
		;

	// The other gets a random value within the grid.
	const free_axis = latched_axis === 'x'
		? 'y'
		: 'x'
		;

	// TODO(riley): Generalize.
	const offset = {
		x: SPACING / 3,
		y: SPACING / 2,
	};

	const axis_spacing = {
		x: (abs_cos_θ || sqrt(3)/4) * SPACING,
		y: (abs_sin_θ || sqrt(3)/4) * SPACING,
	};

	// The direction (+1 or -1) of our line coming out of each axis.
	const d = {
		x: sign(cos_θ),
		y: sign(sin_θ),
	};

	// TODO(riley): Factor LINE_WIDTH into this instead of just
	//              forcing it higher.
	const latched_spacing = axis_spacing[latched_axis];
	const p_latched = d[latched_axis] === 1
		? -2*latched_spacing
		: PX - PX % latched_spacing + latched_spacing
		;

	const free_spacing = axis_spacing[free_axis];
	const p_free = free_spacing *
		(rand(Math.ceil(PX / free_spacing + 2)) - 1);

	return {
		[latched_axis]: p_latched + offset[latched_axis],
		[free_axis]: p_free + offset[free_axis],
	};
}

function rand (a, b, c) {
	const fn = (b === 'true' || c) ? i => i : floor;
	if (typeof b !== 'number') { b = a; a = 0; }
	return fn(random() * (b - a) + a);
}

function approx (n) {
	return +(Math.round(n + 'e4') + 'e-4');
}

canvas.addEventListener('click', go, false);
go();

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console