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 {
	overflow: hidden;
}

canvas {
	display: block;
}
            
          
!
            
              class Vector {
	constructor(x = 0, y = 0) {
		this.x = x;
		this.y = y;
	}
	
	add(v) {
		this.x += v.x;
		this.y += v.y;
		
		return this;
	}
	
	clone() {
		return new Vector(this.x, this.y);
	}
}

class EventEmitter {
	constructor() {
		this.events = {};
	}
	
	on(eventName, handler) {
		if (!this.events[eventName]) {
			this.events[eventName] = [];
		}
		
		this.events[eventName].push(handler);
	}
	
	trigger(eventName, ...args) {
		if (!this.events[eventName]) return;
		
		this.events[eventName]
			.forEach(handler => handler(...args));
	}
}

class Leaf {
	constructor(position, heading, size = 8) {
		this.position = position;
		this.heading = heading;
		this.size = size;
	}
	
	render(context) {
		const positionNew = this.position.clone().add(new Vector(
			Math.cos(this.heading) * this.size,
			Math.sin(this.heading) * this.size
		));
		
		context.beginPath();
		context.moveTo(this.position.x, this.position.y);
		context.lineTo(positionNew.x, positionNew.y);
		
		context.lineWidth = 1;
		context.strokeStyle = '#040';
		context.stroke();
	}
}

class Branch extends EventEmitter {
	constructor(position, heading, size, depth = 0) {
		super();
		
		this.position = position;
		this.heading = heading;
		this.size = size;
		this.depth = depth;
	}
	
	render(context) {
		if (this.size <= 1) {
			this.trigger('die', this);
			return;
		}
		
		const step = 1.2;
		const positionNew = this.position.clone().add(new Vector(
			Math.cos(this.heading) * step,
			Math.sin(this.heading) * step
		));
		
		context.beginPath();
		context.moveTo(this.position.x, this.position.y);
		context.lineTo(positionNew.x, positionNew.y);
		
		context.lineWidth = this.size;
		context.strokeStyle = '#621';
		context.stroke();
		
		this.size -= 0.025;
		
		if (Math.random() < .8) this.trigger('spawnLeaf', this);
		if (Math.random() < .2) this.trigger('spawnBranch', this);
		
		this.position = positionNew;
	}
}

const birand = () => Math.random() * 2 - 1;

const canvas = document.createElement('canvas');
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;

const context = canvas.getContext('2d');

const onWalkerDie = (walker) => {
	const index = walkers.indexOf(walker);
	if (index >= 0) walkers.splice(index, 1);
};

const getHeading = (parent) => {
	const variation = birand() * Math.PI * .25;
	return parent.heading + variation;
};
const getHeadingBase = (parent) => {
	const direction = Math.random() < .5 ? -1 : 1;
	
	const variation = Math.sqrt(Math.random()) * direction * Math.PI * .5;
	return Math.PI * .5 + variation;
};

const spawnLeaf = (parent) => {
	const position = parent.position.clone();
	const heading = parent.heading + birand() * Math.PI * .25;
	
	const leaf = new Leaf(position, heading, 8 + parent.size);
	leaf.render(context);
};

const spawnBranch = (parent) => {
	const position = parent.position.clone();
	const heading = parent.depth ? getHeading(parent) : getHeadingBase(parent);
	const size = parent.size * .6;
	
	const branch = new Branch(position, heading, size, parent.depth + 1);
	branch.on('spawnBranch', spawnBranch);
	branch.on('spawnLeaf', spawnLeaf);
	branch.on('die', onWalkerDie);
	
	walkers.push(branch);
};

const walkers = [];

const start = () => {
	walkers.length = 0;
	context.clearRect(0, 0, canvas.width, canvas.height);
	
	const base = new Branch(
		new Vector(canvas.width * .5, canvas.height * .75),
		Math.PI * -.5,
		10
	);

	base.on('spawnBranch', spawnBranch);
	base.on('spawnLeaf', spawnLeaf);
	base.on('die', onWalkerDie);
	walkers.push(base);
};

const render = () => {
	requestAnimationFrame(render);
	walkers.forEach(w => w.render(context));
};

document.body.appendChild(canvas);
start();
render();

canvas.addEventListener('click', () => start());
            
          
!
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