-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (50 loc) · 1.91 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
/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
document.addEventListener("DOMContentLoaded", function(){
const maxImage = 20;
const img11 = [...document.querySelectorAll('.img-11')];
function getOptions(){
let options = Array.from(Array(maxImage).keys());
options.shift(); // remove zero
shuffleArray(options);
return options;
}
let options = getOptions();
;
// preload all images
options.forEach((option) => {
const img = new Image();
img.src = `./images/11/${option}.jpeg`;
});
// set initial images
for(let i = 0; i < img11.length; i++){
img11[i].style.backgroundImage = `url(./images/11/${options[i]}.jpeg)`;
const hidden = img11[i].childNodes[0];
hidden.classList.add('show');
}
function animate(){
// select random tile
const tile = Math.floor(Math.random() * img11.length);
let options = getOptions();
// filter out the already shown images to avoid duplicates
const existing = img11.map((img) => parseInt(img.style.backgroundImage.split('.jpeg')[0].split('11/')[1]));
options = options.filter((option) => !existing.includes(option));
// animate the tile
const hidden = img11[tile].childNodes[0];
hidden.addEventListener('transitionend', function() {
this.removeEventListener('transitionend',arguments.callee, false);
img11[tile].style.backgroundImage = `url(./images/11/${options[0]}.jpeg)`;
hidden.classList.add('show');
setTimeout(animate, 1000);
}, false);
hidden.classList.remove('show');
}
setTimeout(animate, 1000);
});