Skip to content

Instantly share code, notes, and snippets.

@DKunin
Created June 14, 2017 13:54
Show Gist options
  • Save DKunin/f78a8ca6abab6333a77a8702a4c7fdd7 to your computer and use it in GitHub Desktop.
Save DKunin/f78a8ca6abab6333a77a8702a4c7fdd7 to your computer and use it in GitHub Desktop.
getPPI
function getPPI(){
// create an empty element
var div = document.createElement("div");
// give it an absolute size of one inch
div.style.width="1in";
// append it to the body
var body = document.getElementsByTagName("body")[0];
body.appendChild(div);
// read the computed width
var ppi = document.defaultView.getComputedStyle(div, null).getPropertyValue('width');
// remove it again
body.removeChild(div);
// and return the value
return parseFloat(ppi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment