Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image-compare widget #689

Merged
merged 2 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions sass/components/_image_compare.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
div.image-compare {
display: flex;
align-items: center;
position: relative;
border-radius: 10px;
width: 100%;
aspect-ratio: 16 / 9; // fallback
background-color: black;
--slider-value: 50%;

&::before,
&::after {
position: absolute;
font-weight: bolder;
font-size: 175%;
writing-mode: vertical-rl;
}
&::before {
content: attr(data-title-a);
z-index: 1;
}
&::after {
content: attr(data-title-b);
width: 100%;
}

& img {
width: 100%;
}
& .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;
z-index: 2;

&::-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;
}
}
}
1 change: 1 addition & 0 deletions sass/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@import "components/syntax-theme";
@import "components/tree-menu";
@import "components/asset-card";
@import "components/image_compare";

// Pages
// - Page specific CSS
Expand Down
42 changes: 42 additions & 0 deletions static/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// 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"
// data-title-a="Apples" data-title-b="Oranges">
// <img class="image-a" alt="Apples" src="apples.png" />
// <img class="image-b" alt="Oranges" src="oranges.png" />
// </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 };
7 changes: 7 additions & 0 deletions templates/news-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ <h2 class="news-subtitle">Posted on {{ page.date | date(format="%B %d, %Y") }} b
</div>
{% endif %}
<div class="media-content news-content">{{ page.content | safe }}</div>
<script type="module">
import { enable_image_compare } from '/components.js';

document.addEventListener("DOMContentLoaded", function() {
enable_image_compare();
});
</script>
{% endblock content %}
Loading