-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
146 lines (127 loc) · 6.82 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Comparision Slider</title>
<link rel="icon" href="./images/favicon.png" type="image/png">
<link rel="stylesheet" href="style.css">
<style>
</style>
</head>
<body>
<main>
<header>
<a href="https://mursal.me" target="_blank">
<button>
Portfolio
</button>
</a>
<h1>Image Comparision Slider</h1>
</header>
<div class="container">
<div class="image-container">
<img class="image-before slider-image image-1" src="./images/img1.png" alt="color photo"
onload="setAspectRatio(1)">
<img class="image-after slider-image image-2" src="./images/img2.png" alt="black and white"
onload="setAspectRatio(2)">
</div>
<input type="range" min="0" max="100" value="50" aria-label="Percentage of before photo shown"
class="slider">
<div class="slider-line" aria-hidden="true"></div>
<div class="slider-button" aria-hidden="true">
<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor"
viewBox="0 0 256 256">
<rect width="256" height="256" fill="none"></rect>
<line x1="128" y1="40" x2="128" y2="216" fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="16"></line>
<line x1="96" y1="128" x2="16" y2="128" fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="16"></line>
<polyline points="48 160 16 128 48 96" fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="16"></polyline>
<line x1="160" y1="128" x2="240" y2="128" fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="16"></line>
<polyline points="208 96 240 128 208 160" fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="16"></polyline>
</svg>
</div>
</div>
</main>
<div class="buttons">
<input style="display: none;" type="file" id="imageUpload2" accept="image/*" onchange="selectImage(2)">
<label for="imageUpload2">Select 'Before' Image</label>
<input style="display: none;" type="file" id="imageUpload1" accept="image/*" onchange="selectImage(1)">
<label for="imageUpload1">Select 'After' Image</label>
<label class="showDiff" style="visibility: hidden;" onclick="loadImages()">Show Difference</label>
</div>
<script>
var file1 = {}
var file1Selected = false;
var file2 = {}
var file2Selected = false;
function selectImage(imageNumber) {
const inputElement = document.getElementById(`imageUpload${imageNumber}`);
const file = inputElement.files[0];
if (file && file.type.startsWith('image/')) {
if (imageNumber === 1) {
file1 = file;
file1Selected = true;
document.querySelector(`label[for="imageUpload${imageNumber}"]`).innerText = 'Change After Image';
document.querySelector(`label[for="imageUpload${imageNumber}"]`).style.backgroundColor = 'green';
} else {
file2 = file;
file2Selected = true;
document.querySelector(`label[for="imageUpload${imageNumber}"]`).innerText = 'Change Before Image';
document.querySelector(`label[for="imageUpload${imageNumber}"]`).style.backgroundColor = 'green';
}
if (file1Selected && file2Selected) {
document.querySelector('.showDiff').style.setProperty('visibility', 'visible');
}
} else {
alert('Please select a valid image file.');
inputElement.value = '';
}
}
function setAspectRatio(imageNumber) {
const imageElement = document.querySelector(`.image-${imageNumber}`);
const aspectRatio = imageElement.naturalWidth / imageElement.naturalHeight;
const aspectRatioString = aspectRatio.toString();
document.querySelector('.image-container').style.setProperty('--aspect-ratio', aspectRatioString);
}
const container = document.querySelector('.container');
const slider = document.querySelector('.slider');
slider.addEventListener('input', function (e) {
container.style.setProperty('--position', e.target.value + '%');
});
function loadImages() {
if (file1 && file2) {
const image1 = document.querySelector('.image-1');
const image2 = document.querySelector('.image-2');
image1.src = URL.createObjectURL(file1);
image2.src = URL.createObjectURL(file2);
const checkAspectRatio = () => {
const aspectRatio1 = image1.naturalWidth / image1.naturalHeight;
const aspectRatio2 = image2.naturalWidth / image2.naturalHeight;
if (aspectRatio1 === aspectRatio2) {
setAspectRatio(1);
setAspectRatio(2);
document.querySelector(`label[for="imageUpload1"]`).style.backgroundColor = '#0a192f';
document.querySelector(`label[for="imageUpload2"]`).style.backgroundColor = '#0a192f';
} else {
alert('Aspect ratios of the images must be the same. Please select images with the same aspect ratio.');
image1.src = './images/img1.png';
image2.src = './images/img2.png';
document.querySelector(`label[for="imageUpload1"]`).innerText = 'Select After Image';
document.querySelector(`label[for="imageUpload2"]`).innerText = 'Select Before Image';
document.querySelector(`label[for="imageUpload1"]`).style.backgroundColor = '#0a192f';
document.querySelector(`label[for="imageUpload2"]`).style.backgroundColor = '#0a192f';
document.querySelector('.showDiff').style.setProperty('visibility', 'hidden');
}
};
image1.onload = checkAspectRatio;
image2.onload = checkAspectRatio;
}
}
</script>
</body>
</html>