Last active
January 10, 2019 11:58
-
-
Save brysgo/6fe4e29ae4354665c8fd to your computer and use it in GitHub Desktop.
React native repeating texture component
This file contains hidden or 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
var React = require('react-native'); | |
var { | |
Image, | |
View, | |
} = React; | |
var Texture = React.createClass({ | |
_onLayout: function(event) { | |
let tileWidth = this.props.width; | |
let tileHeight = this.props.height; | |
let {width, height} = event.nativeEvent.layout; | |
let tilesWide = Math.ceil(width / tileWidth); | |
let tilesHigh = Math.ceil(height / tileHeight); | |
if (this.state.tilesWide != tilesWide || | |
this.state.tilesHigh !=tilesHigh) { | |
this.setState({tilesWide, tilesHigh}); | |
} | |
}, | |
getInitialState: function() { | |
return { | |
tilesWide: 2, | |
tilesHigh: 2 | |
}; | |
}, | |
render: function() { | |
let { style, children} = this.props; | |
return ( | |
<View onLayout={this._onLayout} style={style}> | |
<View style={{position: 'absolute', top: 0, left: 0, flexWrap: 'nowrap', flexDirection: 'column'}}> | |
{(Array.apply(null, Array(this.state.tilesHigh))).map(this._renderRow)} | |
</View> | |
{children} | |
</View> | |
); | |
}, | |
_renderRow() { | |
return ( | |
<View style={{flexWrap: 'nowrap', flexDirection: 'row'}}> | |
{(Array.apply(null, Array(this.state.tilesWide))).map(this._renderColumn)} | |
</View> | |
) | |
}, | |
_renderColumn() { | |
let {width, height, source} = this.props; | |
return (<Image source={source} style={{width, height}}/>); | |
} | |
}); | |
module.exports = Texture; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment