Skip to content

Commit 042ea9a

Browse files
authored
feat(comparison): ✨ preload all the images from the comp to improve UI experience (#68)
1 parent 381d69c commit 042ea9a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

static/css/style.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,13 @@ body {
936936
#mobileImageRendering option:hover {
937937
background-color: #404040;
938938
}
939+
940+
#preloadIndicator {
941+
z-index: 1000;
942+
animation: fadeInOut 2s infinite;
943+
}
944+
945+
@keyframes fadeInOut {
946+
0%, 100% { opacity: 0.7; }
947+
50% { opacity: 1; }
948+
}

static/js/compare.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
const { imageUrls, totalColumns, totalRows, imageNames, imageSizes } = compareData;
22
let absoluteIndex = 0;
33

4+
// Replace the existing preloadImages function with this improved version
5+
function preloadImages() {
6+
// Create a promise for each image load
7+
const preloadPromises = imageUrls.map(url => {
8+
return new Promise((resolve, reject) => {
9+
const img = new Image();
10+
img.onload = () => resolve(url);
11+
img.onerror = () => reject(url);
12+
img.src = `/uploads/${url}`;
13+
});
14+
});
15+
16+
// Add loading indicator to the page
17+
const loadingIndicator = document.createElement('div');
18+
loadingIndicator.id = 'preloadIndicator';
19+
loadingIndicator.style.cssText = 'position: fixed; bottom: 20px; right: 20px; background: rgba(0,0,0,0.7); color: white; padding: 10px; border-radius: 5px;';
20+
loadingIndicator.textContent = 'Loading images...';
21+
document.body.appendChild(loadingIndicator);
22+
23+
// Wait for all images to load
24+
Promise.allSettled(preloadPromises)
25+
.then(results => {
26+
const loaded = results.filter(r => r.status === 'fulfilled').length;
27+
const failed = results.filter(r => r.status === 'rejected').length;
28+
console.log(`Preloaded ${loaded} images, ${failed} failed`);
29+
loadingIndicator.remove();
30+
});
31+
}
32+
433
// Get both desktop and mobile elements
534
const currentImage = document.getElementById('currentImage');
635
const currentImageInfoSpan = document.getElementById('currentImageInfo');
@@ -451,6 +480,10 @@ updateDisplay();
451480
updateNavigation();
452481

453482
document.addEventListener('DOMContentLoaded', () => {
483+
// Start preloading images
484+
preloadImages();
485+
486+
// Check for saved preferences
454487
const savedViewMode = getCookie('imageViewMode');
455488
if (savedViewMode === 'original') {
456489
currentImage.classList.remove('fit');

0 commit comments

Comments
 (0)