Skip to content

Commit 684d6f1

Browse files
committed
Add image smoothing options.
Resolves blueimp#95.
1 parent 77452c9 commit 684d6f1

File tree

3 files changed

+142
-18
lines changed

3 files changed

+142
-18
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,16 @@ The optional third argument to **loadImage()** is a map of options:
209209
Defaults to `1` and requires `canvas: true`.
210210
- **downsamplingRatio**: Defines the ratio in which the image is downsampled.
211211
By default, images are downsampled in one step. With a ratio of `0.5`, each
212-
step scales the image to half the size, before reaching the target dimensions.
212+
step scales the image to half the size, before reaching the target
213+
dimensions.
213214
Requires `canvas: true`.
215+
- **imageSmoothingEnabled**: If set to `false`,
216+
[disables image smoothing](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled).
217+
Defaults to `true` and requires `canvas: true`.
218+
- **imageSmoothingQuality**: Sets the
219+
[quality of image smoothing](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingQuality).
220+
Possible values: `'low'`, `'medium'`, `'high'`
221+
Defaults to `'low'` and requires `canvas: true`.
214222
- **crop**: Crops the image to the maxWidth/maxHeight constraints if set to
215223
`true`.
216224
Enabling the `crop` option also enables the `canvas` option.

js/load-image-scale.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,26 @@
8585
destX,
8686
destY,
8787
destWidth,
88-
destHeight
88+
destHeight,
89+
options
8990
) {
90-
canvas
91-
.getContext('2d')
92-
.drawImage(
93-
img,
94-
sourceX,
95-
sourceY,
96-
sourceWidth,
97-
sourceHeight,
98-
destX,
99-
destY,
100-
destWidth,
101-
destHeight
102-
)
91+
var ctx = canvas.getContext('2d')
92+
if (options.imageSmoothingEnabled === false) {
93+
ctx.imageSmoothingEnabled = false
94+
} else if (options.imageSmoothingQuality) {
95+
ctx.imageSmoothingQuality = options.imageSmoothingQuality
96+
}
97+
ctx.drawImage(
98+
img,
99+
sourceX,
100+
sourceY,
101+
sourceWidth,
102+
sourceHeight,
103+
destX,
104+
destY,
105+
destWidth,
106+
destHeight
107+
)
103108
return canvas
104109
}
105110

@@ -246,7 +251,8 @@
246251
0,
247252
0,
248253
canvas.width,
249-
canvas.height
254+
canvas.height,
255+
options
250256
)
251257
sourceX = 0
252258
sourceY = 0
@@ -266,7 +272,8 @@
266272
0,
267273
0,
268274
sourceWidth,
269-
sourceHeight
275+
sourceHeight,
276+
options
270277
)
271278
}
272279
}
@@ -283,7 +290,8 @@
283290
0,
284291
0,
285292
destWidth,
286-
destHeight
293+
destHeight,
294+
options
287295
)
288296
}
289297
img.width = destWidth

test/test.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,114 @@
398398
).to.be.ok
399399
})
400400
})
401+
402+
describe('image smoothing', function () {
403+
if (
404+
!document.createElement('canvas').getContext('2d').imageSmoothingEnabled
405+
) {
406+
return
407+
}
408+
409+
it('imageSmoothingEnabled defaults to true', function (done) {
410+
expect(
411+
loadImage(
412+
blobGIF,
413+
function (img) {
414+
expect(img.width).to.equal(160)
415+
expect(img.getContext('2d').imageSmoothingEnabled).to.equal(true)
416+
done()
417+
},
418+
{ minWidth: 160, canvas: true }
419+
)
420+
).to.be.ok
421+
})
422+
423+
it('Sets imageSmoothingEnabled to false', function (done) {
424+
expect(
425+
loadImage(
426+
blobGIF,
427+
function (img) {
428+
expect(img.width).to.equal(160)
429+
expect(img.getContext('2d').imageSmoothingEnabled).to.equal(false)
430+
done()
431+
},
432+
{ minWidth: 160, canvas: true, imageSmoothingEnabled: false }
433+
)
434+
).to.be.ok
435+
})
436+
437+
if (
438+
document.createElement('canvas').getContext('2d')
439+
.imageSmoothingQuality !== 'low'
440+
) {
441+
return
442+
}
443+
444+
it('imageSmoothingQuality defaults to "low"', function (done) {
445+
expect(
446+
loadImage(
447+
blobGIF,
448+
function (img) {
449+
expect(img.width).to.equal(160)
450+
expect(img.getContext('2d').imageSmoothingQuality).to.equal('low')
451+
done()
452+
},
453+
{ minWidth: 160, canvas: true }
454+
)
455+
).to.be.ok
456+
})
457+
458+
it('Sets imageSmoothingQuality to "medium"', function (done) {
459+
expect(
460+
loadImage(
461+
blobGIF,
462+
function (img) {
463+
expect(img.width).to.equal(160)
464+
expect(img.getContext('2d').imageSmoothingQuality).to.equal(
465+
'medium'
466+
)
467+
done()
468+
},
469+
{ minWidth: 160, canvas: true, imageSmoothingQuality: 'medium' }
470+
)
471+
).to.be.ok
472+
})
473+
474+
it('Sets imageSmoothingQuality to "high"', function (done) {
475+
expect(
476+
loadImage(
477+
blobGIF,
478+
function (img) {
479+
expect(img.width).to.equal(160)
480+
expect(img.getContext('2d').imageSmoothingQuality).to.equal(
481+
'high'
482+
)
483+
done()
484+
},
485+
{ minWidth: 160, canvas: true, imageSmoothingQuality: 'high' }
486+
)
487+
).to.be.ok
488+
})
489+
490+
it('Ignores imageSmoothingQuality if imageSmoothingEnabled is false', function (done) {
491+
expect(
492+
loadImage(
493+
blobGIF,
494+
function (img) {
495+
expect(img.width).to.equal(160)
496+
expect(img.getContext('2d').imageSmoothingQuality).to.equal('low')
497+
done()
498+
},
499+
{
500+
minWidth: 160,
501+
canvas: true,
502+
imageSmoothingQuality: 'high',
503+
imageSmoothingEnabled: false
504+
}
505+
)
506+
).to.be.ok
507+
})
508+
})
401509
})
402510

403511
describe('Cropping', function () {

0 commit comments

Comments
 (0)