简体   繁体   English

fabric.js - toDataURL在画布上有图像时显示空白页面

[英]fabric.js - toDataURL shows blank page when there is image on canvas

I'm using fabric.js for my canvas application, toDataURL method works properly except when there is a image on canvas. 我正在使用fabric.js作为我的canvas应用程序,toDataURL方法正常工作,除非画布上有图像。 When i add an image to canvas and call toDataURL it shows me a blank page. 当我将图像添加到画布并调用toDataURL时,它会显示一个空白页面。

//When i call it from chrome console
canvas.toDataURL();
//It returns a working data url with no problem.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAGkCAYAAAAPPajHAAAgAElEQ…fpmwgogX1TrjoqP0FACewngtZh+iYCSmDflKuOyk8Q+H+CKCqUW0spTgAAAABJRU5ErkJggg=="

//When i execute same code in a .js file it returns a data url which shows a blank image.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAGkCAYAAAAPPajHAAAKC0lEQ…sBAmEBAw6XJzoBA/YDBMICBhwuT3QCBuwHCIQFDDhcnugEHt/IAaW9dzALAAAAAElFTkSuQmCC"

It's interesting that it's working on chrome dev console but not works in .js file even if it's same code. 有趣的是,它正在使用chrome dev控制台,但在.js文件中不起作用,即使它是相同的代码。 I noticed that working data url finishes with '==' but other one not. 我注意到工作数据网址以'=='结束,但其他网址没有。 However i don't know what this means. 但是我不知道这意味着什么。

You didn't give much to analyze but I'll go from there on my gut feeling. 你没有给分析太多,但我会从那里看到我的直觉。

The image you are using is probably violating Cross-Origin Resource Sharing (CORS) requirements. 您正在使用的映像可能违反了跨源资源共享 (CORS)要求。 When this happens the canvas will return a blank image when you try to get the pixel data either by using canvas.toDataURL() or context.getImageData() . 当发生这种情况时,当您尝试使用canvas.toDataURL()context.getImageData()获取像素数据时,画布将返回空白图像。

It basically happens when the image is not located on the same domain as the page or is loaded from the file:// protocol. 它基本上发生在图像与页面不在同一域中或从file://协议加载时。

You can try to add the following to the image object before setting the source: 在设置源之前,您可以尝试将以下内容添加到图像对象:

image.crossOrigin = 'anonymous';
image.src = '...';

From tag: 来自标签:

<img crossOrigin='anonymous' src='...' alt='' />

This will request permission from the server to use the image cross-origin. 这将请求服务器允许使用图像跨源。 If the server allows you should be fine. 如果服务器允许你应该没事。

If not you will either have to copy the image to your own server so it loads from the same domain as your page, or create a proxy page that loads the image externally and serves it to your page from the proxy page (it sounds complicated but really isn't). 如果不是,您将不得不将图像复制到您自己的服务器,以便从与您的页面相同的域加载,或者创建一个代理页面,从外部加载图像并从代理页面将其提供给您的页面(听起来很复杂但是真的不是)。

If the image does not show up at all on the canvas you are probably not using load callback which you need since image loading is asynchronous. 如果图像在画布上根本没有显示,则可能没有使用所需的加载回调,因为图像加载是异步的。 If so just add: 如果是这样,请添加:

image.onload = function() {
    /// now you can draw the image to canvas
}
image.crossOrigin = 'anonymous';
image.src = '...';

The problem is solved. 问题已经解决了。 The point i missed is, i was calling toDataURL function before canvas is loaded, that's why it was showing me a blank page. 我错过的一点是,我在加载画布之前调用了toDataURL函数,这就是为什么它向我显示一个空白页面。

canvas.loadFromJSON(json,function(){
          var dataURL = canvas.toDataURL();
          });

This solved my problem when i gave toDataURL() function as a callback to loadFromJSON function. 当我将toDataURL()函数作为loadFromJSON函数的回调函数时,这解决了我的问题。

But after a while i had a different issue about CORS when i tried to upload my images from s3 bucket and i solved this problem as upward solution. 但过了一段时间我有一个关于CORS的不同问题,当我试图从s3桶上传我的图像时,我解决了这个问题作为向上解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM