Skip to content

Commit cb2a74e

Browse files
committed
fix scale and temp canvas size for bigger images
1 parent f42f987 commit cb2a74e

File tree

5 files changed

+45
-25
lines changed

5 files changed

+45
-25
lines changed

dist/pixelit.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,36 @@ class pixelit {
159159
* Draws a pixelated version of an image in a given canvas
160160
*/
161161
pixelate() {
162-
this.drawto.width = this.drawfrom.width;
163-
this.drawto.height = this.drawfrom.height;
164-
165-
const scaledW = this.drawto.width * this.scale;
166-
const scaledH = this.drawto.height * this.scale;
167-
168-
//var ctx = canvas.getContext("2d");
169-
170-
this.ctx.mozImageSmoothingEnabled = false;
171-
this.ctx.webkitImageSmoothingEnabled = false;
172-
this.ctx.imageSmoothingEnabled = false;
162+
this.drawto.width = this.drawfrom.naturalWidth;
163+
this.drawto.height = this.drawfrom.naturalHeight;
173164

174-
//previous method, would failt for background transparent images
175-
//this.ctx.drawImage(this.drawfrom, 0, 0, scaledW, scaledH);
165+
let scaledW = this.drawto.width * this.scale;
166+
let scaledH = this.drawto.height * this.scale;
176167

177168
//make temporary canvas to make new scaled copy
178169
const tempCanvas = document.createElement("canvas");
170+
171+
//corner case of bigger images, increase the temporary canvas size to fit everything
172+
if(this.drawto.width > 800 || this.drawto.width > 800 ){
173+
//fix sclae to pixelate bigger images
174+
this.scale *= 0.25;
175+
scaledW = this.drawto.width * this.scale;
176+
scaledH = this.drawto.height * this.scale;
177+
//make it big enough to fit
178+
tempCanvas.width = Math.max(scaledW, scaledH ) + 50;
179+
tempCanvas.height = Math.max(scaledW, scaledH ) + 50;
180+
}
179181
// get the context
180182
const tempContext = tempCanvas.getContext("2d");
181183
// draw the image into the canvas
182184
tempContext.drawImage(this.drawfrom, 0, 0, scaledW, scaledH);
185+
document.body.appendChild(tempCanvas);
186+
187+
//configs to pixelate
188+
this.ctx.mozImageSmoothingEnabled = false;
189+
this.ctx.webkitImageSmoothingEnabled = false;
190+
this.ctx.imageSmoothingEnabled = false;
191+
183192
//draw to final canvas
184193
this.ctx.drawImage(
185194
tempCanvas,
@@ -189,8 +198,8 @@ class pixelit {
189198
scaledH,
190199
0,
191200
0,
192-
this.drawfrom.width,
193-
this.drawfrom.height
201+
this.drawfrom.naturalWidth,
202+
this.drawfrom.naturalHeight
194203
);
195204
//remove temp element
196205
tempCanvas.remove();

dist/pixelitmin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/main.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,20 @@ document.addEventListener("DOMContentLoaded", function () {
130130
document.querySelector(".loader").classList.toggle("active");
131131
setTimeout(() => {
132132
document.querySelector(".loader").classList.toggle("active");
133-
}, 500);
133+
}, 1500);
134134
px
135135
.setScale(blocksize.value)
136136
.setPalette(paletteList[currentPalette])
137137
.draw()
138138
.pixelate();
139-
greyscale.checked ? px.convertGrayscale() : null;
140-
palette.checked ? px.convertPalette() : null;
141-
maxheight.value ? px.setMaxHeight(maxheight.value).resizeImage() : null;
142-
maxwidth.value ? px.setMaxWidth(maxwidth.value).resizeImage() : null;
139+
140+
greyscale.checked ? px.convertGrayscale() : null;
141+
palette.checked ? px.convertPalette() : null;
142+
maxheight.value ? px.setMaxHeight(maxheight.value).resizeImage() : null;
143+
maxwidth.value ? px.setMaxWidth(maxwidth.value).resizeImage() : null;
143144
};
144145

146+
145147
const makePaletteGradient = () => {
146148
//create palette
147149
let pdivs = "";

docs/assets/mainmin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)