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

@mapbox/appropriate-images-get-url

v1.2.0

Published

Given an appropriate-images configuration, get the URL of the optimized image appropriate for a situation

Downloads

21

Readme

@mapbox/appropriate-images-get-url

Build Status

Use in combination with appropriate-images.

After you've generated resized, optimized images with appropriate-images, you'll want to use them in the browser. In order to do that, you'll need to determine which variant of the image to load — which size, and whether to load the .webp version or not. That's what this module is for. This is how the image configuration used by appropriate-images can be reused in the browser to select the appropriate image to load at runtime.

If you're thinking about using this function in combination with React, check out appropriate-images-react.

Installation

npm install @mapbox/appropriate-images-get-url

API

getAppropriateImageUrl

getAppropriateImageUrl(options)

Uses the image id, image configuration, and width value to figure out the URL of the image variant that should be loaded. Returns a URL for the appropriate image variant that you created with appropriate-images.

The returned URL will account for

  • the available width,
  • the resolution of the screen, and
  • whether or not the browser supports webp.

The image variant that is selected will be the narrowest variant that is at least as wide as the available width, or else, if the available width exceeds all sizes, the widest variant.

options

imageId

Type: string. Required.

Id of the image to be loaded. Image ids correspond to keys in your appropriate-images configuration.

imageConfig

Type: Object. Required.

Your appropriate-images configuration object.

availableWidth

Type number. Default: Infinity.

Not technically required, but you should provide it. This is the width available to the image. This is key to figuring out which size variant to load.

hiResRatio

Type number. Default: 1.3.

The ratio at which you want to consider a screen "high resolution". If the browser judges that the screen is high resolution, according to this ratio, the availableWidth provided will be multiplied by this ratio when determining which size variant to load. This means that in a 300px-wide space but on a Retina screen, the image at least 600px wide will be loaded.

imageDirectory

Type string.

If provided, this will be prepended to the URL.

Examples

const getAppropriateImageUrl = require('@mapbox/appropriate-images-get-url');

const imageConfig = {
  bear: {
    basename: 'bear.png',
    sizes: [{ width: 300 }, { width: 600 }]
  },
  montaraz: {
    basename: 'montaraz.jpg',
    sizes: [
      { width: 600, height: 500 },
      { width: 1200, height: 800, crop: 'north' },
      { width: 200, height: 200, crop: 'southeast' },
    ]
  }
};

getAppropriateImageUrl({ imageConfig, imageId: 'bear', width: 280 });
// On a regular-resolution screen: bear-300.png or webp
// On a high-resolution screen: bear-600.png or webp

getAppropriateImageUrl({ imageConfig, imageId: 'bear', width: 550 });
// bear-600.png or webp

getAppropriateImageUrl({ imageConfig, imageId: 'bear', width: 800 });
// bear-600.png or webp

getAppropriateImageUrl({
  imageConfig,  
  imageId: 'montaraz',
  width: 400,
  imageDirectory: 'img/optimized/'
});
// On a regular-resolution screen: img/optimized/montaraz-600x500.jpg or webp
// On a high-resolution screen: img/optimized/montaraz-1200x800.jpg or webp