-
Notifications
You must be signed in to change notification settings - Fork 18
/
script.js
112 lines (97 loc) · 4.98 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', function () {
function loadImagesData(section, offset) {
const imagesDataPath = section.dataset.json;
const imagesFolder = section.dataset.folder;
let imagesData = [];
let currentIndex = 0;
let interval;
fetch(imagesDataPath)
.then(response => response.json())
.then(data => {
imagesData = data;
preloadImages(imagesData, imagesFolder, () => {
initializeDots(section);
showImages(currentIndex, section, imagesData, imagesFolder);
startAutoChange(section, imagesData, imagesFolder, offset);
});
})
.catch(error => console.error('Error fetching data:', error));
function preloadImages(imagesData, imagesFolder, callback) {
let loadedCount = 0;
const totalImages = imagesData.length;
imagesData.forEach(image => {
const img = new Image();
img.src = `./${imagesFolder}/${image.FileName}`;
img.onload = img.onerror = () => {
loadedCount++;
if (loadedCount === totalImages) {
callback();
}
};
});
}
function initializeDots(section) {
const dotsContainer = section.querySelector('.dots');
const uniqueTitles = [...new Set(imagesData.map(image => image.Title))];
uniqueTitles.forEach((title, index) => {
const dot = document.createElement('span');
dot.classList.add('dot');
dot.addEventListener('click', () => {
currentIndex = index;
showImages(currentIndex, section, imagesData, imagesFolder);
resetAutoChange(section, imagesData, imagesFolder, offset);
});
dotsContainer.appendChild(dot);
});
updateDots(section, currentIndex);
}
function showImages(index, section, imagesData, imagesFolder) {
const uniqueTitles = [...new Set(imagesData.map(image => image.Title))];
const jpegImage = imagesData.find(image => image.Title === uniqueTitles[index] && image.Type === "JPEG");
const jxlImage = imagesData.find(image => image.Title === uniqueTitles[index] && image.Type === "JXL");
if (jpegImage && jxlImage) {
const jpegElement = section.querySelector('.jpeg-image');
const jxlPicture = section.querySelector('.jxl-image');
const jpegFileSizeKB = (jpegImage["File Size"] / 1024).toFixed(1);
const jxlFileSizeKB = (jxlImage["File Size"] / 1024).toFixed(1);
jpegElement.src = `./${imagesFolder}/${jpegImage.FileName}`;
setFileSize(section.querySelector('.jpeg-file-size'), jpegFileSizeKB);
section.querySelector('.jpeg-bpp').textContent = `BPP: ${jpegImage["BPP"].toFixed(2)}`;
jxlPicture.innerHTML = `
<source srcset="./${imagesFolder}/${jxlImage.FileName}" type="image/jxl">
<img src="./${imagesFolder}/${jxlImage.FallbackFile || jpegImage.FileName}" alt="${jxlImage.Title}">
`;
setFileSize(section.querySelector('.jxl-file-size'), jxlFileSizeKB);
section.querySelector('.jxl-bpp').textContent = `BPP: ${jxlImage["BPP"].toFixed(2)}`;
section.querySelector('.size-reduction').textContent = jxlImage.Smaller ? `${jxlImage.Smaller} Smaller` : '';
}
updateDots(section, index);
}
function setFileSize(element, sizeKB) {
element.innerHTML = `${sizeKB} <span class="headline-s-semibold white add-padding-bottom">KB</span>`;
}
function updateDots(section, index) {
const dots = section.querySelectorAll('.dot');
dots.forEach((dot, dotIndex) => {
dot.classList.toggle('active', dotIndex === index);
});
}
function startAutoChange(section, imagesData, imagesFolder, offset) {
interval = setInterval(() => {
currentIndex = (currentIndex + 1) % (imagesData.length / 2); // Each pair of images (JPEG and JXL) has the same title
showImages(currentIndex, section, imagesData, imagesFolder);
}, 6000 + offset);
}
function resetAutoChange(section, imagesData, imagesFolder, offset) {
clearInterval(interval);
startAutoChange(section, imagesData, imagesFolder, offset);
}
}
const sections = document.querySelectorAll('section');
sections.forEach((section, index) => {
// Check if the section has the necessary data attributes
if (section.dataset.json && section.dataset.folder) {
loadImagesData(section, index * 3000); // 3 second offset
}
});
});