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="paper"></div>
<div class='wrapper'>
	<canvas class="stage"></canvas>
	<p>Click to anywhere generate</p>
</div>
              
            
!

CSS

              
                body{
	color: whitesmoke;
	background:#BB2528;
	font-family: sans-serif;
}
.wrapper{
	margin: 0 auto;
	text-align: center;
	width: 200px;
}
.paper{
	background-color: rgba(187, 37, 40,1);
  	background-blend-mode: overlay;
	bottom:0;
	left:0;
	position:fixed;
	right:0;
	top:0;
	z-index: -1;
}
canvas{
	animation: twirl 60s linear infinite;
}
p{
	user-select: none;
}

@keyframes twirl{
	0%{transform:rotate(0deg);}
	100%{transform:rotate(360deg);}
}
              
            
!

JS

              
                // console.clear();
const canvas = document.querySelector('.stage'),
	  ctx = canvas.getContext('2d'),
	  canvasSize = 200,
	  midpt = {x:canvasSize * .5, y:canvasSize * .5},
	  maxRadius = canvasSize * .4,
	  mcos = Math.cos,
	  mmax = Math.max,
	  mrandom = Math.random,
	  mround = Math.round,
	  msin = Math.sin,
	  msqrt = Math.sqrt,
	  tau = Math.PI * 2,
	  PI180 = Math.PI/180,
	  fillColour = 'hsla(45, 23%, 90%, .3)',
	  strokeColour = 'hsla(45, 23%, 90%, .8)';
const offscreen = document.createElement('canvas'),
	  offscreenCtx = offscreen.getContext('2d'),
	  offscreenSize = 40,
	  paper = document.querySelector('.paper');
let sections = [];

document.body.addEventListener('click', () => animate() );
canvas.width = canvasSize;
canvas.height = canvasSize;
ctx.fillStyle = fillColour;
ctx.strokeStyle = strokeColour;
ctx.lineWidth = .5;

offscreenCtx.width = offscreenSize;
offscreenCtx.height = offscreenSize;

const Trunk = function(o, rot){
	this.root = {x:o.x, y:o.y};
	this.rotation = rot;
	this.length = mrandom() * (maxRadius * .25) + (maxRadius * .75);
	this.tip = getPolarPoint(this.length, this.rotation, this.root);
	this.spurs = [];
	this.spurs1 = [];
	this.spurs2 = [];
	this.spurDeets = [];
	this.numSpurs = mround(mrandom() * 2 + 8);
}
Trunk.prototype.generate = function(){
	let step = (this.length * .95) / this.numSpurs;
	for(let i=0; i<this.numSpurs; i++){
		let spurDeetObj = {};
		spurDeetObj.step = step;
		spurDeetObj.spurLength = step * i + (mrandom() * (i === this.numSpurs-1 ? 5 : 30) + 5);
		spurDeetObj.spurRotation = mrandom() * 10 + (i === 0 ? 30 : 5);
		this.spurDeets.push(spurDeetObj);
	}
	this.buildSpurs();
}
Trunk.prototype.buildSpurs = function(){
	for(let i=0; i<this.numSpurs; i++){
		let spurDeet = this.spurDeets[i];
		let root = getPolarPoint(spurDeet.step * i, this.rotation, this.root);
		this.spurs1.push(new Spur(root, getPolarPoint(spurDeet.spurLength, this.rotation + spurDeet.spurRotation, this.root)));
		this.spurs2.push(new Spur(root, getPolarPoint(spurDeet.spurLength, this.rotation - spurDeet.spurRotation, this.root)));
	}
	this.spurs = this.spurs1.concat(this.spurs2.reverse());
}
Trunk.prototype.clone = function(){
	let cloneObj = {};
	cloneObj.length = this.length;
	cloneObj.tip = this.tip;
	cloneObj.numSpurs = this.numSpurs;
	cloneObj.spurDeets = this.spurDeets;
	return cloneObj;
}
Trunk.prototype.buildFromClone = function(cloneObj){
	this.length = cloneObj.length;
	this.numSpurs = cloneObj.numSpurs;
	this.spurDeets = cloneObj.spurDeets;
	this.buildSpurs();
}
Trunk.prototype.draw = function(seed = .5){
	// Centre of trunk
	ctx.beginPath();
	ctx.moveTo(this.root.x, this.root.y);
	ctx.lineTo(this.tip.x, this.tip.y);
	ctx.stroke();
	// Centre of spurs
	for(let i=0; i<this.spurs.length; i++){
		this.spurs[i].drawSpur();
	}
	// Edges of spur
	ctx.moveTo(this.spurs[0].root.x, this.spurs[0].root.y);
	ctx.beginPath();
	// Outward side
	for(let i=0; i<this.spurs1.length; i++){
		ctx.lineTo(this.spurs1[i].tip.x, this.spurs1[i].tip.y);
	}
	// Draw to tip of trunk
	ctx.lineTo(this.tip.x, this.tip.y);
	// Inward side
	for(let i=0; i<this.spurs2.length; i++){
		ctx.lineTo(this.spurs2[i].tip.x, this.spurs2[i].tip.y);
	}
	if(seed > .5){
		ctx.lineTo(this.spurs[0].root.x, this.spurs[0].root.y);
	}
	ctx.stroke();
	ctx.fill();
}

const Spur = function(root, tip){
	this.root = root;
	this.tip = tip;
}
Spur.prototype.drawSpur = function(){
	ctx.beginPath();
	ctx.moveTo(this.root.x, this.root.y);
	ctx.lineTo(this.tip.x, this.tip.y);
	ctx.stroke();
}

function getPolarPoint(distance, degree, origin){
	let radian = degree * PI180;
	return {x:distance * mcos(radian) + origin.x, y:distance * msin(radian) + origin.y}
}

function create(){
	ctx.clearRect(0,0,canvasSize,canvasSize);
	sections = [];
	let offset = 0;
	// Seems snowflakes always have 6 rays :)
	let howMany = 6;
	let t0 = new Trunk(midpt, offset);
	t0.generate();
	let sharedStructure = t0.clone();
	sections.push(t0);
	for(let i=1; i<howMany; i++){
		let t = new Trunk(midpt, ((360 / howMany) * i) + offset);
		t.buildFromClone(sharedStructure);
		sections.push(t);
	}
}

function useCanvasAsBackground(){
	offscreen.height = offscreen.width = offscreenSize;
	offscreenCtx.drawImage(canvas, 0, 0, offscreenSize, offscreenSize);
	let url = offscreen.toDataURL('image/png');
	paper.style.backgroundImage = 'url(' + url + ')';
}

function draw(){
	let seed = mrandom();
	for(let i=0; i<sections.length; i++){
		sections[i].draw(seed);
	}
	useCanvasAsBackground();
}
function animate(){
	create();
	draw();
}
animate();
              
            
!
999px

Console