Skip to content

Media effects

Live demo. Swipe it, tap it.Open full screen ↗

Every preset, a custom effect and the liquid settings, live.

A media effect gets the signed distance of an image’s page from the current position (offset: 0 in view, -1 one page back, 0.5 halfway to the next) and returns opacity and transforms. It runs on the UI thread, so it must be a worklet:

import type { MediaEffectFn } from 'expo-morph-carousel';
const flip: MediaEffectFn = ({ offset }) => {
'worklet';
const angle = Math.max(-1, Math.min(1, offset)) * 90;
return {
opacity: Math.abs(offset) < 0.5 ? 1 : 0,
transform: [{ perspective: 800 }, { rotateY: `${angle}deg` }],
};
};
<MorphCarousel effect={flip} … />

The input also has width, height, direction and rtl. The built-in presets are exported as mediaEffects if you want to wrap one.

All images near the current page are stacked in one Skia layer and cross-fade by page offset. The layer then goes through two filters:

  1. A blur whose radius peaks halfway between two pages. Both images melt into one soft shape.
  2. An alpha threshold, a color matrix on the alpha channel, that cuts the blurred edge back to a sharp outline. The soft shape reads as a solid blob instead of a haze.

Both filters scale with a triangle curve that is 0 on a page and 1 halfway between two pages, so a page at rest is drawn untouched.

With photos (mediaFit="cover") the threshold also rounds off the rectangle’s corners and eats into its edges during the transition. liquid={{ preserveShape: true }} keeps the rectangle instead: the blur stays inside the image area and repeats its edge pixels, there is no threshold, so the corners stay square and only the content melts.

When photos are decoded. Decoding a photo blocks the thread it runs on (a 1080×810 JPEG takes tens of milliseconds). Skia does it lazily on the first draw, which is the UI thread, in the middle of the swipe that brings the page in. liquid.decode moves it:

  • 'scheduled' (default): on the JS thread when the page mounts ahead of time, one photo per task, the page in view first. Photos of pages that left the window before their turn are skipped.
  • 'immediate': on the JS thread as soon as each file is read.
  • 'onDraw': Skia’s default, on the UI thread at first draw.

Measured on a Redmi Note 8 Pro (Helio G90T, Expo Go, production JS) with the example’s Stress test screen, 40 pages at 350 ms: 'onDraw' dropped about one frame per page (40–41 frames over 25 ms, up to 133 ms). 'immediate' and 'scheduled' dropped 0–1, the longest frame 17 ms. The JS thread pays instead: about 15 gaps over 100 ms in those 40 pages, while onIndexChange still ran 9 ms after the page change on average. 'scheduled' and 'immediate' measured the same; the queue only bounds the cases where many photos arrive at once.

The text pages are ordinary React Native views that slide; only the media area is drawn by Skia.

example/app/playground.tsx
import { MorphCarousel, type MediaEffect, type MediaEffectFn } from 'expo-morph-carousel';
import { useState } from 'react';
import { Pressable, ScrollView, StyleSheet, Switch, Text, View } from 'react-native';
import { products } from '../data/products';
import { column } from '../data/layout';
/** A custom effect: the image turns like a card around its vertical axis. */
const flip: MediaEffectFn = ({ offset }) => {
'worklet';
const angle = Math.max(-1, Math.min(1, offset)) * 90;
return {
opacity: Math.abs(offset) < 0.5 ? 1 : 0,
transform: [{ perspective: 800 }, { rotateY: `${angle}deg` }],
};
};
const effects: { label: string; value: MediaEffect }[] = [
{ label: 'liquid', value: 'liquid' },
{ label: 'crossfade', value: 'crossfade' },
{ label: 'slide', value: 'slide' },
{ label: 'parallax', value: 'parallax' },
{ label: 'stack', value: 'stack' },
{ label: 'custom flip', value: flip },
];
export default function PlaygroundScreen() {
const [effect, setEffect] = useState(0);
const [loop, setLoop] = useState(true);
const [autoplay, setAutoplay] = useState(false);
const [vertical, setVertical] = useState(false);
const [blur, setBlur] = useState(28);
const [index, setIndex] = useState(0);
return (
<ScrollView contentInsetAdjustmentBehavior="automatic" contentContainerStyle={[styles.screen, column]}>
<MorphCarousel
// Remount when the direction changes: the pages are laid out per axis.
key={vertical ? 'v' : 'h'}
testID="playground-carousel"
data={products}
keyExtractor={(p) => p.id}
getImage={(p) => p.image}
getPlaceholderColor={(p) => p.color}
getAccessibilityLabel={(p) => p.title}
effect={effects[effect].value}
liquid={{ blur }}
loop={loop}
autoplay={autoplay ? { interval: 2500 } : false}
direction={vertical ? 'vertical' : 'horizontal'}
initialIndex={index}
onIndexChange={setIndex}
mediaHeight={240}
mediaPadding={24}
style={styles.card}
renderItem={({ item }) => (
<View style={styles.page}>
<Text style={styles.title}>{item.title}</Text>
</View>
)}
/>
<Text style={styles.label}>Effect</Text>
<View style={styles.chips}>
{effects.map((e, i) => (
<Pressable
key={e.label}
onPress={() => setEffect(i)}
style={[styles.chip, i === effect && styles.chipOn]}
testID={`effect-${e.label}`}
>
<Text style={[styles.chipText, i === effect && styles.chipTextOn]}>{e.label}</Text>
</Pressable>
))}
</View>
{effects[effect].value === 'liquid' && (
<>
<Text style={styles.label}>Liquid blur: {blur}</Text>
<View style={styles.chips}>
{[12, 20, 28, 40, 56].map((b) => (
<Pressable
key={b}
onPress={() => setBlur(b)}
style={[styles.chip, b === blur && styles.chipOn]}
>
<Text style={[styles.chipText, b === blur && styles.chipTextOn]}>{b}</Text>
</Pressable>
))}
</View>
</>
)}
<View style={styles.toggles}>
<Toggle label="Loop" value={loop} onChange={setLoop} />
<Toggle label="Autoplay (2.5 s)" value={autoplay} onChange={setAutoplay} />
<Toggle label="Vertical" value={vertical} onChange={setVertical} />
</View>
</ScrollView>
);
}
function Toggle({
label,
value,
onChange,
}: {
label: string;
value: boolean;
onChange: (v: boolean) => void;
}) {
return (
<View style={styles.toggle}>
<Text style={styles.toggleText}>{label}</Text>
<Switch value={value} onValueChange={onChange} accessibilityLabel={label} />
</View>
);
}
const styles = StyleSheet.create({
screen: { padding: 16, gap: 10 },
card: { height: 360, borderRadius: 24, backgroundColor: '#fff' },
page: { flex: 1, alignItems: 'center', justifyContent: 'center' },
title: { fontSize: 22, fontWeight: '700', color: '#1c1c1e' },
label: { marginTop: 8, fontSize: 13, color: '#6e6e73' },
chips: { flexDirection: 'row', flexWrap: 'wrap', gap: 8 },
chip: { paddingHorizontal: 12, paddingVertical: 8, borderRadius: 999, backgroundColor: '#fff' },
chipOn: { backgroundColor: '#5b4bd6' },
chipText: { fontSize: 14, color: '#1c1c1e' },
chipTextOn: { color: '#fff', fontWeight: '600' },
toggles: { marginTop: 8, borderRadius: 16, backgroundColor: '#fff', paddingHorizontal: 14 },
toggle: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 10,
},
toggleText: { fontSize: 15, color: '#1c1c1e' },
});