question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] SVG animation fails when no initial value set

See original GitHub issue

Describe the bug When I don’t define default/initial values as attributes, the component fails with the error:

Uncaught TypeError: Cannot read property 'red' of null

To Reproduce As an example, this component/animation fails. This should be a looping spinner.

const animation = {
  strokeDasharray: ['1px, 200px', '100px, 200px', '100px, 200px'],
  strokeDashoffset: [0, -15, -125],
  transition: { duration: 1.4, ease: 'linear' },
};

const Spinner: FC = (): ReactElement => {
  const controls = useAnimation();

  const handleAnimationComplete = useCallback(() => {
    controls.start(animation);
  }, [controls]);

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

  return (
    <svg viewBox='22 22 44 44'>
      <Circle
        animate={controls}
        onAnimationComplete={handleAnimationComplete}
        cx='44'
        cy='44'
        r='20.2'
        fill='none'
        strokeWidth='3.6'
      />
    </svg>
  );
};

Expected behavior Modifing the circle to the below works, but this is the only way to resolve the issue. Setting the styles in CSS (with styled-components as an example) does not help.

      <Circle
        animate={controls}
        onAnimationComplete={handleAnimationComplete}
        strokeDasharray='80px, 200px'
        strokeDashoffset='0'
        cx='44'
        cy='44'
        r='20.2'
        fill='none'
        strokeWidth='3.6'
      />

Desktop (please complete the following information):

  • OS: MacOS 10.14.5 (18F132)
  • Browser Version: Chrome 75.0.3770.100 (Official Build) (64-bit)

Additional context I had the same issue when trying to add a rotate animation to an SVG element. I could not find any workaround for this one, so any help would be greatly appreciated.

const Spinner: FC= (): ReactElement => {
  return (
    <motion.svg viewBox='22 22 44 44' animate={{ rotate: 100 }}>
      <Circle />
    </motion.svg>
  );
};

Other than this, I’m loving Framer Motion - amazing work!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
mattgperrycommented, Jul 22, 2019

Currently working on this bug but if anyone runs into similar, you can always explicitly set an initial value via the initial prop.

const Spinner = () => (
    <motion.svg viewBox='22 22 44 44' initial={{ rotate: 0 }} animate={{ rotate: 100 }}>
      <Circle />
    </motion.svg>
  )
1reaction
mrmckebcommented, Jul 25, 2019

Thanks for this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

begin - SVG: Scalable Vector Graphics - MDN Web Docs
The element's animation start time is defined relative to the begin or active end of another animation. A valid syncbase-value consists of an...
Read more >
SVG Animation throws a 'Cannot read property 'style' of null ...
My issue is the building of an SVG arch that expands upon ... the application throws an error: Cannot read property 'style' of...
Read more >
A Guide to SVG Animations (SMIL) - CSS-Tricks
The animation elements were initially defined in the SMIL ... If the value for attributeType is not explicitly set or is set to...
Read more >
Animation – SVG 1.1 (Second Edition) - W3C
SMIL Animation defines error processing behavior where the document continues to ... The default value is 'auto'. ... There is no default value...
Read more >
Most Common GSAP Mistakes - Learning Center - GreenSock
Using fromTo() when from() or to() would work; Not setting ALL transforms with GSAP; Not using xPercent and yPercent; Recreating animations over ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found