-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
357 lines (337 loc) · 11.1 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image to pure CSS</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #202327;
color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow-y: hidden;
}
.web-button {
position: absolute;
top: 10px;
right: 11px;
width: 44px;
height: 40px;
border: none;
border-radius: 11px;
background-color: #3b8ea5;
color: #f0f0f0;
cursor: pointer;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
transition: background-color 0.3s ease;
padding: 0;
}
.web-button:hover {
background-color: #41a3be;
}
.git-button {
position: absolute;
top: 10px;
left: 10px;
width: 44px;
height: 40px;
border: none;
border-radius: 11px;
background-color: #3b8ea5;
color: #f0f0f0;
cursor: pointer;
font-size: 26px;
display: flex;
justify-content: center;
align-items: center;
transition: background-color 0.3s ease;
padding: 0;
}
.git-button:hover {
background-color: #41a3be;
}
.container {
background-color: #2d3339;
padding: 20px;
border-radius: 15px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
width: 770px;
text-align: center;
position: relative;
border: 5.5px solid #4f5964;
}
.control-group {
margin-bottom: 10px;
text-align: left;
display: flex;
justify-content: center;
align-items: center;
}
#imageInput {
width: 97.5%;
padding: 10px;
box-sizing: border-box;
border: 3px solid #4f5963;
border-radius: 10px;
font-size: 16px;
background-color: #394047;
color: #f0f0f0;
margin-bottom: 12.5px;
}
#generateButton {
padding: 12px 24px;
margin-left: 0px;
border: none;
border-radius: 11px;
background-color: #3b8ea5;
color: #f0f0f0;
cursor: pointer;
font-size: 16px;
font-weight: 500;
transition: background-color 0.3s ease;
margin-bottom: 5px;
}
#toggleViewButton {
padding: 12px 24px;
margin-left: 10px;
border: none;
border-radius: 11px;
background-color: #3b8ea5;
color: #f0f0f0;
cursor: pointer;
font-size: 16px;
font-weight: 500;
transition: background-color 0.3s ease;
margin-bottom: 5px;
}
#generateButton:hover,
#toggleViewButton:hover {
background-color: #41a3be;
}
#resolutionControls {
display: flex;
align-items: center;
justify-content: center;
margin-top: -5px;
}
#resolutionControls label {
margin-right: 5px;
color: #f0f0f0;
font-size: 16px;
}
#resolutionRange {
width: 200px;
margin-left: 5px;
-webkit-appearance: none;
appearance: none;
height: 10px;
background: #4f5963;
border-radius: 5px;
outline: none;
}
#resolutionRange::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
background: #21d498;
border-radius: 50%;
cursor: pointer;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.6);
transition: background-color 0.3s ease;
}
#resolutionRange::-webkit-slider-thumb:hover {
background: #1fd699;
}
#outputCanvas {
display: block;
margin: 20px auto;
border: 2px solid #ccc;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 100%;
image-rendering: pixelated;
display: none;
}
#outputCode {
margin-top: 20px;
padding: 10px;
background-color: #394047;
border: 3px solid #4f5963;
border-radius: 10px;
font-family: Consolas, monospace;
white-space: pre-wrap;
overflow-x: auto;
max-height: 300px;
color: #f0f0f0;
display: none;
position: relative;
}
#copyButton {
position: absolute;
top: 267px;
right: 32.5px;
padding: 7px 13px;
border: none;
border-radius: 10px;
background-color: #0dbf84;
color: #f0f0f0;
cursor: pointer;
font-size: 15px;
font-weight: 500;
transition: background-color 0.3s ease;
display: none;
z-index: 500000000000000000000;
}
#copyButton:hover {
background-color: #21d498;
}
.error-message {
color: #dc3545;
font-size: 14px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Image to pure CSS converter :D</h1><h3>𝙼𝚊𝚍𝚎 𝚋𝚢 𝚍𝚒𝚗𝚐𝚞𝚜𝚌𝚑𝚊𝚗!</h3> <button class="git-button" onclick="openGit()">
<i class="fa-brands fa-github"></i>
</button>
<button class="web-button" onclick="openWeb()">
<i class="fa-solid fa-globe"></i></button>
<input type="file" id="imageInput" accept="image/*">
<div class="control-group">
<button id="generateButton">Show Preview</button>
<button id="toggleViewButton">Show Code</button>
</div>
<div class="control-group" id="resolutionControls">
<label for="resolutionRange">Resolution:</label>
<input type="range" id="resolutionRange" min="1" max="5" value="2">
<span id="resolutionLabel"> Medium</span>
</div>
<canvas id="outputCanvas"></canvas>
<div id="outputCode"></div>
<button id="copyButton">
<i class="fa-solid fa-copy"></i>
</button>
<div id="errorMessage" class="error-message"></div>
</div>
<script>
document.getElementById('generateButton').addEventListener('click', function() {
var file = document.getElementById('imageInput').files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
var img = new Image();
img.onload = function() {
var canvas = document.getElementById('outputCanvas');
var ctx = canvas.getContext('2d');
var scale = parseFloat(document.getElementById('resolutionRange').value);
canvas.width = img.width * scale;
canvas.height = img.height * scale;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
var pixelSize = 4;
for (var y = 0; y < canvas.height; y += pixelSize) {
for (var x = 0; x < canvas.width; x += pixelSize) {
var pixelData = ctx.getImageData(x, y, 1, 1).data;
ctx.fillStyle = `rgb(${pixelData[0]}, ${pixelData[1]}, ${pixelData[2]})`;
ctx.fillRect(x, y, pixelSize, pixelSize);
}
}
var htmlCode = generateHTMLCode(canvas);
document.getElementById('outputCode').textContent = htmlCode;
document.getElementById('outputCanvas').style.display = 'block';
document.getElementById('outputCode').style.display = 'none';
document.getElementById('copyButton').style.display = 'none';
document.getElementById('toggleViewButton').textContent = 'Show Code';
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
function generateHTMLCode(canvas) {
var htmlCode = ' < div style = "display: inline-block; border: 1px solid #ccc; overflow: hidden; font-size: 0;" > \n ';
var ctx = canvas.getContext('2d');
var pixelSize = 4;
for (var y = 0; y < canvas.height; y += pixelSize) {
htmlCode += '\t < div style = "white-space: nowrap; font-size: 0;" > \n ';
for (var x = 0; x < canvas.width; x += pixelSize) {
var pixelData = ctx.getImageData(x, y, 1, 1).data;
var color = `rgb(${pixelData[0]}, ${pixelData[1]}, ${pixelData[2]})`;
htmlCode += `\t\t
<div style="width: ${pixelSize}px; height: ${pixelSize}px; background-color: ${color}; display: inline-block; margin: 0; padding: 0;"></div>\n`;
}
htmlCode += '\t < /div>\n';
}
htmlCode += ' < /div>';
return htmlCode;
}
document.getElementById('toggleViewButton').addEventListener('click', function() {
var canvas = document.getElementById('outputCanvas');
var outputCode = document.getElementById('outputCode');
var copyButton = document.getElementById('copyButton');
if (canvas.style.display === 'none') {
canvas.style.display = 'block';
outputCode.style.display = 'none';
copyButton.style.display = 'none';
this.textContent = 'Show Code';
} else {
canvas.style.display = 'none';
outputCode.style.display = 'block';
copyButton.style.display = 'block';
this.textContent = 'Show Image';
}
});
document.getElementById('resolutionRange').addEventListener('input', function() {
var resolutionLabel = document.getElementById('resolutionLabel');
var value = parseFloat(this.value);
switch (value) {
case 0.5:
resolutionLabel.textContent = 'Very Low (0.5x)';
break;
case 1:
resolutionLabel.textContent = 'Low (1x)';
break;
case 2:
resolutionLabel.textContent = 'Medium (2x)';
break;
case 3:
resolutionLabel.textContent = 'High (3x)';
break;
case 4:
resolutionLabel.textContent = 'Very High (4x)';
break;
case 5:
resolutionLabel.textContent = 'Ultra (5x)';
break;
default:
break;
}
});
document.getElementById('copyButton').addEventListener('click', function() {
var outputCode = document.getElementById('outputCode');
var range = document.createRange();
range.selectNode(outputCode);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
var copyButton = this;
copyButton.innerHTML = ' < i class = "fa-solid fa-check" > < /i> Copied!';
setTimeout(function() {
copyButton.innerHTML = ' < i class = "fa-solid fa-copy" > < /i>';
}, 1000);
});
</script>
</body>
</html>