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.

svg with animate is not useful

See original GitHub issue

I have a svg file some code like: <circle transform="translate(8 0)" cx="0" cy="16" r="10" fill="#fff"> <animate attributeName="r" values="0; 4; 0; 0" dur="1.2s" repeatCount="indefinite" begin="0" keytimes="0;0.2;0.7;1" keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.6 0.4 0.8" calcMode="spline" /> </circle> but animate is not active

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
msandcommented, Aug 10, 2019

A bit refactored to make it more reusable:

import * as React from 'react';
import { Animated, Easing } from 'react-native';
import { Svg, Circle } from 'react-native-svg';
const AnimatedCircle = Animated.createAnimatedComponent(Circle);

function animateSpline({
  values,
  dur,
  repeatCount,
  begin,
  keyTimes,
  keySplines,
}) {
  const duration = dur * 1000;
  const t = new Animated.Value(keyTimes[0]);
  const splines = keySplines.map((spline, i) => {
    const [x1, y1, x2, y2] = spline;
    const fromValue = keyTimes[i];
    const toValue = keyTimes[i + 1];
    return Animated.timing(t, {
      toValue,
      delay: i == 0 ? begin : 0,
      duration: duration * (toValue - fromValue),
      easing: Easing.bezier(x1, y1, x2, y2),
      useNativeDriver: true,
    });
  });
  const iterations = repeatCount === 'indefinite' ? -1 : +repeatCount;
  const animation = Animated.loop(Animated.sequence(splines), { iterations });
  const value = t.interpolate({
    inputRange: keyTimes,
    outputRange: values,
  });
  return { t, animation, value };
}

export default () => {
  /*
    <circle cx="16" cy="16" r="16">
      <animate 
        attributeName="r" 
        values="0; 4; 0; 0" 
        dur="1.2s" 
        repeatCount="indefinite" 
        begin="0" 
        keytimes="0;0.2;0.7;1" 
        keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.6 0.4 0.8" 
        calcMode="spline" />
    </circle>
  */
  const { animation, value } = animateSpline({
    values: [0, 4, 0, 0],
    dur: 1.2,
    repeatCount: 'indefinite',
    begin: 0,
    keyTimes: [0, 0.2, 0.7, 1],
    keySplines: [
      [0.2, 0.2, 0.4, 0.8],
      [0.2, 0.6, 0.4, 0.8],
      [0.2, 0.6, 0.4, 0.8],
    ],
  });
  animation.start();
  return (
    <Svg width="100%" height="100%" viewBox="0 0 32 32">
      <AnimatedCircle cx="16" cy="16" r={value} />
    </Svg>
  );
};

1reaction
msandcommented, Aug 10, 2019

Couldn’t help myself 😄 https://snack.expo.io/@msand/animated-svg-with-bezier-spline-calcmode

import * as React from 'react';
import { Animated, Easing } from 'react-native';
import { Svg, Circle } from 'react-native-svg';
const AnimatedCircle = Animated.createAnimatedComponent(Circle);
/*
  <circle cx="16" cy="16" r="16">
    <animate 
      attributeName="r" 
      values="0; 4; 0; 0" 
      dur="1.2s" 
      repeatCount="indefinite" 
      begin="0" 
      keytimes="0;0.2;0.7;1" 
      keySplines="0.2 0.2 0.4 0.8;0.2 0.6 0.4 0.8;0.2 0.6 0.4 0.8" 
      calcMode="spline" />
  </circle>
*/
const values = [0, 4, 0, 0];
const dur = 1.2 * 1000;
const keyTimes = [0, 0.2, 0.7, 1];
const keySplines = [
  [0.2, 0.2, 0.4, 0.8],
  [0.2, 0.6, 0.4, 0.8],
  [0.2, 0.6, 0.4, 0.8],
];
export default () => {
  const t = new Animated.Value(keyTimes[0]);
  const splines = keySplines.map((spline, i) => {
    const [x1, y1, x2, y2] = spline;
    const fromValue = keyTimes[i];
    const toValue = keyTimes[i + 1];
    return Animated.timing(t, {
      toValue,
      duration: dur * (toValue - fromValue),
      easing: Easing.bezier(x1, y1, x2, y2),
      useNativeDriver: true,
    });
  });
  Animated.loop(Animated.sequence(splines)).start();
  return (
    <Svg width="100%" height="100%" viewBox="0 0 32 32">
      <AnimatedCircle
        cx="16"
        cy="16"
        r={t.interpolate({
          inputRange: keyTimes,
          outputRange: values,
        })}
      />
    </Svg>
  );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS animation do not work for svg in <img> - Stack Overflow
You can't animate the internals of an <img> from the outside. Even if it is an SVG. There are two reasons for this:...
Read more >
Animated SVG not animating - WordPress.org
I'm using Elementor and trying to use an SVG file that I got from an online SVG generator as the background to a...
Read more >
How I stopped worrying and learned to animate SVG - Medium
first, I'll have to say, SVG's arent good for everything. and as always you should ask yourself what about alternaties like GIF or...
Read more >
<animate> - SVG: Scalable Vector Graphics - MDN Web Docs
The SVG element provides a way to animate an attribute of an element over time.
Read more >
A how-to guide to SVG animation - TinyMCE
SVG animations using JavaScript ... If all of these are not fancy enough already, you can always resort to JavaScript. Animating SVG elements...
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