登录  /  注册
首页 > web前端 > H5教程 > 正文

Canvas图片跨域会遇到的问题及解决方法总结

不言
发布: 2018-09-17 16:04:01
原创
6643人浏览过

本篇文章给大家带来的内容是关于canvas图片跨域会遇到的问题及解决方法总结,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

尽管不通过 CORS 就可以在 Canvas 画布中使用图片,但是这会污染画布。一旦画布被污染,你就无法读取其数据。例如,你不能再使用画布的 toBlob(), toDataURL() 或 getImageData() 方法,调用它们会抛出安全错误。这种机制可以避免未经许可拉取远程网站信息而导致的用户隐私泄露。

HTML 规范中图片有一个 crossorigin 属性,结合合适的 CORS 响应头,就可以实现在画布中使用跨域 Canvas图片跨域会遇到的问题及解决方法总结 元素的图像。

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 上,例如:阿里云或者腾讯云服务,并且会有一个专门的域名来访问这些资源。

以上就是Canvas图片跨域会遇到的问题及解决方法总结的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号