Created
August 30, 2017 17:12
-
-
Save cidicles/e90611844e9993d7c1337bb3c8afb23e to your computer and use it in GitHub Desktop.
Quick React VR Fixed Position 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
import React from 'react'; | |
import { | |
VrHeadModel, | |
View | |
} from 'react-vr'; | |
/** | |
* Helper to fix a component to the viewport. | |
* @module components/fixed | |
* @property {number} stickiness - The delay in ms between state updates. Lower values = less visual lag. | |
*/ | |
class Fixed extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
hmRot: VrHeadModel.rotation() | |
} | |
} | |
componentDidMount(){ | |
let {stickiness} = this.props; | |
this.timer = setInterval(()=>{this.tick()}, stickiness); | |
} | |
componentWillUnmount(){ | |
clearInterval(this.timer); | |
} | |
shouldComponentUpdate(nextProps, nextState){ | |
return nextState.hmRot !== this.state.hmRot; | |
} | |
tick(){ | |
this.setState({ | |
hmRot: VrHeadModel.rotation() | |
}); | |
} | |
render(){ | |
let {hmRot} = this.state; | |
return ( | |
<View | |
style={{ | |
position: 'absolute', | |
layoutOrigin: [0.5, 0.5], | |
transform: [ | |
{translate: [0, 0, 0]}, | |
{rotateX: hmRot[0]}, | |
{rotateY: hmRot[1]}, | |
{rotateZ: hmRot[2]} | |
] | |
}}> | |
<View | |
style={{ | |
position: 'absolute', | |
layoutOrigin: [0.5, 0.5], | |
transform: [ | |
{translate: [0, 0, 0]} | |
] | |
}}> | |
{this.props.children} | |
</View> | |
</View> | |
); | |
} | |
} | |
module.exports = Fixed; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment