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.

Warning while Interpolating animated SVG: '"###.###" is not a valid color or brush'

See original GitHub issue

I am attempting to import svgs from a class template and animating their colors:

My .svgrrc.js:

/* eslint-disable no-shadow */
module.exports = {
  native: true,
  template(
    { template },
    opts,
    {
      imports, componentName, props, jsx, exports,
    },
  ) {
    const flowTpl = template.smart({ plugins: ['flow'] })
    return flowTpl.ast`
      ${imports}
      class ${componentName} extends React.Component<any, any> {
        render () {
          const ${props} = this.props;
          return ${jsx}
        }
  
      }
      ${exports}
    `;
  },
  dimensions: false,
  replaceAttrValues: {
    color: "{props.color}",
    color1: "{props.color1}",
    color2: "{props.color2}",
    color3: "{props.color3}",
    color4: "{props.color4}",
    color5: "{props.color5}",
    opacity: "{props.opacity}",
    opacity1: "{props.opacity1}",
    opacity2: "{props.opacity2}",
    opacity3: "{props.opacity3}",
    opacity4: "{props.opacity4}",
    opacity5: "{props.opacity5}",
    opt: "{props.opt}",
    opt1: "{props.opt1}",
    opt2: "{props.opt2}",
    opt3: "{props.opt3}",
    opt4: "{props.opt4}",
    opt5: "{props.opt5}"
  }
}

I can then animate the imported svgs like so:

import SmileSvg from './SmileSvg.svg';
const AnimatedSmileSvg = Animated.createAnimatedComponent(SmileSvg);

SmileSvg.svg replaces the named attributes (color, color1, opacity1):

<svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect opacity="opacity1" width="80" height="80" rx="40" fill="color1"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="..." fill="color"/>
</svg>

However, when I interpolate I get warnings that appear like the following:

“42853378097.68554577” is not a valid number or brush. The color then does not interpolate.

This occurs only on iOS where I have to process the color,

const colorMap = Platform.select({
  ios: {
    enabled: [processColor(COLORS.WHITE), processColor(COLORS.WHITE), 0.2],
    selected: [processColor(COLORS.PRIMARY_TEAL), processColor(COLORS.WHITE), 1],
  }, 
  android: {
    enabled: [COLORS.WHITE, COLORS.WHITE, 0.2],
    selected: [COLORS.WHITE, COLORS.PRIMARY_TEAL, 1],
  },
});
...
export default class GridIcon extends Component<GridIconProps, GridIconState> {
... // GridIcon constructor
this.state = {
      pressAnim: new Animated.Value(0),
};
...
// GridIcon onPress
Animated.timing(
  this.state.pressAnim,
  { toValue, duration: 200 },
);
...
<AnimatedSmileSvg
      color1={this.state.pressAnim.interpolate({
        inputRange: [0, 1],
        outputRange: [colorMap.enabled[0], colorMap.selected[0]],
      }},
      color={this.state.pressAnim.interpolate({
        inputRange: [0, 1],
        outputRange: [colorMap.enabled[1], colorMap.selected[1]],
      }},
      opacity1={this.state.pressAnim.interpolate({
        inputRange: [0, 1],
        outputRange: [colorMap.enabled[2], colorMap.selected[2]],
      })}
/>

Does anyone have a similar issue?

Notes:

Calling useNativeDriver = true on the Animated.timing function causes an invariant violation:

“Attempt to get native tag from node not marked as ‘native’”

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
msandcommented, Oct 1, 2019

I mean v0.61.0

0reactions
syplcommented, Nov 4, 2020

I was getting this error, trying to replace “@fill”. For some reason the space before the closing tag is important.

This works:

<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M21.4853 12C21.4853 12.5523 21.0376 13 20.4853 13H3.51472C2.96243 13 2.51472 12.5523 2.51472 12C2.51472 11.4477 2.96243 11 3.51472 11H20.4853C21.0376 11 21.4853 11.4477 21.4853 12Z" fill="@fill" />
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 2.51471C12.5523 2.51471 13 2.96242 13 3.51471L13 20.4853C13 21.0376 12.5523 21.4853 12 21.4853C11.4477 21.4853 11 21.0376 11 20.4853L11 3.51471C11 2.96242 11.4477 2.51471 12 2.51471Z" fill="@fill"/>
</svg>

This doesn’t (throws warning "@fill" is not a valid color or brush:

<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M21.4853 12C21.4853 12.5523 21.0376 13 20.4853 13H3.51472C2.96243 13 2.51472 12.5523 2.51472 12C2.51472 11.4477 2.96243 11 3.51472 11H20.4853C21.0376 11 21.4853 11.4477 21.4853 12Z" fill="@fill" />
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 2.51471C12.5523 2.51471 13 2.96242 13 3.51471L13 20.4853C13 21.0376 12.5523 21.4853 12 21.4853C11.4477 21.4853 11 21.0376 11 20.4853L11 3.51471C11 2.96242 11.4477 2.51471 12 2.51471Z" fill="@fill"/>
</svg>

Can you spot the difference? It’s the end of line 2:

Good:

fill="@fill" />

Bad:

fill="@fill"/>

Even weirder is that it doesn’t cause a problem on line three, which ends:

fill="@fill"/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native - how to use local SVG file (and color it)
After doing this, I was able to set fill and stroke properties with the values I needed.
Read more >
Animated SVGs In React Native - Medium
Animate the color of an SVG using react-native-svg and react-native animated API. To give you a short background story, the first app I...
Read more >
Save your files in Photoshop - Adobe Support
To save a file in Photoshop, go to the File menu and select any of the Save commands — Save, Save As, or...
Read more >
Changelog - Kleki
Fixed: Brush shortcut changes brush when switching from different tool ... Fixed: import uncropped SVG as layer - image rasterized before transform ...
Read more >
Using SVG with CSS3 and HTML5
This is SVG not only as illustrations, but as graphical docu‐ ... true even when creating inline SVG elements in an HTML docu‐....
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