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

koa-responsive-image-router

v0.2.37

Published

Koa router that helps to serve responsive images in an SSR app

Downloads

150

Readme

Image Router

Koa router that helps to serve responsive images in an SSR app

KoaResponsiveImageRouter.image() arguments

  • target_ratio - ratio (width divided by height) of an image container.
  • ratio_diff_threshold - describes how much ratio of an image can differ from target_ratio until ratio-crossed-threshold class is added to the image

Both are used to add useful classes to <img> element. See <img> Element classes

<img> Element classes

These help to avoid situations like stripes or uneven whitespace around the image and excessive clipping of images. It is done by setting object-fit values in CSS depending on these classes.

  • horizontal / landscape - added when image width is greater than height
  • vertical / portrait - if image height is greater
  • square - when both dimensions are the same
  • ratio-crossed-threshold - if image ratio differs by at least ratio_diff_threshold from target_ratio (both are KoaResponsiveImageRouter.image() optional arguments)
  • ratio-above-threshold or ratio-below-threshold - added together with ratio-crossed-threshold depending if image ratio is above or below target_ratio

Image Cropping Options

The image() function accepts a crop parameter that allows you to specify cropping options for the displayed image. Use the crop parameter to control how the image is cropped or displayed within the specified dimensions, with consideration for the SmartCrop algorithm.

crop: An object representing cropping options. It can have the following properties:

  • width: The target width for the cropped image. This parameter is used to specify the desired width of the cropped image when using the SmartCrop algorithm.
  • height: The target height for the cropped image. This parameter is used to specify the desired height of the cropped image when using the SmartCrop algorithm.
  • x (optional): The horizontal offset from the left edge of the image. This parameter is used for direct cropping to determine the starting point for cropping horizontally.
  • y (optional): The vertical offset from the top edge of the image. This parameter is used for direct cropping to determine the starting point for cropping vertically..

Example Usage

Here are some examples of how to use the crop parameter in the image() function:

1. Cropping with SmartCrop Algorithm:

${await imageRouter.image({
    resolutions: [600, 1000, 1500, 2000],
    sizes_attr: "(max-width: 600) 100vw, 600px",
    path: paths.exampleSmartCropImg,
    lazy: false,
    img_style: "width: 600px; height: auto",
    crop: { width: 600, height: 600 },
})}

This example utilizes the SmartCrop algorithm to crop the image to a specific width and height.

2. Direct Cropping with Coordinates:

${await imageRouter.image({
    resolutions: [600, 1000, 1500, 2000],
    sizes_attr: "(max-width: 600px) 100vw, 600px",
    path: paths.exampleSmartCropImg,
    lazy: false,
    img_style: "width: 600px; height: auto",
    crop: { width: 2592, height: 3456, x: 2592, y: 0 },
})}

In this example, direct cropping is applied with specific x and y coordinates.

3. Default without cropping

${await imageRouter.image({
    resolutions: [600, 1000, 1500, 2000],
    sizes_attr: "(max-width: 600) 100vw, 600px",
    path: paths.exampleSmartCropImg,
    lazy: false,
    img_style: "width: 600px; height: auto",
})}

When no crop parameter is provided, the image is displayed without cropping.