React.js ライブラリ「react-typewriter-effect」を使ってタイプライティングを実装する

React.js ライブラリ「react-typewriter-effect」を使ってタイプライティングを実装する

ライブラリ「react-typewriter-effect」をインストールすると、タイプライティングを実装することが可能です。ここでは、react.jsでreact-typewriter-effectを利用するための手順と簡単な使い方を記述してます。

環境

  • OS  CentOS Stream release 8
  • node V12.16.3
  • npm 6.14.7
  • React 16.13.0

react.js環境構築

下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。

create-react-app react-app

react-typewriter-effectインストール

作成したプロジェクトに移動して、インストールします。

## 作成したプロジェクトに移動
cd react-app
 
## インストール
npm install react-typewriter-effect

react-typewriter-effect使い方

srcディレクトリにsample.jsと名前で下記のコードを記述します。

import React from 'react'
import TypeWriterEffect from 'react-typewriter-effect'

const Sample = () => {

  return (
    <div>
     <TypeWriterEffect
        textStyle={{
          fontFamily: 'Red Hat Display',
          color: '#3F3D56',
          fontWeight: 500,
          fontSize: '1.5em',
        }}
        startDelay={2000}
        cursorColor="#3F3D56"
        multiText={[
          'Hey there, This is a type writer animation package',
          'it consist of two types...',
          'Single text display and multi text display',
          'Fonts can be customized.',
          'The type speed can be customized as well',
        ]}
        loop={true}
        nextTextDelay={1000}
        typeSpeed={30}
      />
    </div>
  )
}

export default Sample

次に、srcディレクトリ配下にあるApp.jsを下記のように編集します。

import React from 'react';
import Sample from './sample';
import './App.css';

function App() {
  const style = {
    width: "50%",
    margin: "0 auto",
    marginTop: 150,
  };
  return (
    <div className="App">
      <div style={style}>
        <Sample />
      </div>
    </div>
  );
}

export default App;

実行します。

npm start

ブラウザから http://プライベートIP:3000にアクセスすると、タイプライティング実装されていることが確認できます。