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.
<div class="app">
<div class="page-header">
<h1>Fractal Glass Effect</h1>
<button id="downloadButton" class="primary-btn"><i class="ph ph-download"></i> Download</button>
</div>
<div class="container">
<div class="image-preview" id="imagePreview">
<img id="image" src="https://assets.lummi.ai/assets/QmYTMD4bKGJh1ZWLNDfBvFFrtLSgv2PCdqZYSTZsDtqqW7" alt="Preview">
<div class="wrapper" id="wrapper"></div>
</div>
<div class="controls-panel" id="controlsPanel">
<h3>Settings</h3>
<div id="fractal-options">
<div class="control-group">
<div><i class="ph ph-swap"></i><label for="flip-toggle">Flip</label></div>
<input type="checkbox" id="flip-toggle">
</div>
<div class="control-group">
<div><i class="ph ph-sparkles"></i><label for="shimmer-toggle">Shimmer</label></div>
<input type="checkbox" id="shimmer-toggle">
</div>
<div class="control-group">
<div><i class="ph ph-shapes"></i><label for="steps">Steps</label></div>
<input type="range" id="steps" min="12" max="64" value="33">
</div>
<div class="control-group">
<div><i class="ph ph-circle"></i><label for="scale">Scale</label></div>
<input type="range" id="scale" min="0" max="40" value="2">
</div>
<div class="control-group">
<div><i class="ph ph-activity"></i><label for="baseFrequency">Frequency</label></div>
<input type="range" id="baseFrequency" min="0" max="0.2" step="0.01" value="0.05">
</div>
<div class="control-group">
<div><i class="ph ph-wave-square"></i><label for="numOctaves">Octaves</label></div>
<input type="range" id="numOctaves" min="0" max="8" value="4">
</div>
</div>
<hr style="opacity: 0.2;">
<h3>Coloring</h3>
<div id="coloring-options">
<div class="control-group">
<div><i class="ph ph-palette"></i><label for="hue">Hue</label></div>
<input type="range" id="hue" min="-180" max="180" value="0">
</div>
<div class="control-group">
<div><i class="ph ph-paint-bucket"></i><label for="saturation">Saturation</label></div>
<input type="range" id="saturation" min="-100" max="100" value="0">
</div>
<div class="control-group">
<div><i class="ph ph-sun"></i><label for="brightness">Brightness</label></div>
<input type="range" id="brightness" min="-100" max="100" value="0">
</div>
</div>
</div>
<div class="image-menu">
<button id="addImageButton" class="secondary-btn menu-image"><i class="ph ph-plus"></i></button>
<img src="https://assets.lummi.ai/assets/QmYTMD4bKGJh1ZWLNDfBvFFrtLSgv2PCdqZYSTZsDtqqW7" alt="Image 1" class="menu-image active">
<img src="https://assets.lummi.ai/assets/QmXT5AVNEch6v3fSjTjfK33wj5k6KtWLQGtR7EejY29NfB" alt="Image 2" class="menu-image">
<img src="https://assets.lummi.ai/assets/QmUVsdq3KA66USVfKuxsSYunNUoJrXMEnYsJ4vZmPm1AW4" alt="Image 3" class="menu-image">
<img src="https://assets.lummi.ai/assets/QmeV2BbK4b1AoYpk7JEQJrEapAhhSNCgCwnm2aKTWBkCZT" alt="Image 4" class="menu-image">
<img src="https://assets.lummi.ai/assets/QmXes6G3jdCFFm8cH7CCGbPR82z2VhygPm4i5eYy11hA8e" alt="Image 5" class="menu-image">
<img src="https://assets.lummi.ai/assets/QmXfrYx91bommawJxQmoS1YDs9UnJBugq89SF1wTPDG6Pf" alt="Image 6" class="menu-image">
</div>
<div id="uploadModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Upload Image</h2>
<div id="dragDropArea" class="drag-drop-area">
<i class="ph ph-upload-simple"></i>
<p>Drag and drop your image here</p>
<button id="loadFromDevice" class="secondary-btn"><i class="ph ph-folder"></i> Load from Device</button>
<input type="file" id="fileUpload" style="display: none;">
</div>
<div class="url-upload">
<input type="text" id="imageUrl" placeholder="Enter image URL">
<button id="loadFromUrl" class="secondary-btn"><i class="ph ph-globe"></i> Load from URL</button>
</div>
</div>
</div>
</div>
<div class="footer">
<i class="ph ph-heart"></i> Pics by <a href="https://lummi.ai/" target="_blank">Lummi</a>
</div>
<svg width="0" height="0">
<filter id="displacementFilter">
<feturbulence type="turbulence" baseFrequency="0.05" numOctaves="4" result="turbulence" />
<fedisplacementmap in2="turbulence" in="SourceGraphic" scale="10" xChannelSelector="R" yChannelSelector="G" />
</filter>
</svg>
</div>
@import url("https://fonts.googleapis.com/css2?family=Inter+Tight:ital,wght@0,100..900;1,100..900&family=Inter:wght@100..900&family=Teachers:ital,wght@0,400..800;1,400..800&display=swap");
:root {
--font-size: 14px;
--primary-color: #0000ff;
--primary-color-hover:#7676FF;
--secondary-color: rgba(255, 255, 255, 0.12);
--text-color: #ffffff;
--text-color-subtle: rgba(255, 255, 255, 0.7);
--border-radius: 0.5rem;
--padding: 0.5rem 1.5rem;
--button-height: 3rem;
}
h1,
h2,
h3,
h4 {
font-weight: 500;
}
/* Modern CSS Reset */
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
body {
background-color: #121212;
color: var(--text-color);
font-family: "Inter", sans-serif;
display: flex;
justify-content: center;
align-items: flex-start;
height: 100vh;
margin: 0;
padding: 1rem;
box-sizing: border-box;
line-height: 1.5;
}
.app {
height: 90vh;
padding-bottom: 1em;
}
.page-header {
width: 100%;
padding-bottom: 0.5rem;
text-align: left;
background-color: #121212;
box-sizing: border-box;
z-index: 1000;
display: flex;
justify-content: space-between;
align-items: center;
}
.page-header h1 {
margin: 0;
font-size: 1.5rem;
}
#downloadButton,
.secondary-btn {
padding: var(--padding);
border-radius: var(--border-radius);
cursor: pointer;
font-size: 0.85rem;
height: var(--button-height);
}
#downloadButton {
background-color: var(--primary-color);
color: var(--text-color);
}
#downloadButton:hover {
background-color: var(--primary-color-hover);
}
.secondary-btn {
background-color: var(--secondary-color);
color: var(--text-color);
}
.secondary-btn:hover {
background-color: rgba(255, 255, 255, 0.2);
}
.container {
display: grid;
grid-template-areas:
"preview controls"
"menu menu"
"footer footer";
grid-template-rows: 1fr auto auto;
grid-template-columns: 1fr auto;
width: 100%;
height: 100%;
gap: 0.5rem;
}
.image-preview {
grid-area: preview;
position: relative;
width: 100%;
height: 100%;
background-color: #333;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid #444;
border-radius: var(--border-radius);
overflow: hidden;
}
.image-preview img {
max-width: 100%;
max-height: 100%;
object-fit: contain;
z-index: 0;
border-radius: var(--border-radius);
}
.wrapper {
display: flex;
width: 112%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
pointer-events: none;
filter: url(#displacementFilter);
}
.cell {
background-size: cover;
position: relative;
overflow: hidden;
height: 100%;
}
.cell .shimmer {
position: absolute;
top: 0;
right: 0;
width: 100%;
height: 100%;
background: linear-gradient(
90deg,
rgba(255, 255, 255, 0) 72.63%,
rgba(255, 255, 255, 0.08) 99%,
rgba(255, 255, 255, 0.6) 100%
);
pointer-events: none;
}
.controls-panel {
grid-area: controls;
display: flex;
flex-direction: column;
background-color: var(--secondary-color);
padding: 1rem;
border-radius: var(--border-radius);
width: 16rem;
overflow-y: auto;
gap:.5rem;
}
.control-group {
display: grid;
grid-template-columns: 1fr 1fr;
align-items: center;
margin-bottom: 1em;
}
.control-group div {
display: flex;
flex-direction: row;
align-items: center;
gap:.25rem;
}
.control-group label {
display: inline-block;
font-size: 0.85em;
color: var(--text-color-subtle);
align-items: center;
}
.control-group input[type="range"] {
cursor: pointer;
width:100%;
background-color: var(--primary-color);
color: var(--primary-color);
}
.image-menu {
grid-area: menu;
display: flex;
gap: 0.5rem;
justify-content: flex-start;
overflow-x: auto;
align-content: center;
}
.menu-image {
width: 4.5rem;
height: 4.5rem;
cursor: pointer;
border: 2px solid transparent;
transition: border 0.3s;
object-fit: cover;
border-radius: var(--border-radius);
}
.menu-image:hover,
.menu-image.active {
border: 2px solid #fff;
}
.controls-panel h3 {
font-size: 0.85rem;
margin-bottom: 0.5rem;
}
.footer {
grid-area: footer;
text-align: left;
font-size: 0.75rem;
color: var(--text-color);
opacity: 0.7;
padding-left: 0.5rem;
}
.footer a {
color: #89c5ff;
text-decoration: none;
opacity: 1;
}
.footer a:hover {
text-decoration: underline;
}
.modal {
display: none;
position: fixed;
z-index: 1001;
left: 0;
top: 0;
width: 100%;
height: 100vh;
overflow: auto;
background-color: rgba(0, 0, 0, 0.8);
}
.modal-content {
background-color: #333;
margin: 10% auto;
padding: 2rem;
border: 1px solid #888;
width: 80%;
border-radius: var(--border-radius);
position: relative;
animation: slideUp 0.3s ease-out;
}
@keyframes slideUp {
from {
transform: translateY(100%);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
.modal-content h2 {
text-align: left;
margin-bottom:1rem;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
position: absolute;
top: 1rem;
right: 2rem;
cursor: pointer;
}
.close:hover,
.close:focus {
color: #fff;
text-decoration: none;
cursor: pointer;
}
.drag-drop-area {
border: 2px dotted #aaa;
padding: 2em;
text-align: center;
margin-bottom: 1em;
min-height: 24rem;
display: flex;
flex-direction: column;
gap: 0.5em;
align-items: center;
justify-content: center;
}
.drag-drop-area p {
margin-top: 1em;
color: var(--text-color-subtle);
}
.url-upload {
display: flex;
gap: 0.5em;
align-items: center;
}
.url-upload input[type="text"] {
flex-grow: 1;
padding: 0.5em;
height: var(--button-height);
border: 1px solid #ccc;
border-radius: var(--border-radius);
font-size: 0.85rem;
}
.url-upload button {
height: var(--button-height);
}
document.addEventListener("DOMContentLoaded", function () {
const imagePreview = document.getElementById("imagePreview");
const wrapper = document.getElementById("wrapper");
const imageElement = document.getElementById("image");
const stepsSlider = document.getElementById("steps");
const hueSlider = document.getElementById("hue");
const saturationSlider = document.getElementById("saturation");
const brightnessSlider = document.getElementById("brightness");
const flipToggle = document.getElementById("flip-toggle");
const shimmerToggle = document.getElementById("shimmer-toggle");
const baseFrequencySlider = document.getElementById("baseFrequency");
const numOctavesSlider = document.getElementById("numOctaves");
const scaleSlider = document.getElementById("scale");
const downloadButton = document.getElementById("downloadButton");
const addImageButton = document.getElementById("addImageButton");
const uploadModal = document.getElementById("uploadModal");
const closeModal = document.querySelector(".close");
const dragDropArea = document.getElementById("dragDropArea");
const fileUpload = document.getElementById("fileUpload");
const loadFromDevice = document.getElementById("loadFromDevice");
const imageUrlInput = document.getElementById("imageUrl");
const loadFromUrl = document.getElementById("loadFromUrl");
let selectedImage =
"https://assets.lummi.ai/assets/QmYTMD4bKGJh1ZWLNDfBvFFrtLSgv2PCdqZYSTZsDtqqW7";
const maxSteps = 64;
const minSteps = 12;
stepsSlider.min = minSteps;
stepsSlider.max = maxSteps;
function applyFractalGlassEffect() {
const steps = stepsSlider.value;
const hue = hueSlider.value;
const saturation = saturationSlider.value;
const brightness = brightnessSlider.value;
const flipEnabled = flipToggle.checked;
const shimmerEnabled = shimmerToggle.checked;
const baseFrequency = baseFrequencySlider.value;
const numOctaves = numOctavesSlider.value;
const scale = scaleSlider.value;
// Update the displacement filter settings
const displacementFilter = document.querySelector("#displacementFilter feTurbulence");
displacementFilter.setAttribute("baseFrequency", baseFrequency);
const displacementMap = document.querySelector("#displacementFilter feDisplacementMap");
displacementMap.setAttribute("scale", scale);
const turbulence = document.querySelector("#displacementFilter feTurbulence");
turbulence.setAttribute("numOctaves", numOctaves);
wrapper.innerHTML = ""; // Clear previous cells
const hueRotate = `hue-rotate(${hue}deg) saturate(${
100 + parseInt(saturation)
}%) brightness(${100 + parseInt(brightness)}%)`;
for (let i = 0; i < steps; i++) {
const flip = flipEnabled && i % 2 === 0 ? "scaleX(1)" : "scaleX(-1)";
const cellWidth = (100 / steps) * (1 - (Math.abs(i - steps / 2) / (steps / 2)) * 0.2); // Narrower in the middle
const cell = document.createElement("div");
cell.classList.add("cell");
cell.style.backgroundImage = `url(${selectedImage})`;
cell.style.backgroundPosition = `${(i / steps) * 100}% 50%`;
cell.style.transform = `${flip}`;
cell.style.filter = hueRotate;
cell.style.width = `${cellWidth}%`; // Dynamic width based on position
if (shimmerEnabled) {
const shimmer = document.createElement("div");
shimmer.classList.add("shimmer");
cell.appendChild(shimmer);
}
wrapper.appendChild(cell);
}
}
stepsSlider.addEventListener("input", applyFractalGlassEffect);
hueSlider.addEventListener("input", applyFractalGlassEffect);
saturationSlider.addEventListener("input", applyFractalGlassEffect);
brightnessSlider.addEventListener("input", applyFractalGlassEffect);
flipToggle.addEventListener("change", applyFractalGlassEffect);
shimmerToggle.addEventListener("change", applyFractalGlassEffect);
baseFrequencySlider.addEventListener("input", applyFractalGlassEffect);
numOctavesSlider.addEventListener("input", applyFractalGlassEffect);
scaleSlider.addEventListener("input", applyFractalGlassEffect);
document.querySelectorAll(".menu-image").forEach((image) => {
image.addEventListener("click", function () {
if (this.id !== "addImageButton") {
selectedImage = this.src;
imageElement.src = selectedImage;
imageElement.style.width = "auto";
imageElement.style.height = "auto";
imageElement.style.maxWidth = "100%";
imageElement.style.maxHeight = "100%";
document.querySelectorAll(".menu-image").forEach((img) => img.classList.remove("active"));
this.classList.add("active");
applyFractalGlassEffect();
}
});
});
// Function to download the current image with effects applied
downloadButton.addEventListener("click", function () {
html2canvas(imagePreview, {
backgroundColor: null,
width: imagePreview.clientWidth - 16, // 8px margin on each side
height: imagePreview.clientHeight - 16, // 8px margin on each side
x: 8, // 8px margin on each side
y: 8, // 8px margin on each side
}).then((canvas) => {
const link = document.createElement("a");
link.href = canvas.toDataURL("image/png");
link.download = "fractal_glass_effect.png";
link.click();
});
});
// Add image button click event
addImageButton.addEventListener("click", function () {
uploadModal.style.display = "block";
});
// Close modal event
closeModal.addEventListener("click", function () {
uploadModal.style.display = "none";
});
// Drag and drop event
dragDropArea.addEventListener("dragover", function (event) {
event.preventDefault();
dragDropArea.classList.add("dragover");
});
dragDropArea.addEventListener("dragleave", function () {
dragDropArea.classList.remove("dragover");
});
dragDropArea.addEventListener("drop", function (event) {
event.preventDefault();
dragDropArea.classList.remove("dragover");
const files = event.dataTransfer.files;
if (files.length > 0) {
handleFileUpload(files[0]);
}
});
// File upload button click event
loadFromDevice.addEventListener("click", function () {
fileUpload.click();
});
// File upload change event
fileUpload.addEventListener("change", function (event) {
const files = event.target.files;
if (files.length > 0) {
handleFileUpload(files[0]);
}
});
// Load from URL button click event
loadFromUrl.addEventListener("click", function () {
const url = imageUrlInput.value;
if (url) {
addImageToMenu(url);
uploadModal.style.display = "none";
}
});
imageUrlInput.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
const url = imageUrlInput.value;
if (url) {
addImageToMenu(url);
uploadModal.style.display = "none";
}
}
});
function handleFileUpload(file) {
const reader = new FileReader();
reader.onload = function (event) {
const url = event.target.result;
addImageToMenu(url);
uploadModal.style.display = "none";
};
reader.readAsDataURL(file);
}
function addImageToMenu(url) {
const img = document.createElement("img");
img.src = url;
img.alt = "User Uploaded Image";
img.classList.add("menu-image");
document.querySelectorAll(".menu-image").forEach((img) => img.classList.remove("active"));
img.classList.add("active");
img.addEventListener("click", function () {
selectedImage = this.src;
imageElement.src = selectedImage;
imageElement.style.width = "auto";
imageElement.style.height = "auto";
imageElement.style.maxWidth = "100%";
imageElement.style.maxHeight = "100%";
document.querySelectorAll(".menu-image").forEach((img) => img.classList.remove("active"));
this.classList.add("active");
applyFractalGlassEffect();
});
const imageMenu = document.querySelector(".image-menu");
imageMenu.insertBefore(img, addImageButton.nextSibling);
// Automatically select the newly added image
selectedImage = url;
imageElement.src = selectedImage;
applyFractalGlassEffect();
}
applyFractalGlassEffect();
});
Also see: Tab Triggers