-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (103 loc) · 2.61 KB
/
app.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
113
114
115
116
117
118
let mouseTimeout
let screensaverActive = false
let windowWidth = window.innerWidth
let windowHeight = window.innerHeight
const activityEvents = [
'mousedown', 'mousemove', 'keydown', 'scroll', 'touchstart', 'DOMContentLoaded'
]
const images = [
{
src: 'img/ocean-seacoast-rocks-water-87284.jpeg',
width: 800,
height: 533
},
{
src: 'img/pexels-photo-885880.jpeg',
width: 200,
height: 300
},
{
src: 'img/pexels-photo-1112598.jpeg',
width: 1500,
height: 1179
},
{
src: 'img/pexels-photo-1275929.jpeg',
width: 904,
height: 678
},
{
src: 'img/pexels-photo-1437629.jpeg',
width: 377,
height: 600
},
{
src: 'img/pexels-photo-1451074.jpeg',
width: 675,
height: 900
},
{
src: 'img/pexels-photo-1460880.jpeg',
width: 400,
height: 320
},
]
const block = document.getElementById('block')
const screensaver = document.getElementById('screenSaver')
activityEvents.forEach(function(event) {
document.addEventListener(event, function(){
clearTimeout(mouseTimeout)
if (screensaverActive){
console.log('active')
stopScreensaver()
}
mouseTimeout = setTimeout(function(){
startScreensaver()
}, 10000);
}, true)
})
function startScreensaver(){
screensaver.style.display = 'block'
screensaverActive = true
screenSaver(images)
}
function stopScreensaver(){
console.log('hide')
hideImages(screensaver)
screensaverActive = false
}
function screenSaver (images) {
if (screensaverActive) {
showImages(images)
}
}
const getRandomOfArray = (images) => {
return images[Math.floor(Math.random() * images.length)];
}
const getRandomPosition = (item) => {
let x = Math.floor(Math.random() * windowWidth)
let y = Math.floor(Math.random() * windowHeight)
let sumX = x + item.width
let sumY = y + item.height
if (sumX > windowWidth) {
x = 0;
}
if (sumY > windowHeight) {
y = 0;
}
return { x, y }
}
const showImages = (images) => {
setInterval(() => {
let randomImage = getRandomOfArray(images)
let position = getRandomPosition(randomImage)
block.style.position = 'relative'
block.setAttribute('src', randomImage.src)
block.id = 'isVisible'
block.style.left = `${position.x}px`
block.style.top = `${position.y}px`
}, 5000)
}
const hideImages = (screensaver) => {
screensaver.style.display = 'none'
}