Since in almost every project I am working on, there are the same requirements/wishes for showing Images, I decided to develop exactly that and pack as a angular-component.
These requirements are:
Load the images only if they are needed. In fact nothing new here, but almost any solution I came across is using one IntersectionObserver per Image. I want to use One IntersectionObserver for ALL Images
The Images should be preloaded before injecting them into the DOM.
In Browsers that support new Image().decode()
it should be used, as this is
massively reducing the load on the main-thread.
I want to be able to switch Images depending on the current screensize.
Of coursethe first thought is to use the shiny <picture>
element, but because
its not possible to get notified about changes of currentSrc
BEFORE the image
is injected into the DOM (and therefore it's not possible to preload or decode)
this dies not meet the requirements.
Because my layouts are responsive anyways, and in every project I make use of
FlexLayoutModule, the points to switch Images should match the breakpoints
in FlexLayoutMoule.
For performance reasons I want to use ChangeDetectionStrategy.OnPush
You can install with yarn, npm or ng
yarn add @mralexandernickel/ngx-picture
npm install @mralexandernickel/ngx-picture
ng add @mralexandernickel/ngx-picture
<lib-ngx-picture
[images]="item"
[fallbackImage]="fallbackImage"
></lib-ngx-picture>
The Images property/input has to be of type INgxPictureSet | string
.
(..to be continued...)
Here you can find a simple Demo Application
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Run ng generate component component-name
to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module
.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory. Use the --prod
flag for a production build.
Run ng test
to execute the unit tests via Karma.
Run ng e2e
to execute the end-to-end tests via Protractor.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI README.