Canvas图片跨域过程常见问题有什么,怎么解决
Admin 2022-07-15 群英技术资讯 1256 次浏览
这篇文章给大家分享的是Canvas图片跨域过程常见问题有什么,怎么解决。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。尽管不通过 CORS 就可以在 Canvas 画布中使用图片,但是这会污染画布。一旦画布被污染,你就无法读取其数据。例如,你不能再使用画布的 toBlob(), toDataURL() 或 getImageData() 方法,调用它们会抛出安全错误。这种机制可以避免未经许可拉取远程网站信息而导致的用户隐私泄露。
HTML 规范中图片有一个 crossorigin 属性,结合合适的 CORS 响应头,就可以实现在画布中使用跨域 <img> 元素的图像。
| crossOrigin/CORS | 同域 | 跨域无 CORS | 跨域有 CORS |
|---|---|---|---|
| default | 支持 | 支持渲染,不支持 toDataURL |
支持渲染,不支持 toDataURL |
| anonymous | N/A | 同上 | 支持渲染,支持 toDataURL |
| use-credentials | N/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 上,例如:阿里云或者腾讯云服务,并且会有一个专门的域名来访问这些资源。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
css规则的基本格式是“选择器{属性:属性值;}”。“选择器”用于告诉浏览器“{}”包裹的样式将作用于页面中哪些对象,“属性”是CSS提供的设置好的样式选项,“属性值”是用来显示属性效果的参数。
小编主要为大家详细介绍下如何利用CSS3做一些过渡效果的内容,用CSS3做一些过渡效果和动画,特别适合Web前端开发员学,有需要的朋友可以参考了解看看,那么接下来就跟随小编的思路来往下学习吧。
css兼容问题兼容问题1.文字本身的大小不兼容。同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白1px,下留白3px。解决方案:给文字设定line-height。确保所有文字都有默认的line-height值。2.ff下容器高度限定.即容器定义了height之后,容器边框的外形就确定了
我们在制作HTML页面时,经常会遇到浮动问题,如果当子元素浮动时,不清除浮动就会导致后面的元素排版乱序。那么都有什么方法能够清除浮动呢?
CSS实现左边固定宽右边自适应布局怎样做?有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008