-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpixel.html
More file actions
428 lines (360 loc) · 22.7 KB
/
wpixel.html
File metadata and controls
428 lines (360 loc) · 22.7 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>wpixel</title>
</head>
<body>
<div class="ui">
<h1>wpixel</h1>
<h2>pixel art generator for the wplace</h2>
<h3>built with ❤️ by <a href="https://xeaf.dev/">0xeaf</a> with <a href="https://github.com/giventofly/pixelit">PixelIt</a></h3>
<span>note: if you want to see how its built (because turns out pixelit is kind of crap and i had to monkey patch half of it) there are comments in this html file :)<br><br>also my css is crap dont ask</span>
<br><br>
<label>Image:</label>
<input type="file" id="image"><br>
<label>Blocksize:</label>
<input type="number" id="blocksize" value="10" placeholder="i wouldn't put something too large here"><br>
<label>Pixelsize:</label> <!-- only available for my custom implementation of the pixelize function -->
<input type="number" id="pixelsize" value="10" placeholder="wouldn't recommend bigger than 20"><br>
<label>Greyscale <i>(slower, but shouldn't make it crash more)</i>:</label>
<input type="checkbox" id="greyscale"><br>
<label>Exact <i>(crashes on big block sizes)</i>:</label> <!-- only available for my custom implementations of the pixelize function - actually switches between my two custom implementations -->
<input type="checkbox" id="exact"><br>
<label>Color accruacy <i>(slower, might crash on large blocksizes)</i>:</label> <!-- switches between my custom implementation of similarColor and the original one -->
<input type="checkbox" id="color"><br>
<label>No palette <i>(doesnt limit only to color used on the wplace)</i>:</label>
<input type="checkbox" id="palette"><br>
<!-- the following doesnt work with my custom implementation of the pixelize function, its useless to even try -->
<!---<label>Max height:</label>
<input type="number" id="maxheight"><br>
<label>Max width:</label>
<input type="number" id="maxwidth"><br>-->
<br>
<label>Live <i>(might crash on large blocksizes)</i>:</label>
<input type="checkbox" id="live">
<button id="process">Process</button>
</div>
<canvas id="pixelitcanvas"></canvas>
<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" width="0" height="0" alt="" id="pixelitimg" />
</body>
<script> // https://raw.githubusercontent.com/giventofly/pixelit/refs/heads/master/dist/pixelit.min.js
"use strict";function _classCallCheck(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,i){for(var a=0;a<i.length;a++){var e=i[a];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}function _createClass(t,i,a){return i&&_defineProperties(t.prototype,i),a&&_defineProperties(t,a),Object.defineProperty(t,"prototype",{writable:!1}),t}var pixelit=function(){function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,t),this.drawto=i.to||document.getElementById("pixelitcanvas"),this.drawfrom=i.from||document.getElementById("pixelitimg"),this.hideFromImg(),this.scale=i.scale&&i.scale>0&&i.scale<=50?.01*i.scale:.08,this.palette=i.palette||[[140,143,174],[88,69,99],[62,33,55],[154,99,72],[215,155,125],[245,237,186],[192,199,65],[100,125,52],[228,148,58],[157,48,59],[210,100,113],[112,55,127],[126,196,193],[52,133,157],[23,67,75],[31,14,28]],this.maxHeight=i.maxHeight,this.maxWidth=i.maxWidth,this.ctx=this.drawto.getContext("2d"),this.endColorStats={}}return _createClass(t,[{key:"hideFromImg",value:function(){return this.drawfrom.style.visibility="hidden",this.drawfrom.style.position="fixed",this.drawfrom.style.top=0,this.drawfrom.style.left=0,this}},{key:"setFromImgSource",value:function(t){return this.drawfrom.src=t,this}},{key:"setDrawFrom",value:function(t){return this.drawfrom=t,this}},{key:"setDrawTo",value:function(t){return this.drawto=t,this}},{key:"setPalette",value:function(t){return this.palette=t,this}},{key:"setMaxWidth",value:function(t){return this.maxWidth=t,this}},{key:"setMaxHeight",value:function(t){return this.maxHeight=t,this}},{key:"setScale",value:function(t){return this.scale=t>0&&t<=50?.01*t:.08,this}},{key:"getPalette",value:function(){return this.palette}},{key:"colorSim",value:function(t,i){var a,e,h=0;for(a=0,e=t.length;a<e;a++)h+=(t[a]-i[a])*(t[a]-i[a]);return Math.sqrt(h)}},{key:"similarColor",value:function(t){var i,a=this,e=[],h=this.colorSim(t,this.palette[0]);return this.palette.forEach((function(r){(i=a.colorSim(t,r))<=h&&(e=r,h=i)})),e}},{key:"pixelate",value:function(){this.drawto.width=this.drawfrom.naturalWidth,this.drawto.height=this.drawfrom.naturalHeight;var t=this.drawto.width*this.scale,i=this.drawto.height*this.scale,a=document.createElement("canvas");a.width=this.drawto.width,a.height=this.drawto.height,a.style.visibility="hidden",a.style.position="fixed",a.style.top="0",a.style.left="0",(this.drawto.width>900||this.drawto.height>900)&&(this.scale*=.5,t=this.drawto.width*this.scale,i=this.drawto.height*this.scale,a.width=Math.max(t,i)+50,a.height=Math.max(t,i)+50),a.getContext("2d").drawImage(this.drawfrom,0,0,t,i),document.body.appendChild(a),this.ctx.mozImageSmoothingEnabled=!1,this.ctx.webkitImageSmoothingEnabled=!1,this.ctx.imageSmoothingEnabled=!1;var e=this.drawfrom.naturalWidth;this.drawfrom.naturalWidth>300&&(e+=this.drawfrom.naturalWidth>this.drawfrom.naturalHeight?parseInt(this.drawfrom.naturalWidth/(this.drawfrom.naturalWidth*this.scale))/1.5:parseInt(this.drawfrom.naturalWidth/(this.drawfrom.naturalWidth*this.scale)));var h=this.drawfrom.naturalHeight;return this.drawfrom.naturalHeight>300&&(h+=this.drawfrom.naturalHeight>this.drawfrom.naturalWidth?parseInt(this.drawfrom.naturalHeight/(this.drawfrom.naturalHeight*this.scale))/1.5:parseInt(this.drawfrom.naturalHeight/(this.drawfrom.naturalHeight*this.scale))),this.ctx.drawImage(a,0,0,t,i,0,0,e,h),a.remove(),this}},{key:"convertGrayscale",value:function(){for(var t=this.drawto.width,i=this.drawto.height,a=this.ctx.getImageData(0,0,t,i),e=0;e<a.height;e++)for(var h=0;h<a.width;h++){var r=4*e*a.width+4*h,s=(a.data[r]+a.data[r+1]+a.data[r+2])/3;a.data[r]=s,a.data[r+1]=s,a.data[r+2]=s}return this.ctx.putImageData(a,0,0,0,0,a.width,a.height),this}},{key:"convertPalette",value:function(){for(var t=this.drawto.width,i=this.drawto.height,a=this.ctx.getImageData(0,0,t,i),e=0;e<a.height;e++)for(var h=0;h<a.width;h++){var r=4*e*a.width+4*h,s=this.similarColor([a.data[r],a.data[r+1],a.data[r+2]]);a.data[r]=s[0],a.data[r+1]=s[1],a.data[r+2]=s[2]}return this.ctx.putImageData(a,0,0,0,0,a.width,a.height),this}},{key:"resizeImage",value:function(){var t=document.createElement("canvas"),i=t.getContext("2d"),a=1;return this.maxWidth||this.maxHeight?(this.maxWidth&&this.drawto.width>this.maxWidth&&(a=this.maxWidth/this.drawto.width),this.maxHeight&&this.drawto.height>this.maxHeight&&(a=this.maxHeight/this.drawto.height),t.width=this.drawto.width,t.height=this.drawto.height,i.drawImage(this.drawto,0,0),this.drawto.width=this.drawto.width*a,this.drawto.height=this.drawto.height*a,this.ctx.drawImage(t,0,0,t.width,t.height,0,0,this.drawto.width,this.drawto.height),this):0}},{key:"draw",value:function(){return this.drawto.width=this.drawfrom.width,this.drawto.height=this.drawfrom.height,this.ctx.drawImage(this.drawfrom,0,0),this.resizeImage(),this}},{key:"saveImage",value:function(){var t=document.createElement("a");t.download="pxArt.png",t.href=this.drawto.toDataURL("image/png").replace("image/png","image/octet-stream"),document.querySelector("body").appendChild(t),t.click(),document.querySelector("body").removeChild(t)}}]),t}();
</script>
<script>
let live = false;
let px = new pixelit();
// fix the maxsize for pixelation without crashes
/*px.pixelate = function () { // note: this just patches pixelit while keeping its original goal of "pixelating in a set dimension", while this tool needs "pixelating for a set number of blocks"
// Set base size from original image
this.drawto.width = this.drawfrom.naturalWidth;
this.drawto.height = this.drawfrom.naturalHeight;
let scaledW = this.drawto.width * this.scale;
let scaledH = this.drawto.height * this.scale;
// Max safe size (most browsers can do up to 8192–16384, 8192 is safer)
const MAX_CANVAS_SIZE = 8192;
// If scaled size exceeds GPU/browser limits, shrink scale proportionally
if (scaledW > MAX_CANVAS_SIZE || scaledH > MAX_CANVAS_SIZE) {
const ratio = Math.min(MAX_CANVAS_SIZE / scaledW, MAX_CANVAS_SIZE / scaledH);
this.scale *= ratio;
scaledW = this.drawto.width * this.scale;
scaledH = this.drawto.height * this.scale;
}
// Create temp canvas for scaled copy
const tempCanvas = document.createElement("canvas");
tempCanvas.width = scaledW;
tempCanvas.height = scaledH;
tempCanvas.style.visibility = "hidden";
tempCanvas.style.position = "fixed";
tempCanvas.style.top = "0";
tempCanvas.style.left = "0";
const tempContext = tempCanvas.getContext("2d");
// Draw scaled image
tempContext.drawImage(this.drawfrom, 0, 0, scaledW, scaledH);
// Disable smoothing for pixel effect
this.ctx.mozImageSmoothingEnabled = false;
this.ctx.webkitImageSmoothingEnabled = false;
this.ctx.imageSmoothingEnabled = false;
// Draw scaled image back to original size on final canvas
this.ctx.drawImage(tempCanvas, 0, 0, scaledW, scaledH, 0, 0, this.drawto.width, this.drawto.height);
// Remove temp element
tempCanvas.remove();
return this;
}*/
// make the pixelize function actually draw a big pixelated canvas
// that old function crashes when using big blocksize numbers (<=1000)
px.old_pixelate = function () {//yes this was made by chatgpt dont ask
// Size of each fake pixel (real pixels per block)
const BLOCK_SIZE = this.pxsize ? this.pxsize : 10;
// Original image size
const imgW = this.drawfrom.naturalWidth;
const imgH = this.drawfrom.naturalHeight;
// Interpret this.scale as "number of fake pixels across the width"
const fakePixelCountX = this.scale; // same name as your old function's this.scale
const fakePixelCountY = Math.round((imgH / imgW) * fakePixelCountX);
// Downscaled size for pixelation
const scaledW = fakePixelCountX;
const scaledH = fakePixelCountY;
// Final canvas size: each fake pixel is BLOCK_SIZE x BLOCK_SIZE
this.drawto.width = scaledW * BLOCK_SIZE;
this.drawto.height = scaledH * BLOCK_SIZE;
// Temp canvas for the scaled-down image
const tempCanvas = document.createElement("canvas");
tempCanvas.width = scaledW;
tempCanvas.height = scaledH;
tempCanvas.style.visibility = "hidden";
tempCanvas.style.position = "fixed";
tempCanvas.style.top = "0";
tempCanvas.style.left = "0";
const tempContext = tempCanvas.getContext("2d");
tempContext.drawImage(this.drawfrom, 0, 0, scaledW, scaledH);
// Disable smoothing for pixel effect
this.ctx.mozImageSmoothingEnabled = false;
this.ctx.webkitImageSmoothingEnabled = false;
this.ctx.imageSmoothingEnabled = false;
// Draw scaled-down image back to enlarged size
this.ctx.drawImage(tempCanvas, 0, 0, scaledW, scaledH, 0, 0, this.drawto.width, this.drawto.height);
// Remove temp element
tempCanvas.remove();
return this;
}
// that new one caps at a certain max canvas size to never crash, but might makes pixels or the scaling smaller for this
px.new_pixelate = function () {
// Size of each fake pixel (real pixels per block)
const BLOCK_SIZE = this.pxsize ? this.pxsize : 10;
// Original image size
const imgW = this.drawfrom.naturalWidth;
const imgH = this.drawfrom.naturalHeight;
// Interpret this.scale as "number of fake pixels across the width"
let fakePixelCountX = Math.max(1, Math.floor(this.scale));
let fakePixelCountY = Math.max(1, Math.floor((imgH / imgW) * fakePixelCountX));
// Max safe canvas size
const MAX_CANVAS_SIZE = 8192;
// If the resulting canvas would be too big, reduce number of fake pixels
if (fakePixelCountX * BLOCK_SIZE > MAX_CANVAS_SIZE || fakePixelCountY * BLOCK_SIZE > MAX_CANVAS_SIZE) {
const scaleRatio = Math.min(MAX_CANVAS_SIZE / (fakePixelCountX * BLOCK_SIZE), MAX_CANVAS_SIZE / (fakePixelCountY * BLOCK_SIZE));
fakePixelCountX = Math.floor(fakePixelCountX * scaleRatio);
fakePixelCountY = Math.floor(fakePixelCountY * scaleRatio);
// Ensure at least 1 fake pixel
fakePixelCountX = Math.max(1, fakePixelCountX);
fakePixelCountY = Math.max(1, fakePixelCountY);
}
// Final canvas size: each fake pixel is BLOCK_SIZE x BLOCK_SIZE
this.drawto.width = fakePixelCountX * BLOCK_SIZE;
this.drawto.height = fakePixelCountY * BLOCK_SIZE;
// Temp canvas for the scaled-down image
const tempCanvas = document.createElement("canvas");
tempCanvas.width = fakePixelCountX;
tempCanvas.height = fakePixelCountY;
const tempContext = tempCanvas.getContext("2d");
tempContext.drawImage(this.drawfrom, 0, 0, fakePixelCountX, fakePixelCountY);
// Disable smoothing for crisp pixelation
this.ctx.mozImageSmoothingEnabled = false;
this.ctx.webkitImageSmoothingEnabled = false;
this.ctx.imageSmoothingEnabled = false;
// Draw scaled-down image back to enlarged size
this.ctx.drawImage(tempCanvas, 0, 0, fakePixelCountX, fakePixelCountY, 0, 0, this.drawto.width, this.drawto.height);
// Remove temp element
tempCanvas.remove();
return this;
}
this.pixelate = function () { console.log("attempted call to legacy pixelate function"); }
// scientifical mathematical human eye color perception shifting calculation black magic or smth
// basically fixes the color being narrowed down to the actually wrong ones but technically the right ones
// like technically blue is closer to cyan but maybe for a human its closer to another shade of blue that's available
function rgbToLab(rgb) {
let [r, g, b] = rgb.map(v => v / 255);
// sRGB to XYZ
[r, g, b] = [r, g, b].map(c =>
c > 0.04045 ? Math.pow((c + 0.055) / 1.055, 2.4) : c / 12.92
);
let x = r * 0.4124 + g * 0.3576 + b * 0.1805;
let y = r * 0.2126 + g * 0.7152 + b * 0.0722;
let z = r * 0.0193 + g * 0.1192 + b * 0.9505;
// Normalize for D65 white point
const refX = 0.95047, refY = 1.00000, refZ = 1.08883;
[x, y, z] = [x / refX, y / refY, z / refZ].map(v =>
v > 0.008856 ? Math.cbrt(v) : (7.787 * v) + 16/116
);
const L = (116 * y) - 16;
const a = 500 * (x - y);
const bLab = 200 * (y - z);
return [L, a, bLab];
}
function colorSim(rgbColor, compareColor) {
const lab1 = this.rgbToLab(rgbColor);
const lab2 = this.rgbToLab(compareColor);
const dL = lab1[0] - lab2[0];
const dA = lab1[1] - lab2[1];
const dB = lab1[2] - lab2[2];
return Math.sqrt(dL * dL + dA * dA + dB * dB);
}
function similarColor(actualColor) {
let selectedColor = this.palette[0];
let currentSim = this.colorSim(actualColor, selectedColor);
this.palette.forEach((color) => {
const nextSim = this.colorSim(actualColor, color);
if (nextSim < currentSim) {
selectedColor = color;
currentSim = nextSim;
}
});
return selectedColor;
}
px.old_similarColor = px.similarColor;
px.new_similarColor = similarColor;
function process() {
let img = new Image();
img.src = URL.createObjectURL(document.getElementById("image").files[0]);
img.addEventListener("load", function () {
if (document.getElementById("color").checked) {
console.log("using basic color accruacy");
px.similarColor = px.old_similarColor;
} else {
console.log("using advanced color accruacy");
px.similarColor = px.new_similarColor;
}
px.setFromImgSource(img.src);
//px.setScale(document.getElementById("blocksize").value || "7");
// bypass the maximum blocksize of 25 (this is only for the original pixelit, see the first comments)
//px.scale = parseInt(document.getElementById("blocksize").value || "7") * 0.01;
px.scale = parseInt(document.getElementById("blocksize").value || "10");
// the following line will only work if you're using my custom implementation of the pixelate() function
px.pxsize = parseInt(document.getElementById("pixelsize").value || "10");
px.setPalette([ // this is approximately the palette for the wplace, approximately because i took them from a screenshot and not directly from the site, but im lazy and wont put the exact same colors (anyways they're close enough)
[1, 1, 0],
[60, 61, 61],
[120, 121, 121],
[210, 211, 211],
[254, 254, 255],
[96, 1, 25],
[237, 28, 36],
[254, 127, 38],
[246, 170, 9],
[248, 220, 59],
[255, 251, 189],
[14, 184, 105],
[18, 230, 122],
[135, 254, 95],
[13, 129, 111],
[17, 175, 166],
[18, 224, 190],
[40, 81, 159],
[64, 147, 228],
[96, 247, 242],
[106, 81, 246],
[153, 177, 251],
[120, 13, 152],
[171, 57, 185],
[224, 158, 248],
[202, 0, 123],
[237, 31, 129],
[243, 140, 168],
[104, 70, 52],
[149, 104, 42],
[248, 179, 118]
]);
px.draw();
if (document.getElementById("exact").checked) {
console.log("using old pixelate");
px.old_pixelate();
} else {
console.log("using new pixelate");
px.new_pixelate();
}
if (document.getElementById("greyscale").checked) {
px.convertGrayscale();
}
/*if (document.getElementById("maxheight").value) {
px.setMaxHeight(document.getElementById("maxheight").value).resizeImage();
}
if (document.getElementById("maxwidth").value) {
px.setMaxWidth(document.getElementById("maxwidth").value).resizeImage();
}*/
if (!document.getElementById("palette").checked) {
console.log("using wplace palette");
px.convertPalette();
}
});
}
document.getElementById("process").addEventListener("click", process);
function liveRefresh() { if (live) { process(); } }
document.getElementById("image").addEventListener("change", liveRefresh);
document.getElementById("blocksize").addEventListener("change", liveRefresh);
document.getElementById("pixelsize").addEventListener("change", liveRefresh);
document.getElementById("greyscale").addEventListener("change", liveRefresh);
document.getElementById("exact").addEventListener("change", liveRefresh);
document.getElementById("color").addEventListener("change", liveRefresh);
document.getElementById("palette").addEventListener("change", liveRefresh);
document.getElementById("live").addEventListener("change", function (event) {
if (event.target.checked) {
live = true;
document.getElementById("process").disabled = true;
liveRefresh();
} else {
live = false;
document.getElementById("process").disabled = false;
}
});
</script>
<style>
body {
background-color: #faf9f6;
}
.ui {
margin-left: 15%;
font-size: 120%;
font-family: sans-serif;
position: relative;
top: -300px;
}
input, button {
font-size: 80%;
font-family: sans-serif;
}
button {
margin-left: 50px;
}
canvas {
display: block;
margin: 0 auto;
}
span {
opacity: 40%;
width: 40%;
display: block;
}
h1, h2, h3, span {
margin-left: 35%;
position: relative;
top: 300px;
}
i {
opacity: 40%;
font-size: 75%;
}
input[type="number"] {
width: 300px;
}
input[type="number"], input[type="file"] {
margin-bottom: 10px;
}
@media only screen and (max-width: 800px) {
* {
position: unset !important;
}
h1, h2, h3, span {
margin-left: 0 !important;
}
span {
width: 100%;
}
}
@media only screen and (max-width: 1400px) {
.ui {
margin-left: 10px !important;
}
}
</style>
</html>