question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sap.m.Image src=*.svg support

See original GitHub issue

Hi guys, i am currently using svg images as Image sources.

To be able to apply styling via css the external svg needs to be converted as inline svg.

Currently this is a work in progress and i do something like

        jQuery('.svg-inject .sapMImg').each(function(){
            var $img = jQuery(this);
            var imgID = $img.attr("id");
            var imgDataSapUi = $img.attr("data-sap-ui");
            var imgRole = $img.attr("role");
            var imgAriaHidden = $img.attr("aria-hidden");
            var imgClass = $img.attr("class");
            var imgStyle = $img.attr("style");
            var imgURL = $img.attr("src");

            jQuery.get(imgURL, function(data) {
                // Get the SVG tag, ignore the rest
                var $svg = jQuery(data).find("svg");

                // Add replaced image's ID to the new SVG
                if(typeof imgID !== "undefined") {
                    $svg = $svg.attr("id", imgID);
                }
                if(typeof imgDataSapUi !== "undefined") {
                    $svg = $svg.attr("data-sap-ui", imgDataSapUi);
                }
                if(typeof imgRole !== "undefined") {
                    $svg = $svg.attr("role", imgRole);
                }
                if(typeof imgAriaHidden !== "undefined") {
                    $svg = $svg.attr("aria-hidden", imgAriaHidden);
                }
                // Add replaced image's classes to the new SVG
                if(typeof imgClass !== "undefined") {
                    $svg = $svg.attr("class", imgClass + " svg-inline");
                }
                // Add replaced image's styles to the new SVG
                if(typeof imgStyle !== "undefined") {
                    $svg = $svg.attr("style", imgStyle);
                }

                // Remove any invalid XML tags as per http://validator.w3.org
                $svg = $svg.removeAttr("xmlns:a");

                // Check if the viewport is set, if the viewport is not set the SVG wont't scale.
                if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
                    $svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
                }

                // Replace image with new SVG
                $img.replaceWith($svg);
            }, 'xml');

There are also more experienced libs like https://github.com/robbue/jquery.svgInject availbale, which uses an inline cache for replacing img with svg.

This is only a WIP but still works. I just started to thing (and learn) about using SVG. Using it this way, the image will be replaced by an inline svg object.

You also need densityAware = false because svg is a vector and src should not load @2.svg

It would be great, if such feature could be integrated into the sap.m.image, maybe with an inline option by default to be able to use such SVGs also in the sap.m.ObjectHeader.

We use this feature inside out theme for company logo’s, where the external src is ok.

Using SVGs as user placeholder images we like to control the fills, strokes, etc with colors inside our custom specific theme using $BaseColor, $AccentColor and such SVGs needs to be replaced as inline object svg.

What do you thing?

Regards, Holger

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
flovogtcommented, Aug 25, 2022

This issue was covered in backlog BGSOFUIPIRIN-5679 and implemented with https://github.com/SAP/openui5/commit/d459de2f3a44e76d7f608cf58b10cfd646185c91.

1reaction
ManuelBcommented, Apr 27, 2022

@jdichev we used an svg map from germany with all federal states. They were clickable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use .svg images(not from sap Icon pool) in Icon control
I am trying to use .svg image to Icon control src in SAPUi5. This image is not from SAP Icons pool. I am...
Read more >
How to import and use custom icons in UI5? - SAP Community
Hi, I have a set of icons in PNG format. I need to convert them to SVG format and include it as custom...
Read more >
SAPUI5 Image is not displayed | SAP Community
Dear all, I am not able to display an image (UI5 logo in the example). Could someone help what the problem can be...
Read more >
How to convert a viz chart to an image? - SAP Community
If being able to convert a viz chart to svg file format is good enough then you have 2 options: 1. Right click...
Read more >
How use sap.m.Image with UseMap and get the clicked ...
SAPUI5 explored says there is an UseMap parameter, but don't provide enough info or example. I've tried and can't map and get the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found