【问题标题】:How to draw on the screen with React Native?如何使用 React Native 在屏幕上绘图?
【发布时间】:2021-11-27 02:00:20
【问题描述】:

我正在尝试在 react native 中实现一个 android 绘图应用程序。我正在使用 PanResponder 但我不知道如何获取用户触摸的部分的坐标。

我尝试使用react-native-svg,但不知道在哪里放置PanResponder 对象。

class App extends React.Component {
  constructor(props){
    super(props);
    this._panResponder={};
    this._previousLeft = 0;
    this._previousTop = 0;
    this._circleStyles = {};
    this.circle = null;
  }
  componentWillMount(){
    this._panResponder = PanResponder.create({
            onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
      onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
      onPanResponderGrant: this._handlePanResponderGrant,
      onPanResponderMove: this._handlePanResponderMove,
      onPanResponderRelease: this._handlePanResponderEnd,
      onPanResponderTerminate: this._handlePanResponderEnd,
    });
    this._previousLeft = 20;
    this._previousTop = 84;
    this._circleStyles = {
      style: {
        left: this._previousLeft,
        top: this._previousTop,
        backgroundColor: 'green',
      }
    };
  }
  componentDidMount() {
    this._updateNativeStyles();
  }


 render() {
    return (
      <View style={styles.container}>
        <View style={styles.circle}
          ref={(circle) => {
            this.circle = circle;
          }}
          { ...this._panResponder.panHandlers }
        />
      </View>
    );
  }

  _highlight = () => {
    this._circleStyles.style.backgroundColor = 'blue';
    this._updateNativeStyles();
  }

  _unHighlight = () => {
    this._circleStyles.style.backgroundColor = 'green';
    this._updateNativeStyles();
  }

  _updateNativeStyles() {
    this.circle && this.circle.setNativeProps(this._circleStyles);
  }

  _handleStartShouldSetPanResponder() {
    return true;
  }

  _handleMoveShouldSetPanResponder() {
    return true;
  }

  _handlePanResponderGrant = (e, gestureState) => {
    this._highlight();
  }

    _handlePanResponderMove = (e, gestureState) => {
    this._circleStyles.style.left = this._previousLeft + gestureState.dx;
    this._circleStyles.style.top = this._previousTop + gestureState.dy;
    this._updateNativeStyles();
  }

  _handlePanResponderEnd = (e, gestureState) => {
    this._unHighlight();
    this._previousLeft += gestureState.dx;
    this._previousTop += gestureState.dy;
  }

}

在此示例中,圆圈移动,但我希望它留下痕迹。这可以通过获取圆圈移动的点的坐标,然后为该点添加颜色来完成。

【问题讨论】:

  • 粘贴你的代码
  • 好的@SaugatBhattarai

标签: javascript reactjs react-native


【解决方案1】:

我看到的所有其他产品都有问题,尤其是在升级到 IOS14(测试版)时,我找不到任何适用于我的项目的产品。

我创建了自己的基于纯 JavaScript 和 HTML Canvas 的绘图组件。该组件具有透明背景,因此也可以覆盖在任何背景上。这也适用于在线和离线模式。

以最简单的形式,您可以像这样初始化

import RNDrawOnScreen from 'react-native-draw-on-screen';

<RNDrawOnScreen
   penColor={'#000'}
   strokeWidth={20}
/>

如果您为 penColor 和 strokeWidth 传入一个参数,它们将在更新时自动更改。您还可以调用 undo 和 clear 方法。

这是对 Expo 友好的,适用于 IOS/Android,只有一个依赖 react-native-webview 才能呈现视图。

我为此创建了一个 npm 包,希望其他人会发现它有用。包中包含一个简单的 expo 应用程序示例,它使用该组件并创建屏幕截图中显示的简单绘图应用程序。

https://www.npmjs.com/package/react-native-draw-on-screen

【讨论】:

  • 只是一个简单的问题 - 我找不到与 npm 包关联的 GitHub 存储库。计划很快发布?
猜你喜欢
  • 2017-12-14
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2022-07-01
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多