jwagner / simplex-noise.js

A fast simplex noise implementation in Javascript / Typescript.
MIT License
1.61k stars 130 forks source link

Make the library compatible with `react-native-reanimated` #67

Closed xseignard closed 6 months ago

xseignard commented 6 months ago

Why

react-native-reanimated is a popular library for react-native to create and drive animations.

See https://docs.swmansion.com/react-native-reanimated/

react-native is running on 2 threads:

To be able to run simplex-noise on the UI thread with react-native-reanimated we need to workletize the functions.

You can read more here: https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#worklet

How

Added "worklet" directive where needed.

Test

Tested on mobile with react-native@0.72.6 and react-native-reanimated@3.6.2

Impacts

Good to know

Will only work with react-native-reanimated@3.6.2+

jwagner commented 6 months ago

Hey @xseignard ,

Thanks a lot for the PR. I don't quite understand why simplex-noise.js would need annotations to be used with react-native-reanimated. It seems quite backward that a framework would require arbitrary non standard annotations in every library it is used with.

Isn't it possible to just call simplex-noise.js from a function which is annotated? The docs seem sparse but

Worklet runtimes come with performance.now and console.* methods installed out-of-the-box. Other APIs are not available and need to be injected into the runtime or captured via worklet closure. Seems to allude to the fact that dependencies can be captured in a closure. I think that would be a better way to go about this.

I would prefer not to have non-standard annotations in the code that are only relevant for a very small subset of users.

xseignard commented 6 months ago

Thanks for your answer. I perfectly understand your point of view.

Unfortunately you can only call workletized function within worklets.

I'll try to find another approach to leverage your great lib in react-native projects.

Thanks for your work!

jwagner commented 6 months ago

Hey Xavier,

I'd hate to give up on this just yet. There must be some sensible way to get this to work. It's just a bit hard for me to experiment without a running react native setup.