Skip to content

Commit 7233863

Browse files
committed
GestureRecorder
1 parent 17af01e commit 7233863

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

README.md

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
<img src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/img/logo-emoji.png" height="150" />
2-
3-
# React Native Gesture Detector
4-
5-
### Create and detect custom gestures on React Native.
6-
7-
[![Version](https://img.shields.io/npm/v/react-native-gesture-detector)](https://npmjs.org/package/react-native-gesture-detector)
8-
[![License](https://img.shields.io/npm/l/react-native-gesture-detector)](https://github.com/mxmzb/react-native-gesture-detector/blob/master/LICENSE)
9-
[![Bundle Size](https://img.shields.io/bundlephobia/min/react-native-gesture-detector)](https://npmjs.org/package/react-native-gesture-detector)
10-
[![Build](https://img.shields.io/circleci/build/github/mxmzb/react-native-gesture-detector)](https://circleci.com/gh/mxmzb/react-native-gesture-detector/)
1+
<p align="center">
2+
<br />
3+
<img src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/img/logo-emoji.png" height="150" />
4+
</p>
5+
6+
<h1 align="center">React Native Gesture Detector</h1>
7+
<h3 align="center">Create and detect custom gestures on React Native.</h3>
8+
9+
<p align="center">
10+
<a href="https://npmjs.org/package/react-native-gesture-detector">
11+
<img src="https://img.shields.io/npm/v/react-native-gesture-detector" />
12+
</a>
13+
<a href="https://github.com/mxmzb/react-native-gesture-detector/blob/master/LICENSE">
14+
<img src="https://img.shields.io/npm/l/react-native-gesture-detector" />
15+
</a>
16+
<a href="https://npmjs.org/package/react-native-gesture-detector">
17+
<img src="https://img.shields.io/bundlephobia/min/react-native-gesture-detector" />
18+
</a>
19+
<a href="https://circleci.com/gh/mxmzb/react-native-gesture-detector/">
20+
<img src="https://img.shields.io/circleci/build/github/mxmzb/react-native-gesture-detector" />
21+
</a>
22+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" />
23+
</p>
1124

1225
## Demos
1326

1427
<p>
15-
<img width="260" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-coil.gif">
16-
<img width="260" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-multiple-gestures.gif">
17-
<img width="260" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-triangle.gif">
28+
<img width="217" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-coil.gif">
29+
<img width="217" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-multiple-gestures.gif">
30+
<img width="217" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-triangle.gif">
31+
<img width="217" src="https://raw.githubusercontent.com/mxmzb/react-native-gesture-detector/master/example/assets/demo-gesture-recorder.gif">
1832
<p>
1933

20-
### Example app
34+
### Example app and usage
2135

2236
Feel free to run the included Expo app:
2337

@@ -28,11 +42,15 @@ $ yarn
2842
$ yarn start
2943
```
3044

45+
Check [the code for the screens](https://github.com/mxmzb/react-native-gesture-detector/tree/master/example/src/Screen) to see how they are done!
46+
3147
## Intro
3248

33-
This package originated from a real life need to detect custom gestures. The idea for implementation originated from this [stellar answer](https://stackoverflow.com/questions/20821358/gesture-detection-algorithm-based-on-discrete-points) on StackOverflow. The result is not 100% foolproof, but performant and extremely simple to use.
49+
This package originated from a real life **need to detect custom gestures**. The idea for implementation originated from this [stellar answer](https://stackoverflow.com/questions/20821358/gesture-detection-algorithm-based-on-discrete-points) on StackOverflow. The result is not 100% foolproof, but rock solid, performant and extremely simple to use.
3450

35-
Because the library strongly uses React hooks, you must use at least `[email protected]`.
51+
The package comes with another, insanely cool component `GestureRecorder`, which allows you to create gestures **on the fly**. Yep, just plug it in, paint the gesture and you will receive the coordinate data for your supercomplex, custom gesture. You can use it to just **use the data points as a predefined gesture** in your app, or **you can even let your app users create their own custom gestures**, if that fits your game plan!
52+
53+
Because the library significantly uses React hooks, you must use at least `[email protected]`.
3654

3755
## Installation
3856

@@ -44,7 +62,11 @@ $ yarn add react-native-gesture-handler lodash # install peer dependencies
4462
## Quickstart
4563

4664
```jsx
47-
import GestureDetector, { GesturePath, Cursor } from "react-native-gesture-detector";
65+
import GestureDetector, {
66+
GestureRecorder,
67+
GesturePath,
68+
Cursor,
69+
} from "react-native-gesture-detector";
4870

4971
const gestures = {
5072
// this will result in the gesture shown in the first demo give above
@@ -101,6 +123,21 @@ const CoilExample = () => (
101123
)}
102124
</GestureDetector>
103125
);
126+
127+
const RecordGestureExample = () => {
128+
// finishedGesture will look like gestures["Coil"] from the top
129+
const [finishedGesture, setFinishedGesture] = useState([]);
130+
131+
return (
132+
<GestureRecorder onPanRelease={gesture => setFinishedGesture(gesture)}>
133+
{({ gesture }) => (
134+
<View style={{ position: "relative", width: "100%", height: "100%" }}>
135+
<GesturePath path={gesture} color="green" slopRadius={35} />
136+
</View>
137+
)}
138+
</GestureRecorder>
139+
);
140+
};
104141
```
105142

106143
## Documentation and API
@@ -117,6 +154,22 @@ const CoilExample = () => (
117154
| `onGestureFinish` | `(gesture) => {}` | `function` | A callback, which is called when the user finishes a gesture. Receives the gesture key of the finished gesture. |
118155
| `onPanRelease` | `() => {}` | `function` | Callback, when the user releases the finger. Receives no arguments. |
119156

157+
### `GestureRecorder`
158+
159+
`GestureRecorder` is a render props component. The child function has the form `children({ gesture: [{ x: string, y: string }, { x: string, y: string }, ...], gestureDirectionHistory: [{ x: string, y: string }, { x: string, y: string }, ...], offset: { x: number, y: number } })`.
160+
161+
`gesture` is an array of coordinates. They are generated based on the `pointDistance` prop of the component.
162+
163+
`gestureDirectionHistory` will tell you accordingly to `gesture` which direction the gesture is moving there. This might give somewhat unreliable data currently. A direction object looks like `{ x: "left", y: "up" }`.
164+
165+
`offset` will artificially add an horizontal and vertical offset to the coordinates. This does not change the detection of the defined gesture at all. It's just a helper to use with the `GesturePath` component to paint the path where you actually draw. Check the [`GestureRecorder` example screen](https://github.com/mxmzb/react-native-gesture-detector/blob/master/example/src/Screen/CreateGesture.js) for more details on this.
166+
167+
| Prop | Default | Type | Description |
168+
| :-------------- | :---------------: | :--------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
169+
| `pointDistance` | 20 | `number` | The minimum distance between points that you want to be recorded. So default wise, every 20px (or more, usually depending on the phone hardware and the speed of the finger moving over the display) the component will add another point to the `gesture` array |
170+
| `onCapture` | `() => {}` | `function` | A callback, which is called every time the component is adding a coordinate to the `gesture` array |
171+
| `onPanRelease` | `(gesture) => {}` | `function` | Callback, when the user releases the finger. Receives the fully drawn gesture in form of a coordinate array. |
172+
120173
### `GesturePath`
121174

122175
`GesturePath` is a helper component, which paints a gesture visually in a container. The container should have `position: absolute;` set in its style property. `{ x, y }` is a coordinate object. An array of coordinate objects must be passed to paint the gesture on the screen. This component should be only used in development to define and refine gestures.
848 KB
Loading

0 commit comments

Comments
 (0)