-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.js
96 lines (79 loc) · 2.75 KB
/
page.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
// Update score in LocalStorage
function updateStorageScore(actualScore) {
// Update score
let userScore = JSON.parse(localStorage.score);
userScore.push(actualScore);
localStorage.setItem("score", JSON.stringify(userScore));
function addZeroToDate(minutes) {
if (minutes < 10) {
minutes = "0" + minutes;
}
return minutes;
}
// Update Date
let userDate = JSON.parse(localStorage.date);
let date = new Date();
let dateToPush = date.getDate() + "." + (date.getMonth()+1) + "." + date.getFullYear() + " " + date.getHours() + ":" + addZeroToDate(date.getMinutes());
userDate.push(dateToPush);
localStorage.setItem("date", JSON.stringify(userDate));
}
// File API
function handleFile() {
if (this.files[0].type != "image/png") { // Not PNG image
removeErrorText();
fileErrorText("Wrong file format! Please, insert PNG image.");
return;
} else if (this.files[0].size > 999999) { // Larger than 1 MB
removeErrorText();
fileErrorText("Too large file! Insert file with less than 1 MB.");
return;
} else if (this.files[0].type == undefined) {
return;
} else {
removeErrorText();
}
let reader = new FileReader();
reader.readAsDataURL(this.files[0]);
reader.onloadend = function() {
prev.clearContext();
prev.update(reader.result, "background");
localStorage.setItem("background", JSON.stringify(reader.result));
};
function fileErrorText(text) {
let spanEl = document.createElement("span");
spanEl.textContent = text;
document.querySelector("#settings p").appendChild(spanEl);
}
function removeErrorText() {
if (document.querySelector("#settings p span")) {
document.querySelector("#settings p span").parentNode.removeChild(document.querySelector("#settings p span"));
}
}
}
// Drag & Drop
function drag(event) {
event.dataTransfer.setData("imageDrag", event.target.id);
}
function drop(event) {
event.preventDefault();
prev.clearContext();
let dt = event.dataTransfer.getData("imageDrag");
if (dt != "bird_classic" && dt != "bird_profi" && dt != "bird_red" && dt != "black" && dt != "red") {
alert("This drag is not supported!");
return;
}
// Obstacle
if (dt == "black" || dt == "red") {
prev.update(JSON.parse(localStorage.background), "background");
prev.update(dt, "obstacle");
localStorage.setItem("obstacle", dt);
return;
}
dt = "img/" + dt + ".png";
prev.update(JSON.parse(localStorage.background), "background");
localStorage.setItem("bird", dt);
prev.update(dt, "bird");
}
function allowDrop(event) {
event.preventDefault();
}