-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcode-image-generator.html
More file actions
744 lines (664 loc) · 37 KB
/
code-image-generator.html
File metadata and controls
744 lines (664 loc) · 37 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="images/logo.png">
<link rel="apple-touch-icon" href="images/logo.png">
<title>Code Image Generator - Beautiful Code Screenshots | DevDunia</title>
<meta name="description" content="Create beautiful code screenshots. Paste your code, choose a syntax theme, customize the background, and download a stunning PNG image perfect for sharing.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://devdunia.com/code-image-generator.html">
<meta property="og:title" content="Code Image Generator - Beautiful Code Screenshots | DevDunia">
<meta property="og:description" content="Create beautiful code screenshots. Paste your code, choose a syntax theme, and download a stunning PNG image.">
<meta property="og:image" content="https://devdunia.com/images/logo.png">
<meta property="og:site_name" content="DevDunia">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Code Image Generator | DevDunia">
<meta name="twitter:description" content="Create beautiful code screenshots and images. Free, instant, no signup.">
<meta name="twitter:image" content="https://devdunia.com/images/logo.png">
<script src="https://cdn.tailwindcss.com"></script>
<script src="js/common.js"></script>
<!-- Highlight.js -->
<link rel="stylesheet" id="hljs-theme" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<!-- html2canvas -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<!-- Fira Code & Source Code Pro -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500&family=Source+Code+Pro:wght@400;500&display=swap" rel="stylesheet">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebApplication",
"name": "Code Image Generator",
"url": "https://devdunia.com/code-image-generator.html",
"description": "Create beautiful code screenshots. Paste your code, choose a syntax theme, and download a stunning PNG image.",
"applicationCategory": "DeveloperApplication",
"operatingSystem": "Any",
"browserRequirements": "Requires JavaScript",
"offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" },
"creator": { "@type": "Organization", "name": "DevDunia", "url": "https://devdunia.com" }
}
</script>
<style>
/* ── Code Window ── */
#export-canvas-wrapper {
display: flex;
align-items: center;
justify-content: center;
padding: 48px;
min-height: 300px;
border-radius: 0;
transition: background 0.35s ease;
cursor: default;
}
#code-window {
border-radius: 12px;
box-shadow: 0 32px 96px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.07);
overflow: hidden;
min-width: 280px;
max-width: 100%;
background: #282c34;
position: relative;
}
#window-titlebar {
display: flex;
align-items: center;
padding: 13px 16px;
background: rgba(0,0,0,0.28);
position: relative;
}
.window-dots { display: flex; gap: 8px; z-index: 1; }
.dot { width: 13px; height: 13px; border-radius: 50%; flex-shrink: 0; }
.dot-red { background: #ff5f57; }
.dot-yellow { background: #febc2e; }
.dot-green { background: #28c840; }
#window-title-display {
position: absolute;
left: 50%;
transform: translateX(-50%);
font-size: 12px;
color: rgba(255,255,255,0.42);
font-family: 'JetBrains Mono', 'Fira Code', monospace;
pointer-events: none;
white-space: nowrap;
}
#code-display {
padding: 22px 26px;
overflow-x: auto;
}
#code-display pre { margin: 0; background: transparent !important; padding: 0 !important; }
#code-display code { background: transparent !important; padding: 0 !important; border-radius: 0 !important; font-size: inherit !important; font-family: inherit !important; display: block; }
/* ── Toolbar inputs ── */
.tb-select {
background: rgba(15, 23, 42, 0.85);
border: 1px solid rgba(100,116,139,0.35);
color: #e2e8f0;
border-radius: 8px;
padding: 6px 10px;
font-size: 13px;
cursor: pointer;
outline: none;
transition: border-color 0.18s;
min-width: 0;
}
.tb-select:hover, .tb-select:focus { border-color: rgba(168,85,247,0.55); }
.tb-label {
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.07em;
color: #64748b;
margin-bottom: 5px;
white-space: nowrap;
}
/* ── Background swatches ── */
.bg-swatch {
width: 26px;
height: 26px;
border-radius: 50%;
cursor: pointer;
border: 2.5px solid transparent;
transition: transform 0.14s, border-color 0.14s;
flex-shrink: 0;
outline: none;
}
.bg-swatch:hover { transform: scale(1.18); }
.bg-swatch.active {
border-color: #fff;
box-shadow: 0 0 0 2px rgba(255,255,255,0.28);
}
/* ── Toggle switch ── */
.toggle-track {
width: 38px; height: 20px;
background: #334155;
border-radius: 999px;
position: relative;
cursor: pointer;
transition: background 0.2s;
}
.toggle-track.on { background: #7c3aed; }
.toggle-thumb {
position: absolute;
top: 3px; left: 3px;
width: 14px; height: 14px;
border-radius: 50%;
background: #fff;
transition: transform 0.2s;
}
.toggle-track.on .toggle-thumb { transform: translateX(18px); }
/* ── Spinner ── */
@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin 0.8s linear infinite; }
/* ── Range input ── */
input[type=range] { accent-color: #7c3aed; }
/* ── Scrollbar in preview ── */
#code-display::-webkit-scrollbar { height: 4px; }
#code-display::-webkit-scrollbar-track { background: transparent; }
#code-display::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.14); border-radius: 2px; }
/* ── Status ── */
#status-msg { display: none; transition: opacity 0.3s; }
#status-msg.visible { display: block; }
</style>
</head>
<body class="h-screen lg:ml-72">
<div class="fixed inset-0 -z-10 bg-gradient-to-br from-gray-900 via-slate-900 to-gray-900"></div>
<div class="main-content">
<div class="relative z-10 container mx-auto px-4 pt-16 pb-28 sm:px-6 lg:px-8">
<!-- Header -->
<div class="text-center mb-10">
<h1 class="text-3xl sm:text-4xl font-bold mb-3 tracking-tight text-transparent bg-clip-text bg-gradient-to-r from-purple-400 via-pink-400 to-rose-400">
Code Image Generator
</h1>
<p class="text-base text-gray-400 max-w-xl mx-auto">
Paste your code, pick a theme and background, then download a beautiful PNG — like Carbon.sh.
</p>
</div>
<div class="max-w-5xl mx-auto space-y-5">
<!-- ── Toolbar ── -->
<div class="bg-slate-800/70 backdrop-blur-md rounded-2xl border border-slate-700/50 p-5">
<!-- Row 1: dropdowns + font size + padding + title -->
<div class="flex flex-wrap gap-x-5 gap-y-4 items-end">
<div class="flex flex-col">
<div class="tb-label">Theme</div>
<select id="theme-select" class="tb-select">
<option value="atom-one-dark">Atom One Dark</option>
<option value="dracula">Dracula</option>
<option value="monokai">Monokai</option>
<option value="github-dark">GitHub Dark</option>
<option value="night-owl">Night Owl</option>
<option value="vs2015">VS Code Dark</option>
<option value="base16/solarized-dark">Solarized Dark</option>
<option value="tokyo-night-dark">Tokyo Night</option>
<option value="agate">Agate</option>
<option value="nord">Nord</option>
</select>
</div>
<div class="flex flex-col">
<div class="tb-label">Language</div>
<select id="lang-select" class="tb-select">
<option value="auto">Auto Detect</option>
<option value="javascript">JavaScript</option>
<option value="typescript">TypeScript</option>
<option value="python">Python</option>
<option value="go">Go</option>
<option value="rust">Rust</option>
<option value="java">Java</option>
<option value="c">C</option>
<option value="cpp">C++</option>
<option value="csharp">C#</option>
<option value="php">PHP</option>
<option value="ruby">Ruby</option>
<option value="swift">Swift</option>
<option value="kotlin">Kotlin</option>
<option value="bash">Bash / Shell</option>
<option value="sql">SQL</option>
<option value="css">CSS</option>
<option value="html">HTML</option>
<option value="json">JSON</option>
<option value="yaml">YAML</option>
<option value="markdown">Markdown</option>
<option value="plaintext">Plain Text</option>
</select>
</div>
<div class="flex flex-col">
<div class="tb-label">Font</div>
<select id="font-select" class="tb-select">
<option value="'JetBrains Mono',monospace">JetBrains Mono</option>
<option value="'Fira Code',monospace">Fira Code</option>
<option value="'Source Code Pro',monospace">Source Code Pro</option>
<option value="'Courier New',monospace">Courier New</option>
</select>
</div>
<div class="flex flex-col">
<div class="tb-label">Size <span id="font-size-val">14</span>px</div>
<input type="range" id="font-size" min="11" max="22" value="14" class="w-28 mt-1">
</div>
<div class="flex flex-col">
<div class="tb-label">Padding <span id="padding-val">48</span>px</div>
<input type="range" id="padding-range" min="16" max="96" value="48" step="8" class="w-28 mt-1">
</div>
<div class="flex flex-col">
<div class="tb-label">Window Title</div>
<input type="text" id="window-title" value="untitled" maxlength="60"
class="tb-select w-36" placeholder="untitled">
</div>
<!-- Line numbers toggle -->
<div class="flex flex-col">
<div class="tb-label">Line Numbers</div>
<div class="toggle-track mt-1" id="ln-track" onclick="toggleLineNumbers()">
<div class="toggle-thumb" id="ln-thumb"></div>
</div>
</div>
<!-- Shadow toggle -->
<div class="flex flex-col">
<div class="tb-label">Shadow</div>
<div class="toggle-track on mt-1" id="shadow-track" onclick="toggleShadow()">
<div class="toggle-thumb" id="shadow-thumb"></div>
</div>
</div>
</div>
<!-- Row 2: Background swatches -->
<div class="mt-5">
<div class="tb-label mb-2">Background</div>
<div class="flex flex-wrap gap-2 items-center">
<!-- Gradient swatches: data-bg stores the actual CSS value -->
<button class="bg-swatch active" title="Purple Blue"
style="background:linear-gradient(135deg,#667eea,#764ba2)"
data-bg="linear-gradient(135deg,#667eea 0%,#764ba2 100%)"></button>
<button class="bg-swatch" title="Pink Red"
style="background:linear-gradient(135deg,#f093fb,#f5576c)"
data-bg="linear-gradient(135deg,#f093fb 0%,#f5576c 100%)"></button>
<button class="bg-swatch" title="Blue Cyan"
style="background:linear-gradient(135deg,#4facfe,#00f2fe)"
data-bg="linear-gradient(135deg,#4facfe 0%,#00f2fe 100%)"></button>
<button class="bg-swatch" title="Green Teal"
style="background:linear-gradient(135deg,#43e97b,#38f9d7)"
data-bg="linear-gradient(135deg,#43e97b 0%,#38f9d7 100%)"></button>
<button class="bg-swatch" title="Sunset"
style="background:linear-gradient(135deg,#fa709a,#fee140)"
data-bg="linear-gradient(135deg,#fa709a 0%,#fee140 100%)"></button>
<button class="bg-swatch" title="Ocean Night"
style="background:linear-gradient(135deg,#30cfd0,#330867)"
data-bg="linear-gradient(135deg,#30cfd0 0%,#330867 100%)"></button>
<button class="bg-swatch" title="Aurora"
style="background:linear-gradient(135deg,#a8edea,#fed6e3)"
data-bg="linear-gradient(135deg,#a8edea 0%,#fed6e3 100%)"></button>
<button class="bg-swatch" title="Deep Space"
style="background:linear-gradient(135deg,#0f0c29,#302b63,#24243e)"
data-bg="linear-gradient(135deg,#0f0c29 0%,#302b63 50%,#24243e 100%)"></button>
<button class="bg-swatch" title="Slate Dark"
style="background:linear-gradient(135deg,#1e293b,#0f172a)"
data-bg="linear-gradient(135deg,#1e293b 0%,#0f172a 100%)"></button>
<button class="bg-swatch" title="Pure Black"
style="background:#000"
data-bg="#000000"></button>
<button class="bg-swatch" title="Crimson"
style="background:linear-gradient(135deg,#c0392b,#8e44ad)"
data-bg="linear-gradient(135deg,#c0392b 0%,#8e44ad 100%)"></button>
<button class="bg-swatch" title="Emerald Forest"
style="background:linear-gradient(135deg,#134e5e,#71b280)"
data-bg="linear-gradient(135deg,#134e5e 0%,#71b280 100%)"></button>
<!-- Custom colour picker -->
<label class="relative cursor-pointer" title="Custom colour">
<div class="w-7 h-7 rounded-full border-2 border-dashed border-slate-500 hover:border-purple-400 flex items-center justify-center transition-colors overflow-hidden">
<input type="color" id="custom-bg" value="#1a1a2e"
class="absolute opacity-0 w-full h-full cursor-pointer">
<svg class="w-3 h-3 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
</svg>
</div>
</label>
</div>
</div>
</div>
<!-- ── Preview Canvas ── -->
<div class="rounded-2xl border border-slate-700/50 overflow-hidden bg-slate-900/40">
<!-- This entire div gets captured by html2canvas -->
<div id="export-canvas-wrapper">
<div id="code-window">
<!-- Title bar -->
<div id="window-titlebar">
<div class="window-dots">
<div class="dot dot-red"></div>
<div class="dot dot-yellow"></div>
<div class="dot dot-green"></div>
</div>
<div id="window-title-display">untitled</div>
</div>
<!-- Code area -->
<div id="code-display">
<pre><code id="highlighted-code" class="hljs"></code></pre>
</div>
</div>
</div>
</div>
<!-- ── Code Input ── -->
<div class="bg-slate-800/70 backdrop-blur-md rounded-2xl border border-slate-700/50 p-5">
<div class="flex items-center justify-between mb-3">
<label for="code-input" class="text-sm font-medium text-gray-300">Paste your code</label>
<button id="clear-btn" class="text-xs text-slate-500 hover:text-slate-300 transition-colors px-2 py-1 rounded hover:bg-slate-700">Clear</button>
</div>
<textarea
id="code-input"
rows="10"
spellcheck="false"
autocorrect="off"
autocapitalize="off"
class="w-full px-4 py-3 bg-slate-900/70 border border-slate-700 rounded-xl text-gray-200 placeholder-slate-600 focus:outline-none focus:ring-1 focus:ring-purple-500 focus:border-purple-500 resize-y"
style="font-family:'JetBrains Mono',monospace;font-size:13px;line-height:1.6;tab-size:4"
placeholder="// paste your code here..."></textarea>
</div>
<!-- ── Actions ── -->
<div class="flex flex-wrap gap-3 items-center">
<button id="download-btn"
class="flex items-center gap-2 px-7 py-3 bg-gradient-to-r from-purple-600 to-pink-600 hover:from-purple-700 hover:to-pink-700 text-white font-semibold rounded-xl transition-all duration-200 shadow-lg shadow-purple-500/25">
<svg id="dl-icon" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
</svg>
<span id="dl-text">Download PNG</span>
</button>
<button id="copy-img-btn"
class="flex items-center gap-2 px-7 py-3 bg-slate-700 hover:bg-slate-600 text-white font-semibold rounded-xl transition-all duration-200">
<svg id="ci-icon" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>
<span id="ci-text">Copy Image</span>
</button>
<button id="reset-btn"
class="flex items-center gap-2 px-5 py-3 bg-slate-800 hover:bg-slate-700 text-slate-400 hover:text-white font-medium rounded-xl transition-all duration-200">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
</svg>
Reset
</button>
</div>
<!-- Status -->
<div id="status-msg" class="text-center text-sm py-2 px-4 rounded-xl"></div>
</div><!-- /max-w-5xl -->
</div>
</div>
<!-- Sidebar -->
<div id="sidebar-container"></div>
<script>
fetch('final_sidebar.html')
.then(r => r.text())
.then(html => { document.getElementById('sidebar-container').innerHTML = html; })
.catch(() => {});
</script>
<script>
// ─────────────────────────────────────────────
// Constants
// ─────────────────────────────────────────────
const HLJS_CDN = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/';
// Background colour of each hljs theme (used as the code-window bg)
const THEME_BG = {
'atom-one-dark': '#282c34',
'dracula': '#282a36',
'monokai': '#272822',
'github-dark': '#0d1117',
'night-owl': '#011627',
'vs2015': '#1e1e1e',
'base16/solarized-dark': '#002b36',
'tokyo-night-dark': '#1a1b26',
'agate': '#333333',
'nord': '#2e3440',
};
const DEFAULT_CODE =
`const pluckDeep = key => obj =>
key.split('.').reduce((acc, key) => acc[key], obj)
const compose = (...fns) => res =>
fns.reduce((accum, next) => next(accum), res)
const unfold = (f, seed) => {
const go = (f, seed, acc) => {
const res = f(seed)
return res
? go(f, res[1], acc.concat([res[0]]))
: acc
}
return go(f, seed, [])
}`;
// ─────────────────────────────────────────────
// Element refs
// ─────────────────────────────────────────────
const codeInput = document.getElementById('code-input');
const highlightedCode = document.getElementById('highlighted-code');
const codeDisplay = document.getElementById('code-display');
const codeWindow = document.getElementById('code-window');
const canvasWrapper = document.getElementById('export-canvas-wrapper');
const hljsThemeLink = document.getElementById('hljs-theme');
const themeSelect = document.getElementById('theme-select');
const langSelect = document.getElementById('lang-select');
const fontSelect = document.getElementById('font-select');
const fontSizeInput = document.getElementById('font-size');
const fontSizeVal = document.getElementById('font-size-val');
const paddingInput = document.getElementById('padding-range');
const paddingVal = document.getElementById('padding-val');
const windowTitleInput = document.getElementById('window-title');
const windowTitleDisp = document.getElementById('window-title-display');
const lnTrack = document.getElementById('ln-track');
const shadowTrack = document.getElementById('shadow-track');
// ─────────────────────────────────────────────
// State
// ─────────────────────────────────────────────
let lineNumbers = false;
let shadowEnabled = true;
let currentBg = 'linear-gradient(135deg,#667eea 0%,#764ba2 100%)';
// ─────────────────────────────────────────────
// Init
// ─────────────────────────────────────────────
codeInput.value = DEFAULT_CODE;
canvasWrapper.style.background = currentBg;
refreshHighlight();
// ─────────────────────────────────────────────
// Syntax highlight
// ─────────────────────────────────────────────
function refreshHighlight() {
const raw = codeInput.value || '// paste your code here...';
const lang = langSelect.value;
let result;
try {
result = lang === 'auto'
? hljs.highlightAuto(raw)
: hljs.highlight(raw, { language: lang, ignoreIllegals: true });
} catch (_) {
result = hljs.highlightAuto(raw);
}
if (lineNumbers) {
const lines = result.value.split('\n');
// Remove trailing empty line artifact
if (lines[lines.length - 1] === '') lines.pop();
highlightedCode.innerHTML = lines.map((line, i) =>
`<span style="display:block"><span style="display:inline-block;width:2ch;` +
`margin-right:18px;color:rgba(255,255,255,0.25);text-align:right;` +
`user-select:none;-webkit-user-select:none">${i + 1}</span>${line}</span>`
).join('');
} else {
highlightedCode.innerHTML = result.value;
}
}
// ─────────────────────────────────────────────
// Theme change
// ─────────────────────────────────────────────
function applyTheme(theme) {
hljsThemeLink.href = HLJS_CDN + theme + '.min.css';
codeWindow.style.background = THEME_BG[theme] || '#282c34';
}
// ─────────────────────────────────────────────
// Font / size / padding
// ─────────────────────────────────────────────
function applyFont() {
const f = fontSelect.value;
const s = fontSizeInput.value + 'px';
codeDisplay.style.fontFamily = f;
codeDisplay.style.fontSize = s;
fontSizeVal.textContent = fontSizeInput.value;
}
function applyPadding() {
const p = paddingInput.value + 'px';
canvasWrapper.style.padding = p;
paddingVal.textContent = paddingInput.value;
}
// ─────────────────────────────────────────────
// Toggles
// ─────────────────────────────────────────────
function toggleLineNumbers() {
lineNumbers = !lineNumbers;
lnTrack.classList.toggle('on', lineNumbers);
refreshHighlight();
}
function toggleShadow() {
shadowEnabled = !shadowEnabled;
shadowTrack.classList.toggle('on', shadowEnabled);
codeWindow.style.boxShadow = shadowEnabled
? '0 32px 96px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.07)'
: 'none';
}
// ─────────────────────────────────────────────
// Background
// ─────────────────────────────────────────────
function applyBg(bg) {
currentBg = bg;
canvasWrapper.style.background = bg;
}
document.querySelectorAll('.bg-swatch').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.bg-swatch').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
applyBg(btn.dataset.bg);
});
});
document.getElementById('custom-bg').addEventListener('input', function () {
document.querySelectorAll('.bg-swatch').forEach(b => b.classList.remove('active'));
applyBg(this.value);
});
// ─────────────────────────────────────────────
// Event wiring
// ─────────────────────────────────────────────
codeInput.addEventListener('input', refreshHighlight);
themeSelect.addEventListener('change', () => {
applyTheme(themeSelect.value);
refreshHighlight();
});
langSelect.addEventListener('change', refreshHighlight);
fontSelect.addEventListener('change', applyFont);
fontSizeInput.addEventListener('input', applyFont);
paddingInput.addEventListener('input', applyPadding);
windowTitleInput.addEventListener('input', () => {
windowTitleDisp.textContent = windowTitleInput.value.trim() || 'untitled';
});
document.getElementById('clear-btn').addEventListener('click', () => {
codeInput.value = '';
refreshHighlight();
});
document.getElementById('reset-btn').addEventListener('click', () => {
codeInput.value = DEFAULT_CODE;
themeSelect.value = 'atom-one-dark';
langSelect.value = 'auto';
fontSelect.value = "'JetBrains Mono',monospace";
fontSizeInput.value = 14;
paddingInput.value = 48;
windowTitleInput.value = 'untitled';
windowTitleDisp.textContent = 'untitled';
lineNumbers = false;
shadowEnabled = true;
lnTrack.classList.remove('on');
shadowTrack.classList.add('on');
currentBg = 'linear-gradient(135deg,#667eea 0%,#764ba2 100%)';
canvasWrapper.style.background = currentBg;
document.querySelectorAll('.bg-swatch').forEach(b => b.classList.remove('active'));
document.querySelector('.bg-swatch[title="Purple Blue"]').classList.add('active');
applyTheme('atom-one-dark');
applyFont();
applyPadding();
codeWindow.style.boxShadow = '0 32px 96px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.07)';
refreshHighlight();
});
// ─────────────────────────────────────────────
// Export helpers
// ─────────────────────────────────────────────
function spinBtn(btn, iconId, textId, label) {
document.getElementById(iconId).outerHTML =
`<svg id="${iconId}" class="w-5 h-5 spin" fill="none" viewBox="0 0 24 24">` +
`<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>` +
`<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>` +
`</svg>`;
document.getElementById(textId).textContent = label;
btn.disabled = true;
}
function restoreBtn(iconSVG, iconId, textId, label) {
document.getElementById(iconId).outerHTML = iconSVG;
document.getElementById(textId).textContent = label;
}
async function renderCanvas() {
await document.fonts.ready;
return html2canvas(canvasWrapper, {
scale: Math.max(window.devicePixelRatio || 1, 2) * 1.5,
useCORS: true,
allowTaint: false,
backgroundColor: null,
logging: false,
imageTimeout: 8000,
});
}
function showStatus(msg, type) {
const el = document.getElementById('status-msg');
el.textContent = msg;
el.className = 'text-center text-sm py-2 px-4 rounded-xl visible ' + (
type === 'success'
? 'bg-green-900/50 text-green-300 border border-green-700/40'
: 'bg-red-900/50 text-red-300 border border-red-700/40'
);
clearTimeout(el._t);
el._t = setTimeout(() => { el.className = 'text-center text-sm py-2 px-4 rounded-xl'; }, 3500);
}
// ─────────────────────────────────────────────
// Download PNG
// ─────────────────────────────────────────────
const DL_ICON = document.getElementById('dl-icon').outerHTML;
document.getElementById('download-btn').addEventListener('click', async function () {
const btn = this;
spinBtn(btn, 'dl-icon', 'dl-text', 'Generating…');
try {
const canvas = await renderCanvas();
const link = document.createElement('a');
link.download = 'code-snippet-' + Date.now() + '.png';
link.href = canvas.toDataURL('image/png', 1.0);
link.click();
showStatus('PNG downloaded!', 'success');
} catch (err) {
console.error(err);
showStatus('Export failed — try a Chromium-based browser.', 'error');
} finally {
restoreBtn(DL_ICON, 'dl-icon', 'dl-text', 'Download PNG');
btn.disabled = false;
}
});
// ─────────────────────────────────────────────
// Copy image to clipboard
// ─────────────────────────────────────────────
const CI_ICON = document.getElementById('ci-icon').outerHTML;
document.getElementById('copy-img-btn').addEventListener('click', async function () {
const btn = this;
spinBtn(btn, 'ci-icon', 'ci-text', 'Copying…');
try {
const canvas = await renderCanvas();
canvas.toBlob(async blob => {
try {
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
showStatus('Image copied to clipboard!', 'success');
} catch (_) {
showStatus('Clipboard access denied — use Download PNG instead.', 'error');
}
}, 'image/png', 1.0);
} catch (err) {
showStatus('Failed to generate image.', 'error');
} finally {
restoreBtn(CI_ICON, 'ci-icon', 'ci-text', 'Copy Image');
btn.disabled = false;
}
});
</script>
</body>
</html>