I’m pretty new to react-native-skia, I have a grayscale shader applied on an Image and I would like to apply this kind of transition animation on press to switch between grayscale and full colored picture. Here is the code that I’m using to apply a grayscale effect.
const GRAYSCALE_SHADER = Skia.RuntimeEffect.Make(`
uniform shader image;
half4 main(in vec2 xy)
{
//Grabbing the texture color at the current pixel.
vec4 texColor = image.eval(xy).rgba;
vec3 gray_scale = vec3(dot(vec3(0.5, 0.2, 0.2), texColor.rgb));
// Output to screen
return vec4(gray_scale, texColor.a) ;
}
`)!;
const image = useImage('../path/to/image.png');
return (
<Canvas
style={{ flex: 1 }}
>
<Image
image={image}
fit="cover"
x={0}
y={0}
width={200}
height={200}
>
<RuntimeShader source={GRAYSCALE_SHADER} />
</Image>
</Canvas>
)
2
Answers
To create a transition animation between a grayscale image and a full colored image in React Native Skia, you can achieve this by toggling between two different shaders for the Image component. One shader will apply the grayscale effect, while the other will display the full colored image.
Here’s an approach to implement this transition animation:
Here’s an example code snippet to achieve this:
In this code snippet:
This setup allows you to switch between grayscale and full color with a press interaction on the image. You can further customize the transition animation or effects based on your requirements.