Fade Image with background in React-Native with react-native-skia

Contai, West Bengal, India
Search for a command to run...

Contai, West Bengal, India
No comments yet. Be the first to comment.
How I generate per-page Open Graph images at build time using satori and resvg-js - no edge runtime, no external service, just a custom Astro integration.

Ever wondered how you can keep your Telegram groups or online communities safe from inappropriate content automatically? It's actually easier than you might think! In this article, I'll walk you throu

Modern web and mobile applications typically use JWT (JSON Web Tokens) for authentication. These tokens expire after a certain period for security reasons, requiring a refresh mechanism to maintain us

When developing forms in React Native applications, we frequently encounter these challenges: Repetitive Code: Setting up input fields with proper styling and behavior for each form Validation Compl

Method chaining is a very useful and popular method in JavaScript and TypeScript which helps you write more readable and concise code. In this article, I will explain how method chaining works in JS/T

Even though react and react-native have the same fundamentals, designing many basic web things can be pretty hard and non-efficient. Fading Image or any View with the background can be achieved with LinearGradient and React-Native-SVG library but in this tutorial, I will do it with react-native-skia.
yarn add @shopify/react-native-skia
import React from 'react';
import { Dimensions, View } from 'react-native';
import { Canvas, Image, useImage } from '@shopify/react-native-skia';
const { height, width } = Dimensions.get('screen');
export default function App() {
const photo = useImage(
'https://images.pexels.com/photos/1382734/pexels-photo-1382734.jpeg',
);
return (
<View style={{ flex: 1 }}>
<Canvas style={{ height: height / 2 }}>
{photo && (
<Image
image={photo}
x={0}
y={0}
height={height / 2}
width={width}
fit="cover"
/>
)}
</Canvas>
</View>
);
}

To fade the image I will use Mask and LinearGradient by react-native-skia.
**Mask: ** The
Maskcomponent hides an element by masking the content at specific points. Just like its CSS counterpart, there are two modes available:
import React from 'react';
import { Dimensions, View } from 'react-native';
import {
Canvas,
Image,
useImage,
Mask,
LinearGradient,
Rect,
vec,
} from '@shopify/react-native-skia';
const { height, width } = Dimensions.get('screen');
export default function App() {
const photo = useImage(
'https://images.pexels.com/photos/1382734/pexels-photo-1382734.jpeg',
);
return (
<View style={{ flex: 1 }}>
<Canvas style={{ height: height / 2 }}>
<Mask
mask={
<Rect x={0} y={0} height={height / 2} width={width}>
<LinearGradient
start={vec(0, 0)}
end={vec(0, height / 2)}
colors={['white', 'white', 'white', 'transparent']}
/>
</Rect>
}>
{photo && (
<Image
image={photo}
x={0}
y={0}
height={height / 2}
width={width}
fit="cover"
/>
)}
</Mask>
</Canvas>
</View>
);
}
