Skip to content

Instantly share code, notes, and snippets.

@wangpin34
Last active November 21, 2019 07:43
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 wangpin34/7d8f8b9ac27a6cc4f48d5931a236216d to your computer and use it in GitHub Desktop.
Save wangpin34/7d8f8b9ac27a6cc4f48d5931a236216d to your computer and use it in GitHub Desktop.

单位

inch(英寸)

pixel(像素)

ppi (Pixel Per Inch)

ppi,每英寸像素数量。

pix -> ppi

根据像素和屏幕尺寸计算 ppi。

/*
 * @width: number. The pix numbers of screen width
 * @height: number. The pix numbers of screen height
 * @screen: number. The screen size(inches, like iphone 6,4.7 inches)
 */
function ppi(width, height, screen) {
  return Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / screen
}

dpi(Dot Per Inch)

每英寸像素数量,俗称像素密度。

dip/dp(Device Independent Pixels)

设备独立像素。

dpr(Device Pixel Ratio)

设备像素比,表示物理像素和设备独立像素的比值:pixels/dip。web 端通过以下代码获取当前设备的 dpr。

window.devicePixelRatio

media query 支持类似的 selector:

@media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2){ }

参考文献

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment