Create a Photo Filters and Effects Project using HTML CSS & JavaScript
Last Updated :
23 Jul, 2024
In this article, we are going to implement a photo editor that allows users to apply various filters and effects to their images. This project will use HTML, CSS, and JavaScript to provide a user-friendly interface for adjusting brightness, contrast, grayscale, blur, sepia, and hue rotation filters. Users can also reset the filters to their default values and save the edited image.
Preview of final output: Let us have a look at how the final output will look like.

Prerequisites
Approach
Our approach involves building a user interface with input sliders for each filter and applying these filters in real-time to the displayed image. We'll also provide options to reset the filters and save the edited image.
- In HTML structure, we create multiple <input type="range"> for brightness, contrast, grayscale, blur, sepia, hue rotation, and also an input for image input.
- Style the web page using the class names with CSS properties like font weight for text margins, padding to add space, text-align for alignment, and display type for display styling of individual elements.
- Using JavaScript, develop a function that loads an image from an input source. Additionally, create functions that respond to slider changes, capturing their input values. These values are then utilized to dynamically adjust the filter configurations.
- Filters: Add filter sliders for brightness, contrast, grayscale, blur, sepia, and hue rotation.
- Real-time Updates: Apply the selected filters to the image in real-time as users adjust the sliders.
- Reset Functionality: Implement the reset button to revert all filters to their default values.
- Image Saving: Allow users to save the edited image with applied filters.
Example: Write the following code in respective files
- script.js: This file contains the functionalities of the application
- index.html: This file contains skeleton structure of the application
- style.css: This file implements the styling of the application
HTML
<!-- Index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Photo Editor</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>Photo Editor</h1>
<input type="file" id="imageInput" accept="image/*" />
<div class="image-container">
<img src="placeholder.jpg" alt="Image" id="previewImage" />
</div>
<div class="controls">
<label for="brightness">
Brightness:
</label>
<input type="range" id="brightness"
min="0" max="200" value="100" />
<br />
<label for="contrast">
Contrast:
</label>
<input type="range" id="contrast"
min="0" max="200" value="100" />
<br />
<label for="grayscale">
Grayscale:
</label>
<input type="range" id="grayscale"
min="0" max="100" value="0" />
<br />
<label for="blur">
Blur:
</label>
<input type="range" id="blur"
min="0" max="10" value="0" />
<br />
<label for="sepia">
Sepia:
</label>
<input type="range" id="sepia" min="0" max="100" value="0" />
<br />
<label for="hue">
Hue Rotation:
</label>
<input type="range" id="hue"
min="0" max="360" value="0" />
<br />
<button id="reset">Reset</button>
<button id="save">Save</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
/* Style.css */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px
rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
}
.image-container {
margin-bottom: 20px;
}
img {
max-width: 100%;
height: auto;
border: 1px solid #ddd;
border-radius: 5px;
}
.controls {
text-align: left;
}
label {
font-weight: bold;
}
input[type="range"] {
width: 100%;
}
button {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 20px;
margin-top: 10px;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
JavaScript
// Script.js
const imageInput =
document.getElementById(
"imageInput"
);
const previewImage =
document.getElementById(
"previewImage"
);
const brightnessRange =
document.getElementById(
"brightness"
);
const contrastRange =
document.getElementById("contrast");
const grayscaleRange =
document.getElementById(
"grayscale"
);
const blurRange =
document.getElementById("blur");
const sepiaRange =
document.getElementById("sepia");
const hueRange =
document.getElementById("hue");
const resetButton =
document.getElementById("reset");
const saveButton =
document.getElementById("save");
let currentImage = null;
imageInput.addEventListener(
"change",
(e) => {
const file = e.target.files[0];
if (file) {
const reader =
new FileReader();
reader.onload = (event) => {
currentImage =
new Image();
currentImage.src =
event.target.result;
currentImage.onload =
() => {
previewImage.src =
currentImage.src;
resetFilters();
};
};
reader.readAsDataURL(file);
}
});
brightnessRange.addEventListener(
"input",
() => {
applyFilters();
});
contrastRange.addEventListener(
"input",
() => {
applyFilters();
});
grayscaleRange.addEventListener(
"input",
() => {
applyFilters();
});
blurRange.addEventListener(
"input",
() => {
applyFilters();
});
sepiaRange.addEventListener(
"input",
() => {
applyFilters();
});
hueRange.addEventListener(
"input",
() => {
applyFilters();
});
resetButton.addEventListener(
"click",
() => {
resetFilters();
});
saveButton.addEventListener(
"click",
() => {
saveEditedImage();
});
function applyFilters() {
if (currentImage) {
const brightnessValue =
brightnessRange.value;
const contrastValue =
contrastRange.value;
const grayscaleValue =
grayscaleRange.value;
const blurValue =
blurRange.value;
const sepiaValue =
sepiaRange.value;
const hueValue = hueRange.value;
const filterValue = `brightness(${brightnessValue}%)
contrast(${contrastValue}%)
grayscale(${grayscaleValue}%)
blur(${blurValue}px)
sepia(${sepiaValue}%)
hue-rotate(${hueValue}deg)`;
previewImage.style.filter =
filterValue;
}
}
function resetFilters() {
if (currentImage) {
brightnessRange.value = 100;
contrastRange.value = 100;
grayscaleRange.value = 0;
blurRange.value = 0;
sepiaRange.value = 0;
hueRange.value = 0;
previewImage.style.filter =
"none";
}
}
function saveEditedImage() {
if (currentImage) {
const canvas =
document.createElement(
"canvas"
);
canvas.width =
currentImage.width;
canvas.height =
currentImage.height;
const context =
canvas.getContext("2d");
context.filter =
previewImage.style.filter;
context.drawImage(
currentImage,
0,
0,
canvas.width,
canvas.height
);
const link =
document.createElement("a");
link.href = canvas.toDataURL(
"image/jpeg"
);
link.download =
"edited_image.jpg";
link.click();
}
}
Output
Create a Photo Filters and Effects Project using HTML CSS & JavaScript
Similar Reads
Create an QR Code Generator Project using HTML CSS & JavaScript Today, more than 75% of websites and apps use QR codes to share links, contact info, or event details quickly. Here, youâll learn how to make your own QR Code Generator using HTML, CSS, and JavaScript. Weâll guide you step by step, so even if youâre a beginner, you can follow along easily. By the en
3 min read
How to create a Spotlight Effect using HTML and CSS ? In this article, we are going to create a spotlight effect over the image when we hover over it. This is mainly based on HTML, CSS and JavaScript. The below steps have to be followed to create this effect.HTML Section: In this section, we will create a container elements for the background image and
4 min read
How to create a Color Generator using HTML CSS and JavaScript ? In this article, we will develop a Color Generator application using HTML, CSS, and JavaScript.In this application, we have created the Multi Format color generator. Users can select Colour Types like RGB, HEX, and CMYK. Use the sliders to customize the color and Inout field in case of HEX and color
6 min read
How to Create a Portfolio Website using HTML CSS and JavaScript ? A portfolio website is a website that represents you online on the web pages. It contains different sections like Introduction to Yourself, About You, Your Services, Your Contact Details, and Your Portfolio. We are going to build all of the sections with an interactive and attractive web design that
15+ min read
Create a Coin Flip using HTML, CSS & JavaScript We will display the styled INR coin to the user and there will be a styled button (Toss Coin). We will create the entire application structure using HTML and style the application with CSS classes and properties. Here, JavaScript will be used to manage the behavior of the Coin Flip and will be used
4 min read
How to Create Image Lightbox Gallery using HTML CSS and JavaScript ? A lightbox gallery is basically used to view the gallery images in detail specifically. You can code the JavaScript to do so but also we can use some downloaded JS and CSS. In this article, we will download the lightbox and attach the JS and CSS files into our code. For our own design satisfaction,
3 min read