HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!-- Generative design replicating the cover of Young The Giant's self titled album -->
<div class="container"></div>
<button class="button" aria-label="Regenerate" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="refresh-icon">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg> <span>Regenerate</span></button>
* {
box-sizing: border-box;
}
body {
position: relative;
background: #333;
}
.container {
height: 100vh;
width: 100vw;
padding: 1rem;
svg {
width: 100%;
aspect-ratio: 1;
overflow: hidden;
}
svg * {
/* Removes little gaps between the shapes */
shape-rendering: geometricprecision;
}
}
svg image {
width: 100%;
height: auto;
}
.button {
position: absolute;
bottom: 0;
right: 0;
padding: 0.5rem;
margin: 0;
border-top-left-radius: 12px;
background: #ffffffcc;
border: 0.5px solid #555555cc;
line-height: 1;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 0.25rem;
font-weight: 600;
color: #333;
font-size: 0.75rem;
svg {
width: 1.25rem;
transition: 0.3s ease-in-out transform;
}
&:hover svg {
transform: rotate(-45deg);
}
&:focus,
&:active {
outline: 2px solid #f06;
svg {
transform: rotate(-90deg);
}
}
}
import { SVG, Color } from "https://cdn.skypack.dev/@svgdotjs/svg.js@3.1.1";
import { random } from "https://cdn.skypack.dev/@georgedoescode/generative-utils@1.0.37";
import tinycolor from "https://cdn.skypack.dev/tinycolor2@1.4.2";
import gsap from "https://cdn.skypack.dev/gsap@3.9.1";
import "https://cdn.skypack.dev/@svgdotjs/svg.filter.js@3.0.8";
let draw, colors, colorPalette, moonSize;
let images = [];
const backgroundTexture = "https://assets.codepen.io/1385231/goldpaper.jpg";
const moonTexture = "https://assets.codepen.io/1385231/moonpaper2.jpg";
const textures = [
"https://assets.codepen.io/1385231/bluepaper.jpg",
"https://assets.codepen.io/1385231/whitepaper.jpg",
"https://assets.codepen.io/1385231/graypaper.jpg",
"https://assets.codepen.io/1385231/plasterpaper.jpg"
];
function makePyramid(index) {
const group = draw.group().addClass(`pyramid-group pyramid-group-${index}`);
const mask = draw.rect(100, 100).fill("#fff");
group.maskWith(mask);
let baseCenter, scale;
if (index === 0) {
baseCenter = 85;
scale = 1.5;
} else if (index === 1) {
baseCenter = 12;
scale = 1.9;
} else if (index === 2) {
baseCenter = 50;
scale = 1;
}
// make base points
const baseA = random(baseCenter - 50 / scale, baseCenter - 30 / scale, true);
const baseB = random(baseCenter - 20 / scale, baseCenter + 20 / scale, true);
const baseC = random(baseCenter + 30 / scale, baseCenter + 50 / scale, true);
// pick top point
const peakX = random(baseCenter - 5, baseCenter + 5, true);
const peakY = random(25, 45, true) * scale;
const peak = [peakX, peakY];
// Create Masks
const leftMask = group
.polygon(`${baseA},100 ${peak[0]},${peak[1]} ${baseB},100`)
.fill("#fff");
const rightMask = group
.polygon(`${baseB},100 ${peak[0]},${peak[1]} ${baseC},100`)
.fill("#fff");
const [textureOne, textureTwo] = getTwoUniqueFromArray(textures);
const [colorOne, colorTwo] = getTwoUniqueFromArray(colorPalette);
var leftTexture = group.image(textureOne);
var rightTexture = group.image(textureTwo);
leftTexture.maskWith(leftMask);
rightTexture.maskWith(rightMask);
// draw right triangle
const rightTriangle = group
.polygon(`${baseB},100 ${peak[0]},${peak[1]} ${baseC},100`)
.fill(colorOne)
.css("mix-blend-mode", "multiply");
// draw left triangle
const leftTriangle = group
.polygon(`${baseA},100 ${peak[0]},${peak[1]} ${baseB},100`)
.fill(colorTwo)
.css("mix-blend-mode", "multiply");
group.opacity(0.85);
}
function drawBackground() {
const bgMask = draw.rect(100, 100).attr({ fill: "#777" });
var bgTexture = draw.image(backgroundTexture).maskWith(bgMask);
draw
.rect(100, 100)
.attr({ fill: colorPalette[0] })
.css({ "mix-blend-mode": "color" });
var gradient = draw
.gradient("linear", function (add) {
add.stop(0, "#333");
add.stop(1, "#fff");
})
.from(0, 0)
.to(0, 1);
draw
.rect(100, 100)
.fill(gradient)
.opacity(0.7)
.css({ "mix-blend-mode": "overlay" });
}
function drawMoon() {
const xPos = random(30, 70, true);
const yPos = random(20, 40, true);
moonSize = random(40, 60, true);
const group = draw.group().addClass("moon-group");
const moon = group
.circle(moonSize)
.fill("#fff")
.center(xPos, yPos)
.addClass("moon");
group.circle(moonSize).fill(colorPalette[0]).center(xPos, yPos);
const moonImage = group
.image(moonTexture)
.maskWith(moon)
.css({ "mix-blend-mode": "multiply" })
.opacity(1);
// Second Circle
const radius = moonSize / 2;
// const angle = random(0, 360, true);
// const xOffset = radius * Math.cos(angle);
// const yOffset = radius * Math.sin(angle);
const tinyMoonGroup = draw.group().addClass("moon-tiny");
const tinyMoon = tinyMoonGroup
.circle(moonSize * 0.6)
.fill(colorPalette[0])
.center(xPos - radius, yPos)
.css({ "mix-blend-mode": "overlay" });
tinyMoonGroup.circle(1).center(xPos, yPos).fill("none");
group.add(tinyMoonGroup);
group.maskWith(draw.rect(100, 100).fill("white"));
}
function generateNewDrawing() {
colorPalette = random(colors);
draw.clear();
drawBackground();
drawMoon();
for (let i = 0; i < 3; i++) {
makePyramid(i);
}
const tl = gsap.timeline();
tl.fromTo(
".pyramid-group",
{ y: 10, opacity: 0 },
{ y: 0, opacity: 0.8, stagger: 0.2, duration: 1, ease: "power3.out" }
);
tl.fromTo(".moon-group", { opacity: 0 }, { opacity: 1, duration: 1 }, 0.8);
// Slide Tiny Moon
const rotation = random(0, 360, true);
tl.fromTo(
".moon-tiny",
{ opacity: 0, rotate: `${rotation}deg`, transformOrigin: "right center" },
{
opacity: random(0.5, 0.8),
rotate: `${rotation + 180}deg`,
transformOrigin: "right center",
duration: 2,
ease: "power3.out"
},
1.5
);
}
async function init() {
colors = await fetch(
"https://unpkg.com/nice-color-palettes@3.0.0/100.json"
).then((response) => response.json());
let svg = SVG()
.addTo(".container")
.size("100%", "100%")
.viewbox("0,0,100,100");
draw = svg.group();
draw.maskWith(svg.rect(100, 100).fill("white"));
generateNewDrawing();
}
function getTwoUniqueFromArray(arr) {
let indexOne = random(0, arr.length - 1, true);
let indexTwo = random(0, arr.length - 1, true);
if (indexOne === indexTwo) {
indexTwo = indexOne === arr.length - 1 ? 0 : indexOne + 1;
}
return [arr[indexOne], arr[indexTwo]];
}
init();
document.querySelector(".button").addEventListener("click", generateNewDrawing);
Also see: Tab Triggers