index.js 1.49 KB
Newer Older
1 2
import invariant from "invariant";
import { Shaders } from "gl-react";
3 4 5 6
import isAnimated from "gl-react/src/isAnimated";
import makeSurface from "./makeSurface";
import GLCanvas from "./GLCanvas";
import {NativeModules, View, Animated} from "react-native";
7
const {RNGLContext} = NativeModules;
8 9 10 11 12
invariant(RNGLContext,
`gl-react-native: the native module is not available.
Make sure you have properly configured it.
See README install instructions.

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
13
NativeModules.RNGLContext is %s`, RNGLContext);
14 15

// Hook Shaders to RNGLContext
16 17 18 19 20 21 22 23 24
Shaders.setImplementation({
  add: (id, shader) =>
  new Promise((resolve, reject) =>
    RNGLContext.addShader(id, shader, (error, result) => {
      if (error) reject(error);
      else resolve(result);
    })),
  remove: id => RNGLContext.removeShader(id)
});
25 26

module.exports = {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  Surface: makeSurface({
    View,
    GLCanvas,
    dimensionInvariant: (value, field) =>
      isAnimated(value)
      ? invariant(false, "GL.Surface "+field+" prop cannot be an Animated object. Use GL.AnimatedSurface instead")
      : invariant(typeof value === "number" && value > 0, "GL.Surface: "+field+" prop must be a strictly positive number")
  }),
  AnimatedSurface: makeSurface({
    View: Animated.View,
    GLCanvas: Animated.createAnimatedComponent(GLCanvas),
    dimensionInvariant: (value, field) =>
      invariant(
        isAnimated(value) || typeof value === "number" && value > 0,
        "GL.AnimatedSurface: "+field+" must be a strictly positive number OR an Animated object"
      )
  }),
44
};