-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-render.html
168 lines (153 loc) · 5.82 KB
/
svg-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
<!doctype html>
<html lang=en>
<meta charset=utf-8>
<title>SVG Render</title>
<a style="float: right" href="https://github.com/vbezhenar/svg-render">https://github.com/vbezhenar/svg-render</a>
<label>
Select SVG file:
<input id=file type=file accept=.svg>
</label>
<hr/>
<div id=results style="display: none">
<div>
Size:
<select id=width>
<option value=auto selected>Auto</option>
<option>16</option>
<option>24</option>
<option>32</option>
<option>48</option>
<option>64</option>
<option>72</option>
<option>96</option>
<option>128</option>
<option>192</option>
<option>256</option>
<option>512</option>
<option>1024</option>
<option value=custom>Custom</option>
</select>
<input id=width-custom type=number min=1 max=16318 value=100>
×
<select id=height>
<option value=auto selected>Auto</option>
<option>16</option>
<option>24</option>
<option>32</option>
<option>48</option>
<option>64</option>
<option>72</option>
<option>96</option>
<option>128</option>
<option>192</option>
<option>256</option>
<option>512</option>
<option>1024</option>
<option value=custom>Custom</option>
</select>
<input id=height-custom type=number min=1 max=16318 value=100>
<a id=download href=data:,>Download</a>
</div>
<hr/>
<canvas id=canvas width=0 height=0></canvas>
</div>
<script>
const fileElement = document.getElementById("file");
const resultsElement = document.getElementById("results");
const widthElement = document.getElementById("width");
const widthCustomElement = document.getElementById("width-custom");
const heightElement = document.getElementById("height");
const heightCustomElement = document.getElementById("height-custom");
const downloadElement = document.getElementById("download");
const canvasElement = document.getElementById("canvas");
let width;
let height;
let baseFileName = null;
let svgImage = null;
fileElement.onchange = fileChange;
widthElement.onchange = updateDimensions;
heightElement.onchange = updateDimensions;
widthCustomElement.onchange = updateDimensions;
heightCustomElement.onchange = updateDimensions;
updateDimensions();
function fileChange(event) {
svgImage = null;
resultsElement.style.display = "none";
canvasElement.width = canvasElement.height = 0;
const fileElement = event.target;
if (fileElement.files.length > 0) {
fileElement.disabled = true;
const file = fileElement.files[0];
const fileName = file.name;
const dotIndex = fileName.lastIndexOf(".");
baseFileName = dotIndex >= 0 ? fileName.substring(0, dotIndex) : fileName;
const fileReader = new FileReader();
fileReader.onload = fileReaderLoaded;
fileReader.readAsDataURL(file);
}
}
function fileReaderLoaded(event) {
const fileReader = event.target;
fileElement.disabled = false;
svgImage = new Image();
svgImage.onload = svgImageLoaded;
svgImage.src = fileReader.result;
}
function svgImageLoaded(event) {
if (event.target !== svgImage) {
return;
}
updateCanvas();
resultsElement.style.display = "";
}
function updateDimensions() {
width = widthElement.value;
if (width === "custom") {
widthCustomElement.style.display = "";
if (!(widthCustomElement.min <= widthCustomElement.value
&& widthCustomElement.value <= widthCustomElement.max)) {
widthCustomElement.value = widthCustomElement.getAttribute("value");
}
width = widthCustomElement.value;
} else {
widthCustomElement.style.display = "none";
}
height = heightElement.value;
if (height === "custom") {
heightCustomElement.style.display = "";
if (!(heightCustomElement.min <= heightCustomElement.value
&& heightCustomElement.value <= heightCustomElement.max)) {
heightCustomElement.value = heightCustomElement.getAttribute("value");
}
height = heightCustomElement.value;
} else {
heightCustomElement.style.display = "none";
}
if (svgImage != null) {
updateCanvas();
}
}
function updateCanvas() {
let canvasWidth = width;
let canvasHeight = height;
if (canvasWidth === "auto" && canvasHeight === "auto") {
canvasWidth = svgImage.width;
canvasHeight = svgImage.height;
} else if (canvasWidth === "auto") {
// canvasWidth / canvasHeight = width / height
canvasWidth = Math.ceil(svgImage.width * canvasHeight / svgImage.height);
} else if (canvasHeight === "auto") {
canvasHeight = Math.ceil(svgImage.height * canvasWidth / svgImage.width);
}
canvasElement.width = canvasWidth;
canvasElement.height = canvasHeight;
const context = canvas.getContext("2d");
context.drawImage(svgImage, 0, 0, canvasWidth, canvasHeight);
const fileNameSuffix = canvas.width === canvas.height ? `_${canvas.width}` : `_${canvas.width}x${canvas.height}`;
const fileName = `${baseFileName}${fileNameSuffix}.png`;
downloadElement.innerText = `Download ${fileName}`;
downloadElement.href = canvasElement.toDataURL("image/png");
downloadElement.download = fileName;
}
</script>
</html>