A simple utility to convert from physical lengths (meters, inches, etc) to pixels and back, based on the CSS spec. Supports converting to and from the following units:
pc(pica, or 1 / 6 of an inch)
pt(point, or 1 / 72 of an inch)
ft(feet, or 12 inches)
const convert = ;// Convert 10 inches to metersconst result =// -> 0.254// Convert A4 print (210 x 297 mm) to pixels @ 300 PPIconst result = 210 297;// -> [ 2480, 3508 ]
Pixels are computed based on the specified
pixelsPerInch setting (default 96), as per the CSS spec.
Use npm to install and use this. Should work with browserify, Webpack, etc.
npm install convert-length
result = convert(value, fromUnit, toUnit, [options])
value number from the
fromUnit unit string (e.g.
"in") to the
toUnit unit string. The unit strings are case insensitive.
Options can be:
pixelsPerInch(default 96) the number of pixels in one inch, used when converting to and from
precisionif specified, the value will be rounded to the Nth decimal. e.g. A precision of
3will round to
0.001. If not specified, the result will not be rounded.
roundPixel(default true) If enabled, when converting to a
"px"unit the return value will be rounded to a whole pixel. If disabled, the conversion will instead round to the specified Nth
precisiondecimal (or no rounding if
precisionis not specified).
The list of supported units for this module, equivalent to:
'mm' 'cm' 'm' 'pc' 'pt' 'in' 'ft' 'px'
This module was inspired by measures and convert-units, but I wanted something dead-simple for the browser, without all the extra features, and that supports pixels in the same way CSS and Photoshop handle their conversions.
MIT, see LICENSE.md for details.