-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
406 lines (354 loc) · 11.1 KB
/
index.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Begone Horny</title>
<meta name="description" content="Generate a "begone horny" sticker or emote with a text of your choosing.">
<link rel="icon" href="favicon.png">
<style>
body.dark-mode {
background: #37393f;
color: #ddd;
}
</style>
<script type="module">
import {
reactive,
computed,
html,
css,
Component,
register,
classNames,
attachCSSProperties
} from "https://code.okku.dev/destiny-ui/v0.7.1/dist/mod.js";
const notImpact = new FontFace(
"not_impact",
"url('https://code.okku.dev/fonts/not-impact.woff2')"
);
const chalkduster = new FontFace(
"chalkduster",
"url('https://code.okku.dev/fonts/chalkduster.woff2')"
);
const operinaRomano = new FontFace(
"operina_romano",
"url('https://code.okku.dev/fonts/p22-operina-romano.woff2')"
);
const LINE_SPACING = 0.25;
const ALL_FONTS = [notImpact, chalkduster, operinaRomano];
class AppRoot extends Component {
#top = reactive("begone");
#middle = reactive("horny");
#bottom = reactive("teen");
#dimensions = reactive(320);
#scaledUpDimensions = this.#dimensions.pipe(dimensions => Math.max(dimensions * 4, 320));
#showMetrics = reactive(false);
#speckles = reactive(25);
#darkMode = reactive(window.matchMedia('(prefers-color-scheme: dark)').matches);
#canvasElement = reactive();
#canvasContext = this.#canvasElement.pipe(el => el?.getContext('2d'));
#offscreenCanvasElement = reactive(document.createElement('canvas'));
#offscreenCanvasContext = computed(() => {
const dimensions = this.#scaledUpDimensions.value;
this.#offscreenCanvasElement.value.height = dimensions;
this.#offscreenCanvasElement.value.width = dimensions;
return this.#offscreenCanvasElement.value.getContext('2d');
});
#href = reactive("");
static styles = css`
:host {
display: flex;
flex-flow: row wrap;
align-items: flex-start;
justify-content: flex-start;
gap: 20px;
}
.controls {
display: flex;
flex-flow: column nowrap;
gap: 0.75rem;
}
label {
display: flex;
flex-flow: column;
justify-content: flex-start;
gap: 0.25rem;
min-width: 320px;
}
canvas {
outline: 1px dashed red;
}
/* dark mode */
:host-context(.dark-mode) a {
color: lightblue;
}
:host-context(.dark-mode) input {
border-color: #ddd;
border-style: solid;
border-width: 1px;
padding: 2px 3px;
background-color: #444;
color: white;
}
`;
template = html`
<div class="controls">
<label>
Size:
<input type="number" value=${this.#dimensions} />
(128 = emoji, 320 = stickers)
</label>
<label>
Top:
<input type="text" value=${this.#top} />
</label>
<label>
Middle:
<input type="text" value=${this.#middle} />
</label>
<label>
Bottom:
<input type="text" value=${this.#bottom} />
</label>
<label>
# of Speckles:
<input type="number" prop:valueAsNumber=${this.#speckles} />
</label>
<label>
<span>
<input type="checkbox" prop:checked=${this.#showMetrics} />
Show text metrics
</span>
</label>
<label>
<span>
<input type="checkbox" prop:checked=${this.#darkMode} />
Dark mode
</span>
</label>
<a
href=${this.#href}
download=${computed`${this.#top}_${this.#middle}_${this.#bottom}.png`}
>
💾 Save
</a>
<p><i>Code credit: Cassie</i></p>
</div>
<canvas
width=${this.#dimensions}
height=${this.#dimensions}
destiny:ref=${this.#canvasElement}
/>
`;
async connectedCallback() {
computed(() => {
document.body.className = this.#darkMode.value ? 'dark-mode' : '';
}, {dependents: [this]});
await Promise.all(ALL_FONTS.map((font) => font.load()));
ALL_FONTS.forEach((font) => document.fonts.add(font));
computed(() => {
const offscreenContext = this.#offscreenCanvasContext.value;
const context = this.#canvasContext.value;
if (!offscreenContext || !context) {
return;
}
const diameter = this.#dimensions.value;
const offscreenDiameter = this.#scaledUpDimensions.value;
const top = this.#top.value.toLocaleUpperCase();
const middle = this.#middle.value.toLocaleUpperCase();
let bottom = this.#bottom.value;
if ([...bottom].every((char) => char !== char.toLocaleUpperCase())) {
bottom = bottom.toLocaleUpperCase();
}
const showMetrics = this.#showMetrics.value;
const speckles = this.#speckles.value;
context.clearRect(0, 0, diameter, diameter);
offscreenContext.clearRect(0, 0, offscreenDiameter, offscreenDiameter);
offscreenContext.textAlign = "center";
drawLines(
offscreenContext,
{
top,
middle,
bottom,
},
{
diameter: offscreenDiameter,
showMetrics,
speckles,
}
);
context.drawImage(offscreenContext.canvas, 0, 0, diameter, diameter);
this.#href.value = context.canvas
.toDataURL("image/png")
.replace("image/png", "image/octet-stream");
}, {dependents: [this]});
}
}
register(AppRoot);
function drawLines(context, lines, { diameter, showMetrics, speckles }) {
const radius = diameter / 2;
let fontSize = diameter * 0.24;
let metrics;
let lineHeights;
let lineSpacing;
let totalContentHeight;
({ metrics } = measureMetrics(context, fontSize, lines));
// first, adjust font size such that the width of the largest line fits neatly within the canvas
let widthFactor = Infinity;
for (const metric of metrics) {
widthFactor = Math.min(widthFactor, diameter / metric.width);
}
fontSize *= widthFactor;
({ metrics, lineHeights, totalContentHeight } = measureMetrics(
context,
fontSize,
lines
));
// if the content is too big, also adjust the font size to compensate.
const heightFactor = Math.min(1, diameter / totalContentHeight);
fontSize *= heightFactor;
({ metrics, lineSpacing, lineHeights, totalContentHeight } = measureMetrics(
context,
fontSize,
{
...lines,
bottom: 'Pa',
}
));
const verticalMargin = (diameter - totalContentHeight) / 2;
// we're good to draw now!
context.textBaseline = "middle";
let yPosition = verticalMargin + metrics[0].actualBoundingBoxAscent;
setTopFontStyles(context, fontSize);
context.fillText(lines.top, radius, yPosition);
if (showMetrics) {
drawMetrics(context, diameter, yPosition, metrics[0]);
}
speckleRect(context, speckles, {
x: radius - metrics[0].width / 2,
y: verticalMargin,
width: metrics[0].width,
height: lineHeights[0],
});
yPosition +=
metrics[0].actualBoundingBoxDescent +
lineSpacing +
metrics[1].actualBoundingBoxAscent;
setMiddleFontStyles(context, fontSize);
context.fillText(lines.middle, radius, yPosition);
if (showMetrics) {
drawMetrics(context, diameter, yPosition, metrics[1]);
}
yPosition +=
metrics[1].actualBoundingBoxDescent +
lineSpacing +
metrics[2].actualBoundingBoxAscent;
setBottomFontStyles(context, fontSize);
context.fillText(lines.bottom, radius, yPosition);
if (showMetrics) {
drawMetrics(context, diameter, yPosition, metrics[2]);
}
}
function speckleRect(context, speckles, { x, y, width, height }) {
// pick four points to center the speckle effect
// for each of these points, select some number of points to remove colour data from
// also pick a maximum radius somewhere around 5% - 15% of the rectangle
const speckleCentres =
Array.from({length: speckles}, () => getRandomPoint({ x, y, width, height }));
const specklePoints = [];
for (const {x, y} of speckleCentres) {
const numberOfPoints = rand(30, 51) | 0;
const maxRadius = rand(0.05, 0.15) * Math.max(width, height);
specklePoints.push(
...Array.from({ length: numberOfPoints }, () =>
getRandomPointInCircle({ x, y, radius: maxRadius })
)
);
}
const minSpeckleSize = .5;
const maxSpeckleSize = .025 * Math.min(width, height);
for (const {x, y} of specklePoints) {
// clear a randomly small rectangle in each dimension
const width = rand(minSpeckleSize, maxSpeckleSize);
const height = rand(minSpeckleSize, maxSpeckleSize);
context.clearRect(x, y, width, height);
}
}
function getRandomPoint({ x, y, width, height }) {
return { x: rand(x, x + width), y: rand(y, y + height) };
}
// f(x) = x - 2 sqrt(x) + 1
function weightDistribution(val) {
return val - 2 * Math.sqrt(val) + 1
}
function getRandomPointInCircle({ x, y, radius }) {
const angle = rand(0, 2 * Math.PI);
const dist = weightDistribution(rand(0, 1)) * radius;
return { x: x + dist * Math.cos(angle), y: y + dist * Math.sin(angle) };
}
function rand(min, max) {
return Math.random() * (max - min) + min;
}
function measureMetrics(context, fontSize, { top, middle, bottom }) {
const metrics = [];
context.textBaseline = "middle";
// for each line, measure the initial size of the text to be rendered.
setTopFontStyles(context, fontSize);
metrics.push(context.measureText(top));
setMiddleFontStyles(context, fontSize);
metrics.push(context.measureText(middle));
setBottomFontStyles(context, fontSize);
metrics.push(context.measureText(bottom));
const lineHeights = metrics.map(
(metric) => metric.actualBoundingBoxAscent + metric.actualBoundingBoxDescent
);
// use the middle line as the reference line for spacing
const lineSpacing = lineHeights[1] * LINE_SPACING;
const totalContentHeight =
lineHeights.reduce((a, b) => a + b) + lineSpacing * 2;
return {
metrics,
lineHeights,
lineSpacing,
totalContentHeight
};
}
function drawMetrics(context, diameter, baselinePosition, metric) {
context.strokeStyle = "black";
context.beginPath();
context.moveTo(0, baselinePosition);
context.lineTo(diameter, baselinePosition);
context.closePath();
context.stroke();
context.beginPath();
context.moveTo(0, baselinePosition - metric.actualBoundingBoxAscent);
context.lineTo(diameter, baselinePosition - metric.actualBoundingBoxAscent);
context.closePath();
context.stroke();
context.beginPath();
context.moveTo(0, baselinePosition + metric.actualBoundingBoxDescent);
context.lineTo(diameter, baselinePosition + metric.actualBoundingBoxDescent);
context.closePath();
context.stroke();
}
function setTopFontStyles(context, size) {
context.font = `${size}px ${notImpact.family}`;
context.fillStyle = "#fecbfe";
}
function setMiddleFontStyles(context, size) {
context.font = `${size}px ${chalkduster.family}`;
context.fillStyle = "#9898fd";
}
function setBottomFontStyles(context, size) {
context.font = `${size * 1.2}px ${operinaRomano.family}`;
context.fillStyle = "#fd3232";
}
</script>
</head>
<body>
<app-root>Loading…</app-root>
</body>
</html>