# lazySizes RIaS extension (Responsive image as a service / Responsive image on demand)
The RiaS plugin enables lazySizes to generate the best suitable image source based on an URL pattern. It works with pre-build images (i.e. grunt-responsive-images) as also with any third party (ReSrc, Pixtulate, mobify, WURFL's Image Tailor ...) or self hosted restful responsive image services (responsive image on demand).
In general the RIaS plugin combines the simplicity of the famous Imager.js solution with the future power of native responsive images implementations and the webcomponent-like working of lazySizes' ``.lazyload`` elements (self-initialization, self-configuration and self-destroying).
In case the browser does support ``srcset`` the RIaS plugin will also produce a list of source candidates so that any current and future improvements (low bandwidth, metered bandwidth, user preferences, browser zoom etc.) to the native responsive image support is automatically exploited.
The RiaS plugin also allows art direction by combining placeholder URLs with a ``picture`` element.
## Basic/quick usage
* Add the RiaS plugin right before the lazySizes script or concat those scripts into your script bundle:
```html
```
* Add the ``lazyload`` class and the ``data-sizes="auto"`` attribute to your image and include the placeholder ``{width}`` at the point, where your image service expects the requested width of the image.
```html
```
## [Demo](http://afarkas.github.io/lazysizes/rias/)
A [demo with markup and code examples can be seen here](http://afarkas.github.io/lazysizes/rias/).
## Configuration/Options
The RIaS plugin can be configured through the ``lazySizesConfig.rias`` option object, which should be configured before the lazySizes script.
```js
window.lazySizesConfig = window.lazySizesConfig || {};
window.lazySizesConfig.rias = window.lazySizesConfig.rias || {};
// configure available widths to replace with the {width} placeholder
window.lazySizesConfig.rias.widths = [320, 480, 640, 960];
window.lazySizesConfig.rias.absUrl = true;
```
or element specific and declarative with corresponding ``data-*`` attributes:
```html
```
or element specific and functional using the ``lazyriasmodifyoptions`` event.
```html
```
All ``rias`` options can also be used as ``data-*`` attributes.
### URL generation and {placeholder}s
The url is dynamically generated by replacing placeholder values, which are enclosed by curly brackets.
All RiAS options can also be used as a {placeholder} inside the url.
### List of Options
* ``lazySizesConfig.rias.srcAttr`` (default: ``"data-src"``): The attribute, which should be transformed to ``src``/``srcset``. (The extension will also automatically check the ``lazySizesConfig.srcsetAttr`` and ``lazySizesConfig.srcAttr``)
* ``lazySizesConfig.rias.widths`` (``array of numbers``): The widths option reduces the calculated ``width`` to the allowed widths. The numeric width can also be simply mapped to a string (i.e.: small, medium, large) using the ``widthmap`` option. The default value is the following array: ``[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 1944, 2160, 2376, 2592, 2808, 3024]``.
* ``lazySizesConfig.rias.widthmap`` (``{}``): The widthmap option allows you to simply transform a numeric width to a string.
```js
window.lazySizesConfig = window.lazySizesConfig || {};
window.lazySizesConfig.rias = window.lazySizesConfig.rias || {};
window.lazySizesConfig.rias.widths = [320, 640, 940];
window.lazySizesConfig.rias.widthmap = {
320: 'small',
640: 'medium',
940: 'large'
};
```
* ``lazySizesConfig.rias.modifyOptions`` (default: ``function`` noop ): A ``function`` that gets an data object passed with the options as the ``details`` and the corresponding ``img`` element as the ``target``. Can be used to modify existing options/placeholder values or to add new placeholder values. An event with the name ``lazyriasmodifyoptions`` is also fired at the element.
```html
```
* ``lazySizesConfig.rias.absUrl`` (default: ``false``): Wether the value of the ``data-src`` attribute should be resolved to an absolute url. The value must not contain any placeholders in this case. Use in conjunction with ``prefix`` and/or ``postfix`` option.
```html
```
* ``lazySizesConfig.rias.prefix`` (default: ``""``): A string, which is prepended to the generated src.
```html
```
* ``lazySizesConfig.rias.postfix`` (default: ``""`` ): A string, which is appended to the generated src.
With lazySizes + RIaS extension you have a script to rule them all. You won't need to include a script provided by a third party image on demand service.
### Dynamically calculating the height of image elements
You can provide a `apsectratio` option which then will be used to dynamically replace `{height}` placeholders.
```html
```
In case your image has CSS defined width and height dimensions you can provide this dynamically:
```js
document.addEventListener('lazyriasmodifyoptions', function(e) {
e.detail.aspectratio = e.target.offsetWidth / e.target.offsetHeight;
});
```
## Advanced Examples
### Embedding via CDN and combohandler
In case you want to use a CDN you can use jsDelivr's combohandler service:
```html
```
### Using art direction
In case you want to use art direction simply also use placeholder urls inside of your ``source[data-srcset]`` or ``source[data-src]`` attributes.
```html
```
### Using different ``widths`` options for different images
Often you will have different image formats with different allowed available ``widths``. This can be configured in two ways:
#### Descriptive way the ``data-widths`` attribute
```html
```
#### Scripted way using the ``lazyriasmodifyoptions`` event
```html
```
### Overriding existing placeholders or Extending new placeholders
The RIaS plugin is highly flexible in extending possible {placeholder} values. Each {placeholder} will be tried to be replaced by searching it in the ``lazySizesConfig.rias`` option object or by searching for a corresponding ``data-*`` attribute.
Additionally the ``modifyOptions`` callback or the equivalent ``lazyriasmodifyoptions`` event can be used to generate new or modify existing placeholders:
**Using ``data-*`` to override an existing placeholder:**
```html
```
**Using ``data-*`` to define a new placeholder:**
```html
```
**Using the ``lazySizesConfig.rias`` object to define a new placeholder:**
```html
```
**Using the ``lazyriasmodifyoptions`` event to define or change a placeholder object:**
```html
```
### Tip: Constraining the pixel density for a generated ``srcset`` attribute.
In case you don't want to generate additional compressive images for high resolution displays you can combine the RIaS extension with the [optimumx extension](../optimumx) to constrain the maximum pixel density for the generated ``srcset`` list.
```html
```