Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blob URL in SVG as references #769

Open
upsuper opened this issue Dec 30, 2019 · 0 comments
Open

Blob URL in SVG as references #769

upsuper opened this issue Dec 30, 2019 · 0 comments

Comments

@upsuper
Copy link
Member

upsuper commented Dec 30, 2019

In the Features section in SVG 2 spec, references to external resources is defined to be URLs which are neither referencing something inside the document, nor a data URI. (The same definition differs in the SVG Integration spec which there is #768 for.)

I'm wondering whether it makes sense to allow blob URLs when they are generated from a document in the same origin. This would be an good escape from external reference restriction, because it's not always efficient to encode external resources in data URI.

As an example, this code

<!DOCTYPE html>
<img style="background: red">
<script>
  let img = document.querySelector('img');
  let canvas = document.createElement('canvas');
  canvas.width = 200;
  canvas.height = 200;
  let ctx = canvas.getContext('2d');
  ctx.fillStyle = 'green';
  ctx.fillRect(0, 0, 200, 200);
  canvas.toBlob(blob => {
    let url = URL.createObjectURL(blob);
    img.src = `data:image/svg+xml,
<svg xmlns="http://www.w3.org/2000/svg"
     width="200" height="200" viewBox="0 0 200 200">
  <image href="${url}" x="0" y="0" width="200" height="200" style="background: red" />
</svg>
`
  });
</script>

allows the generated SVG to reference some resources generated inside the current page.

As far as I can see, this example currently works in Firefox, but not Chrome or Safari.

It can be useful when you want to have a generated SVG to include

  • external resources that the current document can obtain otherwise, or
  • a generated image within the current document.

Generating such SVG can be helpful when you try to compose images then generate rasterized image (via painting the SVG to the canvas again).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant