首页 > 解决方案 > Expo + React Native:在两种视图的坐标之间画线

问题描述

我目前正在使用这个模块:https ://github.com/mxmzb/react-native-gesture-detector 。我希望能够从创建的点画一条线。但是,它似乎只输出圆圈。

它有一个“创建手势”视图:

<View style={{ position: "relative", width: "100%", height: "100%" }}>
    <GesturePath
        path={gesture.map(coordinate => {
            if (recorderOffset) {
                return {
                    x: coordinate.x + recorderOffset.x,
                    y: coordinate.y + recorderOffset.y,
                };
            }

            return coordinate;
        })}
        color="green"
        slopRadius={30}
        center={false}
    />
</View>

GesturePath 的定义如下:

const GesturePath = ({ path, color, slopRadius, center = true }: GesturePathProps) => {
  const baseStyle: ViewStyle = {
    position: "absolute",
    top: center ? "50%" : 0,
    left: center ? "50%" : 0,
    opacity: 1,
  };

  return (
    <>
      {path.map((point, index) => (
        <Animated.View
          style={Object.assign({}, baseStyle, {
            width: slopRadius,
            height: slopRadius,
            borderRadius: slopRadius,
            backgroundColor: color,
            marginLeft: point.x - slopRadius,
            marginTop: point.y - slopRadius,
          })}
          key={index}
        />
      ))}
    </>
  );
};

当您在该视图上绘图时,它会使用点勾勒出路径,如下所示:

在此处输入图像描述

我希望它是一条平滑的线,而不是上面图像的一系列圆圈。

标签: javascriptreact-nativeexpogesture-recognition

解决方案


您将需要像 Canvas 这样的东西来绘制线条而不是像素(使用视图)。React Native 目前没有 Canvas 实现。

在 expo 中执行此操作的最简单方法是使用该react-native-svg库。

使用它,您可以使用以下实现从手势数据中绘制一条折线:

import Svg, { Polyline } from 'react-native-svg';

const GesturePath = ({ path, color }) => {
  const { width, height } = Dimensions.get('window');
  const points = path.map(p => `${p.x},${p.y}`).join(' ');
  return (
    <Svg height="100%" width="100%" viewBox={`0 0 ${width} ${height}`}>
        <Polyline
          points={points}
          fill="none"
          stroke={color}
          strokeWidth="1"
        />
    </Svg>    
  );
};

您还可以react-native-gesture-detector使用内置的 React Native PanResponder在没有库的情况下记录手势。这是一个例子:

const GestureRecorder = ({ onPathChanged }) => {
  const pathRef = useRef([]);

  const panResponder = useRef(
    PanResponder.create({
      onMoveShouldSetPanResponder: () => true,
      onPanResponderGrant: () => {
        pathRef.current = [];
      },
      onPanResponderMove: (event) => {
        pathRef.current.push({
          x: event.nativeEvent.locationX,
          y: event.nativeEvent.locationY,
        });
        // Update path real-time (A new array must be created
        // so setState recognises the change and re-renders the App):
        onPathChanged([...pathRef.current]);
      },
      onPanResponderRelease: () => {
        onPathChanged(pathRef.current);
      }
    })
  ).current;

  return (
    <View
      style={StyleSheet.absoluteFill}
      {...panResponder.panHandlers}
    />
  );
}

请参阅此小吃以了解将所有内容捆绑在一起的工作应用程序:https ://snack.expo.io/@mtkopone/draw-gesture-path


推荐阅读