admin管理员组文章数量:1430811
I'm creating an SVG rectangle using the Raphael JavaScript library, and assigning it a fill using the following line of code:
this.myBox.attr({fill: 'white'});
This works fine. However, now I want to tie this to a linear gradient. I have borrowed some gradient code to test it out, using the code below:
<defs>
<linearGradient id="orange_red" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);
stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,0,0);
stop-opacity:1"/>
</linearGradient>
</defs>
And in order to assign this to the shape, I tried doing this:
this.myBox.attr({fill:url(#orange_red)});
In this case, I get the error 'Illegal character '#''.
Where am I going wrong?
I'm creating an SVG rectangle using the Raphael JavaScript library, and assigning it a fill using the following line of code:
this.myBox.attr({fill: 'white'});
This works fine. However, now I want to tie this to a linear gradient. I have borrowed some gradient code to test it out, using the code below:
<defs>
<linearGradient id="orange_red" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);
stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,0,0);
stop-opacity:1"/>
</linearGradient>
</defs>
And in order to assign this to the shape, I tried doing this:
this.myBox.attr({fill:url(#orange_red)});
In this case, I get the error 'Illegal character '#''.
Where am I going wrong?
Share Improve this question asked Jun 23, 2010 at 8:49 Jack RoscoeJack Roscoe 4,34310 gold badges38 silver badges46 bronze badges2 Answers
Reset to default 4this.myBox.attr({fill: "315-rgb(255,255,0)-rgb(255,0,0)"});
If you will tie this to linear gradient the way you do, it obviously wouldn’t work in IE. And if you don’t care about IE, then what the point to use Raphaël in the first place?
You've simply forgotten to quote the value string.
this.myBox.attr({fill: 'url(#orange_red)'});
It is a quirk of JavaScript that in the object literal syntax ({
...}
) the name part fill
doesn't always need to be quoted as 'fill'
. However the value can be of any type and so for strings always needs to be quoted.
本文标签: htmlHow to assign gradient fill to SVG shape via JavaScriptStack Overflow
版权声明:本文标题:html - How to assign gradient fill to SVG shape via JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745550181a2662906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论