简体   繁体   English

Framer 运动路径 whileInview

[英]Framer Motion Path whileInview

Why the path animation doesn't work in Google Chrome when animated with whileInView,but in FireFox it works?为什么在使用 whileInView 设置动画时路径 animation 在 Google Chrome 中不起作用,但在 FireFox 中却有效? This is svg i want to animate and his path below.这是 svg 我想制作动画,下面是他的路径。

<motion.path <运动路径

       initial={{pathLength:0}}
       whileInView={{pathLength:1}}
       transition={{duration:.5,delay:1.2}}
        d="M45.2116 113.844C41.991 97.7723 43.8806 74.0318 54.5835 60.6377C59.4391 54.561 76.8443 44.698 82.3209 55.553C88.3209 67.4466 67.1346 70.6046 64.5162 58.711C62.2011 48.1945 69.8349 39.3482 78.5027 34.5433C93.8215 26.0521 110.121 25.8784 125.958 33.0516"
        stroke="white"
        strokeWidth="0.78976"
        strokeMiterlimit="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </motion.svg>

      

I faced same problem when animating SVG paths.我在为 SVG 路径设置动画时遇到了同样的问题。 Ended up using react-intersection-observer to find if the element is in view and using animate prop instead of whileInView on motion.path最终使用react-intersection-observer来查找元素是否在视图中,并在 motion.path 上使用动画道具而不是 whileInView

import { InView } from "react-intersection-observer"
import { motion } from "framer-motion"

const Component = () => {

const [entered, setEntered] = setState(false)

return(
    <InView
        as="div"
        onChange={(inView, entry) => {
          console.log("Inview:", inView, "entry:", entry)
          if (inView === true) {
            setEntered(true)
          }
        }}
        root={null}
        rootMargin="0px"
        threshold={0.8}>
        <svg>
            <motion.path
            initial={{pathLength:0}}
            animate={entered === true ? {pathLength:1} : {pathLength:0}}
            transition={{duration:.5,delay:1.2}}
            d="M45.2116 113.844C41.991 97.7723 43.8806 74.0318 54.5835 60.6377C59.4391 54.561 76.8443 44.698 82.3209 55.553C88.3209 67.4466 67.1346 70.6046 64.5162 58.711C62.2011 48.1945 69.8349 39.3482 78.5027 34.5433C93.8215 26.0521 110.121 25.8784 125.958 33.0516"
            stroke="white"
            strokeWidth="0.78976"
            strokeMiterlimit="1.5"
            strokeLinecap="round"
            strokeLinejoin="round"
            />
        </svg>
    </Inview>
    )
}

export default Component

I had the same issue, but I solved this using framer-motion 's useInView hook我遇到了同样的问题,但我使用framer-motionuseInView钩子解决了这个问题

import { motion, useInView } from 'framer-motion'
import React, { useRef } from 'react'

export function AnimatedPath() {
  const ref = useRef<SVGPathElement>(null)
  const isInView = useInView(ref)

  return (
    <svg>
      <motion.path
        ref={ref}
        initial={{ pathLength: 0 }}
        animate={{ pathLength: isInView ? 1 : 0 }}
        transition={{ duration: 0.5, delay: 1.2 }}
        d="M45.2116 113.844C41.991 97.7723 43.8806 74.0318 54.5835 60.6377C59.4391 54.561 76.8443 44.698 82.3209 55.553C88.3209 67.4466 67.1346 70.6046 64.5162 58.711C62.2011 48.1945 69.8349 39.3482 78.5027 34.5433C93.8215 26.0521 110.121 25.8784 125.958 33.0516"
        stroke="white"
        strokeWidth="0.78976"
        strokeMiterlimit="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  )
}

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

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