网站首页 > 技术文章 正文
本篇文章给大家带来的内容是关于Canvas图片跨域会遇到的问题及解决方法总结,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
尽管不通过 CORS 就可以在 Canvas 画布中使用图片,但是这会污染画布。一旦画布被污染,你就无法读取其数据。例如,你不能再使用画布的 toBlob(), toDataURL() 或 getImageData() 方法,调用它们会抛出安全错误。这种机制可以避免未经许可拉取远程网站信息而导致的用户隐私泄露。
HTML 规范中图片有一个 crossorigin 属性,结合合适的 CORS 响应头,就可以实现在画布中使用跨域 <img> 元素的图像。
crossOrigin/CORS同域跨域无 CORS跨域有 CORSdefault支持支持渲染,不支持 toDataURL支持渲染,不支持 toDataURLanonymousN/A同上支持渲染,支持 toDataURLuse-credentialsN/A同上支持渲染,不支持 toDataURL
总结:Canvas 可以正常的渲染跨域图片,但是在跨域图片没有设置跨域响应头或没有设置 crossOrigin = 'anonymous' 的时候,使用 canvas.toDataURl 会抛出如下错误:
Chrome
没有设置 crossOrigin
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
at Image.img.onload...
跨域
Access to Image at 'http://localhost:3001/canvas.jpg' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
设置了 crossOrigin=use-credentials
Access to Image at 'http://localhost:3002/canvas.jpg' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access.
Safari/Firefox
没有设置 crossOrigin
SecurityError: The operation is insecure.
跨域
[Error] Origin http://192.168.3.99:3000 is not allowed by Access-Control-Allow-Origin.
[Error] Failed to load resource: Origin http://192.168.3.99:3000 is not allowed by Access-Control-Allow-Origin. (canvas.jpg, line 0)
[Error] Cross-origin image load denied by Cross-Origin Resource Sharing policy.
设置了 corssOrigin=use-credentials
[Error] Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true.
[Error] Failed to load resource: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true. (canvas.jpg, line 0)
[Error] Cross-origin image load denied by Cross-Origin Resource Sharing policy.
测试示例
1、启动服务器
npm start:启动服务器
npm run start:corsdisable:启动跨域图片服务器
npm run start:corsable:启动跨域-CORS图片服务器
2、访问 http://localhost:3000
其他问题
1、cossOrigin 存在兼容性问题
对于不支持 cossOrigin 的浏览器(IE 10及以下版本不支持,Android 4.3 及以下版本不支持)可以使用 XMLHttprequest 和 URL.createObjectURL() 来做兼容,参考测试示例 Ajax 解决 Canvas 图片跨域问题。
2、为什么不使用同域图片?
现在的前端开发一般都是将静态资源放置到 CDN 上,例如:阿里云或者腾讯云服务,并且会有一个专门的域名来访问这些资源。
以上就是Canvas图片跨域会遇到的问题及解决方法总结的详细内容,更多请关注其它相关文章!
御弟哥哥· 2019-07-10 17:50:081楼
我想知道解决办法是什么 我就想跨域,爬人家的图片,转为base64 因为是人家的,改不了服务端设置 还有没有办法把图片转为base64
更多技巧请《转发 + 关注》哦!
猜你喜欢
- 2024-09-10 Python奇淫技巧之自动登录哔哩哔哩(解决滑块验证)
- 2024-09-10 html转image 保存到zip(html保存成图片)
- 2024-09-10 前端上传图片被旋转的处理方案(nba常规赛录像高清回放)
- 2024-09-10 Day 3 学习Canvas这一篇文章就够了
- 2024-09-10 JavaScript实现浏览器本地的图像移动、大小调整和裁剪
- 2024-09-10 微信内置浏览器动态生成二维码并长按识别
- 2024-09-10 Python奇淫技巧之自动登录哔哩哔哩
- 2024-09-10 JSPDF + html2canvas A4分页截断(html做分页)
- 2024-09-10 vue手把手教学~搭建web聊天室(vue简单聊天室)
- 2024-09-10 前端:从零实现一款可视化图片编辑器
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)