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 class="wrapper dark">
	<div class="darkmode">
		<h1 class="headline">Image Compare</h1>
		<div id="toggle-darkmode"><i class="fa-solid fa-circle-half-stroke"></i></div>
	</div>
	<div id="image-container" class="image-container">
		<div class="image original"></div>
		<div class="image draft"></div>
		<div id="slider"></div>
		<div class="err">
			<div class="err-msg">Select a valid Image Source</div>
		</div>
		<div class="flag"><span><i class="fi fi-ua"></i>Slava Ukraini!</span></div>
	</div>
	<div class="settings">
		<div class="image-urls">
			<h2 class="headline"><i class="fa-solid fa-image"></i>Image URLs:</h2>
			<div class="input-urls">
				<div class="input-url filter">
					<label>Image URL:<span>Filter</span></label>
					<input id="img-filter" type="url" value="" placeholder="https://your-image-path" />
				</div>
				<div class="input-url original">
					<label>Image URL:<span>Original</span></label>
					<input id="img-original" type="url" value="" placeholder="https://your-image-path" />
				</div>
			</div>
		</div>
		<div class="filter-container">
			<h2 class="headline"><i class="fa-solid fa-filter"></i>Filters:</h2>
			<label class="filter-label">
				<input id="grayscale" class="filter" type="radio" value="grayscale" name="filter" checked />
				Grayscale
			</label>
			<label class="filter-label">
				<input id="blur" class="filter" type="radio" value="blur" name="filter" />
				Blur
			</label>
			<label class="filter-label">
				<input id="sepia" class="filter" type="radio" value="sepia" name="filter" />
				Sepia
			</label>
			<label class="filter-label">
				<input id="none" class="filter" type="radio" value="none" name="filter" />
				None
			</label>
		</div>
	</div>
              
            
!

CSS

              
                * {
	margin: 0;
	padding: 0;
	font-family: "Open Sans", sans-serif;
	box-sizing: border-box;
	transition: color ease-in-out 0.36s;
}
body {
	background-color: #fafafb;
	&.dark {
		background-color: rgba(0, 0, 0, 1);
		.wrapper {
			.darkmode {
				h1.headline {
					color: rgba(255, 255, 255, 1);
				}
				#toggle-darkmode {
					color: rgba(255, 255, 255, 1);
				}
			}
		}
		#image-container {
			border-color: rgba(0, 0, 0, 1);
			#slider {
				background-color: rgba(255, 255, 255, 1);
			}
		}
		.settings {
			background-color: rgba(255, 255, 255, 0.1);
			h2.headline {
				color: rgba(255, 255, 255, 1);
			}
			.input-urls {
				.input-url {
					label {
						color: rgba(255, 255, 255, 1);
					}
				}
			}
			.filter-container {
				.filter-label {
					color: rgba(255, 255, 255, 1);
				}
			}
		}
	}
}
.wrapper {
	width: 640px;
	margin: 46px auto;
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-content: center;
	align-items: center;
	.darkmode {
		width: 100%;
		display: flex;
		h1.headline {
			font-size: 16px;
			letter-spacing: 0.5px;
			margin-bottom: 16px;
			text-transform: uppercase;
			flex-grow: 1;
			color: rgba(0, 0, 0, 0.84);
		}
		#toggle-darkmode {
			color: rgba(0, 0, 0, 0.84);
			cursor: pointer;
		}
	}
	#image-container {
		width: 640px;
		height: 358px;
		background-color: #fafafb;
		border: 1px solid #f1f1f2;
		border-radius: 6px;
		position: relative;
		overflow: hidden;
		.image {
			width: 640px;
			height: 358px;
			position: absolute;
			top: 0;
			left: 0;
			background-size: cover;
			background-position: top left;
			background-repeat: no-repeat;
			background-color: #ff7979;
		}
		#slider {
			width: 5px;
			height: 100%;
			background-color: rgb(0, 0, 0);
			cursor: w-resize;
			position: absolute;
		}
		.err {
			width: 100%;
			height: 100%;
			display: none;
			flex-align: column;
			align-items: center;
			justify-content: center;
			position: absolute;
			top: 0;
			left: 0;
			background: #ff7979;
			&.fadeIn {
				animation-name: fadeIn;
				-webkit-animation-name: fadeIn;
				animation-duration: 0.36s;
				-webkit-animation-duration: 0.36s;
				animation-timing-function: ease-in-out;
				-webkit-animation-timing-function: ease-in-out;
				display: flex;
			}
			.err-msg {
				font-size: 24px;
				color: #fff;
			}
		}
		.flag {
			width: 200px;
			display: flex;
			position: absolute;
			bottom: 16px;
			left: 16px;
			text-shadow: 0px 0px 2px #000000;
			span {
				color: #fff;
				i {
					margin-right: 8px;
				}
			}
		}
	}
	.settings {
		width: 100%;
		display: flex;
		flex: 1 1 0;
		margin: 16px auto 0;
		background-color: rgba(255, 255, 255, 1);
		border-radius: 6px;
		h2.headline {
			width: 100%;
			font-size: 16px;
			letter-spacing: 0.3px;
			margin-bottom: 16px;
			padding-bottom: 8px;
			text-transform: capitalize;
			color: rgba(0, 0, 0, 0.84);
			border-bottom: 1px solid #f1f1f2;
			i {
				margin-right: 8px;
			}
		}
		.image-urls {
			flex-grow: 1;
			flex-basis: 0;
			padding: 16px;
			.input-urls {
				.input-url {
					label {
						display: flex;
						font-size: 16px;
						align-items: center;
						flex-grow: 1;
						span {
							margin-left: auto;
							font-size: 10px;
							text-transform: uppercase;
							color: rgba(0, 0, 0, 0.8);
						}
					}
					input {
						width: 100%;
						margin-top: 8px;
						padding: 8px 12px;
						border: none;
						background-color: #f1f1f2;
						border-radius: 3px;
						&::placeholder {
							color: rgba(0, 0, 0, 0.45);
						}
					}
					&.original {
						margin-top: 16px;
					}
				}
				display: flex;
				flex-direction: column;
			}
		}
		.filter-container {
			display: flex;
			flex-grow: 1;
			flex-basis: 0;
			flex-direction: column;
			justify-content: flex-start;
			align-content: flex-start;
			align-items: flex-start;
			align-self: flex-start;
			padding: 16px;
			.filter-label {
				display: flex;
				margin-bottom: 8px;
				cursor: pointer;
				font-size: 16px;
				letter-spacing: 0.3px;
				color: rgba(0, 0, 0, 0.84);
				.filter {
					margin-right: 8px;
				}
			}
		}
	}
}

// Animations
@keyframes fadeIn {
	0% {
		transform: scale(0);
		opacity: 0;
	}
	100% {
		transform: scale(1);
		opacity: 1;
	}
}

@-webkit-keyframes fadeIn {
	0% {
		-webkit-transform: scale(0);
		opacity: 0;
	}
	100% {
		-webkit-transform: scale(1);
		opacity: 1;
	}
}

              
            
!

JS

              
                const imageCompare = function () {
	const imageContainer = document.getElementById("image-container");
	const canvas = imageContainer.getBoundingClientRect();
	const canvasSize = canvas.width;
	const imageOriginal = document.querySelector(".image.original");
	const imageDraft = document.querySelector(".image.draft");
	const imgSrcOriginal = document.getElementById("img-original");
	const imgSrcDraft = document.getElementById("img-filter");
	const darkMode = document.getElementById("toggle-darkmode");
	const filters = document.querySelectorAll(".filter");
	var isDark = false;

	// Image URLs
	const setImageSrc = function (src1, src2) {
		imgSrcOriginal.value = src1;
		imgSrcDraft.value = src2;

		// Change Image Source
		const changeImgSrc = function (el, src) {
			el.style.backgroundImage = `url("${src.value}")`;
		};

		// Set default img src for bg
		changeImgSrc(imageOriginal, imgSrcOriginal);
		changeImgSrc(imageDraft, imgSrcDraft);

		imgSrcOriginal.addEventListener("input", function () {
			changeImgSrc(imageOriginal, imgSrcOriginal);
		});

		imgSrcDraft.addEventListener("input", function () {
			changeImgSrc(imageDraft, imgSrcDraft);
		});
	};

	// Test URL: https://tinyurl.com/2s3dxphx
	// setImageSrc("https://tinyurl.com/ycksc32p", "https://tinyurl.com/ycksc32p");
	setImageSrc(
		"https://c4.wallpaperflare.com/wallpaper/506/679/697/city-futuristic-digital-art-cheng-yu-wallpaper-preview.jpg",
		"https://c4.wallpaperflare.com/wallpaper/506/679/697/city-futuristic-digital-art-cheng-yu-wallpaper-preview.jpg"
	);

	function dragElement(elmnt) {
		var pos1 = 0,
			pos2 = 0;
		elmnt.style.left = canvasSize / 2 + "px";
		imageDraft.style.width = elmnt.offsetLeft - pos1 + "px";
		if (document.getElementById(elmnt.id)) {
			/* if present, the header is where you move the DIV from:*/
			document.getElementById(elmnt.id).onmousedown = dragMouseDown;
		} else {
			/* otherwise, move the DIV from anywhere inside the DIV:*/
			elmnt.onmousedown = dragMouseDown;
		}

		function dragMouseDown(e) {
			e = e || window.event;
			e.preventDefault();
			// get the mouse cursor position at startup:
			pos2 = e.clientX;

			// pos4 = e.clientY;
			document.onmouseup = closeDragElement;
			// call a function whenever the cursor moves:
			document.onmousemove = elementDrag;
		}

		function elementDrag(e) {
			e = e || window.event;
			e.preventDefault();
			// calculate the new cursor position:
			pos1 = pos2 - e.clientX;
			pos2 = e.clientX;
			if (
				elmnt.offsetLeft - pos1 + 5 >= canvasSize ||
				elmnt.offsetLeft - pos1 + 5 <= 0
			) {
				elmnt.style.left = canvasSize - elmnt.width + "px";
			} else {
				// set the element's new position:
				elmnt.style.left = elmnt.offsetLeft - pos1 + "px";
				imageDraft.style.width = elmnt.offsetLeft + "px";
			}
		}

		function closeDragElement() {
			/* stop moving when mouse button is released:*/
			document.onmouseup = null;
			document.onmousemove = null;
		}
	}

	dragElement(document.getElementById("slider"));

	const setFilters = function () {
		let applyFilter = function (elm, fil) {
			elm.style.filter = fil;
		};
		filters.forEach((filter) => {
			filter.addEventListener("change", function () {
				switch (filter.value) {
					case "grayscale":
						imageDraft.style.filter = applyFilter(imageDraft, "grayscale(100%)");
						break;
					case "blur":
						imageDraft.style.filter = applyFilter(imageDraft, "blur(3px)");
						break;
					case "sepia":
						imageDraft.style.filter = applyFilter(imageDraft, "sepia(1)");
						break;
					case "none":
						imageDraft.style.filter = applyFilter(imageDraft, "none");
						break;
				}
			});
			// Set defauult Filter
			applyFilter(imageDraft, "grayscale(100%)")
		});
		
	};
	setFilters();

	const setMode = function () {
		darkMode.addEventListener("click", function () {
			isDark = !isDark;
			isDark
				? document.body.classList.add("dark")
				: document.body.classList.remove("dark");
		});
	};
	setMode();
};
imageCompare();

              
            
!
999px

Console