Skip to content
  • Android
  • iOS
  • Web

Animations

Animate view styles or game entities with timing, springs and sequences.

Examples

Quick start

nayan-ui/animations
import { AnimationView, useAnimatedStyle, useSharedValue, withTiming } from 'nayan-ui/animations';
import { NButton } from 'nayan-ui/components';

export function FadeButton() {
  const opacity = useSharedValue(1);
  const style = useAnimatedStyle({ opacity });
  return (
    <AnimationView animatedStyle={style}>
      <NButton onPress={() => { opacity.value = withTiming(0.4); }}>Fade</NButton>
    </AnimationView>
  );
}

Spring an interactive view

nayan-ui/animations
import { AnimationView, useAnimatedStyle, useSharedValue, withSpring } from 'nayan-ui/animations';
import { NButton } from 'nayan-ui/components';

export function SlideButton() {
  const offset = useSharedValue(0);
  const style = useAnimatedStyle({ translateX: offset });
  return (
    <AnimationView animatedStyle={style}>
      <NButton onPress={() => { offset.value = withSpring(offset.value === 0 ? 24 : 0); }}>Slide</NButton>
    </AnimationView>
  );
}

Play a preset on a view

nayan-ui/animations
import { useRef } from 'react';
import { AnimationView, animations, type AnimationViewRef } from 'nayan-ui/animations';
import { NButton, NText } from 'nayan-ui/components';

export function Attention() {
  const target = useRef<AnimationViewRef>(null);
  return (
    <>
      <AnimationView ref={target}><NText>New item</NText></AnimationView>
      <NButton onPress={() => { if (target.current) animations.playView(target.current, 'pulse'); }}>Highlight</NButton>
    </>
  );
}

Animate an entity or play a model clip

nayan-ui/animations
import { animations } from 'nayan-ui/animations';
import type { Entity, World } from 'nayan-ui/games';

export function move(world: World, entity: Entity) {
  return animations.animate(world, entity, { position: [0, 2, 0] });
}

export function loopClip(world: World, entity: Entity, clip: string) {
  animations.playClip(world, entity, clip, { loop: true });
  return () => { animations.stopClip(world, entity); };
}
Related guide

Tags