Coder Social home page Coder Social logo

fontmetrics.js's Introduction

This library updates the canvas2d ctx.measureText(<String>) function
so that it returns not just a string's width, but also its height,
ascent wrt the baseline, descent wrt the baseline, the leading distance
(the number of pixels between the baselines of two consecutive lines
of text, pronounced like the metal "lead", not like the management
"leader"), as well as the string's bounding box.

Simply include on a page as:

  <script type="text/javascript" src="fontmetrics.js"></script>

and measureText will return a much more complete object, structured
as follows:

{
  fontsize: <number>, /* as set on the canvas/context */
  width: <number>,    /* typesetting width of the text */
  height: <number>,   /* typesetting height of the text */
  ascent: <number>,   /* height above the baseline for this text */
  descent: <number>,  /* depth below the baseline for this text */
  leading: <number>,  /* the 'lead' (metal) spacing between the baselines of two lines */
  bounds:             /* the bounding box for the rasterised text on a canvas */
  { 
    minx: <number>, /* can be negative */
    miny: <number>, /* can be negative */
    maxx: <number>, /* not necessarily the same as width */
    maxy: <number>  /* not necessarily the same as height */
  }
}

Note: all numbers represent screen pixels (not typographical points)

See http://pomax.github.com/fontmetrics.js/ for a demonstration.

This library is covered by an MIT (Expat flavour) license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.