Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect scale settings (dpi) with JavaScript or CSS

Tags:

javascript

css

I've noticed that a small laptop with a 1920x1080 screen, windows 10 will auto adjust the scaling. I've seen it as high as 150%. Is there a way we can detect this? My media queries don't kick in as they're set in px.

like image 321
Mark Handy Avatar asked Oct 17 '17 13:10

Mark Handy


People also ask

How do I get PPI in Javascript?

js: screenPPI = document. getElementById('ppitest'). offsetWidth; This got me 96, which corresponds to my system's ppi.

How do I set DPI scaling?

Alternatively, right click on an empty area on your desktop and select Display. In System, settings screen click on Displayoption from left side. Under Change the size of text, apps, and other items: 100% (Recommended), move the slider left or right to the DPI percentage you want to set for that display.

How do I get the size of a pixel in CSS?

Try accessing window.devicePixelRatio variable. The Window property devicePixelRatio returns the ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device. This value could also be interpreted as the ratio of pixel sizes: the size of one CSS pixel to the size of one physical pixel.

How can I scale things proportionally with CSS?

CSS alone can’t really do this. But CSS is still the answer! transform: scale (); is what we need. It scales things perfectly proportionally. We just need to give it a number to scale it by, and that scale we can figure out with some JavaScript.

How to get the scale ratio of the screen/page?

You might use the window.devicePixelRatio property to get the scaling ratio of the screen/page. This works well in current IE, Edge, Chrome and Firefox on the desktop (Windows), but it doesn't seem to be a current standard.

How do I get the scale of an object using JavaScript?

We just need to give it a number to scale it by, and that scale we can figure out with some JavaScript. The trick is: var scale = Math.min( availableWidth / contentWidth, availableHeight / contentHeight ); Here’s one possible approach to that, using jQuery and jQuery UI Resizeable (because that’s easy and comfortable for me):


1 Answers

Try accessing window.devicePixelRatio variable.

The Window property devicePixelRatio returns the ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device. This value could also be interpreted as the ratio of pixel sizes: the size of one CSS pixel to the size of one physical pixel. In simpler terms, this tells the browser how many of the screen's actual pixels should be used to draw a single CSS pixel.

More info about it: https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio

You could also use CSS resolution for this, more about this here: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/resolution

@media (resolution: 150dpi) {
  p {
    color: red;
  }
}
like image 129
Martin Adámek Avatar answered Sep 23 '22 20:09

Martin Adámek