-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (154 loc) · 5.65 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//html-element
const tvButtons = document.querySelector(".buttonContainer");
const lightswitch = document.querySelector(".lightswitch");
const colorChange = document.querySelector(".colorChange");
const tv = document.querySelector(".tv");
const animal = document.querySelector("#animal");
const tvScreen = document.querySelector("#tvScreen");
//hämtar eventuell sparad tv-färg från localStorage och laddar den
let savedColor = localStorage.getItem("savedColor");
if (savedColor) {
tv.style.backgroundColor = savedColor
};
//hämtar vald tapet
let savedWallpaper = localStorage.getItem("savedWallpaper");
if (savedWallpaper) {changeWallpaper()}
//eventlisteners
lightswitch.addEventListener("click", changeWallpaper);
tvButtons.addEventListener("click", changeChannel)
animal.addEventListener("mouseover", () => animal.classList.add("jump"));
//video-variabler
const tvVideo = document.querySelector("#tvVideo");
const paintingVideo = document.querySelector("#paintingVideo");
var paintings = ["./videos/water.mp4", "./videos/wind.mp4", "./videos/icewaves.mp4"];
var tvChannels = ["./videos/wiggelysticks.MP4", "./videos/sun.mp4", "./videos/slowsun.mp4", "./videos/icebells.mp4", "./videos/icebubbles.mp4", "./videos/green.mp4"];
let tvCounter = 0;
let paintingCounter = 0;
setInterval(showTime(), 60000);
function deleteChildren(element) {
while (element.hasChildNodes()) {
element.removeChild(element.firstChild);
}
}
function showTime() {
let now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
const hourHand = document.querySelector('.hours');
const minuteHand = document.querySelector('.minutes');
let degreesHours = hours * 30 + minutes * 0.5 + "deg";
let degreesMinutes = minutes * 6 + "deg";
hourHand.style.transform = `rotate(${degreesHours})`
minuteHand.style.transform = `rotate(${degreesMinutes})`
return showTime //specialare för att det ska bli snyggare setInteval
}
function changeWallpaper() {
let walls = document.getElementsByTagName("section");
walls[0].classList.toggle("new-wallpaper");
walls[1].classList.toggle("new-wallpaper");
if (!lightswitch.src.includes("Click")) {
lightswitch.src = "./images/lightswitchClick.png"
localStorage.setItem("savedWallpaper", true);
} else {
lightswitch.src = "./images/lightswitch.png"
localStorage.removeItem("savedWallpaper");
}
}
function changeTvVideo(sources, videoElement, counter) {
tvScreen.classList.add("static");
deleteChildren(videoElement); //bort med gamla subtitles
if (counter >= sources.length) {
counter = 0;
};
console.log("you are watching: ", sources[counter]);
videoElement.src = sources[counter];
if (sources[counter] == "./videos/icebells.mp4") {
showSub("pizzacats.vtt")
}
var isPlaying = videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended &&
videoElement.readyState > videoElement.HAVE_CURRENT_DATA;
if (isPlaying) {
videoElement.pause();
//för att slippa få fel i konsoll att play har avbrutits
}
let playPromise = videoElement.play();
if (playPromise !== undefined) {
playPromise.then(_ => {
tvVideo.classList.remove("hidden");
})
.catch(error => {
console.log("Call tv repair man: ", error)
});
}
counter++;
return counter;
}
function changePaintingVideo(sources, videoElement, counter) {
deleteChildren(videoElement);
if (counter >= sources.length) {
counter = 0;
};
console.log("you are watching: ", sources[counter]);
var isPlaying = videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended &&
videoElement.readyState > videoElement.HAVE_CURRENT_DATA;
if (isPlaying) {
videoElement.pause()
}
videoElement.src = sources[counter];
let playPromise = videoElement.play();
if (playPromise !== undefined) {
playPromise
.catch(error => {
console.log("Call tv repair man: ", error)
});
}
counter++;
return counter;
}
function turnOff() {
tvVideo.pause();
tvVideo.classList.add("hidden");
paintingVideo.pause();
setTimeout(() => tvScreen.classList.remove("static"), 2500)
}
function showSub(subtitleFile) {
deleteChildren(tvVideo);
let subtitle = document.createElement("track");
subtitle.setAttribute("src", subtitleFile);
subtitle.setAttribute("kind", "subtitles")
subtitle.setAttribute("mode", "showing")
subtitle.setAttribute("id", "subtitle")
subtitle.setAttribute("default", "default")
tvVideo.appendChild(subtitle);
}
function changeChannel(e) {
document.querySelectorAll("button").forEach(button => {
button.classList.add("transition")
});
let clickedButton = e.target.id;
switch (clickedButton) {
case "tvButton":
tvCounter = changeTvVideo(tvChannels, tvVideo, tvCounter);
break;
case "paintingButton":
paintingCounter = changePaintingVideo(paintings, paintingVideo, paintingCounter);
break;
case "colorButton":
changeColor()
break;
case "offButton":
turnOff()
break;
default:
break
}
}
function randomVal(min, max) {
return Math.floor(Math.random() * (max - min) + 1) + min;
}
function changeColor() {
tv.classList.add("colorfade");
var randomColor = 'hsl(' + randomVal(0, 360) + ', ' + randomVal(10, 50) + '%, ' + randomVal(30, 50) + '%)';
tv.style.backgroundColor = randomColor;
localStorage.setItem("savedColor", randomColor)
}