Skip to content

Instantly share code, notes, and snippets.

@dotproto
Last active March 29, 2022 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotproto/8f0b3e39215f002a0908be3d3122ce88 to your computer and use it in GitHub Desktop.
Save dotproto/8f0b3e39215f002a0908be3d3122ce88 to your computer and use it in GitHub Desktop.
Calculate pixel height in inches/mm at a given distance
/**
* @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