简体   繁体   English

如何在 Next JS 中的 Scroll 上绘制 SVG

[英]How to draw an SVG on Scroll in Next JS

Background背景
I've been reading everything, and watching everything and can't quite figure it out.我一直在阅读所有内容,观看所有内容,但无法完全弄清楚。 I know the very basics of React JS from FCC and have went through a large chunk of Fireship.io course on Next JS.我从 FCC 了解 React JS 的基本知识,并且在 Next JS 上学习了大量的 Fireship.io 课程。 Any help is appreciated.任何帮助表示赞赏。

What I Am Attempting To Do我正在尝试做什么
I have an SVG logo, and I am attempting to animate it when scrolling.我有一个 SVG 徽标,我试图在滚动时对其进行动画处理。 I'm trying to follow this tutorial: https://www.w3schools.com/howto/howto_js_scrolldrawing.asp which is written for Vanilla JS, HTML/CSS.我正在尝试遵循本教程: https://www.w3schools.com/howto/howto_js_scrolldrawing.asp ,它是为 Vanilla JS、HTML/CSS 编写的。 I've made a website with Vanilla JS and hypertext preprocessor, but am now attempting to learn front-end frameworks, and thus am implementing in React.我已经用 Vanilla JS 和超文本预处理器创建了一个网站,但现在我正在尝试学习前端框架,因此我在 React 中实现。

What Seems to Be The Problem似乎是什么问题
I am using functional components, and these documents are pretty confusing.我正在使用功能组件,这些文档非常混乱。 Apparently, you can't use the ref keywords with functional components and would have to use classes, which I am trying to avoid.显然,您不能将ref关键字与功能组件一起使用,并且必须使用我试图避免的类。

Current Work当前工作
This is what I currently have:这是我目前拥有的:

// Packages to import:
import React from 'react';

export default function Home() {
  // Initialization: 
  var homepage_logo_main_content = React.useRef(); /* So why we can use this but not . notation for font awesome icons? */
  
  // Update value of SVG on scroll to present animation effect:
  function animate_svg_on_scroll(value) {
    var homepage_logo_main_content_total_length = homepage_logo_main_content.current.getTotalLength(); // Getting the total length of the SVG path.
    homepage_logo_main_content.current.style.strokeDasharray = homepage_logo_main_content_total_length; // Get the starting position of the draw.
    homepage_logo_main_content.current.style.strokeDashoffset = homepage_logo_main_content_total_length;
    var draw = homepage_logo_main_content_total_length * value;
  };

  React.useEffect(() => {
    const handleScroll = event => {
      const value = window.scrollY / 1069;
      animate_svg_on_scroll(value);
    };

    window.addEventListener('scroll', handleScroll);

    return () => {
      window.removeEventListener('scroll', handleScroll);
    };
  }, []);

  return (
    <div className = "homepage_main_div">
      <svg xmlns = "http://www.w3.org/2000/svg" version="1.0" width="400.000000pt" height = "400.000000pt" viewBox = "0 0 400.000000 400.000000" preserveAspectRatio = "xMidYMid meet" className = "homepage_logo">
        <g transform = "translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill = "#000000" stroke = "none">
          <path id = "tiger" ref = {homepage_logo_main_content} d = "M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0 -2000z 2z"/>
          <path d = "M820 2176 c-25 -7 -61 -21 -80 -31 -54 -28 -148 -124 -187 -189 -32 -55 -83 -176 -83 -197 0 -15 265 -10 348 7 190 38 282 121 282 251 0 69 -21 110 -75 143 -45 28 -140 35 -205 16z"/>
          <path d = "M1800 1585 c0 -161 2 -186 16 -192 9 -3 20 0 25 8 5 8 8 93 6 189 -2 173 -2 175 -24 178 l-23 3 0 -186z"/>
        </g>
      </svg>

      <h1> Welcome to this math website </h1>
    </div>
  )
}

Next JS doesn't seem to throw an error, and the website renders. Next JS 似乎没有抛出错误,并且网站呈现。 When I scroll, the useEffect hook and the event listener does output the Y scroll position.当我滚动时,useEffect 钩子和事件侦听器执行 output Y 滚动 position。 However, nothing in the animate_svg_on_scroll seems to work.但是, animate_svg_on_scroll中的任何内容似乎都不起作用。

If TL;DR, please see the link above from W3 schools and suggest how to implement in Next JS and what resources to watch/read.如果 TL;DR,请参阅上面来自 W3 学校的链接,并建议如何在 Next JS 中实施以及观看/阅读哪些资源。 Thank you so very much (been working on this for a couple of days).非常感谢你(已经为此工作了几天)。

Your path is not display because of stroke="none" in g tag.由于g标签中的stroke="none" ,您的路径未显示。 Change it to another color then path will display将其更改为另一种颜色,然后将显示路径

<svg
    xmlns="http://www.w3.org/2000/svg"
    version="1.0"
    width="400.000000pt"
    height="400.000000pt"
    viewBox="0 0 400.000000 400.000000"
    preserveAspectRatio="xMidYMid meet"
    className="homepage_logo"
  >
    <g
      transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)"
      fill="none"
      stroke="red"
      stroke-width="20"
    >
      <path
        id="tiger"
        d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0 -2000z 2z"
        ref={homepage_logo_main_content}
      />
      <path d="M820 2176 c-25 -7 -61 -21 -80 -31 -54 -28 -148 -124 -187 -189 -32 -55 -83 -176 -83 -197 0 -15 265 -10 348 7 190 38 282 121 282 251 0 69 -21 110 -75 143 -45 28 -140 35 -205 16z" />
      <path d="M1800 1585 c0 -161 2 -186 16 -192 9 -3 20 0 25 8 5 8 8 93 6 189 -2 173 -2 175 -24 178 l-23 3 0 -186z" />
    </g>
  </svg>

You can refer my codesandbox你可以参考我的代码框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM