How to specify encoding for SVG background rendering?
See original GitHub issueRendering SVG node background images does work in principle (the white background and the text “Node” are rendered as SVG <rect>
and <text>
respectively):
https://jsfiddle.net/u7asnwdp/6/
But if a SVG <text>
element contains an Umlaut (e.g. ä ö ü) SVG rendering breaks completely (here the red background is a result of Cytoscape style 'background-color': 'red'
):
https://jsfiddle.net/jri_/hrxp965e/
Apparently this is an error in the Cytoscape SVG rendering code.
That pure SVG (without Cytoscape involved) has no problem with rendering Umlaute is shown here:
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
09: SVG with Data URIs - CSS-Tricks
If I generate an SVG icon, save the base64 encoded string and use that string in a thousand img tags, does the browser...
Read more >SVG background in CSS without base64 encoding - Possible?
In the past, I always base64 encode the source of the SVG image, which is not needed.
Read more >CSS: Using raw svg in the URL parameter of a background ...
... URL encoded form. i.e. I wanted to render the ▾ character ( U+25BE ). ... background-image: url("data:image/svg+xml;charset=utf8,%3Csvg ...
Read more >Render SVG on webpage | remarkablemark
Encoding §. The one catch with data URI is that certain characters need to be encoded. For example, the following image will fail...
Read more >Getting started - SVG: Scalable Vector Graphics | MDN
Copy the code and paste it in a file demo1.svg. Then open the file in Firefox. It will render as shown in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If it’s working in some browsers and not others, then it’s a bug in the way that the particular browser draws an svg image to canvas. Browsers seem to have differences in how they render svg to canvas as compared to how they render svg in the html dom.
A few years ago, even basic svg in the dom had many bugs and differences between the browsers. Maybe it’s a bit better now, but unreliable browser support for svg is one of the reasons cyjs uses canvas.
There’s not much that can be done on the cyjs end to remedy that. So it’s probably best to file bugs in the issue trackers for the offending browsers.
This is the most reliable way of using svg data uri images that we’ve found:
This way you can explicitly specify the encoding — in both the url and the svg itself just in case the browser ignores one — and
encodeURIComponent()
handles non-ascii characters. Maybe that will work better for you. Even if it does, I think it still makes sense to report bugs in browser issue trackers. The browsers shouldn’t treat proper support for European languages as an afterthought.Indeed, not using Base64 encoding and encode with
encodeURIComponent
instead solves the problem!This way it works in all my browsers (Safari, Firefox, Chrome) even without specifying
charset
in the data URL and without the XML declaration (<?xml version="1.0" encoding="..."?>
), provided the source file contains a proper<meta charset="...">
header (in case of ISO-8859-1 even that header is not necessary).This was a very helpful hint! Thank you very much! 😃
This issue can be closed.
You’re right, we should file browser bug reports.