Skip to content

Commit 0ffaa7f

Browse files
committed
docs: Add tap to focus example
1 parent de9da64 commit 0ffaa7f

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

docs/docs/guides/FOCUSING.mdx

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To focus the camera to a specific point, simply use the Camera's [`focus(...)`](
1010
await camera.current.focus({ x: tapEvent.x, y: tapEvent.y })
1111
```
1212

13-
The focus function expects a [`Point`](/docs/api/interfaces/Point) parameter which represents the location relative to the Camera view where you want to focus the Camera to (in _points_). If you use [react-native-gesture-handler](https://docs.swmansion.com/react-native-gesture-handler/), this will consist of the [`x`](https://docs.swmansion.com/react-native-gesture-handler/docs/next/gesture-handlers/api/tap-gh/#x) and [`y`](https://docs.swmansion.com/react-native-gesture-handler/docs/next/gesture-handlers/api/tap-gh/#y) properties of the tap event payload.
13+
The focus function expects a [`Point`](/docs/api/interfaces/Point) parameter which represents the location relative to the Camera view where you want to focus the Camera to (in _points_). If you use [react-native-gesture-handler](https://github.com/software-mansion/react-native-gesture-handler/), this will consist of the [`x`](https://docs.swmansion.com/react-native-gesture-handler/docs/next/gesture-handlers/api/tap-gh/#x) and [`y`](https://docs.swmansion.com/react-native-gesture-handler/docs/next/gesture-handlers/api/tap-gh/#y) properties of the tap event payload.
1414

1515
So for example, `{ x: 0, y: 0 }` will focus to the upper left corner, while `{ x: VIEW_WIDTH, y: VIEW_HEIGHT }` will focus to the bottom right corner.
1616

@@ -20,6 +20,43 @@ Focussing adjusts auto-focus (AF) and auto-exposure (AE).
2020
`focus(...)` will fail if the selected Camera device does not support focusing (see [`CameraDevice.supportsFocus`](/docs/api/interfaces/CameraDevice#supportsfocus))
2121
:::
2222

23+
## Example (Gesture Handler)
24+
25+
This is an Example on how to use [react-native-gesture-handler](https://github.com/software-mansion/react-native-gesture-handler/) to focus the Camera to a specific point on the screen:
26+
27+
```tsx
28+
import { Camera, useCameraDevice } from 'react-native-vision-camera'
29+
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
30+
31+
export function App() {
32+
const camera = useRef<Camera>(null)
33+
const device = useCameraDevice('back')
34+
35+
const focus = useCallback((point: Point) => {
36+
const c = camera.current
37+
if (c == null) return
38+
c.focus()
39+
}, [])
40+
41+
const gesture = Gesture.Tap()
42+
.onEnd(({ x, y }) => {
43+
runOnJS(focus)({ x, y })
44+
})
45+
46+
if (device == null) return <NoCameraDeviceError />
47+
return (
48+
<GestureDetector gesture={gesture}>
49+
<Camera
50+
ref={camera}
51+
style={StyleSheet.absoluteFill}
52+
device={device}
53+
isActive={true}
54+
/>
55+
</GestureDetector>
56+
)
57+
}
58+
```
59+
2360
<br />
2461

2562
#### 🚀 Next section: [Exposure](exposure)

docs/docs/guides/ZOOMING.mdx

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ A Camera's `zoom` property is represented in a **logarithmic scale**. That means
4343

4444
The above example only demonstrates how to animate the `zoom` property. To actually implement pinch-to-zoom or pan-to-zoom, take a look at the [VisionCamera example app](https://github.com/mrousavy/react-native-vision-camera/tree/main/package/example), the pinch-to-zoom gesture can be found [here](https://github.com/mrousavy/react-native-vision-camera/blob/main/package/example/src/views/CaptureButton.tsx#L189-L208), and the pan-to-zoom gesture can be found [here](https://github.com/mrousavy/react-native-vision-camera/blob/d8551792e97eaa6fa768f54059ffce054bf748d9/example/src/views/CaptureButton.tsx#L185-L205). They implement a real world use-case, where the maximum zoom value is clamped to a realistic value, and the zoom responds very gracefully by using a logarithmic scale.
4545

46-
47-
## Implementation (Reanimated)
46+
## Example (Reanimated + Gesture Handler)
4847

4948
While you can use any animation library to animate the `zoom` property (or use no animation library at all) it is recommended to use [react-native-reanimated](https://github.com/software-mansion/react-native-reanimated) to achieve best performance. Head over to their [Installation guide](https://docs.swmansion.com/react-native-reanimated/docs/installation) to install Reanimated if you haven't already.
5049

@@ -61,8 +60,8 @@ While you can use any animation library to animate the `zoom` property (or use n
6160
The following example implements a pinch-to-zoom gesture using [react-native-gesture-handler](https://github.com/software-mansion/react-native-gesture-handler/) and [react-native-reanimated](https://github.com/software-mansion/react-native-reanimated):
6261

6362
```tsx
64-
import { Camera, useCameraDevice, CameraProps } from "react-native-vision-camera"
65-
import Reanimated, { useAnimatedProps, useSharedValue } from "react-native-reanimated"
63+
import { Camera, useCameraDevice, CameraProps } from 'react-native-vision-camera'
64+
import Reanimated, { useAnimatedProps, useSharedValue } from 'react-native-reanimated'
6665
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
6766

6867
Reanimated.addWhitelistedNativeProps({

0 commit comments

Comments
 (0)