Play a base state
Use play for an ongoing mode such as idle, sleep or thinking. Unlike a temporary reaction, it changes the base state. Try sleep and idle below and watch the state readout.
Try it. It’s alive.LIVE DEMO
Drag the pet, or focus it and use arrow keys. Reduced motion follows your system settings.
Signature
ts
pet.play(state: string): void| Parameter or method | Behavior |
|---|---|
play(name) | Sets the base state and restarts playback. |
setDefaultState(name) | Sets fallback; throws for an unknown name. |
state | Read-only name of the currently rendered state. |
How it works
play clears a triggered reaction. A drag or hover reaction can still take priority. An unknown name resolves to the fallback. When a non-looping base animation completes it returns to the fallback. setDefaultState changes that fallback but does not immediately play it.
Use it in your project
Choose your stack. Each example includes setup and cleanup. For Vanilla, pass a mounted container at least 360px tall and keep the returned cleanup. Method names are identical across frameworks.
ts
import { createPet } from '@flowscape-ui/mimimo';
import { mochi } from '@flowscape-ui/mimimo-pet-mochi';
// Call after your container mounts.
export function mountCompanion(container: HTMLElement) {
const lifetime = new AbortController();
const pet = createPet({
character: mochi,
container,
anchor: 'center',
});
pet.setDefaultState('idle');
pet.play('sleep');
return () => {
lifetime.abort();
pet.destroy();
};
}
// Keep the returned cleanup and call it when the view is removed.tsx
'use client';
import { useEffect, useRef } from 'react';
import { createPet } from '@flowscape-ui/mimimo';
import { mochi } from '@flowscape-ui/mimimo-pet-mochi';
export default function Companion() {
const host = useRef<HTMLDivElement>(null);
useEffect(() => {
const container = host.current;
if (!container) return;
const lifetime = new AbortController();
const pet = createPet({
character: mochi,
container,
anchor: 'center',
});
pet.setDefaultState('idle');
pet.play('sleep');
return () => {
lifetime.abort();
pet.destroy();
};
}, []);
return <div ref={host} style={{ height: 360, position: 'relative' }} />;
}vue
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { createPet } from '@flowscape-ui/mimimo';
import { mochi } from '@flowscape-ui/mimimo-pet-mochi';
const host = ref<HTMLElement | null>(null);
let cleanup: (() => void) | undefined;
onMounted(() => {
const container = host.value;
if (!container) return;
const lifetime = new AbortController();
const pet = createPet({
character: mochi,
container,
anchor: 'center',
});
pet.setDefaultState('idle');
pet.play('sleep');
cleanup = () => {
lifetime.abort();
pet.destroy();
};
});
onBeforeUnmount(() => cleanup?.());
</script>
<template>
<div ref="host" style="height: 360px; position: relative" />
</template>