Last active
December 20, 2015 14:49
-
-
Save bjconlan/6149965 to your computer and use it in GitHub Desktop.
Basic absolute conversions in javascript. Converts a subset of 'http://www.w3.org/TR/css3-values/' in pure javascript. Returns linear measurements in pixels and angles in degrees (perhaps this should be rads...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ppi = 96 * window.devicePixelRatio || 1; // 96 = CSS DPI/PPI reference measurement | |
var absoluteMeasurements = { | |
px : 1, | |
pt : 4 / 3, | |
pc : 12 * (4 / 3), | |
in : ppi, | |
cm : ppi / 2.54, | |
mm : ppi / 25.4, | |
deg: 1, | |
grad: 400 / 360, | |
rad: 180 / Math.PI, | |
turn: 360 | |
}; | |
function parseUnit(value) { | |
var triple = value.match(/([\d|\.]+)(\D*)/); | |
return (triple.length !== 3) ? NaN | |
: absoluteMeasurements[(triple[2] || '').toLowerCase()] * (+triple[1] || 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment