Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Animations and values #193

Merged
merged 103 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
1606253
WIP: First shot at native values
chrfalch Feb 4, 2022
845e26e
Changed redraw request:
chrfalch Feb 4, 2022
b2b1bcd
Added support for creating animation values in started/stopped state
chrfalch Feb 4, 2022
9976f11
Added transform test to Playground
chrfalch Feb 4, 2022
374e08b
Updated with new animation system based on Values and derived values.
chrfalch Feb 8, 2022
288d594
Merge branch 'main' into feature/animated-values
chrfalch Feb 8, 2022
893e640
Removed CanvasProvider with automatic updated of Values
chrfalch Feb 8, 2022
4e4b167
Changed default duration to 1000 ms
chrfalch Feb 8, 2022
2d30d48
Removed unessesary wait lock
chrfalch Feb 8, 2022
29e0ff5
Added useLoop hook
chrfalch Feb 8, 2022
afa9175
Added check to ensure rending is on the correct thread.
chrfalch Feb 10, 2022
3b06e33
Adjusted hooks and constructors for animation and timelines.
chrfalch Feb 10, 2022
6f357ff
Merge branch 'main' into feature/animated-values
chrfalch Feb 10, 2022
d6e2a2e
Merge branch 'main' into feature/animated-values
chrfalch Feb 10, 2022
ab4f80c
Fixed issue with from value
chrfalch Feb 10, 2022
4aa31de
Fixed issue with start values (from)
chrfalch Feb 10, 2022
aa28698
Added animation example for text
chrfalch Feb 10, 2022
a7cf14f
Cleanup
chrfalch Feb 10, 2022
1eac88a
Added easing to interpolation graph
chrfalch Feb 10, 2022
fef7450
Cleanup
chrfalch Feb 10, 2022
04cb704
Updated some colors
chrfalch Feb 10, 2022
6b7a8d6
Merge branch 'main' into feature/animated-values
chrfalch Feb 10, 2022
2f7e229
Added automatic stop for useAnimationValue
chrfalch Feb 10, 2022
81709a1
Updated animation documentation
chrfalch Feb 10, 2022
81bed08
Removed unnecessary generic parameter
chrfalch Feb 10, 2022
5d0aace
Documentation and changes after code review
chrfalch Feb 10, 2022
4780550
Added initializer that sets redraw requests to 1 on startup to make s…
chrfalch Feb 11, 2022
1cd4f01
Fixed issue in JsiSkContourMeasureIter for null results
chrfalch Feb 11, 2022
5219721
Fixed issue with loop/yoyo in timing when duration is equal to t
chrfalch Feb 11, 2022
afa51f2
Merge branch 'main' into feature/animated-values
chrfalch Feb 11, 2022
335e5e3
Reverted debug in Aurora example
chrfalch Feb 11, 2022
c128342
Removed unused test of App.tsx
chrfalch Feb 11, 2022
65cce79
Added simple animation example using only values
chrfalch Feb 11, 2022
d23d514
Adjusted params from last example
chrfalch Feb 11, 2022
5b63820
Adjusted parameters
chrfalch Feb 11, 2022
be0cdcb
Removed unused import
chrfalch Feb 11, 2022
0a80934
Removed unused seek
chrfalch Feb 12, 2022
036969c
Added useValueEffect
chrfalch Feb 12, 2022
7bb36b2
Finished touch handler demo
chrfalch Feb 12, 2022
5573b8c
Fixed types for the value api
chrfalch Feb 12, 2022
7e0ec0a
Refactored animation unit
chrfalch Feb 12, 2022
f572a76
Fixed wrong import after refactoring animation files
chrfalch Feb 12, 2022
f89e897
Added some comments to the examples
chrfalch Feb 12, 2022
587954b
Fixed small typo in animation docs
chrfalch Feb 12, 2022
fb50dff
Changed order of examples
chrfalch Feb 13, 2022
0eb513e
Updated wrong ticks in docs
chrfalch Feb 13, 2022
7482ffe
Renamed setDriver -> _setDriver and remove unused getDriver
chrfalch Feb 13, 2022
a209ec0
Fixed memory leak in JsiSkFont
chrfalch Feb 13, 2022
ec7579d
Changed to use useWindowDimensions
chrfalch Feb 13, 2022
8878299
Resolved form parameter
chrfalch Feb 13, 2022
42ef250
Reverted last commit in useInternaltiming for from parameter
chrfalch Feb 13, 2022
e714cf1
Merge branch 'main' into feature/animated-values
chrfalch Feb 15, 2022
682a8e7
Removed runAfterInteraction for immediate animations
chrfalch Feb 15, 2022
25273d8
Minor fix to mesh gradient example
wcandillon Feb 15, 2022
09c5c82
Merge branch 'feature/animated-values' of https://github.com/Shopify/…
wcandillon Feb 15, 2022
3c363bb
Renamed AnimationValue -> ClockValue and removed I prefix in front of…
chrfalch Feb 15, 2022
64cb3ca
Merge remote-tracking branch 'refs/remotes/origin/feature/animated-va…
chrfalch Feb 15, 2022
d69df8e
Exposed create***Value
chrfalch Feb 15, 2022
d181466
Updated with renamed values
chrfalch Feb 15, 2022
b2f98e6
Fixed velocity in touch handler:
chrfalch Feb 16, 2022
cf964be
Updated with correct velocity values
chrfalch Feb 16, 2022
fb82718
Removed setDriver from RNSkValue
chrfalch Feb 16, 2022
3695e2c
Added extra call to draw loop with invalidated = true to signal to li…
chrfalch Feb 18, 2022
0ee260d
Refactored values into separate files for better overview.
chrfalch Feb 18, 2022
c139852
Updated animation examples
chrfalch Feb 18, 2022
c17bd14
Updated animation functions/hooks to work with the new createAnimatio…
chrfalch Feb 18, 2022
fa63954
Added support for immediate in internalRunTiming
chrfalch Feb 18, 2022
7498f28
Remove Clock Value JSI export (#204)
wcandillon Feb 20, 2022
3802bff
Fixed issue with start position when restarting animation.
chrfalch Feb 22, 2022
771d187
Added support for extracting arguments from Skia nodes
chrfalch Feb 22, 2022
6b21947
Removed visitProps from nodes.
chrfalch Feb 22, 2022
1017684
Merge pull request #206 from Shopify/feature/animated-values-dependen…
chrfalch Feb 23, 2022
03af131
Changed to useLoop instead of useTiming
chrfalch Feb 23, 2022
75a5cc6
Refactored useInternalTiming to detect changes only when changes occur.
chrfalch Feb 23, 2022
7769f59
Fixed velocity calculations after code review
chrfalch Feb 23, 2022
a1d3adf
Feature/animated values refactor folders (#207)
chrfalch Feb 24, 2022
29e2c2e
Merge branch 'main' into feature/animated-values
chrfalch Mar 2, 2022
f474b58
Reverted commit in Canvas
chrfalch Mar 2, 2022
ce55fcd
Merge branch 'main' into feature/animated-values
chrfalch Mar 2, 2022
1d16b0c
Added derived values to glassmorphism exapmles
chrfalch Mar 2, 2022
220e140
Removed unused import
chrfalch Mar 2, 2022
7277d73
Renamed AnimationValue -> Animation
chrfalch Mar 2, 2022
4e504d0
First version of the new animation prop on value implementation.
chrfalch Mar 3, 2022
5488b99
Added decay animation
chrfalch Mar 3, 2022
73c4d73
Updated some docs
chrfalch Mar 3, 2022
152924c
Add utility to cancel animation
wcandillon Mar 3, 2022
0e6a1ca
Add test example
wcandillon Mar 3, 2022
1bd6d47
:wrench:
wcandillon Mar 3, 2022
54c65a0
Add missing files
wcandillon Mar 3, 2022
7636211
Fix dead link
wcandillon Mar 3, 2022
ae69467
Implement review comments of the review comments
wcandillon Mar 3, 2022
d55eeaa
Merge pull request #218 from Shopify/animated-values-review
chrfalch Mar 4, 2022
e0772b2
Fixed wrong filename and included physics based bounce
chrfalch Mar 4, 2022
653e65f
Renamed to Skia Values
chrfalch Mar 4, 2022
270cf8b
Renamed ***Value.value -> ***Value.current
chrfalch Mar 4, 2022
a8b9791
Renamed value -> current in docs
chrfalch Mar 4, 2022
48fe539
Removed test example not used
chrfalch Mar 4, 2022
400f159
Last changes, reverted commit and reverted back to use springs in gra…
chrfalch Mar 4, 2022
1e3ef2a
Fixed value -> current in docs
chrfalch Mar 4, 2022
6134067
Merge branch 'main' into feature/animated-values
wcandillon Mar 4, 2022
45b9969
Update documentation
wcandillon Mar 4, 2022
8f49411
Merge branch 'feature/animated-values' into feature/animated-values-f…
chrfalch Mar 4, 2022
7603d46
Merge pull request #224 from Shopify/feature/animated-values-finalize
chrfalch Mar 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/docs/animations/animations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
id: animations
title: Animations
sidebar_label: Animations
slug: /animations/animations
---

To ease the process of building animation, the library provides some utilities to help you. There are two types of utility functions - imperative functions and hooks.

If you have a Skia Value that you want to animate declaratively, a hook is the best choice.

In the example below we want the position of the rectangle to animate when we toggle a given value, and we want it to do this with a spring animation.

## Declarative animation

```tsx twoslash
import React, { useState } from "react";
import { Canvas, Rect, useSpring } from "@shopify/react-native-skia";
import { Button, StyleSheet } from "react-native";

export const AnimationExample = () => {
const [toggled, setToggled] = useState(false);
const position = useSpring(toggled ? 100 : 0);
return (
<>
<Canvas style={{ flex: 1 }}>
<Rect x={position} y={100} width={10} height={10} color={"red"} />
</Canvas>
<Button title="Toggle" onPress={() => setToggled((p) => !p)} />
</>
);
};
```

## Imperative animation

Almost the same thing can be accomplished with an imperative function (except that we are not toggling back and forth in this example):

```tsx twoslash
import { Canvas, Rect, runSpring, useValue } from "@shopify/react-native-skia";
import { Button } from "react-native";

export const AnimationExample = () => {
const position = useValue(0);
const changePosition = () => runSpring(position, 100);
return (
<>
<Canvas style={{ flex: 1 }}>
<Rect x={position} y={100} width={10} height={10} color={"red"} />
</Canvas>
<Button title="Toggle" onPress={changePosition} />
</>
);
};
```
38 changes: 38 additions & 0 deletions docs/docs/animations/gestures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
id: touch-events
title: Touch Events
sidebar_label: Touch Events
slug: /animations/touch-events
---

### useTouchHandler

The `useTouchHandler` hook handles touches in the `Canvas`.
It is meant to be used with values to animate elements of the canvas.

```tsx twoslash
import {
Canvas,
Circle,
useTouchHandler,
useValue,
} from "@shopify/react-native-skia";

const MyComponent = () => {
const cx = useValue(100);
const cy = useValue(100);

const touchHandler = useTouchHandler({
onActive: ({ x, y }) => {
cx.current = x;
cy.current = y;
},
});

return (
<Canvas style={{ flex: 1 }} onTouch={touchHandler}>
<Circle cx={cx} cy={cy} r={10} color="red" />
</Canvas>
);
};
```
112 changes: 0 additions & 112 deletions docs/docs/animations/overview.md

This file was deleted.

90 changes: 90 additions & 0 deletions docs/docs/animations/values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
id: values
title: Values
sidebar_label: Values
slug: /animations/values
---

React Native Skia supports Animations through the concept of Skia Values. A value can be seen as the state in the library where a change in will trigger a repaint request on the `Canvas` component where it is used.

A simple example is shown below shows how a value is used as a property for the x position of the `Rect` element.

```tsx twoslash
import { Canvas, Rect, useValue } from "@shopify/react-native-skia";
import { useCallback } from "react";
import { Button } from "react-native";

const MyComponent = () => {
const position = useValue(0);
const updateValue = useCallback(
() => (position.current = position.current + 10),
[position]
);

return (
<>
<Canvas style={{ flex: 1 }}>
<Rect x={position} y={100} width={10} height={10} color={"red"} />
</Canvas>
<Button title="Move it" onPress={updateValue} />
</>
);
};
```

## Skia Values

The basic `SkiaValue` is a value that stores some kind of Javascript value. It can be used to store numbers, strings, booleans, objects and even arrays:

```tsx twoslash
import { useValue } from "@shopify/react-native-skia";
const progress = useValue({ x: 100, y: 100 });
const actualValue = progress.current; // actualValue is now {x: 100, y: 100}
```

There are a few more value types in the library that will be described below.

## Derived value

This value is a Skia Value that is derived from other Skia Values.
It takes as its input one or more existing values and a function that will calculate the new value based on the input. The function will be evaluated every time the input value changes.

```tsx twoslash
import { useValue, useDerivedValue } from "@shopify/react-native-skia";

const radius = useValue(100);
const theta = useValue(Math.PI);
const length = useDerivedValue((r, t) => r * t, [radius, theta]);
console.log(length.current); // 314.1592653589793
```

## Clock Value

This value is a value that updates on every display frame on the device.
The value will be updated with the number of milliseconds elapsed since it was started.

```tsx twoslash
import {
useClockValue,
Canvas,
Circle,
useDerivedValue,
} from "@shopify/react-native-skia";

const interval = 3000;

const Demo = () => {
const clock = useClockValue();
const opacity = useDerivedValue(
(t) => {
return (t % interval) / interval;
},
[clock]
);
return (
<Canvas style={{ flex: 1 }}>
<Circle r={100} cx={100} cy={100} color="black" opacity={opacity} />
</Canvas>
);
};
```
2 changes: 1 addition & 1 deletion docs/docs/canvas/canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Behind the scenes, it is using its own React renderer.
|:-----|:---------|:-----------------|
| style | `ViewStyle` | View style. |
| ref? | `Ref<SkiaView>` | Reference to the `SkiaView` object |
| onTouch? | `TouchHandler` | Touch handler for the Canvas (see [touch handler](/docs/animations/overview#usetouchhandler)). |
| onTouch? | `TouchHandler` | Touch handler for the Canvas (see [touch handler](/docs/animations/touch-events#usetouchhandler)). |

## Getting a Canvas Snapshot

Expand Down
7 changes: 4 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"lint": "eslint . --ext .ts,.tsx --max-warnings 0"
},
"dependencies": {
"@docusaurus/core": "^2.0.0-beta.16",
"@docusaurus/preset-classic": "^2.0.0-beta.16",
"@docusaurus/core": "^2.0.0-beta.17",
"@docusaurus/preset-classic": "^2.0.0-beta.17",
"@mdx-js/react": "^1.6.21",
"@shopify/react-native-skia": "link:../package/",
"@svgr/webpack": "^5.5.0",
Expand All @@ -31,8 +31,9 @@
"url-loader": "^4.1.1"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "^2.0.0-beta.16",
"@docusaurus/module-type-aliases": "^2.0.0-beta.17",
"@tsconfig/docusaurus": "^1.0.4",
"@types/react-native": "^0.66.15",
"eslint": "^7.32.0",
"eslint-config-react-native-wcandillon": "^3.7.2",
"twoslash-cli": "^1.3.17",
Expand Down
7 changes: 6 additions & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ const sidebars = {
collapsed: true,
type: "category",
label: "Animations",
items: ["animations/overview", "animations/reanimated"],
items: [
"animations/values",
"animations/animations",
"animations/touch-events",
"animations/reanimated",
],
},
],
};
Expand Down
Loading