Surface.js 1.34 KB
Newer Older
1
const invariant = require("invariant");
2
const {createSurface} = require("gl-react");
3
invariant(typeof createSurface === "function", "gl-react createSurface is not a function. Check your gl-react dependency");
4 5 6
const React = require("react-native");
const {
  View,
7
  PixelRatio
8
} = React;
9 10 11
const GLCanvas = require("./GLCanvas");

const getPixelRatio = props => props.scale || PixelRatio.get();
12

13
function renderVcontent (width, height, id, children, { visibleContent }) {
14 15
  const childrenStyle = {
    position: "absolute",
16
    top: visibleContent ? 0 : height, // as a workaround for RN, we offset the content so it is not visible but still can be rasterized
17 18 19
    left: 0,
    width: width,
    height: height,
20
    overflow: "hidden",
21
  };
22
  return <View key={id} style={childrenStyle}>{children}</View>;
23
}
24

25
function renderVGL (props) {
26
  return <GLCanvas ref="canvas" {...props} />;
27
}
28

29
function renderVcontainer ({ style, width, height, visibleContent, eventsThrough }, contents, renderer) {
30 31
  const parentStyle = {
    position: "relative",
32
    ...style,
33 34
    width: width,
    height: height,
35
    overflow: "hidden",
36
  };
37 38 39
  return <View
    pointerEvents={!visibleContent && eventsThrough ? "none" : "auto"}
    style={parentStyle}>
40 41 42
    {contents}
    {renderer}
  </View>;
43
}
44

45
module.exports = createSurface(renderVcontainer, renderVcontent, renderVGL, getPixelRatio);