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

imgix-optimizer

v0.0.9

Published

imgix Optimizer ==========

Downloads

13

Readme

imgix Optimizer

imgix Optimizer helps you optimize your application's performance by intelligently loading your image assets through imgix.

The optimizer's primary responsibility is to take small "placeholder" images you have loaded onto the page and load the appropriately-sized version after the page has been first painted.

Requirements

This JavaScript libary relies on two other libraries:

This also assumes your project is already configured to work with imgix.

Installation

Once your requirements are in place, you can install the script as an NPM package or a ruby gem.

NPM Package

Use the npm command-line utility to install this package and its dependencies:

$ npm i imgix-optimizer --save

Ruby Gem

If working with a Ruby project, add the gem to your Gemfile:

gem 'imgix-optimizer'

And then install with Bundler:

$ bundle install

Usage

Once your requirements are in place and the package is installed you can load the script on your page. The optimizer script should be loaded after its dependencies.

<body>
  <!-- ... -->

  <!-- Dependencies -->
  <script src="jquery.js"></script>
  <script src="imgix.js"></script>

  <!-- imgix Optimizer -->
  <script src="imgix-optimizer.js"></script>
</body>

Note: src attribute in the example is just an example -- your source paths and filenames are likely different.

Once the scripts are loaded you can initialize the optimizer. This will loop through all images you have designated to be optimized and perform its magic. (See below for designating images.)

new Imgix.Optimizer();

Optimizing Images

There are two steps in setting up an image to be optimized:

  1. Set the placeholder.
  2. Add data attribute.

These two steps differ depending on whether the image is presented as a background image or inline.

Placeholder Image

One major benefit of imgix is that images can be resized on the fly simply by adding a few parameters. For example, I can take an image with a source of https://images.unsplash.com/photo-1487222444179-52db5bc15efe and present a 100px x 100px cropped version of itself with the w, h, and fit parameters, such that a new source of https://images.unsplash.com/photo-1487222444179-52db5bc15efe?w=100&h=100&fit=crop yields this:

Given this flexibility, we can add pixelated placeholder images by loading really small images and stretching them to fill their space. And when we say small, we mean small. Your placeholder image does not need to be larger than 20px unless you have a specific need in which you are looking for more definition in the initial load (although that will slow your page load time down).

So, for example, if you'd like to load a square placeholder image using the above source, you might make it 10px x 10px, like so:

<img src="https://images.unsplash.com/photo-1487222444179-52db5bc15efe?w=10&h=10&fit=crop" class="my-placeholder">
.my-placeholder {
  width: 250px;
}

The trick is that your placeholder image should be stretched to the size at which you ultimately want it displayed using CSS.

Here's an example of what that might look like:

Inline Images

For inline images, set the source directly as the placeholder image and add the data-optimize-img attribute.

<img src="//images.unsplash.com/photo-1487222444179-52db5bc15efe?w=10&h=10&fit=crop" data-optimize-img>

Hint: As mentioned, make sure you're setting the width of this image to match that of what the full-size image would be such that it stretches this placeholder and enables a smooth transition.

Background Images

For background images, the placeholder image should be the background-image CSS property and the data attribute is data-optimize-bg-img.

<div style="background-image: url('//images.unsplash.com/photo-1487222444179-52db5bc15efe?w=10&h=10&fit=crop')" data-optimize-bg-img></div>

License

This project is distributed under the MIT license.