cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="container" id="container">
	<div class="window">
		<h2 class="title">plants.exe</h2>
		<canvas id="c"></canvas>
	</div>
	<button onClick="init();" id="init">new</button>
</div>
<!--

vase inspo https://rosewong.tumblr.com/post/149196349681/sushi-and-vases

<p>making generative plants inspired by this:</p>
<p><img src="https://67.media.tumblr.com/9fdbea69334a77cd814ec389df7e4e13/tumblr_oa2qqi4c2V1qzi2f3o2_r1_1280.jpg" height="400" /></p>
-->

            
          
!
            
              $beige:#F6F3ED;
$gray:#444;

html {box-sizing:border-box;}
html * {box-sizing:inherit;}

html,body {height:100%;width:100%;}
body {
	background-color:desaturate(darken($beige,5),15);
  //background-image:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20xmlns%3Axlink%3D%22http%3A//www.w3.org/1999/xlink%22%20x%3D%220px%22%20y%3D%220px%22%0A%09%20width%3D%2240px%22%20height%3D%2240px%22%20viewBox%3D%220%200%2040%2040%22%20enable-background%3D%22new%200%200%2040%2040%22%20xml%3Aspace%3D%22preserve%22%20stroke%3D%22"+ rgba($gray, 0.15) +"%22%20stroke-linecap%3D%22round%22%20stroke-width%3D%222px%22%3E%0A%3Cline%20x1%3D%2226.7%22%20y1%3D%2225.6%22%20x2%3D%2223.4%22%20y2%3D%2228.3%22/%3E%0A%3Cline%20x1%3D%2229%22%20y1%3D%2228.4%22%20x2%3D%2231.2%22%20y2%3D%2224.7%22/%3E%0A%3Cline%20x1%3D%2215.9%22%20y1%3D%2224.7%22%20x2%3D%2219.5%22%20y2%3D%2227%22/%3E%0A%3Cline%20x1%3D%2217.4%22%20y1%3D%2217.9%22%20x2%3D%2216.4%22%20y2%3D%2222.1%22/%3E%0A%3Cline%20x1%3D%2225.9%22%20y1%3D%2229.7%22%20x2%3D%2227.6%22%20y2%3D%2233.6%22/%3E%0A%3Cline%20x1%3D%2234.4%22%20y1%3D%2225.4%22%20x2%3D%2237.7%22%20y2%3D%2228.1%22/%3E%0A%3Cline%20x1%3D%2234.5%22%20y1%3D%2229.9%22%20x2%3D%2230.6%22%20y2%3D%2232.2%22/%3E%0A%3Cline%20x1%3D%2236.6%22%20y1%3D%2221.1%22%20x2%3D%2232.5%22%20y2%3D%2222%22/%3E%0A%3Cline%20x1%3D%2220.1%22%20y1%3D%2221.3%22%20x2%3D%2222.9%22%20y2%3D%2224.5%22/%3E%0A%3Cline%20x1%3D%2223.2%22%20y1%3D%2231.6%22%20x2%3D%2221.5%22%20y2%3D%2235.4%22/%3E%0A%3Cline%20x1%3D%2218.3%22%20y1%3D%2237.9%22%20x2%3D%2216.2%22%20y2%3D%2233.5%22/%3E%0A%3Cline%20x1%3D%2229.3%22%20y1%3D%2235.5%22%20x2%3D%2226%22%20y2%3D%2238.2%22/%3E%0A%3Cline%20x1%3D%2234.9%22%20y1%3D%2235.5%22%20x2%3D%2237.3%22%20y2%3D%2232%22/%3E%0A%3Cline%20x1%3D%2220.7%22%20y1%3D%2217.7%22%20x2%3D%2223.8%22%20y2%3D%2214.7%22/%3E%0A%3Cline%20x1%3D%2227.6%22%20y1%3D%225.7%22%20x2%3D%2226.3%22%20y2%3D%229.8%22/%3E%0A%3Cline%20x1%3D%2233.1%22%20y1%3D%225.7%22%20x2%3D%2233%22%20y2%3D%221.5%22/%3E%0A%3Cline%20x1%3D%2220.3%22%20y1%3D%228%22%20x2%3D%2223.9%22%20y2%3D%2211.8%22/%3E%0A%3Cline%20x1%3D%2216.4%22%20y1%3D%2214.7%22%20x2%3D%2220.3%22%20y2%3D%2212.8%22/%3E%0A%3Cline%20x1%3D%2215.8%22%20y1%3D%226.5%22%20x2%3D%2217.1%22%20y2%3D%2210.6%22/%3E%0A%3Cline%20x1%3D%2229.4%22%20y1%3D%229.1%22%20x2%3D%2233%22%20y2%3D%2211.5%22/%3E%0A%3Cline%20x1%3D%2235.4%22%20y1%3D%22-1.4%22%20x2%3D%2238%22%20y2%3D%222%22/%3E%0A%3Cline%20x1%3D%2237.3%22%20y1%3D%224.9%22%20x2%3D%2235.3%22%20y2%3D%229%22/%3E%0A%3Cline%20x1%3D%2231.8%22%20y1%3D%22-3.2%22%20x2%3D%2230.6%22%20y2%3D%221%22/%3E%0A%3Cline%20x1%3D%2219.8%22%20y1%3D%225.1%22%20x2%3D%2224.6%22%20y2%3D%226.2%22/%3E%0A%3Cline%20x1%3D%2228.3%22%20y1%3D%2213%22%20x2%3D%2228.9%22%20y2%3D%2217.2%22/%3E%0A%3Cline%20x1%3D%2228.6%22%20y1%3D%2221.7%22%20x2%3D%2225.4%22%20y2%3D%2218.8%22/%3E%0A%3Cline%20x1%3D%2233.6%22%20y1%3D%2215.1%22%20x2%3D%2232.3%22%20y2%3D%2219.1%22/%3E%0A%3Cline%20x1%3D%2237.7%22%20y1%3D%2217.8%22%20x2%3D%2236.8%22%20y2%3D%2213.6%22/%3E%0A%3Cline%20x1%3D%222.3%22%20y1%3D%2231.9%22%20x2%3D%22-1.1%22%20y2%3D%2234.5%22/%3E%0A%3Cline%20x1%3D%224%22%20y1%3D%2234.5%22%20x2%3D%226.3%22%20y2%3D%2230.9%22/%3E%0A%3Cline%20x1%3D%221.3%22%20y1%3D%2237.5%22%20x2%3D%222.9%22%20y2%3D%2241.5%22/%3E%0A%3Cline%20x1%3D%229.5%22%20y1%3D%2231.6%22%20x2%3D%2212.7%22%20y2%3D%2234.4%22/%3E%0A%3Cline%20x1%3D%229.8%22%20y1%3D%2236.7%22%20x2%3D%225.6%22%20y2%3D%2237.9%22/%3E%0A%3Cline%20x1%3D%2210.6%22%20y1%3D%2224.6%22%20x2%3D%2211.1%22%20y2%3D%2228.9%22/%3E%0A%3Cline%20x1%3D%224.6%22%20y1%3D%2214.8%22%20x2%3D%221.3%22%20y2%3D%2217.6%22/%3E%0A%3Cline%20x1%3D%226%22%20y1%3D%2218.4%22%20x2%3D%228.2%22%20y2%3D%2214.7%22/%3E%0A%3Cline%20x1%3D%227.8%22%20y1%3D%221.7%22%20x2%3D%228.7%22%20y2%3D%226.2%22/%3E%0A%3Cline%20x1%3D%223.3%22%20y1%3D%2219.5%22%20x2%3D%225%22%20y2%3D%2223.4%22/%3E%0A%3Cline%20x1%3D%2211%22%20y1%3D%2215.2%22%20x2%3D%2214.3%22%20y2%3D%2217.9%22/%3E%0A%3Cline%20x1%3D%2211.5%22%20y1%3D%2220.2%22%20x2%3D%227.7%22%20y2%3D%2222.1%22/%3E%0A%3Cline%20x1%3D%2213.7%22%20y1%3D%2212.8%22%20x2%3D%229.6%22%20y2%3D%2211.4%22/%3E%0A%3Cline%20x1%3D%22-1.6%22%20y1%3D%2210.6%22%20x2%3D%221.4%22%20y2%3D%2213.6%22/%3E%0A%3Cline%20x1%3D%221.1%22%20y1%3D%2221.9%22%20x2%3D%22-0.6%22%20y2%3D%2225.7%22/%3E%0A%3Cline%20x1%3D%226.7%22%20y1%3D%2225.7%22%20x2%3D%223.3%22%20y2%3D%2228.4%22/%3E%0A%3Cline%20x1%3D%221.3%22%20y1%3D%224.6%22%20x2%3D%226%22%20y2%3D%226.7%22/%3E%0A%3Cline%20x1%3D%226.6%22%20y1%3D%229.2%22%20x2%3D%222.5%22%20y2%3D%2210.4%22/%3E%0A%3Cline%20x1%3D%2213.2%22%20y1%3D%224.6%22%20x2%3D%2211.9%22%20y2%3D%228.7%22/%3E%0A%3Cline%20x1%3D%2218.6%22%20y1%3D%2229.4%22%20x2%3D%2214.5%22%20y2%3D%2230.8%22/%3E%0A%3Cline%20x1%3D%2226%22%20y1%3D%222.6%22%20x2%3D%2222.8%22%20y2%3D%22-1.5%22/%3E%0A%3Cline%20x1%3D%2235.4%22%20y1%3D%2238.6%22%20x2%3D%2238%22%20y2%3D%2242%22/%3E%0A%3Cline%20x1%3D%2231.8%22%20y1%3D%2236.9%22%20x2%3D%2230.6%22%20y2%3D%2241%22/%3E%0A%3Cline%20x1%3D%2212.3%22%20y1%3D%2241.3%22%20x2%3D%2213.6%22%20y2%3D%2237.3%22/%3E%0A%3Cline%20x1%3D%2226%22%20y1%3D%2242.6%22%20x2%3D%2222.8%22%20y2%3D%2238.5%22/%3E%0A%3Cline%20x1%3D%221.3%22%20y1%3D%22-2.5%22%20x2%3D%222.9%22%20y2%3D%221.5%22/%3E%0A%3Cline%20x1%3D%2212.3%22%20y1%3D%221.3%22%20x2%3D%2213.6%22%20y2%3D%22-2.7%22/%3E%0A%3Cline%20x1%3D%2220.2%22%20y1%3D%223.2%22%20x2%3D%2216.6%22%20y2%3D%220.6%22/%3E%0A%3Cline%20x1%3D%2242.3%22%20y1%3D%2231.9%22%20x2%3D%2238.9%22%20y2%3D%2234.5%22/%3E%0A%3Cline%20x1%3D%2238.4%22%20y1%3D%2210.6%22%20x2%3D%2241.4%22%20y2%3D%2213.6%22/%3E%0A%3Cline%20x1%3D%2241.1%22%20y1%3D%2221.9%22%20x2%3D%2239.4%22%20y2%3D%2225.7%22/%3E%0A%3C/svg%3E");
	background-size:100%;
	font-family:monospace;
	margin:0;
	display:flex;
	flex-direction:column;
	justify-content:center;
	align-items:center;
}

.container {
	
	.window {
		border:2px solid $gray;
		box-shadow:4px 4px 0 $gray;
		margin-bottom:2em;
	}
	
	.title {
		font-size:0.8em;
		font-weight:normal;
		padding:0.2em 0.6em 0.4em 0.6em;
		margin:0;
		background-color:$gray;
		color:$beige;
		width:400px;
		&:after {
			content:'⌘';
			float:right;
		}
	}
	
	#c {
		margin-bottom:-3px;
	}
	
	button {
		background-color:$beige;
		border:2px solid $gray;
		box-shadow:4px 4px 0 $gray;
		font-family:monospace;
		color:$gray;
		float:right;
		padding:0.4em 1.4em;
		&:disabled {
			color:lighten($gray,20);
		}
	}
}
            
          
!
            
              function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getRandomFloatInclusive(min, max) {
	return (Math.random() * (max - min) + min);
}



var canvas,
		lines = [];

var initButton = document.getElementById('init');

const lineOptions = {
	stroke: '#444',
	strokeWidth: 1
};

const size = 400;
const canvasCenter = { x: size / 2, y: size / 2 };
const background = '#F6F3ED';

const types = {
	STEM: 'stem',
	LEAF: 'leaf'
}


var plants = [
	{
		center: { x: canvasCenter.x - 100, y: canvasCenter.y - 20 },
		containerType: 'block',
		instructions: {
			0: { count: 1, angleDelta: [5, 10], length: 30, type: types.STEM },
			1: { count: [2, 4], angleDelta: [30, 50], length: 30, type: types.STEM },
			2: { count: 3, angleDelta: [70, 88], length: 6, type: types.LEAF }
		}
	},
	{
		center: { x: canvasCenter.x, y: canvasCenter.y - 20 },
		containerType: 'grid',
		instructions: {
			0: { count: 1, angleDelta: [5, 10], length: 80, type: types.STEM },
			1: { count: [4, 6], angleDelta: [30, 50], length: 30, type: types.STEM },
			2: { count: 3, angleDelta: [70, 88], length: 7, type: types.LEAF }
		}
	},
	{
		center: { x: canvasCenter.x + 100, y: canvasCenter.y - 20 },
		containerType: 'dots',
		instructions: {
			0: { count: 1, angleDelta: [5, 10], length: 40, type: types.STEM },
			1: { count: [2, 4], angleDelta: [30, 50], length: 50, type: types.STEM },
			2: { count: 6, angleDelta: [70, 88], length: 5, type: types.LEAF }
		}
	},
	{
		center: { x: canvasCenter.x - 100, y: canvasCenter.y + 140 },
		containerType: 'dashes',
		instructions: {
			0: { count: 1, angleDelta: [5, 10], length: 40, type: types.STEM },
			1: { count: [2, 6], angleDelta: [30, 50], length: 30, type: types.STEM },
			2: { count: 4, angleDelta: [70, 88], length: 4, type: types.LEAF }
		}
	},
	{
		center: { x: canvasCenter.x, y: canvasCenter.y + 140 },
		containerType: 'block',
		instructions: {
			0: { count: 1, angleDelta: [5, 10], length: 30, type: types.STEM },
			1: { count: [1, 2], angleDelta: [30, 50], length: 60, type: types.STEM },
			2: { count: 3, angleDelta: [70, 88], length: 12, type: types.LEAF }
		}
	},
	{
		center: { x: canvasCenter.x + 100, y: canvasCenter.y + 140 },
		containerType: 'grid',
		instructions: {
			0: { count: 1, angleDelta: [5, 10], length: 50, type: types.STEM },
			1: { count: [2, 4], angleDelta: [30, 50], length: 20, type: types.STEM },
			2: { count: 2, angleDelta: [70, 88], length: 5, type: types.LEAF }
		}
	},
];


class Plant {
	constructor(center, instructions, containerType) {
		
		this.instructions = instructions;

		this.container = this.drawContainer(center, containerType);
		
		this.stems = [];
		this.leaves = [];

		var start = new fabric.Line([center.x, center.y, center.x, center.y], lineOptions);
		this.drawStep(start, 0);
		
		var plantComponents = _.concat(this.stems, this.leaves, this.container);

		var plant = new fabric.Group(plantComponents);
		plant.set({
			originX: 'center',
			originY: 'bottom'
		});
		plant.set({
			top: Math.ceil(center.y),
			left: Math.ceil(center.x)
		});
		canvas.add(plant);
		
		return plant;

	}
	
	drawStep(prevLine, depth) {
		if (depth < Object.keys(this.instructions).length) {
			var plantStep = this.instructions[depth];
			var side = Math.random() < 0.5 ? -1 : 1;
			var count = plantStep.count;
			if (Array.isArray(count)) {
				count = getRandomIntInclusive(count[0], count[1]);
			}
			for (var n = 0; n < count; n++) {
				side = side * -1;
				var angle = getRandomIntInclusive(plantStep.angleDelta[0], plantStep.angleDelta[1]) * side;
				var lineDistance = 0.25 + (0.75 * (n / count)) + (side * getRandomFloatInclusive(0.05, 0.1));
				var point = findPointOnLine(prevLine, lineDistance);
				if (plantStep.type === types.STEM) {
					var line = this.drawStem(point.x, point.y, plantStep.length, angle);
					this.stems.push(line);
					this.drawStep(line, depth + 1);	
				}
				if (plantStep.type === types.LEAF) {
					var line = this.drawStem(point.x, point.y, plantStep.length, angle);
					var prevPlantStep = this.instructions[depth - 1];
					angle = angle - getRandomIntInclusive(prevPlantStep.angleDelta[0], prevPlantStep.angleDelta[1]);
					this.leaves.push(this.drawLeaf(line, angle));	
				}			
			}
		}
	}
	
	drawStem(x, y, length, degrees) {
		var points = [x, y, x, y - length]; // always 'up' for right now
		var rotationOrigin = new fabric.Point(x, y);
		var angle = fabric.util.degreesToRadians(degrees);
		var end = fabric.util.rotatePoint(new fabric.Point(points[2], points[3]), rotationOrigin, angle);
		var rotatedPoints = [x, y, end.x, end.y];
		var line = new fabric.Line(rotatedPoints, lineOptions);
		line.setCoords();
		return line;
	}
	
	drawLineWithOptions(x, y, length, degrees, options) {
		var points = [x, y, x, y - length]; // always 'up' for right now
		var rotationOrigin = new fabric.Point(x, y);
		var angle = fabric.util.degreesToRadians(degrees);
		var end = fabric.util.rotatePoint(new fabric.Point(points[2], points[3]), rotationOrigin, angle);
		var rotatedPoints = [x, y, end.x, end.y];
		var line = new fabric.Line(rotatedPoints, options);
		line.setCoords();
		return line;
	}
	
	drawLeaf(line, degrees) {
		var circumfrence = Math.sqrt(Math.pow(line.x2 - line.x1, 2) + Math.pow(line.y2 - line.y1, 2));
		var point = findPointOnLine(line, 0.5);
		var circleOptions = _.extend({}, lineOptions, {
			fill: background, // leafColor
			radius: circumfrence / 2,
			originX: 'center',
			originY: 'center',
			top: point.y,
			left: point.x
		});
		var circle = new fabric.Circle(circleOptions);
		var group = new fabric.Group([circle]);
		if (circumfrence > 2) {
			var halfLine = this.drawStem(circle.left, circle.top, circumfrence / 2, -degrees);
			group.add(halfLine);		
		}
				
		circle.sendToBack();
		return group;
	}
	
	drawContainer(center, containerType) {
		var gridsize = 8;
		
		var width = 40;
		var height = getRandomIntInclusive(40, 50);
		if (containerType === 'grid') {
			height = Math.ceil(height / gridsize) * gridsize;
		}
		var containerOptions = _.extend({}, lineOptions, {
			width: width,
			height: height,
			fill: background,
			originX: 'left',
			originY: 'top',
			top: Math.ceil(center.y),
			left: Math.ceil(center.x - (width / 2))
		});
		var container = new fabric.Rect(containerOptions);

		var group = new fabric.Group([container]);

		switch (containerType) {
			case 'block':
				var decorationOptions = _.extend({}, lineOptions, {
					width: 40,
					height: getRandomIntInclusive(6, 18),
					fill: lineOptions.stroke,
					top: container.top,
					left: container.left
				});
				group.add(new fabric.Rect(decorationOptions));
				break;
			case 'grid':
				var gridOptions = _.extend({}, lineOptions);
				for (var x = 1; x < (container.width / gridsize); x++) {
					var points = [container.left + (x * gridsize), container.top, container.left + (x * gridsize), container.top + container.height];
					group.add(new fabric.Line(points, gridOptions));
				}
				for (var y = 1; y < (container.height / gridsize); y++) {
					var points = [container.left, container.top + (y * gridsize), container.left + container.width, container.top + (y * gridsize)];
					group.add(new fabric.Line(points, gridOptions));
				}
				break;
			case 'dashes':
				var dashsize = 10;
				var dashOptions = _.extend({}, lineOptions, {
					strokeWidth: 2,
					strokeLineCap: 'round'
				});
				var flip = 1;
				for (var i = -1; i < (container.height / dashsize) + 1; i++) {
					for (var n = -1; n < (container.width / dashsize) + 1; n++) {
						var flip = flip * -1;
						var x = container.left + (dashsize * n) - (flip * 2);
						var y = container.top + (dashsize * i) - (flip * 4);
						var line = this.drawLineWithOptions(x, y, (dashsize / 2), getRandomIntInclusive(flip * 30, flip * 70), dashOptions);
						group.add(line);
					}
				}
				break;
			case 'dots':
				var dotSpacing = 8;
				var dotOffset = 4;
				var dotOptions = _.extend({}, lineOptions, {
					radius: 1,
					fill: lineOptions.stroke,
					strokeWidth: 0
				});
				for (var i = -1; i < (container.height / dotSpacing) + 1; i++) {
					for (var n = -1; n < (container.width / dotSpacing) + 1; n++) {
						var top = container.top + (dotSpacing * i) + dotOffset;
						var left = container.left + (dotSpacing * n) + dotOffset;
						var dot = new fabric.Circle(_.extend({}, dotOptions, {
							top: top,
							left: left
						}));
						group.add(dot);
					}
				}
				// todo
				break;
			default:
				break;
		}
		
		group.clipTo = function(ctx) {
			var width = Math.ceil(container.width) + lineOptions.strokeWidth;
			var height = Math.ceil(container.height) + lineOptions.strokeWidth;
			ctx.rect(-(width / 2), -(height / 2), width, height);
		}
		
		return group;
	}
}


var init = function() {
	
	initButton.disabled = true;
		
	canvas = new fabric.StaticCanvas('c', {
		backgroundColor: background,
		enableRetinaScaling: true,
		width: size,
		height: size
	});
	
	// ?? hmm
	var shuffledPlants = _.shuffle(plants);
	
	for (var i = 0; i < shuffledPlants.length; i++) {
		var plant = shuffledPlants[i];
		var p = new Plant(plant.center, plant.instructions, plant.containerType);
	}

	initButton.disabled = false;

}


var getOrigins = function(points) {
	var origins = { x: undefined, y: undefined };

	// x origin
	if (points[0] > points[2]) {
		origins.x = 'right';
	} else if (points[0] < points[2]) {
		origins.x = 'left';
	} else {
		origins.x = 'left';
	}
	// y origin
	if (points[1] > points[3]) {
		origins.y = 'bottom';
	} else if (points[1] < points[3]) {
		origins.y = 'top';
	} else {
		origins.y = 'bottom';
	}
	
	return origins;
}

// distance should be 0-1
var findPointOnLine = function(line, distance) {
  var xdiff = line.x2 - line.x1;
  var ydiff = line.y2 - line.y1;
  var xstep = xdiff * distance;
  var ystep = ydiff * distance;
	
	return { 
		x:  line.x1 + xstep, 
		y: line.y1 + ystep
	};
}



init();


            
          
!
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