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>
	<canvas width="400" height="400"></canvas>
</div>
              
            
!

CSS

              
                body {
	margin: 0;
	min-block-size: 100svb;
	display: grid;
	place-items: center;
	color: hsl(206 6% 24%);
	background: hsl(210 6% 87%);
	font-family: system-ui;
}

div {
	display: inline-block;
	position: relative;
	aspect-ratio: 1/1;
}

div > button {
	position: absolute;
	inset-inline-start: 50%;
	inset-block-end: 0%;
	translate: -50% 0%;
}

div > canvas {
	display: block;
	width: 400px;
	height: 400px;
}

button {
	--_color: hsl(0 0% 99%);
	--_background: hsl(206 6% 24%);
	--_active: hsl(216 3% 38%);
	font: inherit;
	font-size: 1rem;
	color: var(--_color, hsl(0 0% 90%));
	background: var(--_background, hsl(0 0% 20%));
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.025rem;
	border: none;
	padding: 0.5rem 0.75rem;
	border-radius: 0.5rem;
	transition: scale 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
	transform-origin: 50% 100%;
}

button:focus {
	outline-offset: 0.25rem;
	outline-color: var(--_background, hsl(0 0% 20%));
}

button[data-state="roll"] {
	scale: 0.8;
	--_background: var(--_active);
}

body {
	background-image: url('data:image/svg+xml,<svg viewBox="-7.5 -2.5 15 10" xmlns="http://www.w3.org/2000/svg">\
  <circle id="d" fill="hsl(210 6%25 80%25)" r="0.3" />\
<g transform="translate(5 0)">\
    <use href="%23d" />\
    <use x="1" y="1" href="%23d" />\
    <use x="-1" y="1" href="%23d" />\
    <use x="-1" y="-1" href="%23d" />\
    <use x="1" y="-1" href="%23d" />\
</g>\
<g transform="translate(-5 0)">\
    <use x="1" y="1" href="%23d" />\
    <use x="-1" y="1" href="%23d" />\
    <use x="-1" y="-1" href="%23d" />\
    <use x="1" y="-1" href="%23d" />\
</g>\
<g transform="translate(0 5)">\
    <use x="1" href="%23d" />\
    <use x="-1" href="%23d" />\
    <use x="1" y="1" href="%23d" />\
    <use x="-1" y="1" href="%23d" />\
    <use x="-1" y="-1" href="%23d" />\
    <use x="1" y="-1" href="%23d" />\
</g>\
<g transform="translate(5 5)">\
    <use x="-1" y="1" href="%23d" />\
    <use x="1" y="-1" href="%23d" />\
</g>\
<g transform="translate(-5 5)">\
    <use href="%23d" />\
    <use x="-1" y="1" href="%23d" />\
    <use x="1" y="-1" href="%23d" />\
</g>\
</svg>');
	background-size: 200px;
}

              
            
!

JS

              
                const { Illustration, Group, Box, Hemisphere, TAU, easeInOut } = Zdog;

const root = document.querySelector("div");
const canvas = root.querySelector("canvas");
const button = document.createElement("button");

const colors = {
	white: "hsl(0 0% 99%)",
	black: "hsl(206 6% 24%)",
	red: "hsl(4 77% 55%)"
};

const stroke = 20;
const size = 50;
const diameter = 13;
const offset = 16;

const illustration = new Illustration({
	element: canvas,
	zoom: 2,
	rotate: {
		x: (TAU / 14) * -1,
		y: TAU / 8
	}
});

const dice = new Box({
	addTo: illustration,
	color: colors.white,
	stroke,
	width: size,
	height: size,
	depth: size
});

const dot = new Hemisphere({
	color: colors.black,
	stroke: 0,
	diameter
});

const one = new Group({
	addTo: dice,
	translate: {
		y: (size / 2 + stroke / 2) * -1
	},
	rotate: {
		x: TAU / 4
	}
});

dot.copy({
	addTo: one,
	color: colors.red
});

const six = new Group({
	addTo: dice,
	translate: {
		y: size / 2 + stroke / 2
	},
	rotate: {
		x: (TAU / 4) * -1
	}
});

for (const { x, y } of [
	{ x: offset, y: offset * -1 },
	{ x: offset, y: 0 },
	{ x: offset, y: offset },
	{ x: offset * -1, y: offset },
	{ x: offset * -1, y: 0 },
	{ x: offset * -1, y: offset * -1 }
]) {
	dot.copy({
		addTo: six,
		translate: {
			x,
			y
		}
	});
}

const two = new Group({
	addTo: dice,
	translate: {
		z: (size / 2 + stroke / 2) * -1
	},
	rotate: {
		x: TAU / 2
	}
});

for (const { x, y } of [
	{ x: offset, y: offset * -1 },
	{ x: offset * -1, y: offset }
]) {
	dot.copy({
		addTo: two,
		translate: {
			x,
			y
		}
	});
}

const five = new Group({
	addTo: dice,
	translate: {
		z: size / 2 + stroke / 2
	}
});

for (const { x, y } of [
	{ x: 0, y: 0 },
	{ x: offset, y: offset * -1 },
	{ x: offset, y: offset },
	{ x: offset * -1, y: offset },
	{ x: offset * -1, y: offset * -1 }
]) {
	dot.copy({
		addTo: five,
		translate: {
			x,
			y
		}
	});
}

const three = new Group({
	addTo: dice,
	translate: {
		x: (size / 2 + stroke / 2) * -1
	},
	rotate: {
		y: TAU / 4
	}
});

for (const { x, y } of [
	{ x: 0, y: -0 },
	{ x: offset, y: offset * -1 },
	{ x: offset * -1, y: offset }
]) {
	dot.copy({
		addTo: three,
		translate: {
			x,
			y
		}
	});
}

const four = new Group({
	addTo: dice,
	translate: {
		x: size / 2 + stroke / 2
	},
	rotate: {
		y: (TAU / 4) * -1
	}
});

for (const { x, y } of [
	{ x: offset, y: offset * -1 },
	{ x: offset, y: offset },
	{ x: offset * -1, y: offset },
	{ x: offset * -1, y: offset * -1 }
]) {
	dot.copy({
		addTo: four,
		translate: {
			x,
			y
		}
	});
}

illustration.updateRenderGraph();

button.textContent = "Roll";

let state = "wait";
let frame = null;
let ticker = 0;
const cycle = 125;

let angles = {
	x: illustration.rotate.x,
	y: illustration.rotate.y,
	z: illustration.rotate.z
};

let anglesNext = { ...angles };

const animate = () => {
	if (ticker >= cycle) {
		angles.x = anglesNext.x % TAU;
		angles.y = anglesNext.y % TAU;
		angles.z = anglesNext.z % TAU;

		illustration.rotate.x = angles.x;
		illustration.rotate.y = angles.y;
		illustration.rotate.z = angles.z;

		illustration.updateRenderGraph();

		state = "wait";
		button.setAttribute("data-state", state);

		ticker = 0;
		cancelAnimationFrame(frame);
	} else {
		const ease = easeInOut((ticker / cycle) % 1, 3);

		illustration.rotate.x = angles.x + (anglesNext.x - angles.x) * ease;
		illustration.rotate.y = angles.y + (anglesNext.y - angles.y) * ease;
		illustration.rotate.z = angles.z + (anglesNext.z - angles.z) * ease;

		illustration.updateRenderGraph();

		ticker++;
		frame = requestAnimationFrame(animate);
	}
};

const handleClick = () => {
	if (state !== "wait") return;

	const [x, y, z] = Array(3)
		.fill()
		.map((_) => (Math.floor(Math.random() * 4) * TAU) / 4 + TAU * 2);

	anglesNext = {
		x,
		y,
		z
	};

	state = "roll";
	button.setAttribute("data-state", state);
	animate();
};

button.addEventListener("click", handleClick);

root.appendChild(button);

              
            
!
999px

Console