css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console