-
Notifications
You must be signed in to change notification settings - Fork 3
/
logoGenerator.js
254 lines (218 loc) · 7.93 KB
/
logoGenerator.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const backgroundColor = "transparent";
const simpleBackground = false;
const shadowOpacity = 0.6;
const xShadowOffset = 4;
const yShadowOffset = 3;
const logoContainer = document.getElementById("logo-container");
/**
* Helper function to convert a hexadecimal color to RGBA format.
* @link https://stackoverflow.com/a/28056903/6456163
* @param {String} hex
* @param {Number} opacity
* @returns {String}
*/
function hexToRGB(hex, alpha) {
const r = parseInt(hex.slice(1, 3), 16),
g = parseInt(hex.slice(3, 5), 16),
b = parseInt(hex.slice(5, 7), 16);
return alpha
? `rgba(${r}, ${g}, ${b}, ${alpha})`
: `rgb(${r}, ${g}, ${b})`;
}
/**
* This can be customized to accomplish a wide variety of effects.
* This example creates a gradient inverse of the direction of the
* shadow colors.
* @param {CanvasRenderingContext2D} ctx
* @param {Number} size
*/
const drawComplexBackground = (ctx, size) => {
const rightColor = document.getElementById("colorTwo").value;
const leftColor = document.getElementById("colorOne").value;
const rightColorRGBA = hexToRGB(rightColor, shadowOpacity);
const leftColorRGBA = hexToRGB(leftColor, shadowOpacity);
const gradient = ctx.createLinearGradient(0, 0, size, size);
gradient.addColorStop(0, rightColorRGBA);
gradient.addColorStop(1, leftColorRGBA);
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, size, size);
};
/** Draws a centered horizontal line indicating the center of the canvas. */
const showVerticalCenter = (ctx, size) => {
ctx.strokeStyle = "red";
ctx.moveTo(5, size / 2);
ctx.lineTo(size - 5, size / 2);
ctx.stroke();
};
/**
* Creates a preview of the logo and adds it to the DOM.
* @param {String} fontName
* @param {String} fontPath
* @param {Number} yOffset
*/
const createLogoElement = async (
fontName,
fontPath,
yOffset = 0,
) => {
const font = await createFont(fontName, fontPath);
const size = document.getElementById("previewSize").value;
const logo = await createLogoCanvas({ fontName, size, yOffset });
const logoDiv = document.createElement("div");
logoDiv.className = "logoDiv";
// TODO: Better way of centering this without overflowing
const download = document.createElement("button");
download.innerHTML= `<span>Download '${fontName}'</span>`;
download.onclick = () => downloadLogo(fontName, yOffset);
logoDiv.appendChild(logo);
logoDiv.appendChild(download);
logoContainer.appendChild(logoDiv);
};
/**
* Creates a font with the specified name and path,
* and adds the font to the document so it can be used.
* @param {String} fontName
* @param {String} fontPath
* @returns {FontFace}
*/
const createFont = async (fontName, fontPath) => {
const logoFont = new FontFace(fontName, `url(${fontPath})`);
await logoFont.load();
document.fonts.add(logoFont);
return logoFont;
};
/**
* Creates a canvas element based on the specified parameters.
* @returns {HTMLCanvasElement}
*/
const createLogoCanvas = async ({
fontName,
size,
yOffset,
hidden,
letterSize,
text,
letterColor,
leftColor,
rightColor,
}) => {
if (!size) size = document.getElementById("previewSize").value || 256;
if (!letterSize) letterSize = document.getElementById("fontSize").value || 90;
if (!text) text = document.getElementById("logoText").value || "M";
if (!letterColor) letterColor = document.getElementById("letterColor").value;
if (!leftColor) leftColor = document.getElementById("colorOne").value;
if (!rightColor) rightColor = document.getElementById("colorTwo").value;
// Convert the right and left hex colors to RGBA
const rightColorRGBA = hexToRGB(rightColor, shadowOpacity);
const leftColorRGBA = hexToRGB(leftColor, shadowOpacity);
// Create the necessary elements
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.className = hidden ? "hiddenCanvas" : "visibleCanvas";
canvas.id = fontName;
canvas.dataset.yOffset = yOffset;
// Converts yOffset from a percentage to a pixel value
const yOffsetPixels = yOffset * size / 100;
// https://stackoverflow.com/a/39181687/6456163
canvas.width = size;
canvas.height = size;
// Draw the background
if (simpleBackground) {
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, size, size);
} else {
// Need the white background so the saved image isn't
// darker than the preview.
ctx.fillStyle = "white";
ctx.fillRect(0, 0, size, size);
drawComplexBackground(ctx, size);
}
// Uncomment to debug vertical position
// showVerticalCenter(ctx, size);
// Define the position at which the text will be drawn.
// Note that this will not be perfect for every letter, as some fonts
// have characters of varying widths and heights. Pass an offset
// parameter to adjust for this as necessary.
ctx.textBaseline = "middle";
const xPos = canvas.width / 2;
const yPos = canvas.height / 2 + yOffsetPixels;
// Draw the text in the specified color and font
const fontSize = letterSize / 100 * size;
ctx.font = `${fontSize}px ${fontName}`;
ctx.fillStyle = letterColor;
ctx.textAlign = "center";
ctx.fillText(text, xPos, yPos);
// TODO: Option to change this
// Uncomment if you want rounded blurs instead of sharp edges
// ctx.shadowBlur = 4;
// Draw southeast shadow
ctx.shadowOffsetX = xShadowOffset;
ctx.shadowOffsetY = yShadowOffset;
ctx.shadowColor = rightColorRGBA;
ctx.fillText(text, xPos, yPos);
// Draw northwest shadow
ctx.shadowOffsetX = -xShadowOffset;
ctx.shadowOffsetY = -yShadowOffset;
ctx.shadowColor = leftColorRGBA;
ctx.fillText(text, xPos, yPos);
return canvas;
};
/**
* Downloads the specified logo as a PNG image.
* @param {String} fontName
*/
const downloadLogo = async (fontName, yOffset) => {
const downloadSize = document.getElementById("downloadSize").value;
const fullSizeLogo = await createLogoCanvas({
fontName,
size: downloadSize,
yOffset,
hidden: true
});
const text = document.getElementById("logoText").value || "M";
const link = document.createElement("a");
link.download = `${fontName}_${text}_logo.png`;
link.href = fullSizeLogo.toDataURL("image/png");
link.click();
};
// You can change the % offset of each font as necessary to achieve your
// desired look for your character(s) of choice.
const fontsToRender = [
[ "Adoria", "./fonts/Adoria.ttf", 10 ],
[ "Avalon", "./fonts/Avalon.woff2" ],
[ "Beach Boy", "./fonts/BeachBoy.otf" ],
[ "Bios Bold", "./fonts/Bios-Bold.otf" ],
[ "Bios Regular", "./fonts/Bios-Regular.otf" ],
[ "Broken Console Bold", "./fonts/Broken-Console-Bold.ttf", 15 ],
[ "Broken Console", "./fonts/Broken-Console.ttf", 15 ],
[ "Claudilla", "./fonts/Claudilla.ttf", 5 ],
[ "Dirtchunk", "./fonts/Dirtchunk.otf", 7.5 ],
[ "Enigmatica", "./fonts/Enigmatica.otf", 10 ],
[ "Flare", "./fonts/Flare.ttf", 10 ],
[ "Gluon", "./fonts/Gluon.ttf", -15 ],
[ "Hermes", "./fonts/Hermes.otf", -15 ],
[ "Hotliner Second", "./fonts/Hotliner-Second.otf" ],
[ "Hotliner Third", "./fonts/Hotliner-Third.otf" ],
[ "Hotliner", "./fonts/Hotliner.otf" ],
[ "HotRush Sans", "./fonts/HotRush-Sans.ttf", 10 ],
[ "HotRush Sans Italic", "./fonts/HotRush-SansItalic.ttf", 10 ],
[ "HotRush Sans Striped", "./fonts/HotRush-SansStriped.ttf", 10 ],
[ "HotRush Sans Striped Italic", "./fonts/HotRush-SansStripedItalic.ttf", 10 ],
[ "HotRush Script", "./fonts/HotRush-Script.otf" ],
[ "Hyperbole", "./fonts/Hyperbole.ttf", -5 ],
[ "Kogapunk", "./fonts/Kogapunk.otf", 15 ],
[ "Lagoon Beach Italic", "./fonts/LagoonBeach-Italic.otf", 5 ],
[ "Lagoon Beach", "./fonts/LagoonBeach.otf", 5 ],
[ "Mokoto Glitch", "./fonts/Mokoto-Glitch.ttf", 15 ],
[ "Mokoto Mark", "./fonts/Mokoto-Mark.ttf", 15 ],
[ "Mokoto Outline", "./fonts/Mokoto-Outline.otf", 15 ],
[ "Oxta", "./fonts/Oxta.otf", 5 ],
[ "Raskhal", "./fonts/Raskhal.otf" ],
[ "Researcher Thin", "./fonts/Researcher-Thin.ttf", 5 ],
[ "Researcher", "./fonts/Researcher.ttf", 5 ],
[ "Starlit Drive", "./fonts/StarlitDrive.ttf" ],
[ "Tigerious", "./fonts/Tigerious.otf" ],
[ "Vandals", "./fonts/Vandals.ttf" ],
[ "ZORFICH", "./fonts/ZORFICH.otf", 5 ],
];
fontsToRender.forEach((params) => createLogoElement(...params));