Surface.js 1.36 KB
Newer Older
1 2
import invariant from "invariant";
import {createSurface} from "gl-react";
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
3 4
import React from "react";
import {View, PixelRatio} from "react-native";
5 6 7 8
import GLCanvas from "./GLCanvas";

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

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

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

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

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

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