GLCanvas.js 1.62 KB
Newer Older
1 2 3
const React = require("react-native");
const {
  Component,
4
  requireNativeComponent
5 6
} = React;

7
const captureFrame = require("./GLCanvas.captureFrame");
8

9 10 11 12 13
const GLCanvasNative = requireNativeComponent("GLCanvas", GLCanvas, {
  nativeOnly: {
    onGLChange: true,
    onGLProgress: true,
    onGLCaptureFrame: true
14
  }
15 16 17 18 19 20 21 22 23 24 25 26 27
});

function defer() {
  const deferred = {};
  const promise = new Promise(function(resolve, reject) {
    deferred.resolve = resolve;
    deferred.reject  = reject;
  });
  deferred.promise = promise;
  return deferred;
}

class GLCanvas extends Component {
28
  captureFrame (cb) {
29 30 31 32 33 34 35 36 37 38 39 40 41
    const promise = (
      this._pendingCaptureFrame || // use pending capture OR create a new captureFrame pending
      (captureFrame(React.findNodeHandle(this.refs.native)), this._pendingCaptureFrame = defer())
    ).promise;
    if (typeof cb === "function") {
      console.warn("GLSurface: callback parameter of captureFrame is deprecated, use the returned promise instead"); // eslint-disable-line no-console
      promise.then(cb);
    }
    return promise;
  }
  onGLCaptureFrame = ({ nativeEvent: {frame} }) => {
    this._pendingCaptureFrame.resolve(frame);
    this._pendingCaptureFrame = undefined;
42
  };
43
  render () {
44
    const { width, height, onLoad, onProgress, eventsThrough, ...restProps } = this.props;
45 46 47
    return <GLCanvasNative
      ref="native"
      {...restProps}
48 49 50 51
      onGLLoad={onLoad ? onLoad : null}
      onGLProgress={onProgress ? onProgress : null}
      onGLCaptureFrame={this.onGLCaptureFrame}
      pointerEvents={eventsThrough ? "none" : "auto"}
52 53 54 55 56 57
      style={{ width, height }}
    />;
  }
}

module.exports = GLCanvas;