forked from cdv-poznan/javascript-project-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
234 lines (209 loc) · 7.1 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
document.addEventListener("DOMContentLoaded", () => {
const api = "https://x-colors.herokuapp.com/api/random";
const apiKey = "b2d5f5b8a462e384858bbfeaf3f0bf1b84fcd82e";
const emojiList = `https://emoji-api.com/emojis?access_key=${apiKey}`;
// DOM Elements
const $previousButton = document.getElementById("previous");
const $previousTab = document.getElementById("previous-project-btn");
const $aboutMeTab = document.getElementById("me-btn");
const $drawTab = document.getElementById("new-image-btn");
const $previousSection = document.getElementById("previous-project-display");
const $nextButton = document.getElementById("next");
const $genEmojis = document.getElementById("generated-emojis");
const $save = document.getElementById("save-button");
const $clearBttn = document.getElementById("clear-button");
const $colorButton = document.getElementById("color-button");
const $emojiButton = document.getElementById("emojis-button");
const $colors = document.getElementById("generated-colors");
function openPage(pageName, elmnt) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("page-content");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = "#8573f8";
}
$drawTab.addEventListener("click", function () {
openPage("draw-page", this);
});
$previousTab.addEventListener("click", function () {
openPage("previous-projects-page", this);
});
$aboutMeTab.addEventListener("click", function () {
openPage("about-me-page", this);
});
$drawTab.click();
// -------------------------CANVAS-----------------------
const $canvas = document.getElementById("canvas");
const ctx = $canvas.getContext("2d");
// Resizing
canvas.height = window.innerHeight / 1.5;
canvas.width = window.innerWidth / 2;
window.addEventListener("resize", () => {
canvas.height = window.innerHeight / 1.5;
canvas.width = window.innerWidth / 2;
});
// Canvas position
function getMousePos(canvas, e) {
var rect = canvas.getBoundingClientRect();
return {
x: ((e.clientX - rect.left) / (rect.right - rect.left)) * canvas.width,
y: ((e.clientY - rect.top) / (rect.bottom - rect.top)) * canvas.height,
};
}
// PAINTING
let painting = false;
const startPosition = function () {
painting = true;
};
const finishPosition = function () {
painting = false;
ctx.beginPath();
};
function draw(e) {
if (!painting) return;
ctx.lineWidth = 10;
ctx.lineCap = "round";
var mousePos = getMousePos(canvas, e);
ctx.lineTo(mousePos.x, mousePos.y);
ctx.stroke();
ctx.save();
}
// Canvas event listeners
canvas.addEventListener("mousedown", startPosition);
canvas.addEventListener("mouseup", finishPosition);
canvas.addEventListener("mousemove", draw);
// Clearing canvas and adding previous project
$clearBttn.addEventListener("click", () => {
if (($previousSection.firstElementChild.tagName = "h2")) {
$previousSection.firstElementChild.className = "hide";
}
const addImg = document.createElement("img");
addImg.classList.add("saved-image");
addImg.src = imgLink();
addImg.alt = "Previous project";
addImg.style.alignContent = "center";
$previousSection.append(addImg);
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
// ------------------------CANVAS-END-----------------------------------------
function getApiResponse(url) {
const apiRequest = fetch(url);
return (apiResponse = apiRequest
.then((response) => response.json())
.catch((error) => {
alert(
`Error - data can't be loaded from:
${url}`
);
}));
}
// Generating colors
function addColorStyle(hex, i) {
const colorsElement = document.createElement("span");
colorsElement.style.color = hex;
colorsElement.classList = "material-symbols-rounded";
colorsElement.id = i;
colorsElement.innerText = "circle";
colorsElement.title = "Click to choose";
colorsElement.addEventListener("click", () => ctx.strokeStyle = `${hex}`);
$colors.append(colorsElement);
}
function getColors() {
let oldColors = document.getElementsByClassName("material-symbols-rounded");
for (var i = oldColors.length - 1; i >= 0; i--) {
oldColors[i].parentNode.removeChild(oldColors[i]);
}
getApiResponse(api).then((colors) =>
addColorStyle(colors.hex, "color1")
);
getApiResponse(api).then((colors) =>
addColorStyle(colors.hex, "color2")
);
getApiResponse(api).then((colors) =>
addColorStyle(colors.hex, "color3")
);
}
getColors();
$colorButton.addEventListener("click", () => getColors());
// ------------------------emojis-----------------
class Emoji {
constructor(group, character, id) {
this.id = id;
this.group = group;
this.character = character;
}
emojis = document.getElementById("generated-emojis");
renderGroup() {
const groupElement = document.createElement("span");
groupElement.innerText = this.group;
groupElement.addEventListener("click", () => {
console.log("clicked group");
});
this.emojis.append(groupElement);
}
renderEmoji() {
const emojisElement = document.createElement("span");
emojisElement.innerText = this.character;
emojisElement.title = "Click to choose";
emojisElement.addEventListener("click", () => {
ctx.font = "12vh verdana";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
painting = false;
// draw the emoji
canvas.addEventListener(
"click",
(e) => {
var mousePos = getMousePos(canvas, e);
ctx.fillText(this.character, mousePos.x, mousePos.y);
},
{ once: true }
);
});
this.emojis.append(emojisElement);
}
}
$emojiButton.addEventListener("click", async () => {
const emojiJson = await getApiResponse(emojiList);
var counter = 0;
function changeEmojis() {
for (let i = counter; i < counter + 10; i++) {
const emoji = new Emoji(emojiJson[i].group, emojiJson[i].character, i);
emoji.renderEmoji();
}
}
function delateEmojis() {
while ($genEmojis.firstChild) {
$genEmojis.removeChild($genEmojis.lastChild);
}
}
delateEmojis();
changeEmojis();
$nextButton.addEventListener("click", () => {
delateEmojis();
counter += 10;
changeEmojis();
});
$previousButton.addEventListener("click", () => {
delateEmojis();
counter -= 10;
changeEmojis();
});
});
// Save image
function imgLink() {
const link = canvas.toDataURL("image/png");
$save.href = link;
$save.download = "IMAGE.jpeg";
return link;
}
$save.addEventListener("click", imgLink);
$emojiButton.click();
// end of DOMContentLoaded
});