Skip to content

Commit ef2fc7c

Browse files
committed
Fix backpacking zero-height/width costumes
1 parent d74a5ca commit ef2fc7c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/lib/backpack/jpeg-thumbnail.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ const jpegThumbnail = dataUrl => new Promise((resolve, reject) => {
66

77
const maxDimension = 96; // 3x the maximum displayed size of 32px
88

9-
if (image.height > image.width) {
10-
canvas.height = maxDimension;
11-
canvas.width = (maxDimension / image.height) * image.width;
9+
if (image.height < 1 || image.width < 1) {
10+
canvas.width = canvas.height = maxDimension;
11+
// drawImage can fail if image height/width is less than 1
12+
// Use blank image; the costume is too small to render anyway
13+
ctx.fillStyle = 'white'; // Create white background, since jpeg doesn't have transparency
14+
ctx.fillRect(0, 0, maxDimension, maxDimension);
1215
} else {
13-
canvas.width = maxDimension;
14-
canvas.height = (maxDimension / image.width) * image.height;
16+
if (image.height > image.width) {
17+
canvas.height = maxDimension;
18+
canvas.width = (maxDimension / image.height) * image.width;
19+
} else {
20+
canvas.width = maxDimension;
21+
canvas.height = (maxDimension / image.width) * image.height;
22+
}
23+
ctx.fillStyle = 'white';
24+
ctx.fillRect(0, 0, canvas.width, canvas.height);
25+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
1526
}
16-
17-
ctx.fillStyle = 'white'; // Create white background, since jpeg doesn't have transparency
18-
ctx.fillRect(0, 0, canvas.width, canvas.height);
19-
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
2027
resolve(canvas.toDataURL('image/jpeg', 0.92 /* quality */)); // Default quality is 0.92
2128
};
2229
image.onerror = err => {

0 commit comments

Comments
 (0)