AnimatedHelloGL.js 1019 Bytes
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1
const React = require("react-native");
2
const GL = require("gl-react");
3
const {Surface} = require("gl-react-native");
4
const {Animated} = React;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

const shaders = GL.Shaders.create({
  helloGL: {
    frag: `
precision highp float;
varying vec2 uv;

uniform float value;

void main () {
  gl_FragColor = vec4(uv.x, uv.y, value, 1.0);
}
    `
  }
});

class HelloGL extends React.Component {
  constructor (props) {
    super(props);
    this.state = {
25
      value: new Animated.Value(0)
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
26 27 28
    };
  }
  componentDidMount () {
29 30 31 32 33
    const loop = () => Animated.sequence([
      Animated.timing(this.state.value, { toValue: 1, duration: 1000 }),
      Animated.timing(this.state.value, { toValue: 0, duration: 1000 })
    ]).start(loop);
    loop();
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
34 35 36 37
  }
  render () {
    const { width, height } = this.props;
    const { value } = this.state;
38 39 40 41 42 43
    return <Surface width={width} height={height}>
      <GL.Node
        shader={shaders.helloGL}
        uniforms={{ value }}
      />
    </Surface>;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
44 45 46 47
  }
}

module.exports = HelloGL;