Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active September 6, 2022 20:12
Show Gist options
  • Save yano3nora/1f05cbcc617ef483b61364fbf75dcd2f to your computer and use it in GitHub Desktop.
Save yano3nora/1f05cbcc617ef483b61364fbf75dcd2f to your computer and use it in GitHub Desktop.
[js: Motion One + motion-hooks] Modern WAAPI library + React hook wrapper. #js

Overview

motion.dev/
motion-hooks - github.com/tanvesh01/motion-hooks
tanvesh01/motion-hooks Web Animations APIを使った軽量で高速なオープンソースのアニメーションライブラリ・「Motion One」

まだ beta 感あるけど、多機能かつ調整ツールもあってゲーム開発には凄くマッチしそう。あと web animation api の学習コストが css の animation 周りに比べて楽そうなので、チーム利用もいけそうな気がする。

点滅とか hover でふわっと拡大とか、シンプルなのは従来のもので十分だけど、柔軟なタイミング制御とか、タイムラインとか、もうちょい色々やるならこいつ使っていいかな。

Getting Started

$ npm i motion-hooks motion
import React, { useEffect } from 'react'
import { stagger } from 'motion'
import { useMotionAnimate } from 'motion-hooks'

function App() {
  const { play, isFinished, replay } = useMotionAnimate(
    '.listItem', // ref にもできるぽい
    { y: -20, opacity: 1 },
    {
      delay: stagger(0.3),
      duration: 0.5,
      easing: [0.22, 0.03, 0.26, 1],
    },
    {
      onFinish: () => {
        // こういうのあると安心する
      },
    },
  )

  // Play the animation on mount of the component
  useEffect(() => {
    play()
  }, [])

  return (
    // Replay the animation anytime by calling a function, anywhere
    <div className="App">
      <button disabled={!isFinished} onClick={() => replay()}>
        Replay
      </button>

      <ul className="list">
        <li className="listItem">Item 1</li>
        <li className="listItem">Item 2</li>
        <li className="listItem">Item 3</li>
      </ul>
    </div>
  )
}

Timeline

useMotionTimeline

import { AnimationOptions } from 'motion'
import { useMotionTimeline } from 'motion-hooks'

const { play, isFinished, replay } = useMotionTimeline(
  [
    // You can use Refs too!
    [gifRef, { scale: [0, 1.2], opacity: 1 }],
    ['.heading', { y: [50, 0], opacity: [0, 1] }],
    ['.container p', { opacity: 1 }],
  ],
  { duration: 2 } as AnimationOptions,
  {
    onFinish: () => {},
  },
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment