首页 > 解决方案 > 如何在 React with Hooks 中使用animejs线条绘制

问题描述

首先,我真的很想学习 DOM,因此任何基于 DOM 如何构造事物的解释都会非常有帮助(:

我正在尝试使用带有钩子的 React 中的 Animejs 进行简单的线条绘制。

到目前为止,我的代码是:

import React, { useEffect } from "react";
import anime from "animejs";
import "../App.css";

function Home() {
  function animation() {
    anime({
      targets: "greeting",
      strokeDashoffset: [anime.setDashoffset, 0],
      easing: "easeInOutSine",
      duration: 150,
      delay: function (el, i) {
        return i * 250;
      },
      direction: "alternate",
      loop: true,
    });
  }

  useEffect(() => {
    animation();
  }, []);

  return (
    <div>
      <div>
        <svg
          width="595"
          height="116"
          viewBox="0 0 595 116"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
          className="greeting"
        >
          <mask
            id="path-1-outside-1"
            maskUnits="userSpaceOnUse"
            x="0.289062"
            y="0"
            width="595"
            height="116"
            fill="black"
          >
            <rect fill="white" x="0.289062" width="595" height="116" />
            <path d="M33.9609 78.7266L35.9297 92.2266L38.8125 80.0625L59.0625 8.625H70.4531L90.2109 80.0625L93.0234 92.4375L95.2031 78.6562L111.094 8.625H124.664L99.8438 111H87.5391L66.4453 36.3984L64.8281 28.5938L63.2109 36.3984L41.3438 111H29.0391L4.28906 8.625H17.7891L33.9609 78.7266Z" />
            <path d="M166.992 112.406C156.68 112.406 148.289 109.031 141.82 102.281C135.352 95.4844 132.117 86.4141 132.117 75.0703V72.6797C132.117 65.1328 133.547 58.4062 136.406 52.5C139.312 46.5469 143.344 41.9062 148.5 38.5781C153.703 35.2031 159.328 33.5156 165.375 33.5156C175.266 33.5156 182.953 36.7734 188.438 43.2891C193.922 49.8047 196.664 59.1328 196.664 71.2734V76.6875H145.125C145.312 84.1875 147.492 90.2578 151.664 94.8984C155.883 99.4922 161.227 101.789 167.695 101.789C172.289 101.789 176.18 100.852 179.367 98.9766C182.555 97.1016 185.344 94.6172 187.734 91.5234L195.68 97.7109C189.305 107.508 179.742 112.406 166.992 112.406ZM165.375 44.2031C160.125 44.2031 155.719 46.125 152.156 49.9688C148.594 53.7656 146.391 59.1094 145.547 66H183.656V65.0156C183.281 58.4062 181.5 53.2969 178.312 49.6875C175.125 46.0312 170.812 44.2031 165.375 44.2031Z" />
            <path d="M225.914 111H212.906V3H225.914V111Z" />
            <path d="M277.312 101.789C281.953 101.789 286.008 100.383 289.477 97.5703C292.945 94.7578 294.867 91.2422 295.242 87.0234H307.547C307.312 91.3828 305.812 95.5312 303.047 99.4688C300.281 103.406 296.578 106.547 291.938 108.891C287.344 111.234 282.469 112.406 277.312 112.406C266.953 112.406 258.703 108.961 252.562 102.07C246.469 95.1328 243.422 85.6641 243.422 73.6641V71.4844C243.422 64.0781 244.781 57.4922 247.5 51.7266C250.219 45.9609 254.109 41.4844 259.172 38.2969C264.281 35.1094 270.305 33.5156 277.242 33.5156C285.773 33.5156 292.852 36.0703 298.477 41.1797C304.148 46.2891 307.172 52.9219 307.547 61.0781H295.242C294.867 56.1562 292.992 52.125 289.617 48.9844C286.289 45.7969 282.164 44.2031 277.242 44.2031C270.633 44.2031 265.5 46.5938 261.844 51.375C258.234 56.1094 256.43 62.9766 256.43 71.9766V74.4375C256.43 83.2031 258.234 89.9531 261.844 94.6875C265.453 99.4219 270.609 101.789 277.312 101.789Z" />
            <path d="M318.727 72.2578C318.727 64.8047 320.18 58.1016 323.086 52.1484C326.039 46.1953 330.117 41.6016 335.32 38.3672C340.57 35.1328 346.547 33.5156 353.25 33.5156C363.609 33.5156 371.977 37.1016 378.352 44.2734C384.773 51.4453 387.984 60.9844 387.984 72.8906V73.8047C387.984 81.2109 386.555 87.8672 383.695 93.7734C380.883 99.6328 376.828 104.203 371.531 107.484C366.281 110.766 360.234 112.406 353.391 112.406C343.078 112.406 334.711 108.82 328.289 101.648C321.914 94.4766 318.727 84.9844 318.727 73.1719V72.2578ZM331.805 73.8047C331.805 82.2422 333.75 89.0156 337.641 94.125C341.578 99.2344 346.828 101.789 353.391 101.789C360 101.789 365.25 99.2109 369.141 94.0547C373.031 88.8516 374.977 81.5859 374.977 72.2578C374.977 63.9141 372.984 57.1641 369 52.0078C365.062 46.8047 359.812 44.2031 353.25 44.2031C346.828 44.2031 341.648 46.7578 337.711 51.8672C333.773 56.9766 331.805 64.2891 331.805 73.8047Z" />
            <path d="M416.531 34.9219L416.883 43.3594C422.461 36.7969 429.984 33.5156 439.453 33.5156C450.094 33.5156 457.336 37.5938 461.18 45.75C463.711 42.0938 466.992 39.1406 471.023 36.8906C475.102 34.6406 479.906 33.5156 485.438 33.5156C502.125 33.5156 510.609 42.3516 510.891 60.0234V111H497.883V60.7969C497.883 55.3594 496.641 51.3047 494.156 48.6328C491.672 45.9141 487.5 44.5547 481.641 44.5547C476.812 44.5547 472.805 46.0078 469.617 48.9141C466.43 51.7734 464.578 55.6406 464.062 60.5156V111H450.984V61.1484C450.984 50.0859 445.57 44.5547 434.742 44.5547C426.211 44.5547 420.375 48.1875 417.234 55.4531V111H404.227V34.9219H416.531Z" />
            <path d="M562.148 112.406C551.836 112.406 543.445 109.031 536.977 102.281C530.508 95.4844 527.273 86.4141 527.273 75.0703V72.6797C527.273 65.1328 528.703 58.4062 531.562 52.5C534.469 46.5469 538.5 41.9062 543.656 38.5781C548.859 35.2031 554.484 33.5156 560.531 33.5156C570.422 33.5156 578.109 36.7734 583.594 43.2891C589.078 49.8047 591.82 59.1328 591.82 71.2734V76.6875H540.281C540.469 84.1875 542.648 90.2578 546.82 94.8984C551.039 99.4922 556.383 101.789 562.852 101.789C567.445 101.789 571.336 100.852 574.523 98.9766C577.711 97.1016 580.5 94.6172 582.891 91.5234L590.836 97.7109C584.461 107.508 574.898 112.406 562.148 112.406ZM560.531 44.2031C555.281 44.2031 550.875 46.125 547.312 49.9688C543.75 53.7656 541.547 59.1094 540.703 66H578.812V65.0156C578.438 58.4062 576.656 53.2969 573.469 49.6875C570.281 46.0312 565.969 44.2031 560.531 44.2031Z" />
          </mask>
          <path
            d="M33.9609 78.7266L35.9297 92.2266L38.8125 80.0625L59.0625 8.625H70.4531L90.2109 80.0625L93.0234 92.4375L95.2031 78.6562L111.094 8.625H124.664L99.8438 111H87.5391L66.4453 36.3984L64.8281 28.5938L63.2109 36.3984L41.3438 111H29.0391L4.28906 8.625H17.7891L33.9609 78.7266Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
          <path
            d="M166.992 112.406C156.68 112.406 148.289 109.031 141.82 102.281C135.352 95.4844 132.117 86.4141 132.117 75.0703V72.6797C132.117 65.1328 133.547 58.4062 136.406 52.5C139.312 46.5469 143.344 41.9062 148.5 38.5781C153.703 35.2031 159.328 33.5156 165.375 33.5156C175.266 33.5156 182.953 36.7734 188.438 43.2891C193.922 49.8047 196.664 59.1328 196.664 71.2734V76.6875H145.125C145.312 84.1875 147.492 90.2578 151.664 94.8984C155.883 99.4922 161.227 101.789 167.695 101.789C172.289 101.789 176.18 100.852 179.367 98.9766C182.555 97.1016 185.344 94.6172 187.734 91.5234L195.68 97.7109C189.305 107.508 179.742 112.406 166.992 112.406ZM165.375 44.2031C160.125 44.2031 155.719 46.125 152.156 49.9688C148.594 53.7656 146.391 59.1094 145.547 66H183.656V65.0156C183.281 58.4062 181.5 53.2969 178.312 49.6875C175.125 46.0312 170.812 44.2031 165.375 44.2031Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
          <path
            d="M225.914 111H212.906V3H225.914V111Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
          <path
            d="M277.312 101.789C281.953 101.789 286.008 100.383 289.477 97.5703C292.945 94.7578 294.867 91.2422 295.242 87.0234H307.547C307.312 91.3828 305.812 95.5312 303.047 99.4688C300.281 103.406 296.578 106.547 291.938 108.891C287.344 111.234 282.469 112.406 277.312 112.406C266.953 112.406 258.703 108.961 252.562 102.07C246.469 95.1328 243.422 85.6641 243.422 73.6641V71.4844C243.422 64.0781 244.781 57.4922 247.5 51.7266C250.219 45.9609 254.109 41.4844 259.172 38.2969C264.281 35.1094 270.305 33.5156 277.242 33.5156C285.773 33.5156 292.852 36.0703 298.477 41.1797C304.148 46.2891 307.172 52.9219 307.547 61.0781H295.242C294.867 56.1562 292.992 52.125 289.617 48.9844C286.289 45.7969 282.164 44.2031 277.242 44.2031C270.633 44.2031 265.5 46.5938 261.844 51.375C258.234 56.1094 256.43 62.9766 256.43 71.9766V74.4375C256.43 83.2031 258.234 89.9531 261.844 94.6875C265.453 99.4219 270.609 101.789 277.312 101.789Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
          <path
            d="M318.727 72.2578C318.727 64.8047 320.18 58.1016 323.086 52.1484C326.039 46.1953 330.117 41.6016 335.32 38.3672C340.57 35.1328 346.547 33.5156 353.25 33.5156C363.609 33.5156 371.977 37.1016 378.352 44.2734C384.773 51.4453 387.984 60.9844 387.984 72.8906V73.8047C387.984 81.2109 386.555 87.8672 383.695 93.7734C380.883 99.6328 376.828 104.203 371.531 107.484C366.281 110.766 360.234 112.406 353.391 112.406C343.078 112.406 334.711 108.82 328.289 101.648C321.914 94.4766 318.727 84.9844 318.727 73.1719V72.2578ZM331.805 73.8047C331.805 82.2422 333.75 89.0156 337.641 94.125C341.578 99.2344 346.828 101.789 353.391 101.789C360 101.789 365.25 99.2109 369.141 94.0547C373.031 88.8516 374.977 81.5859 374.977 72.2578C374.977 63.9141 372.984 57.1641 369 52.0078C365.062 46.8047 359.812 44.2031 353.25 44.2031C346.828 44.2031 341.648 46.7578 337.711 51.8672C333.773 56.9766 331.805 64.2891 331.805 73.8047Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
          <path
            d="M416.531 34.9219L416.883 43.3594C422.461 36.7969 429.984 33.5156 439.453 33.5156C450.094 33.5156 457.336 37.5938 461.18 45.75C463.711 42.0938 466.992 39.1406 471.023 36.8906C475.102 34.6406 479.906 33.5156 485.438 33.5156C502.125 33.5156 510.609 42.3516 510.891 60.0234V111H497.883V60.7969C497.883 55.3594 496.641 51.3047 494.156 48.6328C491.672 45.9141 487.5 44.5547 481.641 44.5547C476.812 44.5547 472.805 46.0078 469.617 48.9141C466.43 51.7734 464.578 55.6406 464.062 60.5156V111H450.984V61.1484C450.984 50.0859 445.57 44.5547 434.742 44.5547C426.211 44.5547 420.375 48.1875 417.234 55.4531V111H404.227V34.9219H416.531Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
          <path
            d="M562.148 112.406C551.836 112.406 543.445 109.031 536.977 102.281C530.508 95.4844 527.273 86.4141 527.273 75.0703V72.6797C527.273 65.1328 528.703 58.4062 531.562 52.5C534.469 46.5469 538.5 41.9062 543.656 38.5781C548.859 35.2031 554.484 33.5156 560.531 33.5156C570.422 33.5156 578.109 36.7734 583.594 43.2891C589.078 49.8047 591.82 59.1328 591.82 71.2734V76.6875H540.281C540.469 84.1875 542.648 90.2578 546.82 94.8984C551.039 99.4922 556.383 101.789 562.852 101.789C567.445 101.789 571.336 100.852 574.523 98.9766C577.711 97.1016 580.5 94.6172 582.891 91.5234L590.836 97.7109C584.461 107.508 574.898 112.406 562.148 112.406ZM560.531 44.2031C555.281 44.2031 550.875 46.125 547.312 49.9688C543.75 53.7656 541.547 59.1094 540.703 66H578.812V65.0156C578.438 58.4062 576.656 53.2969 573.469 49.6875C570.281 46.0312 565.969 44.2031 560.531 44.2031Z"
            stroke="#759CFF"
            stroke-width="6"
            mask="url(#path-1-outside-1)"
          />
        </svg>
      </div>
    </div>
  );
}

export default Home;

我的问题是没有任何运行时错误,但我不知道为什么这个词没有动画。我从 figma 导出了带有轮廓且没有填充的 SVG 文件。有谁知道我在这里做错了什么,所以它没有动画?

标签: reactjsdomanime.js

解决方案


推荐阅读