-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.js
81 lines (67 loc) · 2.04 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
import { getThemeToggle } from "./src/theme.js";
import { getVideo, drawVideo } from "./src/camera.js";
import { drawText } from "./src/text.js";
import { Modal } from "./src/modal.js";
const memeCanvas = document.getElementById("meme");
// Create unattached canvas elements
// to serve as "layers" of the meme
const selfieLayer = document.createElement("canvas");
const textLayer = document.createElement("canvas");
for (let canvas of [selfieLayer, textLayer]) {
canvas.width = memeCanvas.width;
canvas.height = memeCanvas.height;
}
// When either layer has changed, we'll
// call this function to redraw the
// meme with the layers' new data
function redrawMeme() {
const memeCtx = memeCanvas.getContext("2d");
memeCtx.drawImage(selfieLayer, 0, 0);
memeCtx.drawImage(textLayer, 0, 0);
}
function setupSettings() {
const settings = document.getElementById("settings");
getThemeToggle();
const darkModal = new Modal(
"Settings",
settings,
settings.querySelector(".modal-content")
);
darkModal.render();
}
function setupAddText() {
const textInputs = document.getElementById("add-text");
const saveTextBtn = document.getElementById("text-save");
saveTextBtn.addEventListener("click", () => {
drawText(textLayer);
redrawMeme();
});
const textModal = new Modal(
"Add some text",
textInputs,
textInputs.querySelector(".modal-content")
);
textModal.render();
}
async function setupTakeSelfie() {
const selfie = document.getElementById("take-selfie");
const savePhotoBtn = document.getElementById("save-photo");
const selfieModal = new Modal(
"Take a selfie",
selfie,
selfie.querySelector(".modal-content")
);
selfieModal.render();
const previewCanvas = document.getElementById("preview");
const video = await getVideo(previewCanvas);
savePhotoBtn.addEventListener("click", () => {
drawVideo(video, selfieLayer);
redrawMeme();
});
}
// IIFE in case we don't have top-level await
(async function run() {
setupSettings();
setupAddText();
await setupTakeSelfie();
})();