1

So I would like to do the same, or similar, to what I would do in HTML:

   <picture>
      <source type="image/webp" srcset="my-image.webp">
      <img src="my-image.jpg">
   </picture>

But, obviously this won't work:

   <svg>
      <picture>
         <source type="image/webp" srcset="my-image.webp">
         <img src="my-image.jpg">
      </picture>
   </svg>

I could use .htaccess approach, but I would prefer not to because of the 302 HTTP redirect.

The solution also needs to work without any JavaScript trickery...

jBoive
  • 1,091
  • 1
  • 12
  • 37
  • 1
    Stick it in a foreignObject tag – Robert Longson Sep 02 '16 at 11:58
  • @RobertLongson Sounds easy enough, but could you provide an example or at least some more clues? I only want the browser to download one of the files and never both. – jBoive Sep 02 '16 at 12:36
  • Add a foreignObject tag as a child of the svg tag, give it width and height attributes and then add the picture element as a child of the foreignObject tag. I.e. just insert the foreignObject tag into your existing hierarchy. – Robert Longson Sep 02 '16 at 12:37
  • That doesn't display anything. Works in an HTML-file. I'll see if I can create an example. – jBoive Sep 02 '16 at 12:43
  • are you inserting the svg as a background image in an html page via a background-image? If so you'll need to have the webp embedded in the svg file itself (as a data uri). – Robert Longson Sep 02 '16 at 12:46

2 Answers2

0

This is what I ended up doing, thanks to the input from Robert Longson:

    <foreignObject x="0" y="0" width="100%" height="100%">
      <picture>
        <source
          width="1180"
          height="500"
          type="image/webp"
          class="lazyload"
          data-srcset="http://satyr.io/1180x500?2&type=webp&text=webp@1x&texture=graphpaper&delay=2g 1x,
             http://satyr.io/2360x1000?type=webp&text=webp@2x&texture=graphpaper&delay=2g 2x" />
        <source
          width="1180"
          height="500"
          type="image/jpeg"
          class="lazyload"
          data-srcset="http://satyr.io/1180x500?2&type=jpg&text=jpg@1x&texture=graphpaper&delay=2g 1x,
             http://satyr.io/2360x1000?type=jpeg&text=jpeg@2x&texture=graphpaper&delay=3g 2x" />
        <img
          width="1180"
          height="500"
          class="lazyload"
          data-src="http://satyr.io/1180x500?2&type=jpeg&text=jpeg@1x&texture=graphpaper&delay=3g"
          alt=""
      /></picture>
    </foreignObject>
Billybobbonnet
  • 3,047
  • 4
  • 21
  • 45
jBoive
  • 1,091
  • 1
  • 12
  • 37
0
<svg>
  <image
    href="hello-world.jpg"
    x="20" y="20"
    height="160" width="160"
  />
</svg>

mozilla docs: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image

duplicate of Does SVG support embedding of bitmap images?

Mila Nautikus
  • 1,481
  • 1
  • 9
  • 18