Lottieで作成されたアニメーションの制御を試してみる

以前にLottieで作成したアニメーションをサイトに組み込む記事を投稿しましたが、今回は組み込んだアニメーションをJavaScriptで制御する方法を試してみます。

サンプルコード

まずは以前の記事でも試した、基本的な埋め込みです。
CDNでプラグインを読み込みます。

<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.12.2/lottie.min.js"></script>

アニメーションを埋め込む要素を追加します。

<div id="sample"></div>

要素のサイズを設定します。

#sample {
	width: 200px;
}

最後に、JavaScriptで埋め込みの設定を行います。

const demo = lottie.loadAnimation({
  container: document.getElementById('sample'), // アニメーションを追加する要素
  renderer: 'svg', // レンダリングのタイプ
  loop: false, // アニメーションをループさせるかどうか
  autoplay: false, // アニメーションを自動再生するかどうか
  path: 'data.json' // アニメーションのjsonファイルのパス
});

これでLottieの埋め込みができましたが、設定のloopやautoplayをfalseにしているので、この時点ではアニメーションは開始されません。
Lottieを埋め込むデモページ

埋め込んだLottieのファイルをJavaScriptで制御してみます。
詳しくはドキュメントをご確認ください。

まずはメソッドを試してみます。

<button id="play">play()</button>
<button id="stop">stop()</button>
<button id="pause">pause()</button>
<button id="setSpeed">setSpeed(0.5)</button>
<button id="goToAndStop">goToAndStop(400)</button>
<button id="goToAndPlay">goToAndPlay(200)</button>
<button id="setDirection">setDirection(-1)</button>
<button id="destroy">destroy()</button>
<button id="getDuration">getDuration()</button>
<button id="getDurationTrue">getDuration(true)</button>
document.getElementById('play').addEventListener('click', function() {
  demo.play();
});
document.getElementById('stop').addEventListener('click', function() {
  demo.stop();
});
document.getElementById('pause').addEventListener('click', function() {
  demo.pause();
});
document.getElementById('setSpeed').addEventListener('click', function() {
  demo.setSpeed(0.5);
});
document.getElementById('goToAndStop').addEventListener('click', function() {
  demo.goToAndStop(400);
});
document.getElementById('goToAndPlay').addEventListener('click', function() {
  demo.goToAndPlay(200);
});
document.getElementById('setDirection').addEventListener('click', function() {
  demo.setDirection(-1);
});
document.getElementById('destroy').addEventListener('click', function() {
  demo.destroy();
});
document.getElementById('getDuration').addEventListener('click', function() {
  console.log(demo.getDuration());
});
document.getElementById('getDuration').addEventListener('click', function() {
  console.log(demo.getDuration(true));
});

Lottieのメソッドのデモページ

play() アニメーションを再生。
再生終了後は終了地点で停止になり、再度再生してもアニメーションが開始されないので、goToAndStop(0)で開始地点に戻すなどの対応が必要になるので注意。
stop() アニメーション再生の終了。
状態は開始時点に戻る。
pause() アニメーション再生の一時停止。
状態は停止した地点で止まる。
setSpeed(speed) アニメーションの速度の変更。
speedの初期値は1。
goToAndStop(value, isFrame) 指定した秒数に移動して、一時停止状態にする。
valueは数値でミリ秒を指定。
isFrameにtrueを指定した場合、フレームベースでの指定になる。(初期値はfalse)
goToAndPlay(value, isFrame) 指定した秒数に移動して、再生を開始する。
valueは数値でミリ秒を指定。
isFrameにtrueを指定した場合、フレームベースでの指定になる。(初期値はfalse)
setDirection(direction) アニメーション再生の方向を変更。
directionの初期値は1(順方向)で、-1で逆方向になる。
destroy() Lottieの埋め込みを破棄。
getDuration(inFrames) アニメーションの秒数またはフレーム数を返す。
inFramesの初期値はfalseで秒数を、trueの場合はフレーム数を返す。

次にイベントです。

complete アニメーション完了後に実行。
アニメーションをstop()やpause()で途中で止めた場合や、loopをtrueにしている場合は実行されない。
デモページ
loopComplete ループでのアニメーション完了毎に実行。
デモページ
enterFrame アニメーションのフレーム変更毎に実行。
デモページ
destroy Lottieの埋め込み破棄後に実行。
デモページ

参考サイト

このエントリーをはてなブックマークに追加

関連記事

コメントを残す

メールアドレスが公開されることはありません。
* が付いている欄は必須項目です

CAPTCHA


コメントが承認されるまで時間がかかります。

2024年4月
 123456
78910111213
14151617181920
21222324252627
282930