Svg with image inside is not showing in safari

后端 未结 5 1290
太阳男子
太阳男子 2021-02-05 20:33

Inside my website, I am embeding a few svgs. They all seem to work just fine in Chrome, Firefox, IE(9+) and in Safari. Howvere as soon as there is image included in the svg, saf

相关标签:
5条回答
  • 2021-02-05 20:49

    It looks like this WebKit bug is responsible for the problem: https://bugs.webkit.org/show_bug.cgi?id=99677

    The workaround we use in our application is to have a script which finds all img elements displaying SVG images and add hidden object elements loading the same SVGs (<object style="position: fixed; width: 0; height: 0;" data="image.svg" type="image/svg+xml"></object>).

    The reason it works is that the object tag properly loads the embedded images into the image cache, so that they are visible within the SVGs loaded using the img tags.

    The advantage of this approach is that the images are still displayed using the img tag, so that the workaround can be applied (and later removed cleanly when the browsers are eventually patched) without affecting the rest of the application/website.

    The disadvantage is the creation of an extra object tag for each SVG image.

    0 讨论(0)
  • 2021-02-05 20:51

    I ran into this problem where I was using Ajax to load the svg spritesheet onto the page. If I had a on the page before the spritesheet was loaded, it would fail and would not resolve once the spritesheet was avaialble. Any added to the dom after the spritesheet was loaded were fine. I had to delay putting the items in the dom until after the spritesheet finished loading.

    This only affected IOS. All other browsers didn't care about the order.

    0 讨论(0)
  • 2021-02-05 20:52

    It seems that answer from this question works: What could make Safari skip clip-path AND mask with SVG?. See instructions in link from answer.

    You have to recreate your clip path to make it visible. And </clippath> tag is not pushed after </image> tag.

    Here is the little changed code (link to my image inserted so you will have to change it):

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         width="328px" height="328px" viewBox="0 0 328 328" enable-background="new 0 0 328 328" xml:space="preserve" >
    <g>
        <clipPath id="SVGID_2_">
            <!--use xlink:href="#SVGID_1_"  overflow="visible"></use-->
            <polygon id="SVGID_1_" points="1.414,164.001 164,326.586 326.586,164.001 164,1.414" />
        </clipPath>
    
        <defs>
            <polygon id="SVGID_1_" points="1.414,164.001 164,326.586 326.586,164.001 164,1.414"/>
        </defs>
    </g>
    
    <image clip-path='url(#SVGID_2_)' overflow="visible" width="338" height="532" id="DSC_x5F_0112-2" xlink:href="images/banner_03.jpg" transform="matrix(0.9818 0 0 0.9818 -2.7197 -11.064)">
    </image>
    
    <g>
        <path fill="#FFFFFF"  d="M164,328.001L0,164.002L164,0.001l164,164.001L164,328.001z M1.414,164.002L164,326.587l162.586-162.585
            L164,1.415L1.414,164.002z"/>
    </g>
    </svg>
    
    0 讨论(0)
  • 2021-02-05 21:00

    This solution works for me when displaying an embedded SVG image in Safari.

    replace

    <img src="image.svg">

    with

    <object data="image.svg" type="image/svg+xml"></object>

    0 讨论(0)
  • 2021-02-05 21:06

    So Anto Jurković pointed out, there are some known issues with Safari and clipPath. What Safari likes is if you dont define polygon but put it straight in to clippPath. Also you must then group clipPath separately.

    But I still could not get svg to render the image if I used img tag for svg - <img src="svg"/>. So at the end I have embeded it like this:

    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         width="328px" height="328px" viewBox="0 0 328 328" enable-background="new 0 0 328 328" xml:space="preserve">
    <g>
        <clipPath id="SVGID_2_">
            <polygon id="SVGID_1_" points="1.414,164.001 164,326.586 326.586,164.001 164,1.414      "/>
        </clipPath>
    </g>
    <image clip-path="url(#SVGID_2_)" overflow="visible" width="338" height="532" id="DSC_x5F_0112-2" xlink:href="data:image/jpeg;base64,/9j/.....r/fdeaS//9k=">
    </image>
    <g>
        <path fill="#FFFFFF" d="M164,328.001L0,164.002L164,0.001l164,164.001L164,328.001z M1.414,164.002L164,326.587l162.586-162.585
            L164,1.415L1.414,164.002z"/>
    </g>
    </svg>
    

    I can confirm that this renders correctly in mobile safari(on ipad and iphone, windows safari and mac safari - latest versions).

    So to sum it up...I have this svgs in separate html file and render them when needed.

    0 讨论(0)
提交回复
热议问题