2015-02-20 04:10:52 +00:00
|
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-02-20 04:10:52 +00:00
|
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
|
* @format
|
2015-03-24 00:09:14 +00:00
|
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
|
const Dimensions = require('Dimensions');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* PixelRatio class gives access to the device pixel density.
|
|
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* ## Fetching a correctly sized image
|
2015-02-20 04:10:52 +00:00
|
|
|
|
*
|
|
|
|
|
* You should get a higher resolution image if you are on a high pixel density
|
|
|
|
|
* device. A good rule of thumb is to multiply the size of the image you display
|
|
|
|
|
* by the pixel ratio.
|
|
|
|
|
*
|
2015-03-16 16:37:45 +00:00
|
|
|
|
* ```
|
|
|
|
|
* var image = getImage({
|
2015-04-21 16:18:51 +00:00
|
|
|
|
* width: PixelRatio.getPixelSizeForLayoutSize(200),
|
|
|
|
|
* height: PixelRatio.getPixelSizeForLayoutSize(100),
|
2015-03-16 16:37:45 +00:00
|
|
|
|
* });
|
|
|
|
|
* <Image source={image} style={{width: 200, height: 100}} />
|
|
|
|
|
* ```
|
2017-10-10 00:37:08 +00:00
|
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* ## Pixel grid snapping
|
2017-10-10 00:37:08 +00:00
|
|
|
|
*
|
|
|
|
|
* In iOS, you can specify positions and dimensions for elements with arbitrary
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* precision, for example 29.674825. But, ultimately the physical display only
|
|
|
|
|
* have a fixed number of pixels, for example 640×960 for iPhone 4 or 750×1334
|
2017-10-10 00:37:08 +00:00
|
|
|
|
* for iPhone 6. iOS tries to be as faithful as possible to the user value by
|
|
|
|
|
* spreading one original pixel into multiple ones to trick the eye. The
|
|
|
|
|
* downside of this technique is that it makes the resulting element look
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* blurry.
|
2017-10-10 00:37:08 +00:00
|
|
|
|
*
|
|
|
|
|
* In practice, we found out that developers do not want this feature and they
|
|
|
|
|
* have to work around it by doing manual rounding in order to avoid having
|
|
|
|
|
* blurry elements. In React Native, we are rounding all the pixels
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* automatically.
|
2017-10-10 00:37:08 +00:00
|
|
|
|
*
|
|
|
|
|
* We have to be careful when to do this rounding. You never want to work with
|
|
|
|
|
* rounded and unrounded values at the same time as you're going to accumulate
|
|
|
|
|
* rounding errors. Having even one rounding error is deadly because a one
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* pixel border may vanish or be twice as big.
|
2017-10-10 00:37:08 +00:00
|
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* In React Native, everything in JavaScript and within the layout engine works
|
2017-10-10 00:37:08 +00:00
|
|
|
|
* with arbitrary precision numbers. It's only when we set the position and
|
|
|
|
|
* dimensions of the native element on the main thread that we round. Also,
|
|
|
|
|
* rounding is done relative to the root rather than the parent, again to avoid
|
2017-08-04 23:14:25 +00:00
|
|
|
|
* accumulating rounding errors.
|
2017-10-10 00:37:08 +00:00
|
|
|
|
*
|
2015-02-20 04:10:52 +00:00
|
|
|
|
*/
|
|
|
|
|
class PixelRatio {
|
2015-03-16 16:37:45 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the device pixel density. Some examples:
|
|
|
|
|
*
|
2015-07-27 16:25:19 +00:00
|
|
|
|
* - PixelRatio.get() === 1
|
|
|
|
|
* - mdpi Android devices (160 dpi)
|
|
|
|
|
* - PixelRatio.get() === 1.5
|
|
|
|
|
* - hdpi Android devices (240 dpi)
|
2015-03-16 16:37:45 +00:00
|
|
|
|
* - PixelRatio.get() === 2
|
|
|
|
|
* - iPhone 4, 4S
|
|
|
|
|
* - iPhone 5, 5c, 5s
|
|
|
|
|
* - iPhone 6
|
2015-07-27 16:25:19 +00:00
|
|
|
|
* - xhdpi Android devices (320 dpi)
|
2015-03-16 16:37:45 +00:00
|
|
|
|
* - PixelRatio.get() === 3
|
|
|
|
|
* - iPhone 6 plus
|
2015-07-27 16:25:19 +00:00
|
|
|
|
* - xxhdpi Android devices (480 dpi)
|
2015-04-21 16:18:51 +00:00
|
|
|
|
* - PixelRatio.get() === 3.5
|
|
|
|
|
* - Nexus 6
|
2015-03-16 16:37:45 +00:00
|
|
|
|
*/
|
2015-03-24 00:09:14 +00:00
|
|
|
|
static get(): number {
|
2015-02-20 04:10:52 +00:00
|
|
|
|
return Dimensions.get('window').scale;
|
|
|
|
|
}
|
2015-04-21 16:18:51 +00:00
|
|
|
|
|
2015-06-01 17:19:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the scaling factor for font sizes. This is the ratio that is used to calculate the
|
|
|
|
|
* absolute font size, so any elements that heavily depend on that should use this to do
|
|
|
|
|
* calculations.
|
|
|
|
|
*
|
|
|
|
|
* If a font scale is not set, this returns the device pixel ratio.
|
|
|
|
|
*
|
|
|
|
|
* Currently this is only implemented on Android and reflects the user preference set in
|
|
|
|
|
* Settings > Display > Font size, on iOS it will always return the default pixel ratio.
|
2015-07-27 16:25:19 +00:00
|
|
|
|
* @platform android
|
2015-06-01 17:19:25 +00:00
|
|
|
|
*/
|
|
|
|
|
static getFontScale(): number {
|
|
|
|
|
return Dimensions.get('window').fontScale || PixelRatio.get();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-21 16:18:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Converts a layout size (dp) to pixel size (px).
|
|
|
|
|
*
|
|
|
|
|
* Guaranteed to return an integer number.
|
|
|
|
|
*/
|
|
|
|
|
static getPixelSizeForLayoutSize(layoutSize: number): number {
|
|
|
|
|
return Math.round(layoutSize * PixelRatio.get());
|
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
2016-01-15 13:14:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* Rounds a layout size (dp) to the nearest layout size that corresponds to
|
|
|
|
|
* an integer number of pixels. For example, on a device with a PixelRatio
|
|
|
|
|
* of 3, `PixelRatio.roundToNearestPixel(8.4) = 8.33`, which corresponds to
|
|
|
|
|
* exactly (8.33 * 3) = 25 pixels.
|
|
|
|
|
*/
|
|
|
|
|
static roundToNearestPixel(layoutSize: number): number {
|
2018-05-10 22:44:52 +00:00
|
|
|
|
const ratio = PixelRatio.get();
|
2016-01-15 13:14:27 +00:00
|
|
|
|
return Math.round(layoutSize * ratio) / ratio;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 21:22:22 +00:00
|
|
|
|
// No-op for iOS, but used on the web. Should not be documented.
|
|
|
|
|
static startDetecting() {}
|
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
|
|
module.exports = PixelRatio;
|