forked from markrai/fontcompare
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
367 lines (308 loc) · 11.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Font Compare v1.0</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
padding-top: 40px;
padding-bottom: 20px;
margin: 0;
background-color: #E5E4E2;
color: #000000;
gap: 20px;
font-family: sans-serif;
transition: background-color 0.3s, color 0.3s;
min-height: calc(100vh - 64px); /* 100vh - (paddings top/bottom + botders top/bottom) */
border: 2px solid #E5E4E2;
}
.dragging-over {
border: 2px dashed #fff;
background-color: #666;
}
.logo {
max-width: 500px;
margin-bottom: 20px;
}
.font-sample-container {
margin: 20px 0;
text-align: center;
}
.font-filename {
font-size: 14px;
color: #555555;
margin-bottom: 5px;
}
#fontSamples {
width: 100%;
}
.font-sample {
font-size: 50px;
margin: 0px auto;
max-width: 90%;
overflow: hidden;
white-space: nowrap;
}
.text-input {
padding: 10px;
font-size: 18px;
width: 300px;
border: 2px solid #ccc;
border-radius: 10px;
outline: none;
transition: border-color 0.3s;
}
.text-input:focus {
border-color: #007BFF;
}
.file-input-wrapper {
position: relative;
display: inline-block;
width: 300px;
}
.file-input {
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
cursor: pointer;
}
.custom-file-button {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
background-color: #317b45;
color: white;
text-align: center;
border-radius: 5px;
border: none;
cursor: pointer;
width: 100%;
transition: background-color 0.3s;
}
.custom-file-button:hover {
background-color: #0056b3;
}
/* Font Parameters Styles */
#fontParams * {
display: inline-block;
}
#fontParams {
display: none;
}
.range-slider{
vertical-align: middle;
accent-color: #ff2f34;
}
label[for=fontSize]{
width:3ch;
}
/* Dark Mode Styles */
body.dark-mode {
background-color: #000000;
color: #ffffff;
}
body.dark-mode .text-input {
border-color: #444444;
}
/* Toggle Switch Styles */
.toggle-switch {
position: fixed;
bottom: 20px;
right: 20px;
display: flex;
align-items: center;
font-size: 14px;
color: black;
gap: 10px;
}
.toggle-switch label {
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}
.toggle-switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: black;
transition: background-color 0.3s;
border-radius: 15px;
}
.slider::before {
content: "";
position: absolute;
width: 21px;
height: 21px;
left: 2px;
bottom: 2px;
background-color: white;
border-radius: 50%;
transition: transform 0.3s;
}
/* Checked State */
.toggle-switch input:checked + .slider {
background-color: white;
}
.toggle-switch input:checked + .slider::before {
transform: translateX(25px);
background-color: black;
}
</style>
</head>
<body ondragover="dragOverHandler(event)" ondragleave="dragLeaveHandler(event)" ondrop="loadFonts(event, 'drop')">
<img src="logolight.png" alt="Logo" class="logo" id="logo">
<div class="file-input-wrapper">
<button class="custom-file-button" id="fontButton">Choose Fonts</button>
<input type="file" class="file-input" accept=".ttf, .otf" multiple onchange="loadFonts(event, 'picker')" />
</div>
<div id="fontParams">
<input type="text" class="text-input" placeholder="Type text here..." oninput="updateText(this.value)" />
<input type="range" id="fontSize" class="range-slider" min="0" max="200" step="1" value="50" autocomplete="off" oninput="updateSize(this.value, currentSizeUnit, 'click')" onwheel="changeRangeWithMouseWheel(event, this.value, this.id)">
<label for="fontSize">50</label>
<select type="select" id="fontSizeUnit" class="dropdown" autocomplete="off" oninput="updateSize(currentSize, this.value)">
<option value="cm">cm</option>
<option value="in">in</option>
<option value="mm">mm</option>
<option value="pc">pc</option>
<option value="pt">pt</option>
<option value="px" selected>px</option>
</select>
</div>
<div id="fontSamples">
</div>
<!-- Dark Mode Toggle Switch -->
<div class="toggle-switch">
<label>
<input type="checkbox" id="darkModeToggle" onclick="toggleDarkMode()">
<span class="slider"></span>
</label>
</div>
<script>
let currentText = "Sample Text";
let currentSize = "50";
let currentSizeUnit = "px";
function dragOverHandler(event) {
event.preventDefault();
const dropZone = document.querySelector('body');
const fontButton = document.getElementById('fontButton');
fontButton.textContent = "Drop fonts";
dropZone.classList.add('dragging-over');
}
function dragLeaveHandler(event) {
event.preventDefault();
const dropZone = document.querySelector('body');
const fontButton = document.getElementById('fontButton');
fontButton.textContent = "Choose fonts";
dropZone.classList.remove('dragging-over');
}
function loadFonts(event, filesource) {
event.stopPropagation();
event.preventDefault();
const dropZone = document.querySelector('body');
dropZone.classList.remove('dragging-over');
const fontContainer = document.getElementById('fontSamples');
const fontButton = document.getElementById('fontButton');
const fontParams = document.getElementById('fontParams');
const fontSize = document.getElementById('fontSize');
fontContainer.innerHTML = '';
fontParams.style.display="inline-block";
const files = (filesource == 'picker') ? Array.from(event.target.files) : Array.from(event.dataTransfer.files);
files.forEach((file, index) => {
const fontName = `CustomFont${index}`;
const fontUrl = URL.createObjectURL(file);
const fontFace = new FontFace(fontName, `url(${fontUrl})`);
fontFace.load().then((loadedFont) => {
document.fonts.add(loadedFont);
const sampleContainer = document.createElement('div');
sampleContainer.classList.add('font-sample-container');
const fileNameDisplay = document.createElement('div');
fileNameDisplay.classList.add('font-filename');
fileNameDisplay.innerText = file.name;
sampleContainer.appendChild(fileNameDisplay);
const sample = document.createElement('div');
sample.classList.add('font-sample');
sample.style.fontFamily = fontName;
sample.innerText = currentText;
sample.style.fontSize = currentSize + currentSizeUnit;
sampleContainer.appendChild(sample);
fontContainer.appendChild(sampleContainer);
});
});
fontButton.innerText = `Choose Fonts (${files.length})`;
}
function updateText(text) {
currentText = text;
document.querySelectorAll('.font-sample').forEach(sample => {
sample.innerText = currentText;
});
}
function updateSize(size, unit, source = 'click') {
currentSize = size;
currentSizeUnit = unit;
if (source == 'click') { //reset default step value if switching from mousewheel control to left click
const picker = document.getElementById('fontSize');
picker.setAttribute('step', '1');
}
const samples = document.getElementsByClassName('font-sample');
for (let i = 0; i < samples.length; i++) {
let sample = samples[i];
sample.style.fontSize = size + unit;
}
const fontSizeLabel = document.querySelector('label[for=fontSize]');
fontSizeLabel.textContent = (size > 2) ? Math.round(size) : size; //round in case of switch from low mousewheel value to high left click value
}
// Enable mousewheel control for range picker
function changeRangeWithMouseWheel(event, size, pickerID) {
const picker = document.getElementById(pickerID);
let stepValue = parseFloat(_changeGranularity(size, currentSize, pickerID));
if (event.deltaY < 0) {
picker.valueAsNumber += stepValue;
updateSize(size, currentSizeUnit, 'wheel')
} else {
picker.value -= stepValue;
updateSize(size, currentSizeUnit, 'wheel')
}
event.preventDefault();
event.stopPropagation();
}
// Helper - Change granularity of range picker
function _changeGranularity(newSize, previousSize, pickerID) {
const picker = document.getElementById(pickerID);
if ( newSize < 2 ) {
picker.setAttribute('step', '0.1');
return 0.1;
} else if ( newSize > 2 && previousSize <= 2 ) {
picker.setAttribute('step', '1');
return 1;
} else {
return 1;
}
}
// Toggle between light and dark modes
function toggleDarkMode() {
const darkModeToggle = document.getElementById('darkModeToggle');
document.body.classList.toggle('dark-mode', darkModeToggle.checked);
const logo = document.getElementById('logo');
if (darkModeToggle.checked) {
logo.src = 'logodark.png';
} else {
logo.src = 'logolight.png';
}
}
</script>
</body>
</html>