-
Notifications
You must be signed in to change notification settings - Fork 22
/
meme-me.html
240 lines (214 loc) · 7.15 KB
/
meme-me.html
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
235
236
237
238
239
240
<!doctype html>
<html lang="en" data-theme="dark">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>meme me!</title>
</head>
<body class="container grid">
<header>
<h1 class="impact">meme me!</h1>
</header>
<main>
<article>
<canvas
id="meme"
name="meme"
aria-label="Meme image"
alt="Empty meme placeholder"
>
</canvas>
</article>
</main>
<nav>
<div id="steps" class="container">
<div class="step" id="take-photo">
<button role="button">Take photo</button>
<dialog name="photo" aria-label="Take photo">
<form method="dialog">
<article>
<header>
Take photo
<button
class="close contrast"
type="button"
aria-label="Done"
></button>
</header>
<main>
<button id="start-webcam" type="button">Start webcam</button>
<input id="photo-complete" type="hidden" value="no" />
</main>
</article>
</form>
</dialog>
</div>
<div class="step" id="add-text">
<button role="button">Add text</button>
<dialog name="text" aria-label="Add text">
<form method="dialog">
<article>
<header>
Add text
<button
class="close contrast"
type="submit"
aria-label="Done"
></button>
</header>
<main>
<button>+</button>
<input id="text-complete" type="hidden" value="no" />
</main>
</article>
</form>
</dialog>
</div>
<div class="step" id="share">
<button role="button">Share</button>
<dialog name="share" aria-label="Share">
<form method="dialog">
<article>
<header>
Share
<button
class="close contrast"
type="submit"
aria-label="Done"
></button>
</header>
<main>
<input
type="email"
value=""
placeholder="your@email.com"
required
autofocus
/>
<button type="button">share</button>
<input id="share-complete" type="hidden" value="no" />
</main>
</article>
</form>
</dialog>
</div>
</div>
</nav>
<footer>
<a href="https://frontendmasters.com/" target="_blank"
>ProJS on FrontendMasters</a
>
<label>
<input
type="checkbox"
id="switch"
name="switch"
role="switch"
checked
/>
Dark mode
</label>
</footer>
</body>
<script id="main.js">
const steps = document.querySelectorAll("#steps > *");
for (let [number, step] of steps.entries()) {
const openButton = step.querySelector("button");
const dialog = step.querySelector("dialog");
const form = step.querySelector("form");
const closeButton = form.querySelector("button.close");
const nextStep = steps.item(number + 1);
if (number > 0) {
openButton.setAttribute("disabled", "true");
}
function checkCompleted() {
const nextStepButton = nextStep?.querySelector("button");
const isCompleted = form.querySelector("input[type=hidden]");
if (isCompleted?.value === "yes") {
step.classList.add("complete");
openButton.classList.add("secondary");
nextStepButton?.removeAttribute("disabled");
return true;
} else {
step.classList.remove("complete");
openButton.classList.remove("secondary");
nextStepButton?.setAttribute("disabled", "true");
return false;
}
}
openButton.addEventListener("click", () => {
dialog.showModal();
});
const checkAndClose = (e) => {
checkCompleted();
dialog.close();
};
form.addEventListener("submit", checkAndClose);
closeButton.addEventListener("click", checkAndClose);
for (let input of form.elements) {
input.addEventListener("input", () => {
if (checkCompleted()) dialog.close();
});
}
}
</script>
<script id="webcam.js">
const takeBtn = document.createElement("button");
takeBtn.type = "button";
takeBtn.textContent = "Take photo";
const retakeBtn = document.createElement("button");
retakeBtn.type = "button";
retakeBtn.textContent = "Retake photo";
retakeBtn.classList.add("contrast");
const acceptBtn = document.createElement("button");
acceptBtn.type = "submit";
acceptBtn.textContent = "Use this photo";
acceptBtn.setAttribute("disabled", "true");
const webcamButton = document.getElementById("start-webcam");
webcamButton.addEventListener("click", async (e) => {
let webcam = null;
try {
webcam = await getVideoSource();
} catch (error) {
webcamButton.textContent = "Failed to access webcam";
}
webcam?.addEventListener("canplay", () => {
const { videoWidth, videoHeight } = webcam;
const meme = document.getElementById("meme");
memeCtx = meme.getContext("2d");
meme.setAttribute("width", videoWidth);
meme.setAttribute("height", videoHeight);
const preview = document.createElement("canvas");
const previewCtx = preview.getContext("2d");
preview.setAttribute("width", videoWidth);
preview.setAttribute("height", videoHeight);
takeBtn.addEventListener("click", () => {
previewCtx.drawImage(webcam, 0, 0, videoWidth, videoHeight);
webcam.pause();
webcam.replaceWith(preview);
takeBtn.replaceWith(retakeBtn);
acceptBtn.removeAttribute("disabled");
});
retakeBtn.addEventListener("click", () => {
webcam.play();
preview.replaceWith(webcam);
retakeBtn.replaceWith(takeBtn);
acceptBtn.setAttribute("disabled", "true");
});
acceptBtn.addEventListener("click", (e) => {
const incomplete = document.getElementById("photo-complete");
incomplete.value = "yes";
memeCtx.drawImage(webcam, 0, 0, videoWidth, videoHeight);
});
const acceptOrRetake = document.createElement("fieldset");
acceptOrRetake.appendChild(takeBtn);
acceptOrRetake.appendChild(acceptBtn);
webcamButton.replaceWith(webcam, acceptOrRetake);
});
});
// startButton.addEventListener('click', () => {
// runPhotobooth(webcam, webcam.videoWidth);
// })
// startButton.removeAttribute('disabled');
</script>
</html>