Skip to Content
ohooks

useDevicePixelRatio

Tracks the current window.devicePixelRatio reactively across screen changes and browser zoom levels.

Loading preview...

The useDevicePixelRatio hook tracks window.devicePixelRatio using useSyncExternalStore and dynamic (resolution: ...dppx) media queries so components update automatically when the window moves between displays or the browser zoom level changes.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useDevicePixelRatio.json

Usage

Examples

Scaling an HTML Canvas for Retina Displays

Multiply the canvas backing store dimensions by dpr so drawings stay crisp on high-DPI screens.

Selecting High-Resolution Assets

Use dpr to pick appropriately scaled image URLs or icons based on the active screen density.

API Reference

UseDevicePixelRatioOptions

Configuration options for useDevicePixelRatio.

Property
Specification
defaultValue?
numberdefault:1
Fallback device pixel ratio used during SSR