Skip to content

Instantly share code, notes, and snippets.

@kashishgrover
Last active December 1, 2018 06:27
Show Gist options
  • Save kashishgrover/460049e1576ef834560627f4c89335d4 to your computer and use it in GitHub Desktop.
Save kashishgrover/460049e1576ef834560627f4c89335d4 to your computer and use it in GitHub Desktop.
Centralized scaling algorithm for React Native
const { width, height } = Dimensions.get('window');
const heightAspect = 568;
const widthAspect= 320;
const verticalScale = height / heightAspect;
const horizontalScale = width / widthAspect;
export const scale = (size) => (verticalScale / horizontalScale) * size;
export const scalePx = (sizePx) => {
const i = parseInt(sizePx, 10);
return `${scale(i)}px`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment