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.
<!-- 4 min Tutorial: https://youtu.be/G9207EJySaA -->
:root {
--glow-rgb: 239 42 201;
}
body {
background: linear-gradient(145deg, rgb(119, 46, 195), rgb(58, 18, 153));
height: 100vh;
overflow: hidden;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.glow-point {
position: absolute;
box-shadow: 0rem 0rem 1.2rem 0.6rem rgb(var(--glow-rgb));
pointer-events: none;
}
.star {
position: absolute;
z-index: 2;
color: white;
font-size: 1rem;
animation-duration: 1500ms;
animation-fill-mode: forwards;
pointer-events: none;
}
@keyframes fall-1 {
0% {
transform: translate(0px, 0px) rotateX(45deg) rotateY(30deg) rotateZ(0deg) scale(0.25);
opacity: 0;
}
5% {
transform: translate(10px, -10px) rotateX(45deg) rotateY(30deg) rotateZ(0deg) scale(1);
opacity: 1;
}
100% {
transform: translate(25px, 200px) rotateX(180deg) rotateY(270deg) rotateZ(90deg) scale(1);
opacity: 0;
}
}
@keyframes fall-2 {
0% {
transform: translate(0px, 0px) rotateX(-20deg) rotateY(10deg) scale(0.25);
opacity: 0;
}
10% {
transform: translate(-10px, -5px) rotateX(-20deg) rotateY(10deg) scale(1);
opacity: 1;
}
100% {
transform: translate(-10px, 160px) rotateX(-90deg) rotateY(45deg) scale(0.25);
opacity: 0;
}
}
@keyframes fall-3 {
0% {
transform: translate(0px, 0px) rotateX(0deg) rotateY(45deg) scale(0.5);
opacity: 0;
}
15% {
transform: translate(7px, 5px) rotateX(0deg) rotateY(45deg) scale(1);
opacity: 1;
}
100% {
transform: translate(20px, 120px) rotateX(-180deg) rotateY(-90deg) scale(0.5);
opacity: 0;
}
}
let start = new Date().getTime();
const originPosition = { x: 0, y: 0 };
const last = {
starTimestamp: start,
starPosition: originPosition,
mousePosition: originPosition
}
const config = {
starAnimationDuration: 1500,
minimumTimeBetweenStars: 250,
minimumDistanceBetweenStars: 75,
glowDuration: 75,
maximumGlowPointSpacing: 10,
colors: ["249 146 253", "252 254 255"],
sizes: ["1.4rem", "1rem", "0.6rem"],
animations: ["fall-1", "fall-2", "fall-3"]
}
let count = 0;
const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
selectRandom = items => items[rand(0, items.length - 1)];
const withUnit = (value, unit) => `${value}${unit}`,
px = value => withUnit(value, "px"),
ms = value => withUnit(value, "ms");
const calcDistance = (a, b) => {
const diffX = b.x - a.x,
diffY = b.y - a.y;
return Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2));
}
const calcElapsedTime = (start, end) => end - start;
const appendElement = element => document.body.appendChild(element),
removeElement = (element, delay) => setTimeout(() => document.body.removeChild(element), delay);
const createStar = position => {
const star = document.createElement("span"),
color = selectRandom(config.colors);
star.className = "star fa-solid fa-sparkle";
star.style.left = px(position.x);
star.style.top = px(position.y);
star.style.fontSize = selectRandom(config.sizes);
star.style.color = `rgb(${color})`;
star.style.textShadow = `0px 0px 1.5rem rgb(${color} / 0.5)`;
star.style.animationName = config.animations[count++ % 3];
star.style.starAnimationDuration = ms(config.starAnimationDuration);
appendElement(star);
removeElement(star, config.starAnimationDuration);
}
const createGlowPoint = position => {
const glow = document.createElement("div");
glow.className = "glow-point";
glow.style.left = px(position.x);
glow.style.top = px(position.y);
appendElement(glow)
removeElement(glow, config.glowDuration);
}
const determinePointQuantity = distance => Math.max(
Math.floor(distance / config.maximumGlowPointSpacing),
1
);
/* --
The following is an explanation for the "createGlow" function below:
I didn't cover this in my video, but I ran into an issue where moving the mouse really quickly caused gaps in the glow effect. Kind of like this:
* * * * * * 🖱️
instead of:
*************************************🖱️
To solve this I sort of "backfilled" some additional glow points by evenly spacing them in between the current point and the last one. I found this approach to be more visually pleasing than one glow point spanning the whole gap.
The "quantity" of points is based on the config property "maximumGlowPointSpacing".
My best explanation for why this is happening is due to the mousemove event only firing every so often. I also don't think this fix was totally necessary, but it annoyed me that it was happening so I took on the challenge of trying to fix it.
-- */
const createGlow = (last, current) => {
const distance = calcDistance(last, current),
quantity = determinePointQuantity(distance);
const dx = (current.x - last.x) / quantity,
dy = (current.y - last.y) / quantity;
Array.from(Array(quantity)).forEach((_, index) => {
const x = last.x + dx * index,
y = last.y + dy * index;
createGlowPoint({ x, y });
});
}
const updateLastStar = position => {
last.starTimestamp = new Date().getTime();
last.starPosition = position;
}
const updateLastMousePosition = position => last.mousePosition = position;
const adjustLastMousePosition = position => {
if(last.mousePosition.x === 0 && last.mousePosition.y === 0) {
last.mousePosition = position;
}
};
const handleOnMove = e => {
const mousePosition = { x: e.clientX, y: e.clientY }
adjustLastMousePosition(mousePosition);
const now = new Date().getTime(),
hasMovedFarEnough = calcDistance(last.starPosition, mousePosition) >= config.minimumDistanceBetweenStars,
hasBeenLongEnough = calcElapsedTime(last.starTimestamp, now) > config.minimumTimeBetweenStars;
if(hasMovedFarEnough || hasBeenLongEnough) {
createStar(mousePosition);
updateLastStar(mousePosition);
}
createGlow(last.mousePosition, mousePosition);
updateLastMousePosition(mousePosition);
}
window.onmousemove = e => handleOnMove(e);
window.ontouchmove = e => handleOnMove(e.touches[0]);
document.body.onmouseleave = () => updateLastMousePosition(originPosition);
Also see: Tab Triggers