-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
div.image-compare { | ||
display: block; | ||
position: relative; | ||
border-radius: 10px; | ||
width: 100%; | ||
aspect-ratio: 16 / 9; // fallback | ||
background-color: black; | ||
--slider-value: 50%; | ||
|
||
&::before { | ||
content: var(--data-aspect-ratio); | ||
background-color: red; | ||
width: 100px; | ||
height: 100px; | ||
position: absolute; | ||
margin-top: -100px; | ||
} | ||
|
||
& .image-a { | ||
position: absolute; | ||
-webkit-clip-path: inset(0 calc(100% - var(--slider-value) + 1.5px) 0 0); | ||
clip-path: inset(0 calc(100% - var(--slider-value) + 1.5px) 0 0); | ||
} | ||
|
||
& .image-b { | ||
float: right; | ||
-webkit-clip-path: inset(0 0 0 calc(var(--slider-value) + 1.5px)); | ||
clip-path: inset(0 0 0 calc(var(--slider-value) + 1.5px)); | ||
} | ||
|
||
& input[type="range"] { | ||
position: absolute; | ||
padding: 0; | ||
margin: 0; | ||
width: inherit; | ||
height: 100%; | ||
background-color: transparent; | ||
-webkit-appearance: none; | ||
appearance: none; | ||
|
||
&::-moz-range-thumb { | ||
border: none; | ||
background-color: transparent; | ||
} | ||
&::-webkit-slider-thumb { | ||
width: 0; // no clue why this is required, but it is | ||
-webkit-appearance: none; | ||
appearance: none; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
// clamp a number to the given range | ||
function clamp(val, min, max) { | ||
return Math.min(Math.max(val, min), max) | ||
} | ||
|
||
// inserts the input element required to activate image_compare components | ||
// | ||
// Usage in a document should look like: | ||
// ```html | ||
// <main> | ||
// <div class="image-compare" style="aspect-ratio: 16 / 9"> | ||
// <img class="image-a" src="$url" /> | ||
// <img class="image-b" src="$url" /> | ||
// </div> | ||
// </main> | ||
// ``` | ||
// | ||
// The `image_compare` scss component should be used. | ||
// | ||
// Ideally the `aspect-ratio` should be set, but it will | ||
// fallback to 16/9. | ||
function enable_image_compare() { | ||
const image_compares = document.querySelectorAll("div.image-compare"); | ||
for (const img_cmp of image_compares) { | ||
// insert the input only when js is running | ||
const slider = document.createElement('input'); | ||
slider.type = "range"; | ||
slider.min = "0"; | ||
slider.max = "100"; | ||
slider.value = "50"; | ||
img_cmp.appendChild(slider); | ||
// setup callback | ||
img_cmp.style.setProperty('--slider-value', clamp(slider.value, 7, 93) + "%"); | ||
slider.addEventListener("input", (event) => { | ||
img_cmp.style.setProperty('--slider-value', clamp(slider.value, 7, 93) + "%"); | ||
}); | ||
} | ||
} | ||
|
||
export { enable_image_compare }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters