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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fit.js

v1.0.0

Published

Fit things into other things

Downloads

517

Readme

Fit.js

fit( theThing, /* into */ theOtherThing );

Fit.js has a very simple purpose and that is to help you fit things into other things. The concept it probably best illustrated by the demo page.

tl;dr Here's a fiddle showing how to use it.


Here are a few simple examples, that should be quite self explanatory.

Given some markup:

<div id="foo">
    <div id="bar"></div>
</div>

You might want to fit bar into foo whilst maintaining it's original aspect ratio

fit( bar, foo );

You can control how it fits, like this

fit( bar, foo, { cover: true } );

Or this

fit( bar, foo, { hAlign: fit.RIGHT } );

or any combination of the options below

fit( bar, foo, {
    
    // Alignment
    hAlign: fit.CENTER, // or fit.LEFT, fit.RIGHT
    vAlign: fit.CENTER, // or fit.TOP, fit.BOTTOM
    
    // Fit within the area or fill it all (true)
    cover: false,

    // Fit again automatically on window resize
    watch: false,
    
    // Apply computed transformations (true) or just
    // return the transformation definition (false)
    apply: true
});

You can also pass a callback and use this to transform bar in whatever way you wish. The callback is passed a transform object that contains all the information you should need.

Here's an example of fitting text by setting the font size of bar such that it fills foo

fit( bar, foo, function( transform ) {
    var style = window.getComputedStyle( bar );
    var size = parseFloat( style.fontSize );
    bar.style.fontSize = size * transform.scale + 'px';
});

You can also simply use one of the built in methods of transforming DOM elements

// Translates and scales the object (default)
fit( bar, foo, fit.cssTransform );

// uses left, top, width and height
fit( bar, foo, fit.cssPosition );

// uses margin-left, margin-top, width and height
fit( bar, foo, fit.cssMargin );

But fit.js was designed to be used with any kind of rectangular object, not just DOM elements

var area = { x: 20, y: 20, width: 400, height: 300 };
var rect = { x: 0, y: 0, width: 100, height: 120 };
fit( rect, area );

If you are using the DOM, you can tell fit to run again whenever the window resizes. To do this, simply set the watch option to true

// This will trigger a fit each time the window resizes
var watching = fit( bar, foo, { watch: true } );

// You can stop watching at any time
watching.off();

// And start watching again
watching.on();

// And trigger a fit manually too
watching.trigger();

For some visual examples, check out the demo page.

Why?

Sometimes, the standard CSS properties just can't manipulate elements to fit in the way you want them to, or your fit methods need to be more dynamic, or you're not using the DOM at all and need to perform these computations more abstractly. Either way, if you've ever needed to work out how an object of any size should fit into another of any size, or find yourself writing the same code to do this over and over again, you'll understand how fit.js might be useful.

Appendix

If you contribute to the script (thanks!) then generate a minified version like so (assuming you have installed uglifyjs):

uglifyjs fit.js --comments /copy/i -cmo fit.min.js