-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathresponsive-image-simulator.html
More file actions
467 lines (409 loc) · 14.8 KB
/
Copy pathresponsive-image-simulator.html
File metadata and controls
467 lines (409 loc) · 14.8 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
<!--
Responsive Image Simulator
Single-file client-side tool to preview a responsive image (srcset/sizes) at
common device breakpoints and see exactly which source the browser selects
and at what rendered width.
Built for One File Tools.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Responsive Image Simulator - One File Tools</title>
<meta name="description" content="Preview a srcset/sizes image at common device breakpoints and see which source the browser selects." />
<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=Space+Grotesk:wght@600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet" />
<style>
:root {
--bg: #030712;
--grid-line: #111827;
--panel: rgba(17, 24, 39, 0.7);
--panel-border: #1f2937;
--accent: #fb923c;
--accent-hover: #f97316;
--accent-glow: rgba(251, 146, 60, 0.15);
--text: #f9fafb;
--text-muted: #9ca3af;
--text-dim: #4b5563;
--radius-sm: 12px;
--radius-md: 20px;
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--font-display: "Space Grotesk", sans-serif;
--color-weak: #ef4444;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-sans);
background-color: var(--bg);
background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
background-size: 32px 32px;
color: var(--text);
padding: 48px 20px 80px;
min-height: 100vh;
display: flex;
justify-content: center;
}
.app {
width: 100%;
max-width: 960px;
}
.hero {
text-align: center;
margin-bottom: 32px;
}
.eyebrow {
font-family: var(--font-mono);
font-size: 12px;
letter-spacing: 0.2em;
color: var(--accent);
text-transform: uppercase;
margin-bottom: 12px;
font-weight: 500;
}
.wordmark {
font-family: var(--font-display);
font-weight: 700;
font-size: clamp(26px, 5vw, 38px);
letter-spacing: -0.02em;
margin-bottom: 12px;
}
.tagline {
color: var(--text-muted);
font-size: 15px;
line-height: 1.5;
max-width: 600px;
margin: 0 auto;
}
.studio {
background: var(--panel);
backdrop-filter: blur(20px);
border: 1px solid var(--panel-border);
border-radius: var(--radius-md);
padding: 28px;
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.5);
margin-bottom: 24px;
}
.section-label {
font-family: var(--font-mono);
font-size: 12px;
font-weight: 600;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.1em;
display: block;
margin-bottom: 14px;
}
.field {
display: flex;
flex-direction: column;
gap: 6px;
margin-bottom: 14px;
}
.field label {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
text-transform: uppercase;
letter-spacing: 0.05em;
}
.field input,
.field textarea {
background: rgba(17, 24, 39, 0.8);
border: 1px solid var(--panel-border);
border-radius: 8px;
color: var(--text);
font-family: var(--font-mono);
font-size: 14px;
padding: 10px 12px;
width: 100%;
}
.field textarea {
resize: vertical;
min-height: 70px;
line-height: 1.5;
}
.field input:focus,
.field textarea:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.field-error {
font-size: 12px;
color: var(--color-weak);
min-height: 14px;
}
.breakpoint-tabs {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-bottom: 16px;
}
.bp-tab {
background: rgba(31, 41, 55, 0.5);
border: 1px solid var(--panel-border);
color: var(--text-muted);
border-radius: 8px;
padding: 8px 14px;
font-family: var(--font-mono);
font-size: 12px;
cursor: pointer;
}
.bp-tab.active {
background: var(--accent);
color: #030712;
border-color: var(--accent);
font-weight: 700;
}
.bp-tab:hover:not(.active) {
background: rgba(55, 65, 81, 0.7);
}
.bp-tab:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.device-frame {
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 16px;
overflow-x: auto;
}
.viewport-shell {
margin: 0 auto;
border: 2px solid var(--panel-border);
border-radius: 10px;
overflow: hidden;
transition: width 0.3s ease;
background: #000;
}
.viewport-shell img {
display: block;
width: 100%;
height: auto;
}
.viewport-meta {
text-align: center;
margin-top: 10px;
font-family: var(--font-mono);
font-size: 12px;
color: var(--text-muted);
}
.info-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
gap: 10px;
margin-top: 16px;
}
.info-card {
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 12px 14px;
}
.info-card .label {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 4px;
}
.info-card .value {
font-family: var(--font-mono);
font-size: 15px;
color: var(--text);
word-break: break-all;
}
.note {
margin-top: 16px;
font-size: 13px;
color: var(--text-muted);
line-height: 1.6;
background: rgba(251, 146, 60, 0.07);
border: 1px solid rgba(251, 146, 60, 0.25);
border-radius: var(--radius-sm);
padding: 12px 14px;
}
.note code {
color: var(--text);
font-family: var(--font-mono);
}
.footer-note {
text-align: center;
color: var(--text-dim);
font-size: 13px;
margin-top: 8px;
}
.footer-note a {
color: var(--text-muted);
}
@media (max-width: 640px) {
.studio {
padding: 20px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="hero">
<div class="eyebrow">Image Tools</div>
<h1 class="wordmark">Responsive Image Simulator</h1>
<p class="tagline">Preview a <code>srcset</code>/<code>sizes</code> image at common device breakpoints, and see exactly which source the browser would pick and at what rendered width.</p>
</header>
<main class="studio">
<span class="section-label">Image markup</span>
<div class="field">
<label for="srcsetInput">srcset</label>
<textarea id="srcsetInput" spellcheck="false">
https://picsum.photos/id/1015/400/300 400w,
https://picsum.photos/id/1015/800/600 800w,
https://picsum.photos/id/1015/1200/900 1200w,
https://picsum.photos/id/1015/1600/1200 1600w</textarea>
<div class="field-error" id="srcsetError"></div>
</div>
<div class="field">
<label for="sizesInput">sizes</label>
<input type="text" id="sizesInput" value="(max-width: 600px) 100vw, (max-width: 1024px) 50vw, 33vw" spellcheck="false" />
</div>
<div class="field">
<label for="fallbackInput">Fallback src (for browsers without srcset support)</label>
<input type="text" id="fallbackInput" value="https://picsum.photos/id/1015/800/600" spellcheck="false" />
</div>
</main>
<main class="studio">
<span class="section-label">Simulated viewport</span>
<div class="breakpoint-tabs" id="breakpointTabs"></div>
<div class="device-frame">
<div class="viewport-shell" id="viewportShell">
<img id="previewImage" alt="Responsive preview" />
</div>
<p class="viewport-meta" id="viewportMeta"></p>
</div>
<div class="info-grid" id="infoGrid"></div>
<div class="note">The simulator renders the image inside a container scaled to the chosen viewport width, then reads the browser's actual <code>currentSrc</code> to report which candidate it selected — the same algorithm your visitors' browsers use, so this reflects real behavior rather than a guess.</div>
</main>
<p class="footer-note">Part of <a href="https://github.com/praveenscience/One-File-Tools">One File Tools</a>. Images load from their original URLs — nothing is uploaded or proxied.</p>
</div>
<script>
/* ==========================================================
Responsive Image Simulator
Vanilla JS, no dependencies.
========================================================== */
const srcsetInput = document.getElementById("srcsetInput");
const sizesInput = document.getElementById("sizesInput");
const fallbackInput = document.getElementById("fallbackInput");
const srcsetError = document.getElementById("srcsetError");
const breakpointTabs = document.getElementById("breakpointTabs");
const viewportShell = document.getElementById("viewportShell");
const previewImage = document.getElementById("previewImage");
const viewportMeta = document.getElementById("viewportMeta");
const infoGrid = document.getElementById("infoGrid");
// Common device breakpoints to simulate. Width is the CSS viewport width
// in pixels; the shell is scaled down visually via max-width for display,
// but the actual <img> element is rendered at this width so srcset/sizes
// resolve exactly as they would on a real device of that width.
const BREAKPOINTS = [
{ id: "mobile-s", label: "Mobile S", width: 360 },
{ id: "mobile-l", label: "Mobile L", width: 480 },
{ id: "tablet", label: "Tablet", width: 768 },
{ id: "laptop", label: "Laptop", width: 1024 },
{ id: "desktop", label: "Desktop", width: 1440 },
{ id: "wide", label: "Wide", width: 1920 }
];
let activeBreakpoint = BREAKPOINTS[2]; // Tablet by default.
/** Renders the breakpoint tab buttons. */
function renderTabs() {
breakpointTabs.innerHTML = "";
BREAKPOINTS.forEach((bp) => {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "bp-tab" + (bp.id === activeBreakpoint.id ? " active" : "");
btn.textContent = bp.label + " · " + bp.width + "px";
btn.addEventListener("click", () => {
activeBreakpoint = bp;
renderTabs();
updatePreview();
});
breakpointTabs.appendChild(btn);
});
}
/** Validates and normalizes the srcset textarea into a single-line attribute value. */
function parseSrcset(rawText) {
const lines = rawText
.split(/[\n,]/)
.map((line) => line.trim())
.filter(Boolean);
if (lines.length === 0) return { value: "", error: "Add at least one candidate (URL + width, e.g. image.jpg 800w)." };
for (const line of lines) {
if (!/^\S+\s+\d+w$/.test(line)) {
return { value: "", error: 'Each candidate must be "URL widthw" (e.g. photo.jpg 800w). Problem: "' + line + '"' };
}
}
return { value: lines.join(", "), error: null };
}
/** Formats dimensions as "W × H" or an em dash when unavailable. */
function formatDimensions(width, height) {
if (!width || !height) return "—";
return width + " × " + height;
}
/** Renders the info cards describing the resolved image source. */
function renderInfo() {
infoGrid.innerHTML = "";
const cards = [
{ label: "Viewport width", value: activeBreakpoint.width + "px" },
{ label: "Selected source", value: previewImage.currentSrc || "(loading…)" },
{ label: "Natural size", value: formatDimensions(previewImage.naturalWidth, previewImage.naturalHeight) },
{ label: "sizes value", value: sizesInput.value.trim() || "(none — defaults to 100vw)" }
];
cards.forEach((card) => {
const div = document.createElement("div");
div.className = "info-card";
const label = document.createElement("div");
label.className = "label";
label.textContent = card.label;
const value = document.createElement("div");
value.className = "value";
value.textContent = card.value;
div.appendChild(label);
div.appendChild(value);
infoGrid.appendChild(div);
});
}
/**
* Rebuilds the <img> with the current srcset/sizes/fallback and sizes the
* viewport shell to the active breakpoint's width, then waits for the
* browser to resolve currentSrc before updating the info cards.
*/
function updatePreview() {
const { value: srcsetValue, error } = parseSrcset(srcsetInput.value);
srcsetError.textContent = error || "";
if (error) return;
viewportShell.style.width = activeBreakpoint.width + "px";
viewportShell.style.maxWidth = "100%";
previewImage.srcset = srcsetValue;
previewImage.sizes = sizesInput.value.trim();
previewImage.src = fallbackInput.value.trim();
viewportMeta.textContent = "Rendered at " + activeBreakpoint.width + "px viewport width";
// currentSrc is only reliably populated after the image finishes
// resolving/loading, so refresh the info cards on load and also once
// immediately for instant feedback while it loads.
renderInfo();
previewImage.onload = renderInfo;
}
[srcsetInput, sizesInput, fallbackInput].forEach((el) => {
el.addEventListener("input", updatePreview);
});
renderTabs();
updatePreview();
</script>
</body>
</html>