npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-responsive-image

v1.0.1

Published

Load properly sized images in an Angular app using the Angular CDK BreakpointObserver to change image sources on breakpoint changes.

Downloads

53

Readme

ngx responsive image

Load properly sized images in an Angular app using the Angular CDK BreakpointObserver to change image sources on breakpoint changes.

Install

npm install --save ngx-responsive-image@latest @angular/cdk@latest

Getting started

Import the NgxResponsiveImageModule and the Angular CDK ObserversModule to app.module.ts.

import { ObserversModule } from '@angular/cdk/observers';
import {
  DEFAULT_BREAKPOINTS,
  DEFAULT_WIDTHS,
  NgxResponsiveImageModule
} from 'ngx-responsive-image';

@NgModule({
  declarations: [AppComponent],
  imports: [
    ObserversModule,
    NgxResponsiveImageModule.forRoot(DEFAULT_BREAKPOINTS, DEFAULT_WIDTHS)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

Making an image responsive

First if the image is on a component in a vhild module import NgxResponsiveImageModule into the module (without calling forRoot()).

Add the responsiveImage directive to the img

<img responsiveImage />

If you want a placeholder/default image, which is recommened if you are using Universal set the src atributes to the placeholder url.

<img responsiveImage src="assets/images/placeholder.jpg" />

Set the imgSrc attribute to the CDN url of the image replace the static width value with :width

<img
  responsiveImage
  src="assets/images/placeholder.jpg"
  imgSrc="http://imageflare/image1/:width"
/>

If you a binding to a component property in that case remember to use [imgSrc]

If you are using custom breakpoints

You can set custom breakpoints when call NgxResponsiveImageModule.forRoot() be sure to supply the correpsonding image widths to be used in the same order as the breakpoints.

@NgModule({
  declarations: [AppComponent],
  imports: [
    NgxResponsiveImageModule.forRoot(
      [Breakpoints.HandsetPortrait, Breakpoints.HandsetLandscape],
      [600, 960]
    )
  ],
  providers: [],
  bootstrap: [AppComponent]
})

If your url requirements are more complicated than just replacing the width

Set the manual attribute to true on the image(s) and use the breakpointUp event.

<img
  imgSrc="http://localhost:4000/cdn/banner/:width"
  src="assets/images/placeholder.jpg"
  responsiveImage
  [manual]="true"
  alt="test manual image"
  (breakpointUp)="updateImage($event, img)"
  mat-card-image
  #img
/>
updateImage(event: BreakpointChangeEvent, img: HTMLImageElement) {
  this.renderer2.setAttribute(
    img,
    'src',
    event.imgSrc.replace(':width', event.width.toString())
  );
}