MCPcopy Create free account
hub / github.com/devforth/painterro / doPixelize

Method doPixelize

js/selecter.js:43–110  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41 }
42
43 doPixelize() {
44 const c = this.area.topl;
45 const size = [
46 this.area.bottoml[0] - c[0], // width
47 this.area.bottoml[1] - c[1],
48 ];
49
50 this.pixelizePixelSize = this.main.params.pixelizePixelSize;
51
52 if (this.pixelizePixelSize.slice(-1) === '%') {
53 this.pixelSize = (Math.min(size[0], size[1]) / (100.0 / this.pixelizePixelSize.slice(0, -1)));
54 } else if (this.pixelizePixelSize.slice(-2).toLowerCase() === 'px') {
55 this.pixelSize = this.pixelizePixelSize.slice(0, -2);
56 } else {
57 this.pixelSize = this.pixelizePixelSize;
58 }
59
60
61 if (this.pixelSize < 2) {
62 this.pixelSize = 2; // prevent errors
63 }
64
65 if (size[1] < size[0]) {
66 this.pixelSizeY = this.pixelSize;
67 const desiredHorPxs = Math.round(size[0] / this.pixelSizeY);
68 this.pixelSizeX = (size[0] * 1.0) / desiredHorPxs;
69 } else {
70 this.pixelSizeX = this.pixelSize;
71 const desiredVerPxs = Math.round(size[1] / this.pixelSizeX);
72 this.pixelSizeY = (size[1] * 1.0) / desiredVerPxs;
73 }
74 const pxData = [];
75 const pxSize = [size[0] / this.pixelSizeX, size[1] / this.pixelSizeY];
76 for (let i = 0; i < pxSize[0]; i += 1) {
77 const row = [];
78 for (let j = 0; j < pxSize[1]; j += 1) {
79 row.push([0, 0, 0, 0, 0]);
80 }
81 pxData.push(row);
82 }
83 const data = this.ctx.getImageData(c[0], c[1], size[0], size[1]);
84 for (let i = 0; i < size[0]; i += 1) {
85 for (let j = 0; j < size[1]; j += 1) {
86 const ii = Math.floor(i / this.pixelSizeX);
87 const jj = Math.floor(j / this.pixelSizeY);
88 const base = ((j * size[0]) + i) * 4;
89 pxData[ii][jj][0] += data.data[base];
90 pxData[ii][jj][1] += data.data[base + 1];
91 pxData[ii][jj][2] += data.data[base + 2];
92 pxData[ii][jj][3] += data.data[base + 3];
93 pxData[ii][jj][4] += 1;
94 }
95 }
96 for (let i = 0; i < pxSize[0]; i += 1) {
97 for (let j = 0; j < pxSize[1]; j += 1) {
98 const s = pxData[i][j][4];
99 this.ctx.fillStyle = `rgba(
100${Math.round(pxData[i][j][0] / s)},

Callers 1

constructorMethod · 0.80

Calls 1

captureStateMethod · 0.80

Tested by

no test coverage detected