You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I investigated this further after looking at the test case shared. It turned out that I was running encodeURIComponent on the SVG first which I usually do for SVG data-uri on the web, but node-canvas didn't like that. It worked without doing any prior encoding. Thanks @LinusU
Hmm, this is interesting. We aim to be as compatible with the web as possible, so we should probably be doing decodeURI or decodeURIComponent in our code 🤔
Without ";base64", the data (as a sequence of octets) is represented using ASCII encoding for octets inside the range of safe URL characters and using the standard %xx hex encoding of URLs for octets outside that range.
-- https://www.rfc-editor.org/rfc/rfc2397#section-2
Hey I have invested quite some time in performance testing for converting svgData into a rasterized image for node-canvas and browser. I recommend to differentiate between browser and node-canvas to get the maximum performance.
Why does it even matter? For our software we use 1000s of different svgs over different frames extracted from lottie files and manipulated with additional CSS to color specific svg elements by classes. So svg creation speed is super crucial for us.
Ideal Solution
This is my ideal solution, but needs differentiation between browser and node-canvas. I haven't came across any svg that needed more than # to be replaced. And I have tested quite some. But maybe there are more characters.
/**
* Returns serialized SVG source.
*/
private static getSerializedSvgSrc(svgString, isBrowser: boolean): string | Buffer {
// This is way faster than base64 encoding.
// svgString.replace(/#/g, "%23") is additionally around 30 times faster than encodeURIComponent(svgString).
if (isBrowser) {
return 'data:image/svg+xml;utf8,' + svgString.replace(/#/g, "%23");
} else {
return Buffer.from(svgString, 'utf-8');
}
}
Alternatives
By the way the fastest conversion to have a valid input for backend would be this here: return 'data:image/svg+xml;utf8,' + svgString, but I have looked into the node-canvas code an the string is later read and then put in a Buffer anyway so its probably faster to put it in the Buffer directly.
If you just want to have some code that works for browser and node-canvas at the same time you could for example use:
@LinusU please don't add decodeURIComponent to the canvas code, because it would mean you first have to encode and then decode. Probably meaning to loose 1-2 ms per svg vs. only using a small fraction of a 1 ms. And if you do it anyway at least keep the option to pass the Buffer without the decoding directly. I would prefer, if we instead add some more info to the README file on how to handle svgs.
Postscript
By the way the 2.50.5 version of librsvg is crap. It fails to render some svg properties that are supported in later versions. I would recommend to adjust the prebuild binaries to use 2.54.4 or later. This fixed a lot of issues I had with svg rendering. See here for an OS supporting this version: https://repology.org/project/librsvg/versions
Activity
LinusU commentedon Oct 11, 2022
@josephrexme there is a test case covering this:
node-canvas/test/image.test.js
Lines 132 to 143 in b3e7df3
Could it be that your build of Canvas doesn't include SVG support at all? That would happen if
librsvgwasn't found when building.Could you try and run this command and paste its output:
node -p 'require("canvas").rsvgVersion'josephrexme commentedon Oct 11, 2022
@LinusU thanks for your response. I got
2.50.5after running the commandjosephrexme commentedon Oct 11, 2022
So I investigated this further after looking at the test case shared. It turned out that I was running
encodeURIComponenton the SVG first which I usually do for SVG data-uri on the web, but node-canvas didn't like that. It worked without doing any prior encoding. Thanks @LinusULinusU commentedon Oct 12, 2022
Hmm, this is interesting. We aim to be as compatible with the web as possible, so we should probably be doing
decodeURIordecodeURIComponentin our code 🤔Opening this again with a slightly changed title.
[-]Support for SVG data-URI with Image()[/-][+]Data-URI doesn't handle URI-encoding (percent-escaping)[/+]zbjornson commentedon Oct 22, 2022
Indeed:
flohall commentedon Jan 1, 2023
Hey I have invested quite some time in performance testing for converting svgData into a rasterized image for node-canvas and browser. I recommend to differentiate between browser and node-canvas to get the maximum performance.
Why does it even matter? For our software we use 1000s of different svgs over different frames extracted from lottie files and manipulated with additional CSS to color specific svg elements by classes. So svg creation speed is super crucial for us.
Ideal Solution
This is my ideal solution, but needs differentiation between browser and node-canvas. I haven't came across any svg that needed more than
#to be replaced. And I have tested quite some. But maybe there are more characters.Alternatives
By the way the fastest conversion to have a valid input for backend would be this here:
return 'data:image/svg+xml;utf8,' + svgString, but I have looked into the node-canvas code an the string is later read and then put in a Buffer anyway so its probably faster to put it in the Buffer directly.If you just want to have some code that works for browser and node-canvas at the same time you could for example use:
or this should do the job in both environments as well, but faster (also unescape is deprecated):
Some more options I have compared
vs
vs
vs
Conclusion
@LinusU please don't add
decodeURIComponentto the canvas code, because it would mean you first have to encode and then decode. Probably meaning to loose 1-2 ms per svg vs. only using a small fraction of a 1 ms. And if you do it anyway at least keep the option to pass the Buffer without the decoding directly. I would prefer, if we instead add some more info to the README file on how to handle svgs.Postscript
By the way the 2.50.5 version of librsvg is crap. It fails to render some svg properties that are supported in later versions. I would recommend to adjust the prebuild binaries to use 2.54.4 or later. This fixed a lot of issues I had with svg rendering. See here for an OS supporting this version: https://repology.org/project/librsvg/versions