makeSurface.js 1.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import invariant from "invariant";
import {createSurface} from "gl-react";
import React from "react";
import {PixelRatio} from "react-native";

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

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

export default C => {
  const renderVcontainer = ({ style, width, height, visibleContent, eventsThrough }, contents, renderer) => {
    const parentStyle = [
      {
        position: "relative",
      },
      style,
      {
        width: width,
        height: height,
        overflow: "hidden",
      }
    ];
    return <C.View
      pointerEvents={!visibleContent && eventsThrough ? "none" : "auto"}
      style={parentStyle}>
      {contents}
      {renderer}
    </C.View>;
  };
  const renderVcontent = (width, height, id, children, { visibleContent }) => {
    const childrenStyle = {
      position: "absolute",
      top: visibleContent ? 0 : height, // as a workaround for RN, we offset the content so it is not visible but still can be rasterized
      left: 0,
      width: width,
      height: height,
      overflow: "hidden",
    };
    return <C.View key={id} style={childrenStyle}>{children}</C.View>;
  };
  const renderVGL = props => {
    C.dimensionInvariant(props.width, "width");
    C.dimensionInvariant(props.height, "height");
    return <C.GLCanvas ref="canvas" {...props} />;
  };

  return createSurface(
    renderVcontainer,
    renderVcontent,
    renderVGL,
    getPixelRatio,
53
    C.getGLCanvas,
54 55
  );
};