Skip to content

Commit 379922b

Browse files
committed
Ensure pixelRatio scaling is idempotent.
1 parent 4931b41 commit 379922b

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

js/load-image-scale.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,17 @@
225225
if (useCanvas) {
226226
pixelRatio = options.pixelRatio
227227
if (pixelRatio > 1) {
228-
canvas.style.width = destWidth + 'px'
229-
canvas.style.height = destHeight + 'px'
230-
destWidth *= pixelRatio
231-
destHeight *= pixelRatio
232-
canvas.getContext('2d').scale(pixelRatio, pixelRatio)
228+
if (parseInt(img.style.width, 10) === width / pixelRatio) {
229+
// Source image is already scaled according to device pixel ratio
230+
canvas.style.width = destWidth / pixelRatio + 'px'
231+
canvas.style.height = destHeight / pixelRatio + 'px'
232+
} else {
233+
canvas.style.width = destWidth + 'px'
234+
canvas.style.height = destHeight + 'px'
235+
destWidth *= pixelRatio
236+
destHeight *= pixelRatio
237+
canvas.getContext('2d').scale(pixelRatio, pixelRatio)
238+
}
233239
}
234240
downsamplingRatio = options.downsamplingRatio
235241
if (

test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@
300300
expect(img.height).to.equal(160)
301301
expect(img.style.width).to.equal('120px')
302302
expect(img.style.height).to.equal('80px')
303+
// Check if pixelRatio scaling is idempotent:
304+
var img2 = loadImage.scale(img, { minWidth: 120, pixelRatio: 2 })
305+
expect(img2.width).to.equal(240)
306+
expect(img2.height).to.equal(160)
307+
expect(img2.style.width).to.equal('120px')
308+
expect(img2.style.height).to.equal('80px')
303309
done()
304310
},
305311
{ minWidth: 120, canvas: true, pixelRatio: 2 }
@@ -316,6 +322,12 @@
316322
expect(img.height).to.equal(40)
317323
expect(img.style.width).to.equal('30px')
318324
expect(img.style.height).to.equal('20px')
325+
// Check if pixelRatio scaling is idempotent:
326+
var img2 = loadImage.scale(img, { minWidth: 30, pixelRatio: 2 })
327+
expect(img2.width).to.equal(60)
328+
expect(img2.height).to.equal(40)
329+
expect(img2.style.width).to.equal('30px')
330+
expect(img2.style.height).to.equal('20px')
319331
done()
320332
},
321333
{ maxWidth: 30, canvas: true, pixelRatio: 2 }

0 commit comments

Comments
 (0)