Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of canvas's toDataURL method for SVG? [closed]

I am trying to load an svg image into canvas for pixel manipulation I need a method like toDataURL or getImageData for svg

on Chrome/Safari I can try doing it through and image and canvas

var img = new Image()
img.onload = function(){
  ctx.drawImage(img,0,0) //this correctly draws the svg image to the canvas! however...
  var dataURL = canvas.toDataURL(); //SECURITY_ERR: DOM Exception 18
  var data = ctx.getImageData(0,0,img.width, img.height).data //also SECURITY_ERR: DOM Exception 18
  }
  img.src = "image.svg" //that is an svg file. (same domain as html file :))

But I get security errors. Any other way?

Here is a live demo of the problem http://clstff.appspot.com/gist/462846 (you can view source)

like image 587
Drew LeSueur Avatar asked Jul 03 '10 22:07

Drew LeSueur


People also ask

What is toDataURL()?

toDataURL() method returns a data URL containing a representation of the image in the format specified by the type parameter. The desired file format and image quality may be specified. If the file format is not specified, or if the given format is not supported, then the data will be exported as image/png .

How do I turn a photo into a canvas?

function convertCanvasToImage() { let canvas = document. getElementById("canvas"); let image = new Image(); image. src = canvas. toDataURL(); return image; } let pnGImage = convertCanvasToImage(); document.

How do you use toDataURL in react?

Just move the toDataURL() inside the onload handler. Right now toDataURL() is executed before the image has been drawn, hence the blank image. You also want to consider using a callback/promise after obtaining the data-url as the call is asynchronous, and if you intend to pass the data-url to some other function.


1 Answers

var dataUrl = 'data:image/svg+xml,'+encodeURIComponent(svgString);
like image 124
Hubert OG Avatar answered Sep 21 '22 22:09

Hubert OG