-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_editor.html
More file actions
343 lines (281 loc) · 11.3 KB
/
img_editor.html
File metadata and controls
343 lines (281 loc) · 11.3 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css" media="screen">
*{
margin: 0;
box-sizing: border-box;
}
html, body{
height: 100%;
}
body{
background-color: rgb(255, 255, 255);
display: flex;
align-items: center;
flex-direction: column;
}
.navbar{
top: 0px;
padding: 4px;
margin-bottom: 1em;
align-items: center;
background-color: rgb(246, 246, 246);
border-radius: 3px;}
}
.btn, input{
padding: 0 1em;
}
input.sizeh {
width: 102px;
margin: 1px;
}
input.sizew {
width: 74px;
margin: 2px;
}
.container{
--sizeh: 4;
--sizew: 4;
width: auto;
display: grid;
grid-template-columns: repeat(var(--sizeh), 1fr);
grid-template-rows: repeat(var(--sizew), 1fr);
gap: 2px;
padding: 2px;
background-color: rgb(50, 50, 50);
border-radius: 3px;
}
.pixel{
background-color: rgb(0, 0, 0);
border-radius: 1px;
height: 12px;
width: 12px;
user-drag: none;
user-select: none;
}
</style>
<title>Editor obrázků</title>
</head>
<body>
<div class="navbar">
<label for="sizeh">šířka:</label>
<input type="number" name="sizeh" value="8" class="sizeh" maxlength="3" size="3">
<br>
<label for="sizeh">výška 8*:</label>
<input type="number" name="sizew" value="1" class="sizew" maxlength="3" size="3">
<br>
<button class="btn" style="width: 65px; margin:4px;">Reset</button>
<input type="button" value="Export" style="width: 65px; margin:4px;" onclick="export_img();">
<br>
<input type="button" value="Bílá" style="width: 65px;height:40px; margin:4px; background-color: #FFFFFF; color: black;" onclick="color = '#FFFFFF';">
<input type="button" value="Černá" style="width: 65px;height:40px; margin:4px; background-color: #000000; color: white;" onclick="color = '#000000';">
<br>
<input type="button" value="Načíst obrázek z PC" onclick="code_upload_pc();" style="width: 142px; margin:4px;">
</div>
<div class="container">
</div>
<br>
<input type="button" value="Kopírovat data do schránky" onclick="export_img();">
<p id="demo"></p>
<canvas id="canv" width="1024" height="1024" style="border:1px solid black;display: none;"></canvas>
<input id="upload_file_input" type="file" style="position: absolute; top: 99%; left: 99%; width: 0px; height: 0px; padding: 0px"/>
<img id="outputpng" class="" src="" style="display: none"/>
<script>
const container = document.querySelector('.container')
const sizeElh = document.querySelector('.sizeh')
let sizeh = sizeElh.value
const sizeElw = document.querySelector('.sizew')
let sizew = sizeElw.value*8
var color = '#FFFFFF'
const resetBtn = document.querySelector('.btn')
let draw = false
// return a promise
function copyToClipboard(textToCopy) {
// navigator clipboard api needs a secure context (https)
if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard api method'
return navigator.clipboard.writeText(textToCopy);
} else {
// text area method
let textArea = document.createElement("textarea");
textArea.value = textToCopy;
// make the textarea out of viewport
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((res, rej) => {
// here the magic happens
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
}
function populate(sizeh,sizew) {
container.style.setProperty('--sizeh', sizeh)
container.style.setProperty('--sizew', sizew)
for (let i = 0; i < sizew; i++) {
for (let ii = 0; ii < sizeh; ii++) {
const div = document.createElement('div')
div.setAttribute("id", "x" + ii + "y" + i);
div.classList.add('pixel')
div.addEventListener('mouseover', function(){
if(!draw) return
div.style.backgroundColor = color
})
div.addEventListener('touchmove', function(event){
var touch = event.touches[0];
var elm = document.elementFromPoint(touch.clientX, touch.clientY);
if (elm.className == "pixel")
{
elm.style.backgroundColor = color
}
})
div.addEventListener('mousedown', function(){
div.style.backgroundColor = color
})
container.appendChild(div)
}
}
}
function export_img()
{
var out_byte = 0;
var out_data = [];
var count = 0;
var str_out = "bytearray(b'";
for (let y = 0; y < sizew/8; y++) {
for (let x = 0; x < sizeh; x++) {
out_byte = 0;
for (let i = 0; i < 8; i++) {
if (document.getElementById("x"+x+"y"+(y*8+i)).style.backgroundColor == 'rgb(255, 255, 255)')
{
out_byte = out_byte + Math.pow(2, i);
}
//console.log(document.getElementById("x"+x+"y"+(y*8+i)).style.backgroundColor);
}
if (out_byte < 16)
{
str_out = str_out + String.fromCharCode(92, 120, 48) + out_byte.toString(16);
}
else
{
str_out = str_out + String.fromCharCode(92, 120) + out_byte.toString(16);
}
//out_data[count] = out_byte;
out_data[count] = String.fromCharCode(92, 120) + out_byte.toString(16);
//console.log(out_data[count]);
count++;
}
}
//document.getElementById("demo").innerHTML = str_out + "'), " + sizeh + ", " + sizew;
//navigator.clipboard.writeText(str_out + "'), " + sizeh + ", " + sizew);
copyToClipboard(str_out + "'), " + sizeh + ", " + sizew)
document.getElementById("demo").innerHTML = "Data uložena do schránky";
}
function import_img()
{
code_upload_pc();
var canvas = document.getElementById('canv');
// Create an image object
var image = document.getElementById('outputpng');
// When the image has finished loading, draw it to the canvas
image.onload = function() {
canvas.getContext('2d').drawImage(image, 0, 0);
console.log(image.naturalHeight);
console.log(image.naturalWidth);
sizew = image.naturalHeight;
sizeh = Math.ceil(image.naturalWidth/8)*8;
sizeElh.value = image.naturalWidth;
sizeElw.value = Math.ceil(image.naturalHeight/8);
reset()
for (let x = 0; x < image.naturalWidth; x++) {
for (let y = 0; y < image.naturalHeight; y++) {
var imageData = canvas.getContext('2d').getImageData(x, y, 1, 1);
// Get the red, green, blue, and alpha values of the pixel
var r = imageData.data[0];
var g = imageData.data[1];
var b = imageData.data[2];
var a = imageData.data[3];
var brightness = r+g+b / 3;
// Print the pixel color
//console.log('Pixel color: (' + r + ', ' + g + ', ' + b + ', ' + a + ')');
if (brightness < 128)
{
document.getElementById("x"+x+"y"+y).style.backgroundColor = 'rgb(255,255,255)';
}
}
}
}
}
window.addEventListener("mousedown", function(){
draw = true
})
window.addEventListener("mouseup", function(){
draw = false
})
window.addEventListener('touchstart', function(){
draw = true
})
window.addEventListener('touchend', function(){
draw = true
})
function reset(){
container.innerHTML = '';
document.getElementById("demo").innerHTML = '';
populate(sizeh,sizew)
}
resetBtn.addEventListener('click', reset)
sizeElh.addEventListener('keyup', function(){
sizeh = sizeElh.value
sizew = sizeElw.value*8
reset()
})
sizeElw.addEventListener('keyup', function(){
sizeh = sizeElh.value
sizew = sizeElw.value*8
reset()
})
sizeElh.addEventListener('change', function(){
sizeh = sizeElh.value
sizew = sizeElw.value*8
reset()
})
sizeElw.addEventListener('change', function(){
sizeh = sizeElh.value
sizew = sizeElw.value*8
reset()
})
function code_upload_pc() {
document.getElementById('upload_file_input').addEventListener('change', readSingleFile, false);
document.getElementById('upload_file_input').click();
}
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
try {
console.log(contents);
var output = document.getElementById('outputpng');
output.src = contents;
import_img();
} catch (e) {
alert(e);
}
};
reader.readAsDataURL(file);
}
populate(sizeh,sizew)
</script>
</body>
</html>