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

当调用 canvas.toDataURL() 时抛出异常:SECURITY_ERR: DOM Exception 18 #18

Open
scplwq opened this issue Dec 13, 2017 · 4 comments
Projects

Comments

@scplwq
Copy link

scplwq commented Dec 13, 2017

项目中有用到 wx.canvasToTempFilePath,看到框架已经有一些实现代码,尝试连起来,未果。发现一个 bug。

如何重现

  1. 运行 iOS Demo,在 Demo中有一个运行 drawImage 的演示
  2. 点击 drawImage 按钮
  3. 点击 toTempFilePath 按钮

出错代码在 h5/src/view/components/wx-canvas.js

var dataUrl = self.$.canvas.toDataURL();

2017-12-13 5 44 30

搜索Error信息,网上的结果是 [跨域问题]
(https://stackoverflow.com/questions/2390232/why-does-canvas-todataurl-throw-a-security-exception?noredirect=1&lq=1), 但是我并没有修改image地址,image 只是本地图片/image/wechat.png. 好像上次 getImageInfo 修复前还没有这个bug(待确认)

@IOriens
Copy link
Contributor

IOriens commented Dec 14, 2017

原因

Html5 Canvas with Cross-Domain content

可能的解决方案

方案一

h5 将 canvas 的位置信息发送给客户端,客户端去对应位置截图

方案二

客户端实现 canvas 组件

方案三

客户端中起一个local server,所有本地资源从 server 中拉取,这样它们都在同一个域中

方案四

将 Webview 初始化时设置的 baseurl 改为http://test.com/ ,使用NSURLProtocol拦截本地请求

方案五

图片都使用网络资源,但需要服务器对其设置CORS 请求头


微信可能用的方案二?我也不晓得客户端能不能做。。。

@IOriens IOriens added this to To Do in Schedule Dec 14, 2017
@scplwq
Copy link
Author

scplwq commented Dec 14, 2017

@IOriens 根据微信小程序官方论坛里的资料,微信使用的方案二。坑有点大~~

@IOriens
Copy link
Contributor

IOriens commented Dec 14, 2017

@liwangqiang 下一步应该是要把这几个组件 Native 化的,要辛苦客户端同学了😆

@scplwq
Copy link
Author

scplwq commented Dec 14, 2017

@IOriens iOS 测试了下

  1. 如果图片使用网络资源,且服务器支持跨域请求,并且设置 image.crossOrigin = "anonymous" 则 canvas.toDataURL() 没问题
  2. 如果是本地图片,则可以设置 image.src="data:image/png;base64,iVBORw0KG..." 这种 Data URI 格式,也是可以规避跨域问题的。

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

No branches or pull requests

2 participants