Angular 8+ directive for the Viewer.js library 🔎
This is a simple Angular 8+ directive wrapping the amazing Viewer.js library.
Add both this library and the original Viewer.js library as dependencies to your project:
yarn add ngx-viewer viewerjs
Import the module into your application’s app.module.ts
:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// import ngx-viewer module
import { NgxViewerModule } from 'ngx-viewer';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgxViewerModule // add ngx-viewer module here
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Finally, you’ll need to import the styles for Viewer.js somehow, for example in your base styles.(s)css:
@import '~viewerjs/dist/viewer.css';
The ngxViewer
directive can be added to a container wrapping multiple images, or to an individual image. The default Viewer.js configuration options will be used.
<img src="https://picsum.photos/2000/1500" ngxViewer>
<div ngxViewer>
<img src="https://picsum.photos/2000/1500/?random=1" alt="Image 1">
<img src="https://picsum.photos/2000/1500/?random=2" alt="Image 2">
<img src="https://picsum.photos/2000/1500/?random=3" alt="Image 3">
</div>
All options exposed by Viewer.js can be set for this directive using [viewerOptions]
:
Template file (HTML)
<img src="https://picsum.photos/2000/1500" ngxViewer [viewerOptions]="viewerOptions">
Component file (TS)
public viewerOptions: any = {
navbar: false,
toolbar: {
zoomIn: 4,
zoomOut: 4,
oneToOne: 4,
reset: 4,
prev: 4,
play: {
show: 4,
size: 'large',
},
next: 4,
rotateLeft: 4,
rotateRight: 4,
flipHorizontal: 4,
flipVertical: 4,
}
};
Note: The Viewer.js defaults are always used as standard, with the exception of the
transition
option.
All events exposed by Viewer.js are made available by the directive as output bindings:
<img src="https://picsum.photos/2000/1500" ngxViewer (viewerReady)="onViewerReady($event)">
The available events are:
See Viewer.js’ docs for more information on each event.
There’s a known issue with the Viewer.js transitions that are normally enabled by default. They work fine in development but break (without errors) whenever building with optimizations active (i.e building for production). To get around this, the directive disables transitions by default. If you want to re-enable them (if, for some reason, you aren’t running optimizations as part of your production builds) you can set transition: true
in your viewerOptions
.
See this issue.
The MIT License
Copyright © 2018, WINTER AGENCY
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.