-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgallery.js
94 lines (89 loc) · 2.52 KB
/
gallery.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
const images = [
{
o: "images/MonaLisa.jpg",
svg: "MonaLisa-400k.svg"
},
{
o: "images/MonaLisaFace.jpg",
svg: "MonaLisaFace-130k.svg"
},
{
o: "images/AmericanGothic.jpg",
svg: "AmericanGothic-200k.svg"
},
{
o: "images/StarryNight.jpg",
svg: "StarryNight-100k.svg"
},
{
o: "images/GrandCanyon.jpg",
svg: "GrandCanyon-140k.svg"
},
{
o: "images/Flowers.jpg",
svg: "Flowers-87k.svg"
},
{
o:
"https://lh4.googleusercontent.com/-q-R5clDIXIg/S1fefky5yRI/AAAAAAAAIYc/yeYl_tYXQwM/s571/IMG_2564.JPG",
svg: "OrangeButterflyFish-40k.svg"
}
];
let imgIndex = -1;
let box = null;
let source = null;
let painting = null;
let timeoutImage = null;
let timeoutOpacity = null;
function scaleImages() {
const border = parseInt(
document.defaultView.getComputedStyle(source).borderLeftWidth ||
source.currentStyle.borderWidth
);
const bw = box.clientWidth - 2 * border;
const bh = box.clientHeight - 2 * border;
const nw = source.naturalWidth;
const nh = source.naturalHeight;
const ratio = Math.min(bh / nh, bw / nw);
const w = Math.ceil(nw * ratio);
const h = Math.ceil(nh * ratio);
painting.style.width = source.style.width = `${w}px`;
painting.style.height = source.style.height = `${h}px`;
painting.style.left = source.style.left = `${Math.floor((bw - w) / 2)}px`;
painting.style.top = source.style.top = `${Math.floor((bh - h) / 2)}px`;
}
function nextImage() {
imgIndex = (imgIndex + 1) % images.length;
source.src = images[imgIndex].o;
}
function transitionOpacity() {
const opacity = Math.round(painting.style.opacity * 100) + 10;
painting.style.opacity = String(opacity / 100);
source.style.opacity = String(1 - opacity / 100);
if (opacity === 100) {
timeoutImage = setTimeout(nextImage, 5000);
} else {
timeoutOpacity = setTimeout(transitionOpacity, 100);
}
}
window.addEventListener("load", () => {
box = document.getElementById("imgbox");
source = document.getElementById("source");
painting = document.getElementById("painting");
source.addEventListener("load", () => {
scaleImages();
source.style.opacity = 1;
painting.style.opacity = 0;
painting.src = "samples/" + images[imgIndex].svg;
});
painting.addEventListener("load", () => {
timeoutOpacity = setTimeout(transitionOpacity, 1000);
});
painting.addEventListener("click", () => {
clearTimeout(timeoutImage);
clearTimeout(timeoutOpacity);
nextImage();
});
window.addEventListener("resize", scaleImages);
nextImage();
});