Surface.js 1.34 KB
Newer Older
1 2 3
import invariant from "invariant";
import {createSurface} from "gl-react";
import React, {
4
  View,
5
  PixelRatio
6 7 8 9 10
} from "react-native";
import GLCanvas from "./GLCanvas";

invariant(typeof createSurface === "function",
"gl-react createSurface is not a function. Check your gl-react dependency");
11 12

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

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

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

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

50 51 52 53 54
module.exports = createSurface(
  renderVcontainer,
  renderVcontent,
  renderVGL,
  getPixelRatio);