@qyu/anim-react
v2.0.0
Published
Declarative animations for react
Maintainers
Readme
@qyu/anim-react
React hooks for @qyu/anim-core to manage declarative animations
Concept
- Library mainly focuses on controled declarative animations
- It also provides some kinds of uncontrolled animations for convenience purpose
import * as ar from "@qyu/anim-react"
const App = () => {
const [target, target_set] = useState(100)
const [velocity, velocity_set] = useState(1e-1)
// run animation with animation frames
ar.useRunAnimInterval({
src: ar.useAnimLine({
init: ar.useInputConstant({
state: 0
}),
config: ar.useInputDynamicSet({
target,
velocity,
effect: state => {
console.log("Animation Tick: ", state)
}
}, [state])
}),
})
return <>
<button onClick={() => { target_set(target_old => target_old + 100) } }>
Increase target {target}
</button>
<button onClick={() => { velocity_set(velocity_old => velocity_old * 2) } }>
Increase velocity {velocity}
</button>
</>
}Animation Flow
- Create animation with
useAnim*hook - Pass it to
useRunAnim*hook - When initial condition changes - it restarts, when config changes - it adapts
- Process of continuing the animation from the current point with new config/target is called adapting
Input Values
- To track the changes in input conditions, the special kind of value is used
- You create input with
useInput*hook - It is either static or dynamic kind. Dynamic allows controlling animation outside of react, while static will always restart animation on change
- There multiple ways of creating an InputValue:
useInputConstantis static and never changesuseInputStaticchanges on deps changeuseInputStaticLazychanges on deps change, accepts getter instead of a valueuseInputDynamiccreated from signal, changes when source signal changesuseInputDynamicSetmanages the signal itself, schedules an update after react render if deps have changeduseInputDynamicSetLazymanages the signal itself, schedules an update after react render if deps have changed, accepts getter instead of a value
Running the animations
- Animations are ran by
useAnimRunIntervalhook - It accepts the source animation, and some config
.spreadparameter means, that when there is multiple animations (eg. merged), when one restarts - it will try to preserve the state of others- It runs animations using provided
.schedulerparameter. If no.schedulerprovided, it will use animation-frames in browser orsetTimeoutin node
Kinds of controled animations
- Library implements many variants of controlled animations allowing to combine them into complex sequences
useAnimLinebasic linear animation (A -> A1)useAnimSpringbasic spring animation (A -> A1)useAnimMergeallows emitting multiple animation in parallel (A | B -> A1 | B1)useAnimSequenceemits animations one-by-one, will merge finished animation on adapt (A -> A1 & B -> B1)useAnimSequenceStrictemits animation one-by-one, does not merge finished animations, so only emits one-at-a-time (A -> A1 & B -> B1)useAnimChainis similar to theSequence, but all animations in aChainshare the same point (A -> A1 -> A2)useAnimChainMapis like aChainbut allows animating multiple threads with optional gaps (a: A, b: B -> a: A1 -> a: A2, b: B2)useAnimLooploops the animation (( A -> A1 ) * n)useAnimClusterprevents.spreadparameter in the runner to preserve the state. Meaning if one animation restarts - all douseAnimPlaybackspeed up or slow down the animationuseAnimPipeallows to convert incompatible points (eg. for animatingchain(linear, spring))useStyleMapSpringanimates styles to given targets, uses spring animationsuseStyleMapLineanimates styles to given targets, uses linear animations
import * as ar from "@qyu/anim-react"
const App = () => {
const [target, target_set] = useState(100)
ar.useRunAnimInterval({
scheduler: scheduler_raf,
// animates the same point in-order
src: ar.useAnimChain([
// adapt line point to the spring one
ar.useAnimPipe({
src: ar.useAnimLine({
init: ar.useInputConstant({
state: 0
}),
config: ar.useInputDynamicSet({
target: target,
velocity: 1e-1,
effect: state => {
console.log("Animation 1: ", state)
}
})
}),
pipei: (point_input: AnimSpring_Point): AnimLine_Point => ({
state: point_input.state
}),
pipeo: (point_input: AnimLine_Point): AnimSpring_Point => ({
state: point_input.state,
velocity: 1e-1
}),
}),
// see later about usePath*
ar.usePathSpring(ar.useInputDynamicSet({
natfreq: 1e-2,
dampratio: 0.1,
target: target * 2,
effect: state => {
console.log("Animation 2: ", state)
}
}))
] as const)
})
return <button onClick={ () => { target_set(target_old => target_old + 100) } }>
Increase target {target}
</button>
}Path and Init for animations
- Every animation defines
InitandPathconfig Inittells how where to start,Pathtells where (and how) to gouseAnim*hooks define bothInitandPath,useInit*andusePathallows to define them separately- For
Chain(and some others) animations share aPoint, so only the first animation needs to define theInit, others can skip it
Uncontrolled animations
- Uncontrolled animations can be used through
motioncomponents - It includes
Layoutanimations and simple to-target animations for styles
Layout Animation
- When position or size of an element changes, it will be slowly animated to new position instead of instant change
- All changes to the component position must trigger a rerender of that component
transformproperty is used to transition. That means layou changes instantly, only visual representation is animatedElementshould never reach the size of0as you can not scale the 0-size
import * as ar from "@qyu/anim-react"
import { motion } from "@qyu/anim-react"
const App = () => {
const [state, state_set] = r.useState(100)
return <div>
<button onClick={() => state_set(n => n + 6.28 * Math.random())}>
Move
</button>
<motion.div
// minimal setup
layout
// with config
layout={{
kind: "track",
// transformOrigin of the element, center center is default
origin: [0.5, 0.5],
// providing id allows to do layou animation over different elements
id: "red-square",
// it uses spring animations, that is the default config
anim_translate_config: { natfreq: 1e-2, dampratio: 1, },
anim_scale_config: { natfreq: 1e-2, dampratio: 1, precision: { velocity: 1e-3, displacement: 1e-2 } },
}}
// this is just normal style property of a div, it is not animated by default
style={{
background: "red",
width: `${(Math.sin(state) + 2) * 100}px`,
height: `${(Math.cos(state) + 2) * 100}px`,
position: "relative",
left: `${(Math.sin(state) + 2) * 100}px`,
top: `${(Math.cos(state) + 2) * 100}px`,
}}
/>
</div>
}Child Distortion
- Sometimes children of the animated
Elementmay get distorted. - In such cases you need to wrap them into their own
motioncomponent layout="normalize"will only animate position, while keeping scale stable- Note, that
transformproperty does not work oninlineelements, so you need to setdisplay: inline-block
import * as ar from "@qyu/anim-react"
import { motion } from "@qyu/anim-react"
const App = () => {
const [state, state_set] = r.useState(100)
return <div>
<button onClick={() => state_set(n => n + 6.28 * Math.random())}>
Move
</button>
<motion.div
layout
style={{
background: "red",
width: `${(Math.sin(state) + 2) * 150}px`,
height: `${(Math.cos(state) + 2) * 150}px`,
position: "relative",
left: `${(Math.sin(state) + 2) * 150}px`,
top: `${(Math.cos(state) + 2) * 150}px`,
}}
>
<motion.div layout>
Text will be distorted
</motion.div>
<motion.div layout>
<motion.span layout="normalize" style={{ display: "inline-block" }}>
Text will be preserved
</motion.span>
</motion.div>
</motion.div>
</div>
}Layout animations in scaled environments
- Lets see the following example
import * as ar from "@qyu/anim-react"
import { motion } from "@qyu/anim-react"
const App = () => {
const [state, state_set] = r.useState(100)
return <div>
<button onClick={() => state_set(n => n + 6.28 * Math.random())}>
Move
</button>
<div style={{ width: "1000px", height: "1000px", background: "gray", transform: "scale(0.5)" }}>
<motion.div
layout
style={{
background: "red",
width: `${(Math.sin(state) + 2) * 150}px`,
height: `${(Math.cos(state) + 2) * 150}px`,
position: "relative",
left: `${(Math.sin(state) + 2) * 150}px`,
top: `${(Math.cos(state) + 2) * 150}px`,
}}
/>
</div>
</div>
}- Here animated element stays inside of the element with
scale(0.5)and it breaks the animation - To avoid that, use
motionelement withlayout={{ kind: "static", scale: 0.5 }} - Layout animation can not be performed in a negative-scale environment
import * as ar from "@qyu/anim-react"
import { motion } from "@qyu/anim-react"
const App = () => {
const [state, state_set] = r.useState(100)
return <div>
<button onClick={() => state_set(n => n + 6.28 * Math.random())}>
Move
</button>
<motion.div
layout={{ kind: "static", scale: 0.5, }}
style={{ width: "1000px", height: "1000px", background: "gray", transform: "scale(0.5)" }}
>
<motion.div
layout
style={{
background: "red",
width: `${(Math.sin(state) + 2) * 150}px`,
height: `${(Math.cos(state) + 2) * 150}px`,
position: "relative",
left: `${(Math.sin(state) + 2) * 150}px`,
top: `${(Math.cos(state) + 2) * 150}px`,
}}
/>
</motion.div>
</div>
}Virtual Layout Animation
- When you can not directly render the element as
motionelement, you can still use virtual layout tracking
import * as ar from "@qyu/anim-react"
import { motion } from "@qyu/anim-react"
const App = () => {
const [state, state_set] = r.useState(100)
return <div>
<button onClick={() => state_set(n => n + 6.28 * Math.random())}>
Move
</button>
<motion.div
layout={{ kind: "static", scale: 0.5, }}
style={{ width: "1000px", height: "1000px", background: "gray", transform: "scale(0.5)" }}
>
{/* must be rendered above the element */}
<CmpMotion_LayoutVirtual layout target={() => ref_element.current}>
<div
ref={ref_element}
style={{
background: "red",
width: `${(Math.sin(state) + 2) * 150}px`,
height: `${(Math.cos(state) + 2) * 150}px`,
position: "relative",
left: `${(Math.sin(state) + 2) * 150}px`,
top: `${(Math.cos(state) + 2) * 150}px`,
}}
/>
</CmpMotion_LayoutVirtual>
</motion.div>
</div>
}To-Target style animations
- Animates styles of
motionelement to specified targets - Uses spring animations
- Under the hood just runs
useAnimStyleMapSpring, so interface is the same
import * as ar from "@qyu/anim-react"
import { motion } from "@qyu/anim-react"
const App = () => {
const [state, state_set] = r.useState(100)
return <div>
<button onClick={() => state_set(n => n + 100)}>
Move
</button>
<motion.div
// that is the default config
anim_style_config={{ natfreq: 1e-2, dampratio: 0.5, }}
anim_style={ar.transvalue_parse({
width: `${state}px`,
background: `rgb(${( state + 100 ) % 256}, ${(state + 200) % 256}, ${(state + 300) % 256})`,
height: ar.transvalue_new_cssunit({
from: 0,
unit: "px",
target: state,
// will restart when state goes beyound 500
deps: [state > 500],
config: {
tracker: { input: state => console.log(state) }
}
}),
})}
/>
</div>
}- It uses
TransValueto determain theInit,Pathand format of each property - You can use
transvalue_parseto identify common properties such as units (eg. 100px) or colors - For colors only
rgb,rgbaandhexformats are available. You also need to put commas between arguments - Transforms and other complex properties are not auto-recognised, you need to provide them manually
