-
Notifications
You must be signed in to change notification settings - Fork 447
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
feat: skottie #657
Closed
Closed
feat: skottie #657
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d1bb19e
render first frame
hannojg eaf45d7
progress animation using REA
hannojg c68c8f2
remove canvas.drawAnimation, use same structure as skottie does
hannojg 0b7116b
fix: setup ios
hannojg 725ce57
fix(ios): podfile version
hannojg fc0a7b3
fix: copy headers script
hannojg 27d1c40
fix(build): correct framework path
hannojg 17ec676
revert change
hannojg add2e46
fix(build): include sksg
hannojg 1488dc4
expose fps of animation
hannojg 8159a5d
fix: animation not progressing properly
hannojg 67162c6
Merge branch 'main' into feat/skottie
52d0ac9
Merge branch 'main' into feat/skottie
hannojg 428daea
fix(example): just use skia animations
hannojg cb73221
fix(example): loop animation
hannojg 901e174
temp fix typescript, by adding empty Skottie impl for web
hannojg 788ab9a
Merge branch 'main' of github.com:Shopify/react-native-skia into feat…
hannojg 88d6e1b
Merge branch 'Shopify:main' into feat/skottie
hannojg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ export type Routes = { | |
Freeze: undefined; | ||
UseCanvas: undefined; | ||
Reanimated: undefined; | ||
Skottie: undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useMemo } from "react"; | ||
import { ScrollView, useWindowDimensions } from "react-native"; | ||
import { | ||
Canvas, | ||
Skia, | ||
SkottieAnimation, | ||
useTiming, | ||
Easing, | ||
} from "@shopify/react-native-skia"; | ||
|
||
import _LottieAnim from "../../assets/material_wave_loading.json"; | ||
|
||
const LottieAnim = JSON.stringify(_LottieAnim); | ||
|
||
export const SkottieAnimations = () => { | ||
const { width, height } = useWindowDimensions(); | ||
|
||
// TODO: build a hook that abstracts this logic | ||
const skottieAnimation = useMemo(() => Skia.SkottieAnimation(LottieAnim), []); | ||
|
||
const progress = useTiming( | ||
{ | ||
from: 0, | ||
to: 1, | ||
loop: true, | ||
}, | ||
{ | ||
duration: skottieAnimation.duration * 1000, | ||
easing: Easing.linear, | ||
} | ||
); | ||
|
||
return ( | ||
<ScrollView> | ||
<Canvas style={{ width, height }}> | ||
<SkottieAnimation | ||
x={0} | ||
y={0} | ||
width={width} | ||
height={height} | ||
progress={progress} | ||
anim={skottieAnimation} | ||
/> | ||
</Canvas> | ||
</ScrollView> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"assets":[],"ddd":0,"fr":29.9700012207031,"h":256,"ip":0,"layers":[{"ao":0,"bm":0,"ddd":0,"ind":1,"ip":0,"ks":{"a":{"a":0,"k":[-70,-0.5,0]},"o":{"a":0,"k":100},"p":{"a":1,"k":[{"e":[208.6,88,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[208.6,127.969,0],"t":20,"ti":[0,-0.00520833348855,0],"to":[0,-6.66145849227905,0]},{"e":[208.6,128,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[208.6,88,0],"t":30,"ti":[0,-6.66666650772095,0],"to":[0,0.00520833348855,0]},{"t":40.0000016292334}]},"r":{"a":0,"k":0},"s":{"a":0,"k":[75,75,100]}},"nm":"Shape Layer 3","op":300.00001221925,"shapes":[{"cix":2,"it":[{"d":1,"mn":"ADBE Vector Shape - Ellipse","nm":"Ellipse Path 1","p":{"a":0,"k":[0,0]},"s":{"a":0,"k":[33.75,34.5]},"ty":"el"},{"c":{"a":0,"k":[0.9843137,0.5490196,0,1]},"mn":"ADBE Vector Graphic - Fill","nm":"Fill 1","o":{"a":0,"k":100},"r":1,"ty":"fl"},{"a":{"a":0,"ix":1,"k":[0,0]},"nm":"Transform","o":{"a":0,"ix":7,"k":100},"p":{"a":0,"ix":2,"k":[-70.125,-0.5]},"r":{"a":0,"ix":6,"k":0},"s":{"a":0,"ix":3,"k":[100,100]},"sa":{"a":0,"ix":5,"k":0},"sk":{"a":0,"ix":4,"k":0},"ty":"tr"}],"ix":1,"mn":"ADBE Vector Group","nm":"Ellipse 1","np":3,"ty":"gr"}],"sr":1,"st":0,"ty":4},{"ao":0,"bm":0,"ddd":0,"ind":2,"ip":0,"ks":{"a":{"a":0,"k":[-70,-0.5,0]},"o":{"a":0,"k":100},"p":{"a":1,"k":[{"e":[168.6,88,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[168.6,128,0],"t":15,"ti":[0,0,0],"to":[0,-6.66666650772095,0]},{"e":[168.6,128,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[168.6,88,0],"t":25,"ti":[0,-6.66666650772095,0],"to":[0,0,0]},{"t":35.0000014255792}]},"r":{"a":0,"k":0},"s":{"a":0,"k":[75,75,100]}},"nm":"Shape Layer 2","op":300.00001221925,"shapes":[{"cix":2,"it":[{"d":1,"mn":"ADBE Vector Shape - Ellipse","nm":"Ellipse Path 1","p":{"a":0,"k":[0,0]},"s":{"a":0,"k":[33.75,34.5]},"ty":"el"},{"c":{"a":0,"k":[0.9921569,0.8470588,0.2078431,1]},"mn":"ADBE Vector Graphic - Fill","nm":"Fill 1","o":{"a":0,"k":100},"r":1,"ty":"fl"},{"a":{"a":0,"ix":1,"k":[0,0]},"nm":"Transform","o":{"a":0,"ix":7,"k":100},"p":{"a":0,"ix":2,"k":[-70.125,-0.5]},"r":{"a":0,"ix":6,"k":0},"s":{"a":0,"ix":3,"k":[100,100]},"sa":{"a":0,"ix":5,"k":0},"sk":{"a":0,"ix":4,"k":0},"ty":"tr"}],"ix":1,"mn":"ADBE Vector Group","nm":"Ellipse 1","np":3,"ty":"gr"}],"sr":1,"st":0,"ty":4},{"ao":0,"bm":0,"ddd":0,"ind":3,"ip":0,"ks":{"a":{"a":0,"k":[-70,-0.5,0]},"o":{"a":0,"k":100},"p":{"a":1,"k":[{"e":[128.594,88,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[128.594,127.969,0],"t":10,"ti":[0,-0.00520833348855,0],"to":[0,-6.66145849227905,0]},{"e":[128.594,128,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[128.594,88,0],"t":20,"ti":[0,-6.66666650772095,0],"to":[0,0.00520833348855,0]},{"t":30.0000012219251}]},"r":{"a":0,"k":0},"s":{"a":0,"k":[75,75,100]}},"nm":"Shape Layer 1","op":300.00001221925,"shapes":[{"cix":2,"it":[{"d":1,"mn":"ADBE Vector Shape - Ellipse","nm":"Ellipse Path 1","p":{"a":0,"k":[0,0]},"s":{"a":0,"k":[33.75,34.5]},"ty":"el"},{"c":{"a":0,"k":[0.2627451,0.627451,0.2784314,1]},"mn":"ADBE Vector Graphic - Fill","nm":"Fill 1","o":{"a":0,"k":100},"r":1,"ty":"fl"},{"a":{"a":0,"ix":1,"k":[0,0]},"nm":"Transform","o":{"a":0,"ix":7,"k":100},"p":{"a":0,"ix":2,"k":[-70.125,-0.5]},"r":{"a":0,"ix":6,"k":0},"s":{"a":0,"ix":3,"k":[100,100]},"sa":{"a":0,"ix":5,"k":0},"sk":{"a":0,"ix":4,"k":0},"ty":"tr"}],"ix":1,"mn":"ADBE Vector Group","nm":"Ellipse 1","np":3,"ty":"gr"}],"sr":1,"st":0,"ty":4},{"ao":0,"bm":0,"ddd":0,"ind":4,"ip":0,"ks":{"a":{"a":0,"k":[-70,-0.5,0]},"o":{"a":0,"k":100},"p":{"a":1,"k":[{"e":[88.6,88,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[88.6,127.969,0],"t":5,"ti":[0,-0.00520833348855,0],"to":[0,-6.66145849227905,0]},{"e":[88.6,128,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[88.6,88,0],"t":15,"ti":[0,-6.66666650772095,0],"to":[0,0.00520833348855,0]},{"t":25.0000010182709}]},"r":{"a":0,"k":0},"s":{"a":0,"k":[75,75,100]}},"nm":"Shape Layer 4","op":300.00001221925,"shapes":[{"cix":2,"it":[{"d":1,"mn":"ADBE Vector Shape - Ellipse","nm":"Ellipse Path 1","p":{"a":0,"k":[0,0]},"s":{"a":0,"k":[33.75,34.5]},"ty":"el"},{"c":{"a":0,"k":[0.1176471,0.5333334,0.8980392,1]},"mn":"ADBE Vector Graphic - Fill","nm":"Fill 1","o":{"a":0,"k":100},"r":1,"ty":"fl"},{"a":{"a":0,"ix":1,"k":[0,0]},"nm":"Transform","o":{"a":0,"ix":7,"k":100},"p":{"a":0,"ix":2,"k":[-70.125,-0.5]},"r":{"a":0,"ix":6,"k":0},"s":{"a":0,"ix":3,"k":[100,100]},"sa":{"a":0,"ix":5,"k":0},"sk":{"a":0,"ix":4,"k":0},"ty":"tr"}],"ix":1,"mn":"ADBE Vector Group","nm":"Ellipse 1","np":3,"ty":"gr"}],"sr":1,"st":0,"ty":4},{"ao":0,"bm":0,"ddd":0,"ind":5,"ip":0,"ks":{"a":{"a":0,"k":[-70,-0.5,0]},"o":{"a":0,"k":100},"p":{"a":1,"k":[{"e":[48.6,88,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[48.6,127.969,0],"t":0,"ti":[0,-0.00520833348855,0],"to":[0,-6.66145849227905,0]},{"e":[48.6,128,0],"i":{"x":0.667,"y":1},"n":"0p667_1_0p333_0","o":{"x":0.333,"y":0},"s":[48.6,88,0],"t":10,"ti":[0,-6.66666650772095,0],"to":[0,0.00520833348855,0]},{"t":20.0000008146167}]},"r":{"a":0,"k":0},"s":{"a":0,"k":[75,75,100]}},"nm":"Shape Layer 5","op":300.00001221925,"shapes":[{"cix":2,"it":[{"d":1,"mn":"ADBE Vector Shape - Ellipse","nm":"Ellipse Path 1","p":{"a":0,"k":[0,0]},"s":{"a":0,"k":[33.75,34.5]},"ty":"el"},{"c":{"a":0,"k":[0.8980392,0.2235294,0.2078431,1]},"mn":"ADBE Vector Graphic - Fill","nm":"Fill 1","o":{"a":0,"k":100},"r":1,"ty":"fl"},{"a":{"a":0,"ix":1,"k":[0,0]},"nm":"Transform","o":{"a":0,"ix":7,"k":100},"p":{"a":0,"ix":2,"k":[-70.125,-0.5]},"r":{"a":0,"ix":6,"k":0},"s":{"a":0,"ix":3,"k":[100,100]},"sa":{"a":0,"ix":5,"k":0},"sk":{"a":0,"ix":4,"k":0},"ty":"tr"}],"ix":1,"mn":"ADBE Vector Group","nm":"Ellipse 1","np":3,"ty":"gr"}],"sr":1,"st":0,"ty":4}],"nm":"Comp 1","op":40.0000016292334,"v":"4.6.8","w":256} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#pragma once | ||
|
||
#include <JsiSkHostObjects.h> | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdocumentation" | ||
|
||
#include <modules/skottie/include/Skottie.h> | ||
|
||
#pragma clang diagnostic pop | ||
|
||
#include <jsi/jsi.h> | ||
|
||
// TODO: we probably also want to turn that into a factory. | ||
namespace RNSkia { | ||
using namespace facebook; | ||
|
||
class JsiSkSkottieAnimation : public JsiSkWrappingSkPtrHostObject<skottie::Animation> | ||
{ | ||
public: | ||
//#region Properties | ||
JSI_PROPERTY_GET(duration) { return static_cast<double>(getObject()->duration()); } | ||
JSI_PROPERTY_GET(fps) { return static_cast<double>(getObject()->fps()); } | ||
|
||
JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(JsiSkSkottieAnimation, duration), | ||
JSI_EXPORT_PROP_GET(JsiSkSkottieAnimation, fps)) | ||
//#endregion | ||
|
||
|
||
//#region Methods | ||
JSI_HOST_FUNCTION(seek) { | ||
getObject()->seek(arguments[0].asNumber()); | ||
return jsi::Value::undefined(); | ||
} | ||
|
||
JSI_HOST_FUNCTION(render) { | ||
auto canvas = JsiSkCanvas::fromValue(runtime, arguments[0]); | ||
auto rect = JsiSkRect::fromValue(runtime, arguments[1]); | ||
if (canvas != nullptr && rect != nullptr) | ||
getObject()->render(canvas, rect.get()); | ||
|
||
return jsi::Value::undefined(); | ||
} | ||
|
||
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkSkottieAnimation, seek), | ||
JSI_EXPORT_FUNC(JsiSkSkottieAnimation, render), ) | ||
//#endregion | ||
|
||
/** | ||
Constructor | ||
*/ | ||
JsiSkSkottieAnimation(std::shared_ptr<RNSkPlatformContext> context, | ||
const sk_sp<skottie::Animation> animation) | ||
: JsiSkWrappingSkPtrHostObject<skottie::Animation>(std::move(context), std::move(animation)){} | ||
|
||
/** | ||
Returns the jsi object from a host object of this type | ||
*/ | ||
static sk_sp<skottie::Animation> fromValue(jsi::Runtime &runtime, const jsi::Value &obj) { | ||
return obj.asObject(runtime) | ||
.asHostObject<JsiSkSkottieAnimation>(runtime) | ||
->getObject(); | ||
} | ||
|
||
/** | ||
* Creates the function for contructing a new instance of the | ||
* JsiSkSkottieAnimation class. | ||
* | ||
* @param context platform context | ||
* @return A function for creating a new host object wrapper for the JsiSkSkottieAnimation class. | ||
*/ | ||
static const jsi::HostFunctionType | ||
createCtor(std::shared_ptr<RNSkPlatformContext> context) { | ||
return JSI_HOST_FUNCTION_LAMBDA { | ||
auto jsonStr = arguments[0].asString(runtime).utf8(runtime); | ||
auto animation = skottie::Animation::Builder() | ||
.make(jsonStr.c_str(), jsonStr.size()); | ||
|
||
// Return the newly constructed object | ||
return jsi::Object::createFromHostObject( | ||
runtime, std::make_shared<JsiSkSkottieAnimation>(std::move(context), std::move(animation))); | ||
}; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
package/src/renderer/components/skottie/SkottieAnimation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react"; | ||
|
||
import type { AnimatedProps } from "../../processors"; | ||
import type { RectProps } from "../shapes"; | ||
import { processRect } from "../../processors"; | ||
import { createDrawing } from "../../nodes/Drawing"; | ||
import type { SkSkottieAnimation } from "../../../skia/types/SkottieAnimation"; | ||
|
||
export type SkottieProps = RectProps & { | ||
anim: SkSkottieAnimation; | ||
// value from 0 - 1 (or negative as well?) | ||
progress: number; | ||
}; | ||
|
||
const onDraw = createDrawing<SkottieProps>( | ||
({ canvas, Skia }, { anim, progress, ...rectProps }) => { | ||
const rect = processRect(Skia, rectProps); | ||
anim.seek(progress); | ||
anim.render(canvas, rect); | ||
} | ||
); | ||
export const SkottieAnimation = (props: AnimatedProps<SkottieProps>) => { | ||
return <skDrawing onDraw={onDraw} {...props} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./SkottieAnimation"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { SkCanvas } from "./Canvas"; | ||
import type { SkRect } from "./Rect"; | ||
|
||
export interface SkSkottieAnimation { | ||
readonly duration: number; | ||
readonly fps: number; | ||
readonly seek: (time: number) => void; | ||
readonly render: (canvas: SkCanvas, rect: SkRect) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the API design is to call
animation.render(canvas, progress)
I needed to get the canvas as parameter when called from JS