Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does canvas.toDataURL() throw a security exception?

Did I not get enough sleep or what? This following code

var frame=document.getElementById("viewer"); frame.width=100; frame.height=100;  var ctx=frame.getContext("2d"); var img=new Image(); img.src="http://www.ansearch.com/images/interface/item/small/image.png"  img.onload=function() {     // draw image     ctx.drawImage(img, 0, 0)      // Here's where the error happens:     window.open(frame.toDataURL("image/png")); } 

is throwing this error:

SECURITY_ERR: DOM Exception 18 

There's no way this shouldn't work! Can anyone explain this, please?

like image 773
pop850 Avatar asked Mar 05 '10 22:03

pop850


People also ask

What is canvas 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 .

Is canvas toDataURL async?

Yes, images are loaded asynchronously by the browser.

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.


1 Answers

In the specs it says:

Whenever the toDataURL() method of a canvas element whose origin-clean flag is set to false is called, the method must raise a SECURITY_ERR exception.

If the image is coming from another server I don't think you can use toDataURL()

like image 112
Bob Avatar answered Sep 22 '22 10:09

Bob