Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JS change SVG gradient-color[#]

ANSWERED

 

Changing the fill attribute on a rect element works fine:

if (data.colorBackground) {
    bGround.style.fill = data.colorBackground;
}

But I cannot find an equivalent for changing a gradient-color[1/2] attribute of a gradientRect element:

 

if (data.colorBackground) {
    bGroundGrade.style.gradientColor1 = data.colorBackground;
}

Produces: Unhandled TypeError: Cannot set property 'gradientColor1' of undefined

 

Meanwhile,

if (data.colorBackground) {
    bGroundGrade.gradientColor1 = data.colorBackground;
}

doesn't throw an error, but neither does it change my background gradient color.

 

Any help would be appreciated, thanks!

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Try: 

bGroundGrade.gradient.colors.c1 = data.colorBackground;

View best answer in original post

Best Answer
4 REPLIES 4

Hey, I'm going through the same thing and will definitely find an answer somehow. I'll let you know what I discover, if anything. Do you have any idea how to change the viewport-fill of an SVG by chance? Pretty similar problem.

Best Answer
0 Votes

Try: 

bGroundGrade.gradient.colors.c1 = data.colorBackground;
Best Answer

That worked wonderfully, thank you. Any hints on changing the viewport fill color?

Best Answer
0 Votes

I decided that I can just use a rect across the entire background and change its .style.fill property instead.

Best Answer
0 Votes