Skip to content

Commit

Permalink
feat: create a DPR srcset when a fixed height is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdeansanchez committed Jan 22, 2021
1 parent d5c34a0 commit fa5f36a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ https://testing.imgix.net/image.jpg?w=8192&s=59eb881b618fed314fe30cf9e3ec7b00 81

#### Fixed Image Rendering

In cases where enough information is provided about an image's dimensions, `buildSrcSet()` will instead build a `srcset` that will allow for an image to be served at different resolutions. The parameters taken into consideration when determining if an image is fixed-width are `w`, `h`, and `ar`. By invoking `buildSrcSet()` with either a width **or** the height and aspect ratio (along with `fit=crop`, typically) provided, a different `srcset` will be generated for a fixed-size image instead.
Specifying either a `w` or a `h` parameter to `buildSrcSet()` will create a DPR-based srcset. This DPR-based srcset allows for the fixed-sized image to be served at different resolutions (i.e. at different pixel densities).

```js
var client = new ImgixClient({
Expand Down
4 changes: 2 additions & 2 deletions src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ export default class ImgixClient {
}

buildSrcSet(path, params = {}, options = {}) {
const { w, h, ar } = params;
const { w, h } = params;

if (w || (h && ar)) {
if (w || h) {
return this._buildDPRSrcSet(path, params, options);
} else {
return this._buildSrcSetPairs(path, params, options);
Expand Down
17 changes: 2 additions & 15 deletions test/test-buildSrcSet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,8 @@ describe('SrcSet Builder:', function describeSuite() {
});
});

it('should generate the expected default srcset pair values', function testSpec() {
assertCorrectWidthDescriptors(srcset, RESOLUTIONS);
});

it('should return the expected number of `url widthDescriptor` pairs', function testSpec() {
assert.strictEqual(srcset.split(',').length, 31);
});

it('should not exceed the bounds of [100, 8192]', function testSpec() {
assertMinMaxWidthBounds(srcset, 100, 8192);
});

// a 17% testing threshold is used to account for rounding
it('should not increase more than 17% every iteration', function testSpec() {
assertWidthsIncreaseByTolerance(srcset, 0.17);
it('creates a DPR srcset with default DPR values', function testSpec() {
assertIncludesDefaultDprParamAndDescriptor(srcset);
});

it('should correctly sign each URL', function testSpec() {
Expand Down

0 comments on commit fa5f36a

Please sign in to comment.