Last active
September 25, 2021 17:18
-
-
Save bitsmanent/16b5bd4c8c1f70846510ffbcfd53ab16 to your computer and use it in GitHub Desktop.
Simple sprite sheets for React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import {ImageBackground} from "react-native"; | |
export function Sprite(props) { | |
const [w, h] = [props.geometry.width, props.geometry.height]; | |
const r = Math.min(w / (props.width / props.cols), h / (props.height / props.rows)); | |
return ( | |
<ImageBackground | |
source={props.source} | |
style={{ | |
width: w, | |
height: h, | |
overflow: "hidden" | |
}} | |
imageStyle={{ | |
width: props.width * r, | |
height: props.height * r, | |
left: props.geometry.x, | |
top: props.geometry.y | |
}} | |
/>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Where:
If icons have all the same height you can use the
-h*n
formula to select the nth icon in the sprite (as the above example).Enjoy.