Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <div class="controls">
	<button id="button-koch">
		<svg width="24" height="12" xmlns="http://www.w3.org/2000/svg">
				<path d="M0 12 L 6 12 L 12 0 L 18 12 L 24 12" fill="transparent" stroke="white" stroke-width="2" />
		</svg>

		Koch
	</button>

	<button id="button-quadratic-1">
		<svg width="24" height="12" xmlns="http://www.w3.org/2000/svg">
				<path d="M0 12 L 6 12 L 6 0 L 18 0 L 18 12 L 24 12" fill="transparent" stroke="white" stroke-width="2" />
		</svg>

		Quadratic type 1
	</button>

	<button id="button-quadratic-2">
		<svg width="24" height="12" xmlns="http://www.w3.org/2000/svg">
				<path d="M0 6 L 6 6 L 6 0 L 12 0 L 12 12 L 18 12 L 18 6 L 24 6" fill="transparent" stroke="white" stroke-width="2" />
		</svg>

		Quadratic type 2
	</button>
</div>
              
            
!

CSS

              
                body {
	background-color: #fafafa
}

button {
	$color: #3f659f;
	
	background-color: $color;
	border: 1px solid darken($color, 10%);
	color: #fff;
	padding: .75em 1em;
	
	&:hover, &:focus {
		background-color: lighten($color, 4%);
	}
	&:active {
		background-color: lighten($color, 8%);
	}
	
	&.is-active {
		border: 1px solid darken($color, 20%);
		background-color: darken($color, 10%);
	}
}

canvas {
	display: block;
}

.controls {
	position: absolute;
	left: 1em;
	top: 1em;
}
              
            
!

JS

              
                class Vector2 {
	constructor(x = 0, y = 0) {
		this.x = x;
		this.y = y;
	}
	
	add(v) {
		this.x += v.x;
		this.y += v.y;
		return this;
	}
	
	addScalar(s) {
		this.x += s;
		this.y += s;
		return this;
	}
	
	subtract(v) {
		this.x -= v.x;
		this.y -= v.y;
		return this;
	}
	
	divideScalar(s) {
		this.x /= s;
		this.y /= s;
		return this;
	}
	
	multiplyScalar(s) {
		this.x *= s;
		this.y *= s;
		return this;
	}
	
	multiply(v) {
		this.x *= v.x;
		this.y *= v.y;
		return this;
	}
	
	normalize() {
		const length = this.getLength();
		return this.divideScalar(length);
	}
	
	getAngle() {
		return Math.atan2(this.y, this.x);
	}
	
	getLength() {
		return Math.sqrt(
			this.x * this.x +
			this.y * this.y
		);
	}
	
	setLength(s) {
		return this.normalize().multiplyScalar(s);
	}
	
	clone() {
		return new Vector2(this.x, this.y);
	}
	
	min() {
		return Math.min(this.x, this.y);
	}
	
	static fromScalar(s = 0) {
		return new Vector2(s, s);
	}
	
	static subtract(a, b) {
		return new Vector2(
			b.x - a.x,
			b.y - a.y
		);
	}
	
	static lerp(a, b, t) {
		return new Vector2(
			a.x + (b.x - a.x) * t,
			a.y + (b.y - a.y) * t
		);
	}
}

class Path {
	constructor(points = []) {
		this.points = points;
	}
	
	break(breakLine) {
		let newPoints = [];
		
		for (let i = 0; i < this.points.length - 1; i++) {
			const start = this.points[i];
			const end = this.points[i + 1];
			const line = Vector2.subtract(start, end);
			
			newPoints = newPoints.concat(breakLine(start, end, line));
		}
		
		this.points = newPoints;
		return this;
	}
	
	render(context, size = Vector2.fromScalar(1)) {
		const center = size.clone().divideScalar(2);
		const scale = size.min() / 2;
		const startPosition = this.points[0].clone()
			.multiplyScalar(scale)
			.add(center);
		
		context.beginPath();
		context.moveTo(startPosition.x, startPosition.y);
		
		this.points.slice(1).forEach((point, index) => {
			const position = point.clone()
				.multiplyScalar(scale)
				.add(center);
			
			context.lineTo(position.x, position.y);
		});
		
		context.lineWidth = 1;
		context.strokeStyle = '#000';
		context.stroke();
	}
}

class Fractal {
	constructor() {
		this.canvas = document.createElement('canvas');
		this.context = this.canvas.getContext('2d');
		
		this.size = new Vector2(1, 1);
		
		this.setRenderSettings(null);
		this.loop = this.render.bind(this);
	}
	
	setRenderSettings(settings) {
		this.renderSettings = settings;
		this.reset();
	}
	
	setSize(size) {
		this.size = size;
		
		this.canvas.height = size.y;
		this.canvas.width = size.x;
	}
	
	reset() {
		this.path = new Path([
			new Vector2(-1, 0),
			new Vector2(1, 0),
		]);
		
		this.iteration = -1;
	}
	
	render() {
		if (this.renderTimeout) {
			clearTimeout(this.renderTimeout);
			this.renderTimeout = null;
		}
		
		if (++this.iteration < this.renderSettings.iterations) {
			this.renderTimeout = setTimeout(this.loop, 500);
		}
		
		this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
		this.path.render(this.context, this.size);
		this.path.break(this.renderSettings.breakLine);
	}
}

const presets = {
	koch: {
		iterations: 5,
		breakLine: (start, end, line) => {
			const lineLength = line.getLength();
			const extrude = Math.sqrt(3) / 2 * lineLength / 3;
			const extrudeAngle = line.getAngle() + Math.PI / -2;
			
			return [
				start,
				Vector2.lerp(start, end, 1/3),
				Vector2.lerp(start, end, 1/2)
					.add(new Vector2(
						Math.cos(extrudeAngle) * extrude,
						Math.sin(extrudeAngle) * extrude
					)),
				Vector2.lerp(start, end, 2/3),
				end,
			];
		},
	},
	
	quadraticOne: {
		iterations: 4,
		breakLine: (start, end, line) => {
			const lineLength = line.getLength();
			const extrudeAngle = line.getAngle() + Math.PI / -2;
			
			const extrusion = new Vector2(
				Math.cos(extrudeAngle),
				Math.sin(extrudeAngle)
			).multiplyScalar(lineLength / 3);
			
			return [
				start,
				Vector2.lerp(start, end, 1/3),
				Vector2.lerp(start, end, 1/3)
					.add(extrusion),
				Vector2.lerp(start, end, 2/3)
					.add(extrusion),
				Vector2.lerp(start, end, 2/3),
				end,
			];
		},
	},
	
	quadraticTwo: {
		iterations: 4,
		breakLine: (start, end, line) => {
			const lineLength = line.getLength();
			const extrudeAngle = line.getAngle() + Math.PI / -2;
			
			const extrusion = new Vector2(
				Math.cos(extrudeAngle),
				Math.sin(extrudeAngle)
			).multiplyScalar(lineLength / 4);
			
			return [
				start,
				Vector2.lerp(start, end, 1/4),
				Vector2.lerp(start, end, 1/4)
					.add(extrusion),
				Vector2.lerp(start, end, 2/4)
					.add(extrusion),
				Vector2.lerp(start, end, 2/4),
				Vector2.lerp(start, end, 2/4)
					.subtract(extrusion),
				Vector2.lerp(start, end, 3/4)
					.subtract(extrusion),
				Vector2.lerp(start, end, 3/4),
				end
			];
		},
	},
};

const fractal = new Fractal();
fractal.setSize(new Vector2(window.innerWidth, window.innerHeight));
fractal.setRenderSettings(presets.koch);
fractal.render();

document.body.appendChild(fractal.canvas);

const presetButtons = {
	koch: document.getElementById('button-koch'),
	quadraticOne: document.getElementById('button-quadratic-1'),
	quadraticTwo: document.getElementById('button-quadratic-2'),
};
let activePresetId = null;

const setActivePresetButton = (buttonId) => {
	if (activePresetId) {
		const currentlyActiveButton = presetButtons[activePresetId];
		currentlyActiveButton.classList.remove('is-active');
	}
	
	const newActivePresetButton = presetButtons[buttonId];
	newActivePresetButton.classList.add('is-active');
	
	activePresetId = buttonId;
};

for (let id in presetButtons) {
	const button = presetButtons[id];
	
	button.addEventListener('click', () => {
		fractal.setRenderSettings(presets[id]);
		fractal.render();
		
		setActivePresetButton(id);
	});
}

setActivePresetButton('koch');

window.addEventListener('resize', () => {
	fractal.setSize(new Vector2(window.innerWidth, window.innerHeight));
	fractal.reset();
	fractal.render();
});
              
            
!
999px

Console