Skip to content

Commit 46baf0c

Browse files
committed
Only apply the new width/height after all calculations have been done.
1 parent fb21dd5 commit 46baf0c

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

load-image.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Load Image 1.1.4
2+
* JavaScript Load Image 1.1.5
33
* https://github.com/blueimp/JavaScript-Load-Image
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -56,29 +56,33 @@
5656
loadImage.scale = function (img, options) {
5757
options = options || {};
5858
var canvas = document.createElement('canvas'),
59+
width = img.width,
60+
height = img.height,
5961
scale = Math.max(
60-
(options.minWidth || img.width) / img.width,
61-
(options.minHeight || img.height) / img.height
62+
(options.minWidth || width) / width,
63+
(options.minHeight || height) / height
6264
);
6365
if (scale > 1) {
64-
img.width = parseInt(img.width * scale, 10);
65-
img.height = parseInt(img.height * scale, 10);
66+
width = parseInt(width * scale, 10);
67+
height = parseInt(height * scale, 10);
6668
}
6769
scale = Math.min(
68-
(options.maxWidth || img.width) / img.width,
69-
(options.maxHeight || img.height) / img.height
70+
(options.maxWidth || width) / width,
71+
(options.maxHeight || height) / height
7072
);
7173
if (scale < 1) {
72-
img.width = parseInt(img.width * scale, 10);
73-
img.height = parseInt(img.height * scale, 10);
74+
width = parseInt(width * scale, 10);
75+
height = parseInt(height * scale, 10);
7476
}
7577
if (!options.canvas || !canvas.getContext) {
78+
img.width = width;
79+
img.height = height;
7680
return img;
7781
}
78-
canvas.width = img.width;
79-
canvas.height = img.height;
82+
canvas.width = width;
83+
canvas.height = height;
8084
canvas.getContext('2d')
81-
.drawImage(img, 0, 0, img.width, img.height);
85+
.drawImage(img, 0, 0, width, height);
8286
return canvas;
8387
};
8488

load-image.min.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)