-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
127 lines (110 loc) · 3.25 KB
/
index.js
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
let imgCan;
let imgCtx;
let imgData;
let uploader;
let svgString;
let filename;
function makeSVG() {
const svg = document.getElementById("svgImage");
// Empty the SVG
svg.innerHTML = "";
svg.setAttribute("viewBox", `0 0 ${imgCan.width} ${imgCan.height}`);
let color = [];
const colors = [];
for (var i = 0; i < imgData.data.length; i++) {
const channel = i % 4;
var x = (i - channel) % imgCan.width;
var y = ((i - channel) - x) / imgCan.width;
if (channel == 3) {
// Remember the previous color
color.push(imgData.data[i]);
colors.push(color);
// Flush the previous color
color = [];
} else {
color.push(imgData.data[i]);
}
// console.log("C = " + channel);
// console.log("X = " + x);
// console.log("Y = " + y);
}
for (var i = 0; i < colors.length; i++) {
var x = i % imgCan.width;
var y = (i - x) / imgCan.width;
if (colors[i][3] !== 0) {
const rect = document.createElement("rect");
rect.setAttribute("x", x);
rect.setAttribute("y", y);
rect.setAttribute("width", 1);
rect.setAttribute("height", 1);
rect.setAttribute("fill",
`rgb(${colors[i][0]},${colors[i][1]},${colors[i][2]})`);
if (colors[i][3] !== 255) {
rect.setAttribute("fill-opacity", colors[i][3] / 255);
}
svg.appendChild(rect);
}
}
// Refresh SVG
svg.innerHTML = svg.innerHTML;
svgString = `<svg viewBox="${svg.getAttribute("viewBox")}">${svg.innerHTML}</svg>`;
// Hide the upload overlay
toggleOverlay();
// Show the download button
document.getElementById("downloadButton").style.display = "block";
}
function handleImages(e) {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
// Get the image into imageData
imgCan.width = img.width;
imgCan.height = img.height;
imgCtx.drawImage(img, 0, 0);
imgData = imgCtx.getImageData(0, 0, imgCan.width, imgCan.height);
makeSVG();
};
img.src = e.target.result;
};
reader.readAsDataURL(e.target.files[0]);
filename = e.target.files[0].name;
}
function setup() {
uploader = document.getElementById("uploader");
document.getElementById("uploader").onchange = handleImages;
imgCan = document.getElementById("testCanvas");
imgCtx = imgCan.getContext("2d");
}
window.addEventListener("load", setup);
function download() {
type = "image/svg+xml";
filename = `${filename.replace(/\.[^/.]+$/, "")}.svg`;
data = `<?xml version="1.0" encoding="UTF-8" standalone="no"?> ${svgString}`;
const file = new Blob([data], {
type
});
if (window.navigator.msSaveOrOpenBlob) { // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
} else { // Others
const a = document.createElement("a");
const url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
function toggleOverlay() {
if (document.getElementById("uploadWrapper").style.opacity == "0") {
document.getElementById("uploadWrapper").style.opacity = "1";
document.getElementById("uploadWrapper").style.visibility = "visible";
} else {
document.getElementById("uploadWrapper").style.opacity = "0";
document.getElementById("uploadWrapper").style.visibility = "hidden";
}
}