-
Notifications
You must be signed in to change notification settings - Fork 0
/
404.html
334 lines (309 loc) · 9.87 KB
/
404.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
name="description"
content="Render Emoji in Html Canvas, save emoji as png image, emoji, .png"
/>
<title>Render Emoji in Html Canvas</title>
<link
rel="stylesheet"
href="/emoji-to-image-via-html-canvas/assets/reset.css"
/>
<link
rel="icon"
href="/emoji-to-image-via-html-canvas/assets/favicon.ico"
/>
<style>
body {
background-color: #262626;
color: aliceblue;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#canvas {
background-image: linear-gradient(45deg, #999 25%, transparent 25%),
linear-gradient(-45deg, #999 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #999 75%),
linear-gradient(-45deg, transparent 75%, #999 75%);
background-size: 20px 20px;
background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
margin: 10px;
border: 1px solid black;
cursor: pointer;
}
#render-emoji-canvas {
background-color: #4caf50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
transition-duration: 0.4s;
}
#save-canvas {
background-color: #4caf50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
transition-duration: 0.4s;
}
.fab {
font-size: 24px;
width: 56px;
height: 56px;
border-radius: 50%;
position: fixed;
right: 16px;
bottom: 16px;
background-color: #4caf50;
color: white;
border: none;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.15);
cursor: pointer;
transition: background-color 0.3s ease;
}
.fab:hover {
background-color: #45a049;
}
input[type="number"],
input[type="text"] {
width: 80%;
box-sizing: border-box;
border: 2px solid #888;
border-radius: 4px;
font-size: 14px;
background-color: #555;
color: #fff;
padding: 4px 10px 4px 10px;
transition: width 0.4s ease-in-out;
}
input[type="number"]:focus,
input[type="text"]:focus {
border-color: #aaa;
outline: none;
}
button {
background-color: #4acdc4;
border: none;
color: white;
padding: 7px 12px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
transition-duration: 0.4s;
}
</style>
<!-- Google tag (gtag.js) -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-KWQZB7C65G"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-KWQZB7C65G");
</script>
</head>
<body>
<div>
<canvas id="canvas" width="256" height="256"></canvas>
<br />
<div style="display: flex; flex-direction: column">
<button id="copyClipboardItem">Copy as ClipboardItem</button>
<button id="copyDataUrl">Copy as Data URL</button>
<button id="copyBase64">Copy as Base64</button>
<button id="downloadCanvasButton">Download as File</button>
</div>
</div>
<br />
<form id="emoji-form">
<label for="emoji-input">Emoji:</label>
<br />
<input
type="text"
id="emoji-input"
placeholder="Enter emoji"
value="🧑🔬"
/>
<br />
<label for="font-size">Font Size:</label>
<br />
<input
type="text"
id="font-size"
placeholder="Enter font size"
value="146"
/>
<br />
<label for="canvas-size">Canvas Size:</label>
<input
type="text"
id="canvas-size"
placeholder="Enter canvas size"
value="256"
/>
<br />
<label for="x-offset">x offset:</label>
<input type="text" id="x-offset" placeholder="Enter x offset" value="0" />
<br />
<label for="y-offset">y offset:</label>
<input type="text" id="y-offset" placeholder="Enter y offset" value="0" />
<br />
<button id="render-emoji-canvas">Render Emoji</button>
<br />
<button id="save-canvas">Render & Download Canvas</button>
</form>
<button id="fab-open-source" class="fab">
<img
src="/emoji-to-image-via-html-canvas/assets/ic_code_24dp.svg"
alt="GitHub Icon"
/>
</button>
<script>
const canvas = document.querySelector("#canvas");
const context = canvas.getContext("2d");
const form = document.querySelector("#emoji-form");
const input = document.querySelector("#emoji-input");
const fontSizeInput = document.querySelector("#font-size");
const renderEmojiButton = document.querySelector("#render-emoji-canvas");
const canvasSizeInput = document.querySelector("#canvas-size");
const xOffsetInput = document.querySelector("#x-offset");
const yOffsetInput = document.querySelector("#y-offset");
const saveButton = document.querySelector("#save-canvas");
const fabGitHubButton = document.querySelector("#fab-open-source");
form.addEventListener("submit", function (event) {
event.preventDefault();
});
renderEmojiButton.addEventListener("click", function (event) {
const size = parseInt(canvasSizeInput.value, 10);
if (!isNaN(size)) {
canvas.width = size;
canvas.height = size;
renderEmojiButton.click();
}
const emoji = input.value;
const xOffset = parseInt(xOffsetInput.value, 10) || 0;
const yOffset = parseInt(yOffsetInput.value, 10) || 0;
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// The size of the emoji is set with the font
context.font = fontSizeInput.value + "px serif";
// use these alignment properties for "better" positioning
context.textAlign = "center";
context.textBaseline = "middle";
// draw the emoji
context.fillText(
emoji,
canvas.width / 2 + xOffset,
canvas.height / 2 + yOffset
);
});
const downloadCanvas = function (filename) {
const dataUrl = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.download = filename
? filename
: `icon_${canvas.height}_f${fontSizeInput.value}_x${xOffsetInput.value}_y${yOffsetInput.value}_${input.value}.png`;
link.href = dataUrl;
link.click();
};
saveButton.addEventListener("click", function () {
renderEmojiButton.click();
downloadCanvas();
});
fabGitHubButton.addEventListener("click", function () {
window.location.href =
"https://github.com/gnekich/emoji-to-image-via-html-canvas";
});
document
.getElementById("copyBase64")
.addEventListener("click", async function () {
const dataUrl = canvas.toDataURL("image/png");
const base64ImagePayload = dataUrl.split(",")[1];
navigator.clipboard.writeText(base64ImagePayload);
});
document
.getElementById("copyDataUrl")
.addEventListener("click", function () {
const dataUrl = canvas.toDataURL("image/png");
navigator.clipboard.writeText(dataUrl);
});
document
.getElementById("copyClipboardItem")
.addEventListener("click", async function () {
const dataUrl = canvas.toDataURL("image/png");
try {
const response = await fetch(dataUrl);
const blob = await response.blob();
navigator.clipboard.write([
new ClipboardItem({
"image/png": blob,
}),
]);
} catch (error) {
console.error(error);
}
});
document
.getElementById("downloadCanvasButton")
.addEventListener("click", async function () {
downloadCanvas("canvas.png");
});
canvas.addEventListener("click", function () {
const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.addEventListener("change", function () {
const file = this.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
// Resize the canvas to match the image size
canvas.width = img.width;
canvas.height = img.height;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(img, 0, 0, canvas.width, canvas.height);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
input.click();
});
document.addEventListener("DOMContentLoaded", function () {
renderEmojiButton.click();
});
</script>
</body>
</html>