AnimatedHelloGL.js 1.02 KB
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");
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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 = {
      value: 0
    };
  }
  componentDidMount () {
    const loop = time => {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
29
      this.raf = requestAnimationFrame(loop);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
30 31 32 33
      this.setState({
        value: (1 + Math.cos(time / 1000)) / 2 // cycle between 0 and 1
      });
    };
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
34 35 36 37
    this.raf = requestAnimationFrame(loop);
  }
  componentWillUnmount () {
    cancelAnimationFrame(this.raf);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
38 39 40 41
  }
  render () {
    const { width, height } = this.props;
    const { value } = this.state;
42 43 44 45 46 47
    return <Surface width={width} height={height}>
      <GL.Node
        shader={shaders.helloGL}
        uniforms={{ value }}
      />
    </Surface>;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
48 49 50 51
  }
}

module.exports = HelloGL;