-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defs block for extracting gradients. #74
Comments
Hey @jaywolters, it's not really within svg-sprite's scope to perform SVG manipulations like that. You could, however, use a custom svg-sprite doesn't use Cheerio but operates on a raw DOM implementation instead (xmldom). Please see here for some info about custom callback transformations. Finally, I am considering adding a global post-transformation on the final sprite, but that has to be done yet. Hope this helps!? Cheers, |
Using the Symbol method and when working with pre-colored Icons that use gradients-- moving the gradients to a <Defs> block is essential for FF and IE to render the SVG correctly. This problem has been brought up in a few SO posts and grunt/gulp-svgstore made the change just recently. Thanks for your response. |
Good to know, thanks for this info. You don't happen to have some resources handy elaborating on the topic? I might consider hardwiring it then ... |
Refer to David Bushell's blog post on SVG and MDN using gradients in SVG you will see where it shows the SVG markup and how all the Gradients are placed inside of the <defs> block. |
Thanks a lot for these useful resources! You convinced me that this should be a core feature of svg-sprite, as it will greatly improve the overall usability of the sprites. I'll try to implement the gradient extraction, but please give me some time for that. Unfortunately, I'm extremely overloaded these days an am having a hard time getting all my things done. Again, thanks a lot! |
Necromancy! Since nobody is working on this issue I decided to quickly create a solution using transforms in config. @jkphl in the next few weeks I could create PR with this solution (cleaned up, of course). Though I am not sure where to place this kind of functionality. Any ideas?
|
Basically I have added the |
Any updates on this? |
Sharing this in case it helps anyone (most likely my future self). Snippet will add your transform: [
function (svg) {
const defs = `
<defs>
<linearGradient id="gradient" x1="56.0849658%" y1="50%" x2="105.749837%" y2="128.905203%">
<stop stop-color="#FFAE6C" offset="0%"></stop>
<stop stop-color="#FF4E51" offset="100%"></stop>
</linearGradient>
</defs>
`
return svg
.replace('<symbol', `${defs.split(/\n/).map((s) => s.trim()).join('')}<symbol`)
.replace(/<symbol/gi, '<symbol fill="url(#gradient)"')
},
], |
I ran into this issue as well recently (gradients not appearing in spritesheet SVGs), and also came up with a solution. This just replaces all the individual svg: {
transform: [
function(svg) {
let globalDefs = '';
return svg
.replace(/<defs>(.+?)<\/defs>/g, (_match, def) => { globalDefs += def })
.replace('<symbol', `<defs>${ globalDefs }</defs><symbol`);
},
],
}, Minor edit: Obviously, |
Any updates on this? Any alternative library? |
With Symbols is it possible to configure svg-sprite to extract all the gradients <linearGradient> (etc) into a <defs> block -- or will I need to use Cheerio for that?
The text was updated successfully, but these errors were encountered: