···1399139914001400The flow looks as follows:
1401140114021402-Loading...
14031403-14041402### Events
1405140314061404There are three types of events in RNGH2: `StateChangeEvent`, `GestureEvent` and `PointerEvent`. The `StateChangeEvent` is send every time a gesture moves to a different state, while `GestureEvent` is send every time a gesture is updated. The first two carry a gesture-specific data and a `state` property, indicating the current state of the gesture. `StateChangeEvent` also carries a `oldState` property indicating the previous state of the gesture. `PointerEvent` carries information about raw touch events, like touching the screen or moving the finger. These events are handled internally before they are passed along to the correct callbacks:
-130
out/llms-full-react-native-reanimated.txt
···160160161161### Example
162162163163-Loading...
164164-165163### Remarks
166164167165- Animated styles take precedence over React Native's static styles. All values specified in animated styles override values from static styles.
···320318321319### Example
322320323323-Loading...
324324-325321### Remarks
326322327323- The returned handler may be passed to multiple components. In such situation, the handler will invoke for the given events each time any of the components dispatches them.
···408404409405### Example
410406411411-Loading...
412412-413407This example can be more easily implemented using `useScrollViewOffset`.
414408415409### Remarks
···500494The hook returns a context that will be reused by event handlers and value that indicates whether worklets should be rebuilt. If different implementation is needed for web, `useWeb` boolean is returned to check for web environment
501495502496### Example
503503-504504-Loading...
505497506498This example can be more easily implemented using `useScrollViewOffset`.
507499···609601610602### Example
611603612612-Loading...
613613-614604### Platform compatibility
615605616606| Android | iOS | Web |
···1157114711581148### Example
1159114911601160-Loading...
11611161-11621150### Remarks
1163115111641152- The returned handler may be passed to multiple components. In such situation, the handler will invoke for the given events each time any of the components dispatches them.
···12281216`scrollTo` returns `undefined`.
1229121712301218### Example
12311231-12321232-Loading...
1233121912341220### Remarks
12351221···1319130513201306#### Example
1321130713221322-Loading...
13231323-13241308### Platform compatibility
1325130913261310| Android | iOS | Web |
···1409139314101394### Example
1411139514121412-Loading...
14131413-14141396### Platform compatibility
1415139714161398| Android | iOS | Web |
···14651447`useScrollViewOffset` returns a shared value which holds the current offset of the `ScrollView`.
1466144814671449### Example
14681468-14691469-Loading...
1470145014711451### Remarks
14721452···1579155915801560And that's it! Pretty straightforward, isn't it? Let's see it in its full glory in an interactive example:
1581156115821582-Loading...
15831583-15841562You can make use of composing gestures to more complex behaviors. But what if we'd like to create something a bit more interesting?
1585156315861564### Handling pan gestures
···1626160416271605You can play around with the example below and see how the circle changes and reacts to the gesture:
1628160616291629-Loading...
16301630-16311607### Using `withDecay`
1632160816331609Remember when some time ago we said that we'll come back to `withDecay`? Now this is the time!
···1657163316581634Play around and see how the square decelerates when let go with some speed!
1659163516601660-Loading...
16611661-16621636Make sure to check the full `withDecay` API reference to get to know the rest of the configuration options.
1663163716641638### Summary
···16791653## withRepeat
1680165416811655`withRepeat` is an animation modifier that lets you repeat an animation given number of times or run it indefinitely.
16821682-16831683-Loading...
1684165616851657### Reference
16861658···1739171117401712A parameter that determines how the animation responds to the device's reduced motion accessibility setting.
1741171317421742-Loading...
17431743-17441714#### Returns
1745171517461716`withRepeat` returns an animation object which holds the current state of the animation. It can be either assigned directly to a shared value or can be used as a value for a style object returned from useAnimatedStyle.
1747171717481718### Example
17491749-17501750-Loading...
1751171917521720### Remarks
17531721···1823179118241792### Example
1825179318261826-Loading...
18271827-18281794### Remarks
1829179518301796- `measure` is implemented only on the UI thread. When using `measure` inside event handlers, it has to be wrapped with the `runOnUI` function.
···1958192419591925### Example
1960192619611961-Loading...
19621962-19631927### Remarks
1964192819651929- Ensure you do not mutate the same shared value in the `result` function that you've used in the `prepare` function, as this will lead to an infinite loop.
···1996196019971961`withSequence` is an animation modifier that lets you run animations in a sequence.
1998196219991999-Loading...
20002000-20011963### Reference
2002196420031965```
···20412003`withSequence` returns an animation object which holds the current state of the animation. It can be either assigned directly to a shared value or can be used as a value for a style object returned from useAnimatedStyle.
2042200420432005### Example
20442044-20452045-Loading...
2046200620472007### Platform compatibility
20482008···2137209721382098`useFrameCallback` lets you run a function on every frame update.
2139209921402140-Loading...
21412141-21422100### Reference
2143210121442102```
···22032161- `callbackId` a number indicating a unique identifier of the frame callback
2204216222052163### Example
22062206-22072207-Loading...
2208216422092165### Remarks
22102166···2475243124762432### Example
2477243324782478-Loading...
24792479-24802434### Platform compatibility
2481243524822436| Android | iOS | Web |
···26372591| state | `SharedValue<KeyboardState>` | A shared value containing current state of the keyboard. Possible states: `{ CLOSED, OPEN, CLOSING, OPENING }` |
2638259226392593### Example
26402640-26412641-Loading...
2642259426432595### Remarks
26442596···2826277828272779`withClamp` is an animation modifier that lets you limit the scope of movement of your animation to make it stay within some predefined range. Use it with withSpring animation.
2828278028292829-Loading...
28302830-28312781### Reference
2832278228332783```
···29082858For a full, in depth documentation follow the Sensors guide on Android Developers and the Device motion in the Apple Developer documentation.
2909285929102860The playground below mimics the behaviour and returned values of selected sensors:
29112911-29122912-Loading...
2913286129142862### Reference
29152863···3172312031733121### Example
3174312231753175-Loading...
31763176-31773123### Remarks
3178312431793125- You should always reach for `useAnimatedStyle` and `useAnimatedProps` first when animating styles or properties.
···3299324533003246### Example
3301324733023302-Loading...
33033303-33043248### Remarks
3305324933063250- Changing the reduced motion system setting doesn't cause your components to rerender.
···3371331533723316### Example
3373331733743374-Loading...
33753375-33763318### Remarks
3377331933783320info
···3533347535343476You can see it in action in the example below:
3535347735363536-Loading...
35373537-35383478### Animating props
3539347935403480Most of the values that developers animate (`width`, `color`, `transform` etc.) are modified by passing them as an object to the `style` property of an element. But that's not always the case.
···3613355336143554Check out the full example below:
3615355536163616-Loading...
36173617-36183556### Summary
3619355736203558In this section, we went through the differences between animating styles and props and how to use `useAnimatedStyle` and `useAnimatedProps`. To sum up:
···37583696- `ReduceMotion.Never` - This option ensures that the animation remains enabled at all times.
3759369737603698### Example
37613761-37623762-Loading...
3763369937643700### Platform compatibility
37653701···4223415942244160Reanimated comes with a bunch of predefined animations you can customize. For more advanced use-cases, you can use Keyframes or create your own custom entering/exiting animations.
4225416142264226-Loading...
42274227-42284162info
4229416342304164Spring-based animations are yet to be introduced to the web. Due to that, playground doesn't cover `springify()` options but they can be applied to your animations on iOS and Android platforms.
···5595552955965530### Example
5597553155985598-Loading...
55995599-56005532### Remarks
5601553356025534- Don't read or modify the value of a shared value during a component's render. Access to `value` property or calling `get`/`set` methods is a side-effect. Triggering side-effects during render violates the Rules of React. All reads from and writes to a shared value should happen in relevant callbacks which aren't executed during render, i.e. in `useAnimatedStyle` or `useEffect` hooks.
···59235855## Layout transitions
5924585659255857Layout transitions allows you to replace layout changes with smooth transitions. Each layout change may include changes of size and position and both of them can be animated.
59265926-59275927-Loading...
5928585859295859### Predefined Transitions
59305860···7157708771587088### Example
7159708971607160-Loading...
71617161-71627090### Remarks
7163709171647092- You can share animated props between components to avoid code duplication.
···76857613## withTiming
7686761476877615`withTiming` lets you create an animation based on duration and easing.
76887688-76897689-Loading...
7690761676917617### Reference
76927618···7754768077557681The timing animation configuration.
7756768277577757-Loading...
77587758-77597683Available properties:
7760768477617685| Name | Type | Default | Description |
···77697693The `easing` parameter lets you fine-tune the animation over the specified time duration. For example, you can make the animation begin with fast acceleration and then slow down towards the end, or start slowly, then pick up speed before slowing down again towards the end.
7770769477717695It will all start to make sense when you compare a `linear` easing side by side with the default `Easing.inOut(Easing.quad)` easing.
77727772-77737773-Loading...
7774769677757697Reanimated provides a selection of ready-to-use easing functions in the `Easing` module. You can find a visualization of some common easing functions at <http://easings.net/>.
77767698···78147736`withTiming` returns an animation object which holds the current state of the animation. It can be either assigned directly to a shared value or can be used as a value for a style object returned from useAnimatedStyle.
7815773778167738### Example
78177817-78187818-Loading...
7819773978207740### Remarks
78217741···80307950`useScrollViewOffset` returns a shared value which holds the current offset of the `ScrollView`.
8031795180327952### Example
80338033-80348034-Loading...
8035795380367954### Remarks
80377955···8074799280757993It all will start to make sense when you compare side by side a `linear` easing with the default easing.
8076799480778077-Loading...
80788078-80797995Reanimated comes with a handful of predefined easing functions. You can play around with them in the interactive playground below or check the full `withTiming` API reference.
8080799680817997`withTiming` interactive playground
···8098801480998015The mass of a spring influences how hard is it to make an object move and to bring it to a stop. Mass adds a feeling of _inertia_ to the object you're trying to move. You can see in the playground that the spring with greater mass moves more "sluggish" compared to the default configuration.
8100801681018101-Loading...
81028102-81038017Stiffness affects how bouncy the spring is. As an example, think about the difference between a steel spring (with very high stiffness) and a spring made out of soft plastic (with low stiffness).
8104801881058019Damping describes how quickly the spring animation finishes. Higher damping means the spring will come to rest faster. In the real world, you could think about the same spring bouncing in the air and underwater. For example, a spring in a vacuum would have zero friction and thus zero damping.
···81238037## withSpring
8124803881258039`withSpring` lets you create spring-based animations.
81268126-81278127-Loading...
8128804081298041### Reference
81308042···81978109##### `config`Optional
8198811081998111The spring animation configuration.
82008200-82018201-Loading...
8202811282038113Available for physics-based spring:
82048114···8242815282438153### Example
8244815482458245-Loading...
82468246-82478155### Remarks
8248815682498157- The callback passed to the 3rd argument is automatically workletized and ran on the UI thread.
···8421832984228330You can see it in action in the example below:
8423833184248424-Loading...
84258425-84268332### Animating props
8427833384288334Most of the values that developers animate (`width`, `color`, `transform` etc.) are modified by passing them as an object to the `style` property of an element. But that's not always the case.
···8501840785028408Check out the full example below:
8503840985048504-Loading...
85058505-85068410### Summary
8507841185088412In this section, we went through the differences between animating styles and props and how to use `useAnimatedStyle` and `useAnimatedProps`. To sum up:
···85808484`useDerivedValue` returns a new readonly shared value based on a value returned from the `updater` function.
8581848585828486### Example
85838583-85848584-Loading...
8585848785868488### Remarks
85878489···8636853886378539### Example
8638854086398639-Loading...
86408640-86418541### Remarks
8642854286438543- Functions passed to `runOnJS` must be defined in the JavaScript thread scope, i.e. in the component body or the global scope. This code won't work because `myFunction` is defined in the `withTiming` callback, which is only executed in the UI thread:
···87108610Don't forget to call the function returned from `runOnUI`.
8711861187128612### Example
87138713-87148714-Loading...
8715861387168614### Remarks
87178615···8867876588688766And voilà, we've made our first animation using Reanimated! You can see how it works in its full glory in a preview below:
8869876788708870-Loading...
88718871-88728768### Summary
8873876988748770In this section, we gained a firm grasp on the Reanimated fundamentals. We learned about `Animated` components, shared values and how to use them to create a simple animation. To sum up:
···88858781## withDecay
8886878288878783`withDecay` lets you create animations that mimic objects in motion with friction. The animation will start with the provided velocity and slow down over time according to the given deceleration rate until it stops.
88888888-88898889-Loading...
8890878488918785### Reference
88928786···8932882689338827The decay animation configuration.
8934882889358935-Loading...
89368936-89378829Available properties:
8938883089398831| Name | Type | Default | Description |
···89558847`withDecay` returns an animation object which holds the current state of the animation. It can be either assigned directly to a shared value or can be used as a value for a style object returned from useAnimatedStyle.
8956884889578849### Example
89588958-89598959-Loading...
8960885089618851### Remarks
89628852···90188908`createAnimatedComponent` returns a component that Reanimated is capable of animating.
9019890990208910### Example
90219021-90229022-Loading...
9023891190248912### Platform compatibility
90258913···91329020`cancelAnimation` returns `undefined`.
9133902191349022### Example
91359135-91369136-Loading...
9137902391389024### Remarks
91399025···92629148}
92639149```
9264915092659265-Loading...
92669266-92679151### Repeating an animation
9268915292699153To implement our desired shake animation we can use `withRepeat` modifier. This modifier lets you repeat a provided animation.
···93099193```
9310919493119195When we run this code we can see that the box jiggles left to right between the set offset and a starting position. After the animation finishes the box doesn't come back to its initial place. It's not really how we imagined a shake animation but we'll get there in a second!
93129312-93139313-Loading...
9314919693159197### Running animations in a sequence
93169198···93489230 };
93499231```
9350923293519351-Loading...
93529352-93539233### Starting the animation with delay
9354923493559235As a cherry on top, we'll add a little bit of suspense by adding a slight delay before the animation begins. For this exact purpose, Reanimated comes with a `withDelay` modifier.
···93889268 );
93899269 };
93909270```
93919391-93929392-Loading...
9393927193949272### Summary
93959273···9410928894119289`interpolate` lets you map a value from one range to another using linear interpolation.
9412929094139413-Loading...
94149414-94159291### Reference
9416929294179293```
···95019377`interpolate` returns a mapped value within the output range.
9502937895039379### Example
95049504-95059505-Loading...
9506938095079381### Platform compatibility
95089382···9554942895559429### Example
9556943095579557-Loading...
95589558-95599431### Platform compatibility
9560943295619433| Android | iOS | Web |
···96449516`interpolateColor` returns the color after interpolation from within the output range in `rgba(r, g, b, a)` format.
9645951796469518### Example
96479647-96489648-Loading...
9649951996509520### Remarks
96519521