How to draw square to tag an object. (React Native)

不问归期 提交于 2019-11-30 10:25:19

You can use React Native ART library to draw shapes on top of image. It is a standard library that ships with React Native (although not linked to the binary by default).

Regarding the algorithm:

  • Render an image
  • Overlay image with React Native's ART.Surface
  • Detect taps to get coordinates + overlay the rest of the image
  • Once you have coordinates of the tap, you can draw a shape you want
  • Stop drawing shape when user removes his finger (onPressOut event)

Where to go from here:

You can add children (in your case, square Views) to an Image tag, so you could do something like

<Image src={...}>
  <View
    style={{
      position: 'absolute',
      top: 120
      left: 100,
      height: 50,
      width: 50,
      borderWidth: 1
    }}
  />
</Image>

You can get the x and y coordinates with the PanResponder API instead of hardcoding the top and left style properties

Edit: RN 50=< Removed support of nested content inside , use ImageBackground instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!