Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="no-support" data-support="css-trig-fns"><p>🚨 Your browser does not support the CSS Trigonometric Functions. Therefore, this demo will not work properly. Please try Safari 15.4, Firefox 108, or Chrome 111.</p></div>

<form data-has-support="at-property" method="">
	<label for="enable_animation">
		<input type="checkbox" name="enable_animation" id="enable_animation" checked />
		Auto-rotate
	</label>
	<label for="show_debug">
		<input type="checkbox" name="show_debug" id="show_debug" />
		Show Debug Lines
	</label>
</form>

<div class="wrapper">
	<div id="visual" data-countchildren>
		<span class="dot"></span>
		<span class="dot"></span>
		<span class="dot"></span>
		<span class="dot"></span>
		<span class="dot"></span>
	</div>
</div>

<div id="controls" data-show-when-has-support="css-trig-fns">
	<button data-trigger="add">Add dot</button>
	<button data-trigger="remove">Remove dot</button>
</div>

<footer>
	<p>Demo for <a href="https://web.dev/css-trig-functions/" target="_top">https://web.dev/css-trig-functions/</a></p>
</footer>
              
            
!

CSS

              
                @layer base, demo, demosupport;

/* Register --angle. That way we can transition and animate it 😎 */
@property --angle {
	syntax: '<angle>';
	initial-value: 0deg;
	inherits: true;
}
/* Initial value for browsers that don’t support @property */
:root {
	--angle: 0deg;
}

/* Keyframes that animates the --angle value */
@keyframes adjust-angle {
	to {
		--angle: 360deg;
	}
}

/* Enable animation when checkbox is checked */
:root {
	animation: adjust-angle linear 20s infinite paused;
}
:root:has(#enable_animation:checked) {
	animation-play-state: running;
}

/* The colored dots */
.dot {
	/* Spread the dots evenly over the circle.
	   We do this by dividing 360deg by the number of children.
       E.g. 360 degrees / 3 children = 120 degrees between each child element.	
	*/
	--offset-per-child: calc(360deg / (var(--nth-siblings) + 1));
	
	/* Each child will that offset into account, based its index.
	   E.g. - the 1st out of 3 children gets offset by 0 x 120deg = 0deg
	        - the 2nd out of 3 children gets offset by 1 x 120deg = 120deg
	        - the 3rd out of 3 children gets offset by 2 x 120deg = 240deg
	*/
	--angle-offset: calc(var(--nth-child) * var(--offset-per-child));
	
	/* Make it round */
	display: block;
	width: var(--tracksize);
	aspect-ratio: 1;
	border-radius: 50%;
	
	/* Color it, giving each color a different hue based on the --angle-offset */
	background: hsl(var(--angle-offset) 100% 50%);
	border: 1px solid #333;
	
	/* Center it */
	position: absolute;
	left: calc(50% - (var(--tracksize) / 2));
	top: calc(50% - (var(--tracksize) / 2));
	
	/* Adjust its position based on the --angle, while also taking the --angle-offset into account */
	translate: calc(cos((var(--angle) + var(--angle-offset))) * var(--radius))
		       calc(sin((var(--angle) + var(--angle-offset))) * var(--radius) * -1);
	
	/* Other things */
	pointer-events: none;
}


















@layer demo {
	@layer visual {
		/* The visualization */
		#visual {
			/* Dimensions of the visualization */
			--radius: 20vmin;
			--tracksize: 5vmin;

			/* Make it a circle, based on the dimensions */
			width: calc(var(--radius) * 2 + var(--tracksize));
			aspect-ratio: 1;
			border-radius: 50%;
			border: var(--tracksize) solid transparent;

			/* Some generic positioning stuff */
			margin: 0 auto;
			position: relative;
		}

		#visual {
			border-color: #e0e0e0;
		}

		/* Inject a dot at the center of the visualization */
		#visual::after {
			content: '';
			--size: calc(var(--tracksize) / 2);

			/* Make it round */
			display: block;
			width: var(--size);
			aspect-ratio: 1;
			border-radius: 50%;

			/* Put it in the center */
			position: absolute;
			left: calc(50% - (var(--size) / 2));
			top: calc(50% - (var(--size) / 2));
			z-index: 2;

			/* Make it black */
			background: #333;
		}
	}
	
	@layer debug {
		/* Draw debug lines for the red dot */
		:root:has(#show_debug:checked) .dot:nth-last-child(1)::before,
		:root:has(#show_debug:checked) .dot:nth-last-child(1)::after {
			/* Inject it */
			content: '';
			display: block;

			/* Color it */
			background: hsl(var(--angle-offset) 100% 50%);

			/* Position it */
			position: absolute;
			left: calc(50%);
			top: calc(50%);
			z-index: -1;

			transform-origin: 0 0;
		}

		:root:has(#show_debug:checked) .dot:nth-last-child(1)::before {
			width: var(--radius);
			height: 2px;

			/* Draw line on horizontal axis*/
			transform: 
				/* Adjust size based on cos() of angle */
				scaleX(cos(calc(var(--angle) + var(--angle-offset))))
				/* Move back so that it goes into the center of the dot */
				translate(-100%, -50%)
				/* Move back so that it appears on X axis instead of on the dot itself */
				translateY(calc(sin((var(--angle) + var(--angle-offset))) * var(--radius)))
			;
		}

		:root:has(#show_debug:checked) .dot:nth-last-child(1)::after {
			width: 2px;
			height: var(--radius);

			/* Draw line on vertical axis*/
			transform: 
				/* Adjust size based on sin() of angle */
				scaleY(sin(calc((var(--angle) + var(--angle-offset)) * -1)))
				/* Move back so that it goes into the center of the dot */
				translate(-50%, -100%)
			;
		}
	}
}


@layer demosupport {
	@layer layout {
		label, #controls, form {
			text-align: center;
		}
	}	
}

/* Non-demo styles below */
@layer base {
	@layer reset {
		* {
			box-sizing: border-box;
		}
		body {
			margin: 0;
			padding: 0;
		}
		html, body {
			height: 100%;
		}
	}
	@layer layout {
		html {
			max-width: 84ch;
			padding: 3rem 2rem;
			margin: auto;
		}
		body {
			display: grid;
			place-content: safe center;
			gap: 3rem;
		}

		html {
			--font-sans: system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif;
			--font-serif: ui-serif,serif;
			--font-mono: Dank Mono,Operator Mono,Inconsolata,Fira Mono,ui-monospace,SF Mono,Monaco,Droid Sans Mono,Source Code Pro,monospace;
		}
		body {
			font-family: var(--font-sans);
		}
		pre, code {
			font-family: var(--font-mono);
			font-size: 0.8rem;
		}
		input, button {
			font-family: inherit;
		}
		
		a,
		a:visited {
			color: blue;
		}
		
		h2 {
			margin-top: 2em;
		}

		summary {
			cursor: pointer;
		}

		dd + dt {
			margin-top: 0.5em;
		}

		button {
			cursor: pointer;
		}
		
		footer {
			text-align: center;
			font-style: italic;
		}
	}
	
	@layer code {
		pre {
			border: 1px solid #dedede;
			padding: 1em;
			background: #f7f7f7;
			font-family: "Courier 10 Pitch", Courier, monospace;
			overflow-x: auto;
			border-left: 0.4em solid cornflowerblue;
			tab-size: 4;
		}
		
		code:not(pre code), output:not(code:has(output) output) {
			background: #f7f7f7;
			border: 1px solid rgb(0 0 0 / 0.2);
			padding: 0.1rem 0.3rem;
			margin: 0.1rem 0;
			border-radius: 0.2rem;
			display: inline-block;
		}
	}

	@layer support {
		.no-support,
		.has-support {
			margin: 1em 0;
			padding: 1em;
			border: 1px solid #ccc;
		}

		.no-support {
			background-color: #ff00002b;
		}
		.no-support[data-level="warn"] {
			background-color: #ffff002b;
		}
		.has-support {
			background-color: #00ff002b;
		}
		.no-support, [data-show-when-no-support] {
			display: block !important;
		}
		.has-support, [data-show-when-has-support] {
			display: none !important;
		}
		:is(.has-support, .no-support) > :first-child {
			margin-top: 0;
		}
		:is(.has-support, .no-support) > :last-child {
			margin-bottom: 0;
		}
		
		@property --supports-at-property {
			syntax: "*";
			initial-value: ;
			inherits: true;
		}
		.no-support[data-support="at-property"], [data-no-support="at-property"] {
			--value-when-supported: var(--supports-at-property) none;
			--value-when-not-supported: block;
			display: var(--value-when-supported, var(--value-when-not-supported)) !important;
		}
		.has-support[data-support="at-property"], [data-has-support="at-property"] {
			--value-when-supported: var(--supports-at-property) block;
			--value-when-not-supported: none;
			display: var(--value-when-supported, var(--value-when-not-supported)) !important;
		}

		@supports (transform: scaleX(cos(360deg))) {
			.no-support[data-support="css-trig-fns"] {
				display: none !important;
			}
			.has-support[data-support="css-trig-fns"], [data-show-when-has-support="css-trig-fns"] {
				display: block !important;
			}
		}
	}
	
	@layer nth-child {
		[data-countchildren] > :nth-child(1) {
			--nth-child: 1;
		}
		[data-countchildren] > :nth-child(2) {
			--nth-child: 2;
		}
		[data-countchildren] > :nth-child(3) {
			--nth-child: 3;
		}
		[data-countchildren] > :nth-child(4) {
			--nth-child: 4;
		}
		[data-countchildren] > :nth-child(5) {
			--nth-child: 5;
		}
		[data-countchildren] > :nth-child(6) {
			--nth-child: 6;
		}
		[data-countchildren] > :nth-child(7) {
			--nth-child: 7;
		}
		[data-countchildren] > :nth-child(8) {
			--nth-child: 8;
		}
		[data-countchildren] > :nth-child(9) {
			--nth-child: 9;
		}
		[data-countchildren] > :nth-child(10) {
			--nth-child: 10;
		}

		[data-countchildren]:has(> :nth-child(1):last-child) > * {
			--nth-siblings: 0;
		}
		[data-countchildren]:has(> :nth-child(2):last-child) > * {
			--nth-siblings: 1;
		}
		[data-countchildren]:has(> :nth-child(3):last-child) > * {
			--nth-siblings: 2;
		}
		[data-countchildren]:has(> :nth-child(4):last-child) > * {
			--nth-siblings: 3;
		}
		[data-countchildren]:has(> :nth-child(5):last-child) > * {
			--nth-siblings: 4;
		}
		[data-countchildren]:has(> :nth-child(6):last-child) > * {
			--nth-siblings: 5;
		}
		[data-countchildren]:has(> :nth-child(7):last-child) > * {
			--nth-siblings: 6;
		}
		[data-countchildren]:has(> :nth-child(8):last-child) > * {
			--nth-siblings: 7;
		}
		[data-countchildren]:has(> :nth-child(9):last-child) > * {
			--nth-siblings: 8;
		}
		[data-countchildren]:has(> :nth-child(10):last-child) > * {
			--nth-siblings: 9;
		}
	}
}
              
            
!

JS

              
                const $visual = document.querySelector('#visual');
const $add = document.querySelector('button[data-trigger="add"]');
const $remove = document.querySelector('button[data-trigger="remove"]');

let numDots = document.querySelectorAll('.dot').length;
const maxNumDots = 10;

$add.addEventListener('click', e => {
	if (numDots == maxNumDots) return;
	
	// Create new dot and append it
	const $newDot = document.createElement('span');
	$newDot.classList.add('dot');
	$visual.appendChild($newDot);
	
	numDots++;
});

$remove.addEventListener('click', e => {
	if (numDots == 1) return;
	
	// Find last dot and remove it
	const $lastDot = $visual.querySelector(':scope > .dot:last-child');
	$lastDot.remove();
	
	numDots--;
});

// Firefox does not support :has() yet, so we have to set the --nth-siblings count through JS
const supportsComplexHas = CSS.supports('selector(:has(>*))');
if (!supportsComplexHas) {
	const updateChildCount = ($parent) => {
		$parent.querySelectorAll(':scope > *').forEach($child => {
			$child.style.setProperty('--nth-siblings', $parent.childElementCount - 1);
		});
	};
	const updateChildCountAll = () => {
		document.querySelectorAll('[data-countchildren]').forEach($parent => {
			updateChildCount($parent);
		});
	}
	updateChildCountAll();
	
	$add.addEventListener('click', e => {
		updateChildCount($visual);
	});
	$remove.addEventListener('click', e => {
		updateChildCount($visual);
	});
}
              
            
!
999px

Console