Last active
March 29, 2022 21:52
-
-
Save dotproto/8f0b3e39215f002a0908be3d3122ce88 to your computer and use it in GitHub Desktop.
Calculate pixel height in inches/mm at a given distance
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
/** | |
* @param {number} srcPixels Number of pixels used when rendering on a standard desktop dispaly. Defaults to 1 pixel | |
* @param {number} distance Distance in inches at which the item is rendered. Defaults to 28 inches (distance specified in the CSS spec) | |
* | |
* https://www.w3.org/TR/css-values-4/#reference-pixel | |
*/ | |
function getProjectedPixel({pixels = 1, distance = 28} = {}) { | |
const inToMm = (inch) => inch * 25.4; | |
const opposite = distance; | |
const srcPixels = pixels; | |
const refAdjacent = 28; // inches from viewer | |
const refOpposite = srcPixels/96; // height in inches of pixels | |
const refAngle = Math.atan2(refOpposite, refAdjacent);// 180 / Math.PI; | |
const result = { | |
distance: { | |
inches: distance, | |
mm: inToMm(distance), | |
}, | |
angle: { | |
radians: refAngle, | |
degress: refAngle * 180 / Math.PI, | |
}, | |
height: { | |
inches: Math.tan(refAngle) * opposite, | |
mm: inToMm(Math.tan(refAngle) * opposite), | |
}, | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment