-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
svg-progressive-render.html
325 lines (278 loc) · 9.96 KB
/
svg-progressive-render.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
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 1em;
background: #f5f5f5;
}
.input-group {
display: flex;
gap: 10px;
margin-bottom: 20px;
align-items: center;
}
.input-group textarea {
flex-grow: 1;
height: 2.8em;
padding: 8px 12px;
font-family: monospace;
border: 2px solid #ccc;
border-radius: 4px;
resize: none;
}
.duration-input {
width: 60px;
height: 100%;
padding: 8px;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
.duration-label {
font-size: 14px;
color: #666;
white-space: nowrap;
}
button {
padding: 0 20px;
background: #0066cc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-weight: 600;
height: 100%;
}
button:hover {
background: #0052a3;
}
button:disabled {
background: #cccccc;
cursor: not-allowed;
}
.editor textarea {
width: 100%;
height: 150px;
padding: 12px;
margin-bottom: 20px;
font-family: monospace;
border: 2px solid #ccc;
border-radius: 4px;
resize: vertical;
}
#output {
background: white;
border: 2px solid #ddd;
border-radius: 4px;
padding: 20px;
min-height: 300px;
display: flex;
align-items: center;
justify-content: center;
}
#output svg {
max-width: 100%;
max-height: 260px;
}
.error {
color: #cc0000;
margin-top: 10px;
font-size: 14px;
min-height: 20px;
}
.label {
font-weight: 600;
margin-bottom: 8px;
color: #333;
}
.progress {
height: 4px;
background: #eee;
margin-bottom: 20px;
border-radius: 2px;
overflow: hidden;
}
.progress-bar {
height: 100%;
background: #0066cc;
width: 0%;
transition: width 0.1s ease-out;
}
#loadExampleLink {
color: #0066cc;
text-decoration: none;
margin-right: 20px;
}
#loadExampleLink:hover {
text-decoration: underline;
}
.top-controls {
display: flex;
align-items: center;
margin-bottom: 8px;
}
</style>
<title>Progressively render SVG</title>
</head>
<body>
<h1>Progressively render SVG</h1>
<div class="top-controls">
<a href="https://gist.githubusercontent.com/simonw/aedecb93564af13ac1596810d40cac3c/raw/83e7f3be5b65bba61124684700fa7925d37c36c3/tiger.svg" id="loadExampleLink">Load example image</a>
</div>
<div class="input-group">
<textarea id="fullSvg" placeholder="Paste SVG here"></textarea>
<input type="number" id="duration" class="duration-input" value="5" min="0.1" step="0.1">
<span class="duration-label">seconds</span>
<button id="renderBtn">Render</button>
</div>
<div class="progress">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="label">Live editor:</div>
<div class="editor">
<textarea id="input" spellcheck="false"><svg><rect x="10" y="10" width="80" height="80" fill="blue"></rect><circle cx="120" cy="50" r="30" fill="red"</textarea>
</div>
<div class="label">Live Preview:</div>
<div id="output"></div>
<div id="error" class="error"></div>
<script>
function completeSVG(incompleteSVG) {
if (!incompleteSVG || !incompleteSVG.trim()) {
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"></svg>';
}
const openTags = [];
const tagRegex = /<\/?([a-zA-Z0-9]+)(\s+[^>]*)?>/g;
let match;
let currentPos = 0;
let processedSVG = '';
while ((match = tagRegex.exec(incompleteSVG)) !== null) {
const fullTag = match[0];
const tagName = match[1];
const isClosingTag = fullTag.startsWith('</');
processedSVG += incompleteSVG.slice(currentPos, match.index);
processedSVG += fullTag;
currentPos = tagRegex.lastIndex;
if (!isClosingTag && !fullTag.endsWith('/>')) {
openTags.push(tagName);
} else if (isClosingTag) {
if (openTags.length > 0 && openTags[openTags.length - 1] === tagName) {
openTags.pop();
}
}
}
processedSVG += incompleteSVG.slice(currentPos);
if (!incompleteSVG.includes('<svg')) {
processedSVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100">' + processedSVG;
openTags.unshift('svg');
}
while (openTags.length > 0) {
const tagName = openTags.pop();
processedSVG += `</${tagName}>`;
}
return processedSVG;
}
const input = document.getElementById('input');
const fullSvg = document.getElementById('fullSvg');
const duration = document.getElementById('duration');
const renderBtn = document.getElementById('renderBtn');
const output = document.getElementById('output');
const error = document.getElementById('error');
const progressBar = document.getElementById('progressBar');
const loadExampleLink = document.getElementById('loadExampleLink');
// Prevent editor updates during manual editing
let isManualEdit = false;
input.addEventListener('input', () => {
if (!isManualEdit) {
updateSVG();
}
});
function updateSVG() {
try {
const completed = completeSVG(input.value);
output.innerHTML = completed;
error.textContent = '';
} catch (e) {
error.textContent = 'Error: ' + e.message;
}
}
function animateSVG(svgString) {
const durationMs = Math.max(100, duration.value * 1000); // Minimum 0.1 seconds
const interval = 100; // 100ms steps
const steps = durationMs / interval;
let currentStep = 0;
// Disable the render button during animation
renderBtn.disabled = true;
// Clear any existing animation
if (window.currentAnimation) {
clearInterval(window.currentAnimation);
}
window.currentAnimation = setInterval(() => {
currentStep++;
const progress = currentStep / steps;
const charCount = Math.floor(svgString.length * progress);
// Update progress bar
progressBar.style.width = `${progress * 100}%`;
// Get substring and complete it
const partial = svgString.substring(0, charCount);
const completed = completeSVG(partial);
// Update both the preview and editor
output.innerHTML = completed;
isManualEdit = true; // Prevent recursive updates
input.value = partial;
// Scroll editor to bottom
input.scrollTop = input.scrollHeight;
isManualEdit = false;
// End animation when complete
if (currentStep >= steps) {
clearInterval(window.currentAnimation);
renderBtn.disabled = false;
progressBar.style.width = '0%';
}
}, interval);
}
// Handle paste and input events on the fullSvg textarea
fullSvg.addEventListener('input', () => {
// Clear the editor and preview
input.value = '';
output.innerHTML = '';
error.textContent = '';
});
renderBtn.addEventListener('click', () => {
if (fullSvg.value.trim()) {
animateSVG(fullSvg.value);
}
});
// Load example image
loadExampleLink.addEventListener('click', async (e) => {
e.preventDefault();
try {
const response = await fetch(loadExampleLink.href);
if (!response.ok) throw new Error('Network response was not ok');
const svgText = await response.text();
fullSvg.value = svgText;
error.textContent = '';
// Clear the editor and preview
input.value = '';
output.innerHTML = '';
} catch (error) {
console.error('Error loading example:', error);
error.textContent = 'Error loading example: ' + error.message;
}
});
// Initial render
updateSVG();
// Prevent negative durations
duration.addEventListener('input', () => {
if (duration.value < 0.1) duration.value = 0.1;
});
</script>
</body>
</html>