-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathsvg-render.html
More file actions
412 lines (378 loc) · 15.5 KB
/
svg-render.html
File metadata and controls
412 lines (378 loc) · 15.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG to JPEG/PNG</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
font-size: 16px;
}
textarea, input, button, select {
font-size: 16px;
}
#dropZone {
width: 100%;
height: 150px;
border: 2px dashed #ccc;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
position: relative;
box-sizing: border-box;
}
#dropZone.dragover {
border-color: #000;
background-color: #f0f0f0;
}
#svgInput {
width: 100%;
height: 100%;
border: none;
resize: none;
box-sizing: border-box;
}
#imageContainer {
margin-top: 20px;
text-align: center;
}
#convertedImage {
max-width: 90%;
cursor: pointer;
transition: max-width 0.3s ease;
border: 2px dashed #666;
}
#convertedImage.full-size {
max-width: unset;
}
#base64Output {
word-break: break-all;
white-space: pre-wrap;
}
.option-group {
margin-bottom: 10px;
}
#fileInput {
margin-bottom: 10px;
}
#downloadLink, #loadExampleLink {
display: inline-block;
margin-top: 10px;
margin-right: 10px;
}
#widthInput, #paddingInput {
width: 60px;
}
#bgColor {
vertical-align: middle;
}
#fileSize {
margin-left: 10px;
}
</style>
</head>
<body>
<h1>SVG to JPEG/PNG</h1>
<input type="file" id="fileInput" accept=".svg">
<a href="?url=https://gist.githubusercontent.com/simonw/aedecb93564af13ac1596810d40cac3c/raw/83e7f3be5b65bba61124684700fa7925d37c36c3/tiger.svg" id="loadExampleLink">Load example image</a>
<div id="dropZone">
<textarea id="svgInput" placeholder="Paste your SVG code here or drag & drop an SVG file"></textarea>
</div>
<div class="option-group">
<label>
<input type="radio" name="format" value="image/jpeg" checked> JPEG
</label>
<label>
<input id="pngRadio" type="radio" name="format" value="image/png"> PNG
</label>
</div>
<div class="option-group">
<label for="bgColor">Background Color:</label>
<input type="color" id="bgColor" value="#ffffff">
<label>
<input type="checkbox" id="transparentBg"> Transparent
</label>
</div>
<div class="option-group">
<label for="widthInput">Output Width:</label>
<input type="number" id="widthInput" value="800" min="1">
</div>
<div class="option-group">
<label for="paddingInput">Padding:</label>
<input type="number" id="paddingInput" value="0" min="0">
<span>px</span>
</div>
<button id="convertButton" onclick="convertSvgToImage()">Convert SVG</button>
<div id="imageContainer"></div>
<a id="downloadLink" style="display: none;">Download Image</a>
<span id="fileSize"></span>
<div id="output" style="display: none;">
<h2>Base64 Image Tag:</h2>
<button id="copyImageButton" onclick="copyBase64Tag()">Copy image tag</button>
<pre id="base64Output"></pre>
</div>
<script>
const dropZone = document.getElementById('dropZone');
const svgInput = document.getElementById('svgInput');
const fileInput = document.getElementById('fileInput');
const widthInput = document.getElementById('widthInput');
const paddingInput = document.getElementById('paddingInput');
const bgColor = document.getElementById('bgColor');
const transparentBg = document.getElementById('transparentBg');
const fileSizeSpan = document.getElementById('fileSize');
const outputDiv = document.getElementById('output');
const copyImageButton = document.getElementById('copyImageButton');
const formatRadios = document.querySelectorAll('input[name="format"]');
// ===== Hash <-> Text Sync Helpers =====
function getTextFromHash() {
if (!location.hash || location.hash.length <= 1) return null;
try {
return decodeURIComponent(location.hash.slice(1));
} catch (e) {
console.warn('Could not decode hash:', e);
return null;
}
}
function setHashFromText(text) {
// Avoid spamming history with each render
const encoded = encodeURIComponent(text);
const target = '#' + encoded;
if (location.hash !== target) {
if (history.replaceState) {
history.replaceState(null, '', target);
} else {
location.hash = encoded;
}
}
}
function loadFromHashIfPresent() {
const text = getTextFromHash();
if (text) {
svgInput.value = text;
convertSvgToImage();
}
}
window.addEventListener('hashchange', () => {
const text = getTextFromHash();
if (text !== null) {
svgInput.value = text;
convertSvgToImage();
}
});
// Add event listeners for instant preview updates
paddingInput.addEventListener('input', debounce(convertSvgToImage, 300));
widthInput.addEventListener('input', debounce(convertSvgToImage, 300));
bgColor.addEventListener('input', debounce(convertSvgToImage, 300));
formatRadios.forEach(radio => {
radio.addEventListener('change', convertSvgToImage);
});
transparentBg.addEventListener('change', convertSvgToImage);
// Debounce function to prevent too many updates
function debounce(func, wait) {
let timeout;
return function() {
const context = this;
const args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args);
}, wait);
};
}
// Load SVG from ?url= parameter if present
function loadFromUrlParamIfPresent() {
const urlParams = new URLSearchParams(window.location.search);
const svgUrl = urlParams.get('url');
if (svgUrl) {
fetch(svgUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.text();
})
.then(data => {
svgInput.value = data;
convertSvgToImage();
})
.catch(error => {
console.error('Error loading SVG from URL:', error);
alert('Failed to load SVG from URL: ' + error.message);
});
return true;
}
return false;
}
// Color and transparency handling
bgColor.addEventListener('input', () => {
transparentBg.checked = false;
});
transparentBg.addEventListener('change', () => {
if (transparentBg.checked) {
bgColor.value = "#ffffff";
pngRadio.click();
}
});
// Drag and drop functionality
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file && file.type === 'image/svg+xml') {
readFile(file);
}
});
// File input functionality
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file && file.type === 'image/svg+xml') {
readFile(file);
}
});
function readFile(file) {
const reader = new FileReader();
reader.onload = (e) => {
svgInput.value = e.target.result;
convertSvgToImage(); // Update preview with the loaded file
};
reader.readAsText(file);
}
function convertSvgToImage() {
let svgContent = document.getElementById('svgInput').value;
// If there's no SVG content, don't try to convert (and clear preview)
if (!svgContent.trim()) {
document.getElementById('imageContainer').innerHTML = '';
document.getElementById('downloadLink').style.display = 'none';
document.getElementById('fileSize').textContent = '';
document.getElementById('base64Output').textContent = '';
document.getElementById('output').style.display = 'none';
return;
}
const imageContainer = document.getElementById('imageContainer');
const base64Output = document.getElementById('base64Output');
const downloadLink = document.getElementById('downloadLink');
const format = document.querySelector('input[name="format"]:checked').value;
const newWidth = parseInt(widthInput.value) || 800;
const padding = parseInt(paddingInput.value) || 0;
// Clear previous content
base64Output.textContent = '';
downloadLink.style.display = 'none';
fileSizeSpan.textContent = '';
// Extract the SVG element, ignoring any surrounding content
const lowerContent = svgContent.toLowerCase();
const svgStart = lowerContent.indexOf('<svg');
const svgEnd = lowerContent.lastIndexOf('</svg>');
if (svgStart === -1 || svgEnd === -1) {
alert('Invalid SVG input');
return;
}
svgContent = svgContent.slice(svgStart, svgEnd + '</svg>'.length);
// Create a temporary SVG element
const svgElement = new DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
if (!svgElement || svgElement.nodeName !== 'svg') {
alert('Invalid SVG input');
return;
}
// Get SVG viewBox
let viewBox = svgElement.getAttribute('viewBox');
let width, height;
if (viewBox) {
[, , width, height] = viewBox.split(' ').map(Number);
} else {
width = parseInt(svgElement.getAttribute('width')) || 300;
height = parseInt(svgElement.getAttribute('height')) || 150;
}
// Calculate new dimensions with padding
const aspectRatio = width / height;
const contentWidth = newWidth;
const contentHeight = Math.round(contentWidth / aspectRatio);
// Total canvas dimensions including padding
const totalWidth = contentWidth + (padding * 2);
const totalHeight = contentHeight + (padding * 2);
// Create off-screen canvas
const canvas = document.createElement('canvas');
canvas.width = totalWidth;
canvas.height = totalHeight;
// Draw SVG on canvas
const ctx = canvas.getContext('2d');
// Set background color if not transparent
if (!transparentBg.checked) {
ctx.fillStyle = bgColor.value;
ctx.fillRect(0, 0, totalWidth, totalHeight);
}
const svgBlob = new Blob([svgContent], {type: 'image/svg+xml;charset=utf-8'});
const URL_ = window.URL || window.webkitURL || window;
const svgUrl = URL_.createObjectURL(svgBlob);
const img = new Image();
img.onload = function() {
// Draw the image with padding
ctx.drawImage(img, padding, padding, contentWidth, contentHeight);
URL_.revokeObjectURL(svgUrl);
// Convert to selected format
const imageDataUrl = canvas.toDataURL(format, 0.9);
// Calculate file size
const fileSizeInBytes = Math.round((imageDataUrl.length * 3) / 4);
const fileSizeInKB = (fileSizeInBytes / 1024).toFixed(2);
// Display converted image
imageContainer.innerHTML = ''; // Clear previous image
const convertedImg = document.createElement('img');
convertedImg.src = imageDataUrl;
convertedImg.id = 'convertedImage';
convertedImg.onclick = toggleImageSize;
imageContainer.appendChild(convertedImg);
// Set up download link and display file size
downloadLink.href = imageDataUrl;
downloadLink.download = `converted_image.${format === 'image/jpeg' ? 'jpg' : 'png'}`;
downloadLink.style.display = 'inline-block';
fileSizeSpan.textContent = `(${fileSizeInKB} KB)`;
// Display base64 image tag
const imgTag = `<img src="${imageDataUrl}" alt="Converted ${format === 'image/jpeg' ? 'JPEG' : 'PNG'}" width="${totalWidth}" height="${totalHeight}">`;
base64Output.textContent = imgTag;
outputDiv.style.display = 'block';
// ✅ Update URL hash with current textarea contents AFTER a successful render
// (but not when loading from ?url= parameter - the URL already references the source)
const urlParams = new URLSearchParams(window.location.search);
if (!urlParams.get('url')) {
setHashFromText(svgInput.value);
}
};
img.src = svgUrl;
}
function toggleImageSize() {
const img = document.getElementById('convertedImage');
if (img) img.classList.toggle('full-size');
}
function copyBase64Tag() {
const base64Output = document.getElementById('base64Output');
const range = document.createRange();
range.selectNode(base64Output);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
const origText = copyImageButton.innerText;
copyImageButton.innerText = 'Copied to clipboard';
setTimeout(() => {
copyImageButton.innerText = origText;
}, 2000);
}
// On first load, check for ?url= parameter first, then fall back to hash
window.addEventListener('DOMContentLoaded', () => {
if (!loadFromUrlParamIfPresent()) {
loadFromHashIfPresent();
}
});
</script>
</body>
</html>