Skip to content

Commit 3513a68

Browse files
authored
Merge pull request scratchfoundation#5197 from paulkaplan/fix-backpack-thumbnailing
Draw backpack thumbnails smaller
2 parents 38aa76f + fb4b492 commit 3513a68

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/lib/backpack/jpeg-thumbnail.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ const jpegThumbnail = dataUrl => new Promise((resolve, reject) => {
33
image.onload = () => {
44
const canvas = document.createElement('canvas');
55
const ctx = canvas.getContext('2d');
6-
// TODO we may want to draw to a smaller, fixed size to optimize file size
7-
canvas.width = image.width;
8-
canvas.height = image.height;
6+
7+
const maxDimension = 96; // 3x the maximum displayed size of 32px
8+
9+
if (image.height > image.width) {
10+
canvas.height = maxDimension;
11+
canvas.width = (maxDimension / image.height) * image.width;
12+
} else {
13+
canvas.width = maxDimension;
14+
canvas.height = (maxDimension / image.width) * image.height;
15+
}
916

1017
ctx.fillStyle = 'white'; // Create white background, since jpeg doesn't have transparency
1118
ctx.fillRect(0, 0, canvas.width, canvas.height);
12-
13-
ctx.drawImage(image, 0, 0);
14-
// TODO we can play with the `quality` option here to optimize file size
19+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
1520
resolve(canvas.toDataURL('image/jpeg', 0.92 /* quality */)); // Default quality is 0.92
1621
};
1722
image.onerror = err => {

0 commit comments

Comments
 (0)