ガイド · 02
タイミング、スプリング、組み合わせ ドライバーを選び、組み合わせ、止めたり置き換えたりしたときに何が起きるかを正確に把握します。
timing、spring、decay timing は決まった duration の間カーブに沿って動きます。spring は物理法則に従い、決まった長さがありません。decay は速度を持って始まり、減速していきます。
同じドライバーが数値、ValueXY、レコードを受け付けるので、{ x, y, opacity } を一緒に動かせます。
JavaScript がオフになっています。下のコードがサンプルの全体です。
App.jsx import { Easing , parallel, spring, timing, useAnimatedValue } from '@kkwaenggwari/animated' ;
import './styles.css' ;
export default function App ({ stiffness = 180 , damping = 12 , mass = 1 , duration = 500 } ) {
const eased = useAnimatedValue (0 );
const standard = useAnimatedValue (0 );
const custom = useAnimatedValue (0 );
const go = ( ) => {
const toValue = custom.getValue () < 0.5 ? 1 : 0 ;
parallel ([
timing (eased, { toValue, duration, easing : Easing .inOut (Easing .cubic ) }),
spring (standard, { toValue }),
spring (custom, { toValue, stiffness, damping, mass }),
]).start ();
};
const lanes = [
['timing' , eased],
['spring()' , standard],
['spring(props)' , custom],
];
return (
<div className ="stage" >
<button type ="button" onClick ={go} >
Go
</button >
{lanes.map(([label, value]) => (
<div key ={label} className ="lane" >
<code > {label}</code >
<div className ="rail" >
<span
style ={{
left: `calc (${value } * (100 % - 40px ))`,
}}
/>
</div >
</div >
))}
</div >
);
}styles.css .stage {
display : grid;
gap : 14px ;
padding : 28px ;
}
button {
justify-self : start;
min-height : 44px ;
padding : 0 24px ;
border : 0 ;
border-radius : 999px ;
background : #2854ff ;
color : #fff ;
font : inherit;
font-weight : 650 ;
cursor : pointer;
}
.lane {
display : grid;
grid-template-columns : 120px 1 fr;
align-items : center;
gap : 12px ;
}
.lane code {
color : #1a33c7 ;
font-size : 13px ;
}
.rail {
position : relative;
height : 40px ;
border-radius : 999px ;
background : #f5f6fb ;
box-shadow : inset -2px 0 0 #e4e6ef ;
}
.rail span {
position : absolute;
top : 0 ;
width : 40px ;
height : 40px ;
border-radius : 50% ;
background : #8fb0ff ;
}
.lane :first-of-type .rail span {
background : #5c7cff ;
}
.lane :last-of-type .rail span {
background : #2854ff ;
}スプリングのパラメーター パラメーターの指定方法はひとつだけ使います。何も指定しなければ React Native のデフォルトのスプリング(tension 40、friction 7)になります。スプリングの velocity のデフォルトは、中断した軌跡の速度です。
springs.js コードをコピー spring (value, { toValue : 1 });
spring (value, { toValue : 1 , stiffness : 260 , damping : 20 });
spring (value, { toValue : 1 , bounciness : 8 , speed : 12 });
spring (value, { toValue : 1 , tension : 40 , friction : 7 }); 組み合わせる sequence は順番に、parallel は同時に実行し、stagger は遅延をつけて順に開始し、delay は待ち、loop は繰り返します。結果もまたコントローラーなので、組み合わせを入れ子にできます。
JavaScript がオフになっています。下のコードがサンプルの全体です。
App.jsx import {
delay,
loop,
parallel,
sequence,
spring,
stagger,
timing,
useAnimated,
} from '@kkwaenggwari/animated' ;
import './styles.css' ;
const keys = ['a' , 'b' , 'c' , 'd' , 'e' ];
export default function App ({ mode = 'stagger' , gap = 90 } ) {
const bars = useAnimated (
{ a : 0 , b : 0 , c : 0 , d : 0 , e : 0 },
(value ) => {
const group = (list ) =>
mode === 'sequence'
? sequence (list)
: mode === 'parallel'
? parallel (list)
: stagger (gap, list);
const rise = keys.map ((key ) =>
spring (value[key], { toValue : 1 , stiffness : 220 , damping : 16 }),
);
const fall = keys.map ((key ) => timing (value[key], { toValue : 0 , duration : 260 }));
loop (sequence ([group (rise), delay (500 ), group (fall), delay (300 )]));
},
[mode, gap],
);
return (
<div className ="stage" >
{keys.map((key) => (
<span key ={key} style ={{ transform: `scaleY (calc (0.15 + ${bars [key ]} * 0.85 ))` }} />
))}
</div >
);
}styles.css .stage {
display : flex;
align-items : flex-end;
justify-content : center;
gap : 14px ;
height : 260px ;
padding : 32px ;
}
.stage span {
width : 44px ;
height : 100% ;
border-radius : 12px ;
background : #2854ff ;
transform-origin : bottom;
}
.stage span :nth-child (2 n) {
background : #8fb0ff ;
}
.stage span :nth-child (3 ) {
background : #5c7cff ;
}start、stop、reset すべてのコントローラーに start(callback?)、stop()、reset() があります。コールバックは { finished } を受け取り、モーションが止められたか置き換えられた場合は false です。
同じ値で新しいドライバーを開始すると、実行中のものを現在の位置から置き換えます。どちらかの端へ飛ぶことはありません。
controller.js コードをコピー const move = timing (value, { toValue : 1 , duration : 600 });
move.start (({ finished } ) => {
});
move.stop ();
move.reset (); 別の値を追いかける toValue に動く値を渡すと、その値が変わるたびにドライバーが再開します。React Native のトラッキングと同じです。follow(target, source) は、同じスプリングのつながりを名前付きの関数にしたものです。
tracking.js コードをコピー
spring (badge, { toValue : pointer, stiffness : 300 , damping : 26 }).start ();
follow (badge, pointer, { stiffness : 300 , damping : 26 });イージングとキーフレーム Easing は React Native と同じです: linear、quad、cubic、sin、circle、exp、elastic、back、bounce、bezier と、in、out、inOut のヘルパー。keyframes(value, frames, options) は、省略可能な times と repeat を使って、ひとつの値を複数の数値へ順に動かします。