Skip to content

UnitConversion.js

Travis CI User edited this page Sep 24, 2018 · 1 revision

Functions

cmToPx(cm)number

Converts CSS centimeters to CSS pixels. Pixels are the unit you want to work with in JavaScript.

mmToPx(mm)number

Converts CSS millimeters to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

qToPx(Q)number

Converts CSS quarters of millimeters to CSS pixels. The Q unit is an experimental unit and is not supported in most browsers. Pixels are the length unit you want to work with in JavaScript.

inToPx(inches)number

Converts CSS inches to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

pcToPx(pc)number

Converts CSS picas to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

ptToPx(pt)number

Converts CSS points to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

toPx(val, unit)number

Converts CSS s to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

stringToPx(val)number | void

Parses and converts CSS s in strings to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

cmToPx(cm) ⇒ number

Converts CSS centimeters to CSS pixels. Pixels are the unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value in px
See: https://mzl.la/2xAwniR

Param Type Description
cm number value that will be converted to px

Example

UnitConversion.cmToPx(0) //is 0
  UnitConversion.cmToPx(2.54/96) //is 1

mmToPx(mm) ⇒ number

Converts CSS millimeters to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value in px
See: https://mzl.la/2LiyyK3

Param Type Description
mm number value that will be converted to px

Example

UnitConversion.mmToPx(0) //is 0
  UnitConversion.mmToPx(25.4/96) //is 1

qToPx(Q) ⇒ number

Converts CSS quarters of millimeters to CSS pixels. The Q unit is an experimental unit and is not supported in most browsers. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value in px
See: https://mzl.la/2LPH9Vr

Param Type Description
Q number value that will be converted to px

Example

UnitConversion.qToPx(0) //is 0
  UnitConversion.qToPx(101.6/96) //is 1

inToPx(inches) ⇒ number

Converts CSS inches to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value in px
See: https://mzl.la/2LRYRrv

Param Type Description
inches number value that will be converted to px

Example

UnitConversion.inToPx(0) //is 0
  UnitConversion.inToPx(1/96) //is 1

pcToPx(pc) ⇒ number

Converts CSS picas to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value in px
See: https://mzl.la/2kGAJf8

Param Type Description
pc number value that will be converted to px

Example

UnitConversion.pcToPx(0) //is 0
  UnitConversion.pcToPx(6/96) //is 1

ptToPx(pt) ⇒ number

Converts CSS points to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value in px
See: https://mzl.la/2kGBk0m

Param Type Description
pt number value that will be converted to px

Example

UnitConversion.ptToPx(0) //is 0
  UnitConversion.ptToPx(72/96) //is 1

toPx(val, unit) ⇒ number

Converts CSS s to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number - input value converted to px
Throws:

  • Error throws ' is not a known unit' if the unit isn't supported (yet). In case you need other units, please contribute.
Param Type Description
val number value that will be converted to px
unit string unit of the input value

Example

UnitConversion.toPx(72/96, 'pt') //is 1
  UnitConversion.toPx(0.00000001, 'light-years') //throws an error

stringToPx(val) ⇒ number | void

Parses and converts CSS s in strings to CSS pixels. Pixels are the length unit you want to work with in JavaScript.

Kind: global function
Returns: number | void - input value converted to px
Throws:

  • Error throws "can't parse value" if the string isn't formatted well.

Todo

  • bug: numbers with '.' don't work. Change Regex
Param Type Description
val string length string from a CSS property

Example

UnitConversion.stringToPx('0pt') //is 0
  UnitConversion.stringToPx('ABC0.01DEF') //throws an error