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

ember-text-measurer

v0.6.0

Published

Ember addon with a service to calculate text dimensions on a canvas

Downloads

147,231

Readme

Build Status

Ember-text-measurer

This addon provides a very simple service to measure the width of a string using an in-memory canvas, so it doesn't cause any layout reflow for max performance.

Installation

ember install ember-text-measurer

Usage

This addon just provides a service that you can inject wherever you need.

The service for now has three methods:

  • width(string, font = null) will return the width of the text with the given font information.
textMeasurer.width('foobar', '24px Arial');             // ~ 68.02px
textMeasurer.width('foobar', '20px Arial');             // ~ 56.64px
textMeasurer.width('foobar', '20px Times New Roman');   // ~ 52.19px
  • fitTextSize(string, width, font = null) will return the font size with which the given text will fully fit in the available width with the given (option) font information.
textMeasurer.fitTextSize('foobar this is too long', 200, 'normal 24px Times'); // 22px
textMeasurer.fitTextSize('foobar this is too long', 200, 'normal 24px Helvetica'); // 21px
textMeasurer.fitTextSize('foobar this is too long', 200, 'normal 24px Georgia'); // 20px

Note: The font size passed to this method is irrelevant, the important part is the font style and family.

  • lines(string, width, font = null) will return the number of lines that this text would require when rendered in a container of the given width with the given font.
const sampleMultilineText = `Lorem
ipsum dolor sit amet, ex legimus mandamus sea, qui no doctus option. Ei pri commune maiestatis. Mea at facete appetere tincidunt. Et sea quaestio expetendis. No eius virtute delenit per.


Ea mel error latine, usu harum delicata forensibus id, ut est probo quodsi regione. Sumo definitiones ex has, percipit voluptatum an qui. Eius solet aeterno sea ut, qui ex inani persequeris. In nostro facilis consetetur mea. Ut audiam virtute nostrum eam, omnes luptatum splendide eam at.

Veri zril ex vel, pri habemus delicata et. Te minimum expetenda dissentiet est, homero omnium expetenda no pri, enim fuisset usu ei. Mel ex quidam scripserit, pri aliquip debitis id. Vis ea legere persius recteque, utamur blandit volutpat ea vel.`;

textMeasurer.lines(sampleMultilineText, 400, 'normal 24px Helvetica'); // 26 lines
textMeasurer.lines(sampleMultilineText, 600, 'normal 24px Helvetica'); // 19 lines
textMeasurer.lines(sampleMultilineText, 600, 'normal 14px Helvetica'); // 14 lines

Please note than measuring the lines is significantly more expensive than measuting the width and might take a non-negligible amount of time when performed on text over a few thousands words, and results might not be accurate on browsers that don't have subpixel precission on text measurements

What can I do with this?

Check THE DEMO for some ideas ;-)

Browser compatibility

Pretty good, in theory all back to IE9, although IE9 might give slightly unnacurate results, probably not innacurate enough to be a problem.

Caveats

  • It doesn't work in Fastboot because there's no canvas support in node. If you are very interested in this feature, it is proably doable using in some library like node-canvas PRs welcomed.
  • If you are using a custom font and you perform the measurement before it is fully loaded, the result will be innacurate.