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

pixel-perfect.js

v1.2.0

Published

Display Images pixel perfect

Downloads

3

Readme

pixel-perfect.js

Display images pixel-perfect

See example

Note:

It looks like image-resolution: snap CSS is supposed to do exactly what this library does. As if July 25th 2022 no browser has implemented it.

How to use

In your image tags that you want to be pixel perfect give them the class pixel-perfect and set a CSS size that is some integer multiple of your image.

Example:

<img src="100x150.png" class="pixel-perfect" style="width: 400px; height 600px">

Here we want the image displayed 4x it's original size. The reason to do it this way is so if the user has JavaScript disabled they'll still a size that matches your design.

Define your pixel-perfect class as follows

.pixel-perfect {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

Include this script on your page

<script type="module" src="pixel-perfect.js"></script>;

Done!

Canvas

For <canvas> set both a width and height and a CSS width and height. Example:

<canvas width="200" height="100" class="pixel-perfect" style="width: 400px; height: 200px;"></canvas>

Note: as it says on the main page, this solution only works for designs that allow the canvas to change its display size relative to other elements. For example the canvas above is 200x100 pixels. To stay pixel perfect it can not go smaller, and going larger it must change to integer multiples like 400x200, 600x300, 800x400, etc...

If you have a canvas that fits a container or the entire page or that's required to have a fixed size then you need a different solution.

Special use cases

You can also import pixel-perfect.js as an es6 module.

import {makePixelPerfect} from './pixel-perfect.js';

You can then call makePixelPerfect on any <img> or <canvas> element.

For example:

import {makePixelPerfect} from './pixel-perfect.js';

function makeAllPixelPerfect() {
  document.querySelectorAll(".pixel-perfect").forEach(makePixelPerfect);
}
window.addEventListener('resize', makeAllPixelPerfect);
makeAllPixelPerfect();

License

MIT