Skip to content

Commit 9fc1855

Browse files
committed
1 parent dfadfa3 commit 9fc1855

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

js/load-image.js

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,60 @@
242242
}
243243
}
244244
if (useCanvas) {
245-
canvas.width = destWidth;
246-
canvas.height = destHeight;
247-
loadImage.transformCoordinates(
248-
canvas,
249-
options
250-
);
251-
return loadImage.renderImageToCanvas(
252-
canvas,
253-
img,
254-
sourceX,
255-
sourceY,
256-
sourceWidth,
257-
sourceHeight,
258-
0,
259-
0,
260-
destWidth,
261-
destHeight
262-
);
245+
if (destWidth < sourceWidth && destHeight < sourceHeight) {
246+
var stepScale = 0.5;
247+
canvas.width = sourceWidth;
248+
canvas.height = sourceHeight;
249+
250+
while (canvas.width > destWidth) {
251+
if (canvas.width * stepScale > destWidth) {
252+
canvas.width = canvas.width * stepScale;
253+
canvas.height = canvas.height * stepScale;
254+
} else {
255+
canvas.width = destWidth;
256+
canvas.height = destHeight;
257+
}
258+
loadImage.transformCoordinates(
259+
canvas,
260+
options
261+
);
262+
canvas = loadImage.renderImageToCanvas(
263+
canvas,
264+
img,
265+
sourceX,
266+
sourceY,
267+
sourceWidth,
268+
sourceHeight,
269+
0,
270+
0,
271+
canvas.width,
272+
canvas.height
273+
);
274+
img = new Image();
275+
img.src = canvas.toDataURL();
276+
sourceWidth = canvas.width;
277+
sourceHeight = canvas.height;
278+
}
279+
} else {
280+
canvas.width = destWidth;
281+
canvas.height = destHeight;
282+
loadImage.transformCoordinates(
283+
canvas,
284+
options
285+
);
286+
return loadImage.renderImageToCanvas(
287+
canvas,
288+
img,
289+
sourceX,
290+
sourceY,
291+
sourceWidth,
292+
sourceHeight,
293+
0,
294+
0,
295+
destWidth,
296+
destHeight
297+
);
298+
}
263299
}
264300
img.width = destWidth;
265301
img.height = destHeight;

0 commit comments

Comments
 (0)