Window - DevicePixelRatio Property - Web APIs - MDN
Window - DevicePixelRatio Property - Web APIs - MDN
Value
A double-precision floating-point value indicating the ratio of the display's resolution in physical pixels to the resolution in CSS pixels. A
value of 1 indicates a classic 96 DPI display, while a value of 2 is expected for HiDPI/Retina displays.
Other values may be returned in the case of unusually low resolution displays or, more often, when a screen has a higher pixel density
than double the standard resolution of 96 DPI. Modern mobile device screens - which offer high display resolutions at small physical sizes
- often yield a devicePixelRatio value greater than 2.
Examples
Correcting resolution in a <canvas>
A <canvas> can appear too blurry on retina screens. Use window.devicePixelRatio to determine how much extra pixel density should be
added to allow for a sharper image.
HTML
HTML
<canvas id="canvas"></canvas>
JavaScript
JS
// Set actual size in memory (scaled to account for extra pixel density).
const scale = window.devicePixelRatio; // Change to 1 on retina screens to see blurry canvas.
canvas.width = Math.floor(size * scale);
canvas.height = Math.floor(size * scale);
ctx.fillStyle = "#bada55";
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#monitoring_screen_resolution_or_zoom_level_changes 1/4
28/2/24, 11:32 Window: devicePixelRatio property - Web APIs | MDN
ctx.fillRect(10, 10, 300, 300);
ctx.fillStyle = "#ffffff";
ctx.font = "18px Arial";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
const x = size / 2;
const y = size / 2;
<div id="container">
<p>
This example demonstrates the effect of zooming the page in and out
(or moving it to a screen with a different scaling factor) on the
value of the <code>devicePixelRatio</code> property.</p>
<p>Try it and watch what happens!</p>
</p>
</div>
<div id="output"></div>
CSS
CSS Play
body {
font:
22px arial,
sans-serif;
}
#container {
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#monitoring_screen_resolution_or_zoom_level_changes 2/4
28/2/24, 11:32 Window: devicePixelRatio property - Web APIs | MDN
border: 2px solid #22d;
margin: 1rem auto;
padding: 1rem;
background-color: #a9f;
}
JavaScript
The string mqString is set to a media query which checks to see if the current display resolution matches a specific number of device dots
per px .
The media variable is a MediaQueryList object that's initialized with the media query string. When the result of running mqString against the
document changes, the media object's change event fires, and the code logs the new resolution.
Note that every time the resolution changes, the example has to create a new media query, based on the new resolution, and a new
MediaQueryList instance.
JS Play
updatePixelRatio();
Result
To test the example, try zooming the page in and out, and note the difference in the logged value of devicePixelRatio .
Play
Specifications
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#monitoring_screen_resolution_or_zoom_level_changes 3/4
28/2/24, 11:32 Window: devicePixelRatio property - Web APIs | MDN
Specification
CSSOM View Module
# dom-window-devicepixelratio
Browser compatibility
Report problems with this compatibility data on GitHub
diordnA arepO
emorhC
xoferiF
arepO
irafaS
egdE
Chrome 1 Edge 12 Firefox 18 Opera 11.1 Safari 3 Chrome 18 Firefox 18 Opera 11.1
devicePixelRatio Android for Android
Android
Tip: you can click/tap on a cell for more information.
Full support
See also
Media queries
Using media queries
CSS resolution media query
The image-resolution property
This page was last modified on Dec 13, 2023 by MDN contributors.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#monitoring_screen_resolution_or_zoom_level_changes 4/4