cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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, body
	height: 100%

body
	display: flex
	align-items: center
	justify-content: center
	margin: 0

.dg.a
	margin-right: 0 !important

            
          
!
            
              const {abs, cos, PI, pow, sin} = Math;
const PI2 = PI * 2;

const FG = [68, 171, 218];
const BG = [86, 103, 220];
const N = 10;
const INITIAL_RADIUS = 10;
const RADIUS_STEP = 20;

const size = 380;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.height = canvas.width = size;
ctx.setTransform(1, 0, 0, -1, size / 2, size / 2);

function draw () {
	const {a, b, m1, m2, n1, n2, n3, smoothness} = this;
	const superformula = φ => pow(pow(abs(cos(m1*φ/4)/a), n2) + pow(abs(sin(m2*φ/4)/b), n3), -1/n1);
	
	ctx.clearRect(-size/2, -size/2, size, size);
	const angleStep = PI2 / smoothness;
	for (let level = N; level >= 0; --level) {
		const radius = INITIAL_RADIUS + RADIUS_STEP * level;
		ctx.beginPath();
		ctx.moveTo(radius * superformula(0), 0);
		for (let angle = angleStep; angle < PI2; angle += angleStep) {
			const r = superformula(angle) * radius;
			const x = cos(angle) * r;
			const y = sin(angle) * r;
			ctx.lineTo(x, y);
		}
		ctx.closePath();
		ctx.fillStyle = `rgb(${level % 2 ? FG : BG})`;
		ctx.fill();
	}
}

const gui = new dat.GUI();
gui.close();

const defaults = {
	a: 8,
	b: 8,
	m1: 2,
	m2: 100,
	n1: 12,
	n2: 3,
	n3: 6,
	smoothness: 120,
};
Object.keys(defaults).forEach(key => {
	let item;
	switch (key) {
		case 'smoothness':
			item = gui.add(defaults, key).min(10);
			break;
		default:
			item = gui.add(defaults, key, -100, 100);
	}
	item.step(1).onChange(draw.bind(defaults));
});
gui.remember(defaults);

draw.call(defaults);
document.body.style.background = `rgb(${BG})`;
document.body.appendChild(canvas);

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console