Skip to content

Commit 4ff37f2

Browse files
authored
1 parent d18d292 commit 4ff37f2

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

image-to-svg.html

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Image to SVG</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
max-width: 800px;
11+
margin: 0 auto;
12+
padding: 20px;
13+
}
14+
#drop-zone {
15+
border: 2px dashed #ccc;
16+
border-radius: 20px;
17+
width: 100%;
18+
height: 200px;
19+
display: flex;
20+
justify-content: center;
21+
align-items: center;
22+
cursor: pointer;
23+
}
24+
#drop-zone.dragover {
25+
background-color: #e9e9e9;
26+
}
27+
#svg-container, #svg-code {
28+
margin-top: 20px;
29+
}
30+
#svg-container {
31+
max-width: 100%;
32+
box-sizing: border-box;
33+
margin: 20px;
34+
}
35+
#svg-container svg {
36+
max-width: 100%;
37+
height: auto;
38+
}
39+
#svg-code {
40+
width: 100%;
41+
height: 200px;
42+
}
43+
#copy-button {
44+
margin-bottom: 10px;
45+
padding: 5px 10px;
46+
cursor: pointer;
47+
}
48+
</style>
49+
</head>
50+
<body>
51+
<h1>Image to SVG</h1>
52+
<p>Using <a href="https://github.com/jankovicsandras/imagetracerjs">imagetracerjs</a> by András Jankovics.</p>
53+
<div id="drop-zone">
54+
<p>Drag and drop a JPG or PNG image here, or click to select a file</p>
55+
</div>
56+
<input type="file" id="file-input" style="display: none;" accept="image/jpeg, image/png">
57+
<div id="svg-container"></div>
58+
<h2>SVG Code:</h2>
59+
<button id="copy-button">Copy to clipboard</button>
60+
<textarea id="svg-code" readonly></textarea>
61+
62+
<script type="module">
63+
import imagetracerjs from 'https://cdn.jsdelivr.net/npm/imagetracerjs@1.2.6/+esm';
64+
65+
const dropZone = document.getElementById('drop-zone');
66+
const fileInput = document.getElementById('file-input');
67+
const svgContainer = document.getElementById('svg-container');
68+
const svgCode = document.getElementById('svg-code');
69+
const copyButton = document.getElementById('copy-button');
70+
71+
dropZone.addEventListener('click', () => fileInput.click());
72+
dropZone.addEventListener('dragover', (e) => {
73+
e.preventDefault();
74+
dropZone.classList.add('dragover');
75+
});
76+
dropZone.addEventListener('dragleave', () => {
77+
dropZone.classList.remove('dragover');
78+
});
79+
dropZone.addEventListener('drop', handleDrop);
80+
fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
81+
82+
copyButton.addEventListener('click', () => {
83+
navigator.clipboard.writeText(svgCode.value).then(() => {
84+
copyButton.textContent = 'Copied';
85+
setTimeout(() => {
86+
copyButton.textContent = 'Copy to clipboard';
87+
}, 1500);
88+
}).catch(err => {
89+
console.error('Failed to copy text: ', err);
90+
});
91+
});
92+
93+
function handleDrop(e) {
94+
e.preventDefault();
95+
dropZone.classList.remove('dragover');
96+
const file = e.dataTransfer.files[0];
97+
if (file && (file.type === 'image/jpeg' || file.type === 'image/png')) {
98+
handleFile(file);
99+
}
100+
}
101+
102+
function handleFile(file) {
103+
const reader = new FileReader();
104+
reader.onload = (e) => {
105+
const img = new Image();
106+
img.onload = () => convertToSVG(img);
107+
img.src = e.target.result;
108+
};
109+
reader.readAsDataURL(file);
110+
}
111+
112+
function convertToSVG(img) {
113+
const canvas = document.createElement('canvas');
114+
canvas.width = img.width;
115+
canvas.height = img.height;
116+
const ctx = canvas.getContext('2d');
117+
ctx.drawImage(img, 0, 0);
118+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
119+
120+
const svgString = imagetracerjs.imagedataToSVG(imageData, {viewbox: true});
121+
122+
svgContainer.innerHTML = svgString;
123+
svgCode.value = svgString;
124+
}
125+
</script>
126+
</body>
127+
</html>

0 commit comments

Comments
 (0)