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

shrink-to-fit

v1.0.0

Published

Automatically shrink text to fit on a specified number of lines at a specified width.

Downloads

167

Readme

shrink-to-fit

Have you ever recoiled in terror when a designer has asked you to make text fit on a maximum of two lines? Just shrink the font size if you need to, that's simple, right?

With shrink-to-fit, it is!

Short demonstration

Quickstart

Add the shrink-to-fit package to your project:

$ npm install --save shrink-to-fit

Then use it to determine font size:

import shrinkToFit from 'shrink-to-fit';


const target = document.getElementById('text-container');
const widthPx = target.clientWidth;

const text = "I am the very model of a modern Major-General. " +
             "I've information vegetable, animal, and mineral.";
const fittingFontSize = shrinkToFit(text, widthPx);

target.style.fontSize = `${fittingFontSize}px`;
target.innerText = text;

Reference

The shrinkToFit function accepts two required arguments: text and widthPx. text is a string representing the text you want to display. widthPx is the width, in pixels, of the box in which you'd like the text to fit.

shrinkToFit also accepts a settings object as an optional third parameter. You can specify any combination of the following properties of the settings object to change the library's behavior, or omit the object entirely to accept the defaults:

  • settings.maxLines (number) Maximum number of lines to which text should wrap. Defaults to 2.
  • settings.fontFamily (string) Font face/family to use. Defaults to sans-serif.
  • settings.startingSizePx (number) Starting font size (in px) to use. Defaults to 90.
  • settings.minSizePx (number) Minimum font size (in px) to drop to. Takes higher priority than maxLines -- i.e. if text can't fit on maxLines lines at minSize, it'll return at minSize anyway. Defaults to 30.

For example, to specify that the text's width should be calculated using the font face Helvetica and contain at most three lines:

const text = "I know the kings of England, and I quote the fights historical " +
             "from Marathon to Waterloo, in order categorical. ";
const widthPx = 400;
const settings = { fontFamily: "Helvetica", maxLines: 3 };
const fittingFontSize = shrinkToFit(text, widthPx, settings);

Code of Conduct

We are committed to fostering an open and welcoming environment. Please read our code of conduct before participating in or contributing to this project.

Contributing

We welcome contributions and collaboration on this project. Please read our contributor's guide to understand how best to work with us.

License and Authors

Syncromatics Engineering logo Syncromatics Engineering

license GitHub contributors

This software is made available by Syncromatics Engineering under the MIT license.