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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<svg id="svg"></svg>
<header>
<h1>Random Flower Generator</h1>
<a href="https://codepen.io/collection/DPjZxo/" target="_blank">SVG Collection</a>
</header>
<footer>
Click to generate random flowers
</footer>
body {
margin: 0;
font-family: 'Montserrat', sans-serif;
font-size: 0.9rem;
color: #000;
text-shadow: 1px 1px 1px #fff;
}
#svg {
position: fixed;
width: 100%;
height: 100%;
cursor: pointer;
filter: drop-shadow(0 0 4px #000);
}
.flower path {
stroke-linecap: round;
stroke-linejoin: round;
}
.layer {
animation-name: layerAnim1;
animation-duration: 4s;
animation-fill-mode: both;
transform-box: fill-box;
transform-origin: center center;
}
.layer path {
stroke: rgba(0, 0, 0, 0.7);
stroke-width: 0.75px;
}
a {
color: #555;
text-decoration: none;
border-bottom: 0.25rem solid transparent;
transition: all 0.4s;
}
a:hover {
color: #000;
border-bottom-color: rgba(0, 0, 0, 0.7);
}
header, footer {
position: absolute;
width: 100%;
text-align: center;
}
header h1 {
font-size: 2em;
margin: 0.5em 0 0;
}
footer {
bottom: 0.5em;
}
@-moz-document url-prefix() {
#svg {
filter: none;
}
.layer {
filter: drop-shadow(0 0 2px #000);
}
}
@keyframes layerAnim1 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes layerAnim2 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: scale3d(0.2, 0.2, 0.2); }
to { opacity: 1; transform: scale3d(1, 1, 1); }
}
@keyframes layerAnim3 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: rotateZ(-270deg); }
to { opacity: 1; opacity: 1; transform: rotateZ(0deg); }
}
@keyframes layerAnim4 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: rotateZ(360deg) scale3d(0.2, 0.2, 0.2); }
to { opacity: 1; opacity: 1; transform: rotateZ(0deg) scale3d(1, 1, 1); }
}
@keyframes layerAnim5 {
from, 20%, 40%, 60%, 80%, to {
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
0% {
opacity: 0;
transform: scale3d(0.3, 0.3, 0.3);
}
20% {
transform: scale3d(1.1, 1.1, 1.1);
}
40% {
transform: scale3d(0.9, 0.9, 0.9);
}
60% {
transform: scale3d(1.03, 1.03, 1.03);
}
80% {
transform: scale3d(0.97, 0.97, 0.97);
}
to {
opacity: 1;
transform: scale3d(1, 1, 1);
}
}
@keyframes layerAnim6 {
from, 20%, 40%, 60%, 80%, to {
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
0% {
opacity: 0;
transform: rotateZ(180deg) scale3d(0.3, 0.3, 0.3);
}
20% {
transform: rotateZ(-25.71deg) scale3d(1.1, 1.1, 1.1);
}
40% {
transform: rotateZ(25.71deg) scale3d(0.9, 0.9, 0.9);
}
60% {
transform: rotateZ(-7.71deg) scale3d(1.03, 1.03, 1.03);
}
80% {
transform: rotateZ(7.71deg) scale3d(0.97, 0.97, 0.97);
}
to {
opacity: 1;
transform: rotateZ(0deg) scale3d(1, 1, 1);
}
}
@keyframes layerAnim7 {
from, 20%, 40%, 60%, 80%, to {
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
0% {
opacity: 0;
transform: rotateZ(-180deg) scale3d(0.3, 0.3, 0.3);
}
20% {
transform: rotateZ(25.71deg) scale3d(1.1, 1.1, 1.1);
}
40% {
transform: rotateZ(-25.71deg) scale3d(0.9, 0.9, 0.9);
}
60% {
transform: rotateZ(7.71deg) scale3d(1.03, 1.03, 1.03);
}
80% {
transform: rotateZ(-7.71deg) scale3d(0.97, 0.97, 0.97);
}
to {
opacity: 1;
transform: rotateZ(0deg) scale3d(1, 1, 1);
}
}
@keyframes layerAnim8 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: rotate3d(1, 1, 1, 360deg); }
to { opacity: 1; transform: rotate3d(1, 1, 1, 0deg); }
}
@keyframes layerAnim9 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: rotate3d(1, 1, 1, 360deg) scale3d(0.1, 0.1, 0.1); }
to { opacity: 1; transform: rotate3d(1, 1, 1, 0deg) scale3d(1, 1, 1); }
}
@keyframes layerAnim10 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: scale3d(2, 2, 2); }
to { opacity: 1; transform: scale3d(1, 1, 1); }
}
@keyframes layerAnim11 {
from, to { animation-timing-function: ease-out; }
from { opacity: 0; transform: rotateZ(-360deg) scale3d(2, 2, 2); }
to { opacity: 1; transform: rotateZ(0deg) scale3d(1, 1, 1); }
}
const { random, PI, sin, cos, round, floor, ceil, min, max } = Math;
function App() {
const svg = document.getElementById('svg');
let width, height, cx, cy;
let zoom = 100;
let vbOX = 0;
let vbOY = 0;
init();
function init() {
onResize();
initEventHandlers();
new Flower({
parent: svg,
x: 0,
y: 0,
petals: 8,
rings: 12,
step: 4,
colors: ['#21B91B', '#FFFF80', '#D1104D', '#7F266A'],
alpha: 0.8,
angle: rnd(PI),
layerAnim: 'layerAnim5',
reverseDelay: false,
});
}
function initEventHandlers() {
window.addEventListener('resize', onResize);
document.body.addEventListener('click', onClick);
}
function onClick(e) {
createFlower({
x: e.clientX - cx,
y: e.clientY - cy,
});
}
function createFlower(params) {
const iRadiusCoef = rnd(1, 7);
const oRadiusCoef = rnd(iRadiusCoef, 7);
new Flower({
parent: svg,
petals: rndInt(4, 12),
rings: rndInt(3, 8),
step: rndInt(3, 8),
alpha: rnd(0.7, 1),
angle: rnd(PI),
iRadius: rnd(2, 5),
iRadiusCoef,
oRadius: rnd(5, 10),
oRadiusCoef,
...params
});
}
function onResize() {
const r = svg.getBoundingClientRect();
width = r.width;
height = r.height;
cx = width / 2;
cy = height / 2;
updateViewBox();
}
function updateViewBox() {
const vbW = (width * 100) / zoom;
const vbH = (height * 100) / zoom;
const vbX = vbOX - vbW / 2;
const vbY = vbOY - vbH / 2;
svg.setAttribute('viewBox', `${vbX} ${vbY} ${vbW} ${vbH}`);
}
}
class Flower {
constructor(conf) {
this.conf = {
alpha: 1,
angle: 0,
iRadius: 2, iRadiusCoef: 5,
oRadius: 5, oRadiusCoef: 10,
layerAnim: 'layerAnim' + (rndInt(1, 12)),
reverseDelay: rnd(1) > 0.5,
...conf
};
this.create();
}
create() {
let { parent, x, y, petals, rings, step, colors, alpha, angle, iRadius, iRadiusCoef, oRadius, oRadiusCoef, layerAnim, reverseDelay } = this.conf;
this.group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
this.group.classList.add('flower');
parent.appendChild(this.group);
if (!colors) {
colors = [chroma.random(), chroma.random()];
if (rings > 6) colors.push(chroma.random());
}
const cscale = chroma.scale(colors);
let layer, np, color, di;
for (let i = rings; i > 0; i--) {
layer = document.createElementNS('http://www.w3.org/2000/svg', 'g');
layer.classList.add('layer');
np = floor((i + step - 1) / step) * petals;
color = cscale(i * 1 / rings).alpha(alpha);
this.createPetalsRing(layer, x, y, np, iRadius + i * iRadiusCoef, oRadius + i * oRadiusCoef, angle + i % 2 * PI / np, color);
di = reverseDelay ? (rings - i + 1) : i;
layer.style.animationName = layerAnim;
layer.style.animationDuration = (rings * 0.3) + 's';
layer.style.animationDelay = (di * 0.15) + 's';
this.group.appendChild(layer);
}
}
createPetalsRing(parent, x, y, n, iRadius, oRadius, angle, fill) {
const da = 2 * PI / n;
const dr = oRadius - iRadius;
const cpda = rnd(0.5 * da / 5, 1.5 * da / 5);
const cpdr = rnd(dr * 0.25, dr * 1.1);
let a;
for (let i = 0; i < n; i++) {
a = angle + i * da;
new Petal({
parent,
x, y,
startA: a,
endA: a + da,
iRadius,
oRadius,
cpda,
cpdr,
fill,
});
}
}
}
class Petal {
constructor({ parent, x, y, startA, endA, iRadius, oRadius, cpda, cpdr, fill }) {
this.parent = parent;
this.fill = fill;
const da = endA - startA;
const iRadius0 = iRadius / 5;
this.sp = [x + cos(startA + da / 2) * iRadius0, y + sin(startA + da / 2) * iRadius0];
this.cp1 = [x + cos(startA - cpda) * (iRadius + cpdr), y + sin(startA - cpda) * (iRadius + cpdr)];
this.cp2 = [x + cos(endA + cpda) * (iRadius + cpdr), y + sin(endA + cpda) * (iRadius + cpdr)];
this.ep = [x + cos(startA + da / 2) * oRadius, y + sin(startA + da / 2) * oRadius];
this.curve1 = new QCurve({ sp: this.sp, cp: this.cp1, ep: this.ep });
this.curve2 = new QCurve({ sp: this.sp, cp: this.cp2, ep: this.ep });
this.create();
}
create() {
this.path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
this.path.setAttribute('d', this.pathD());
this.path.style.fill = this.fill;
this.parent.appendChild(this.path);
}
pathD() {
return [
this.curve1.pathD(),
this.curve2.pathD(),
].join(' ');
}
}
class QCurve {
constructor({ sp, cp, ep }) {
this.sp = sp;
this.cp = cp;
this.ep = ep;
}
pathD() {
return [
`M${this.sp[0]} ${this.sp[1]}`,
`Q${this.cp[0]} ${this.cp[1]}`,
`${this.ep[0]} ${this.ep[1]}`,
].join(' ');
}
}
function clamp(v, min, max) {
return min(max(v, min), max);
}
function rnd(min, max) {
if (max === undefined) { max = min; min = 0; }
return random() * (max - min) + min;
}
function rndInt(min, max) {
if (max === undefined) { max = min; min = 0; }
min = ceil(min);
max = floor(max);
return floor(random() * (max - min)) + min;
}
App();
Also see: Tab Triggers