html5 - canvas 跨域问题
问题描述
在微信上给用户修改头像的时候,用canvas来截图。结果报错:Owechat_login.js:226 Uncaught TypeError: Failed to execute ’getImageData’ on ’CanvasRenderingContext2D’: The provided double value is non-finite.代码:function cropImage(targetCanvas, x, y, width, height) {
var targetctx = targetCanvas.getContext(’2d’);var targetctxImageData = targetctx.getImageData(x, y, width, height); // sx, sy, sWidth, sHeight var c = document.createElement(’canvas’);var ctx = c.getContext(’2d’); c.width = width;c.height = height; ctx.rect(0, 0, width, height);ctx.fillStyle = ’white’;ctx.fill();ctx.putImageData(targetctxImageData, 0, 0); // imageData, dx, dy document.getElementById(’image’).src = c.toDataURL(’image/jpeg’, 0.92);document.getElementById(’image’).style.display = ’initial’; }
问题解答
回答1:初步看了下代码貌似没什么问题的,排除掉图片可能存在的跨域问题,还有一个问题楼主可以查看下就是getImageData 的传参,需要是number类型,楼主可以先使用
console.log(typeof x, typeof y, typeof width, typeof height)
来看看
回答2:应该不是跨域吧,跨域会写 The canvas has been tainted by cross-origin data
console.log一下getImageData的参数吧。The provided double value is non-finite有可能是吧string当数传进来了。
相关文章:
1. docker-compose 为何找不到配置文件?2. 为什么我ping不通我的docker容器呢???3. debian - docker依赖的aufs-tools源码哪里可以找到啊?4. vim - docker中新的ubuntu12.04镜像,运行vi提示,找不到命名.5. docker网络端口映射,没有方便点的操作方法么?6. mac连接阿里云docker集群,已经卡了2天了,求问?7. golang - 用IDE看docker源码时的小问题8. java - 一个泛型标签问题9. java类中的成员成员变量赋值之后什么时候会被回收,有什么办法监听到某一个对象被回收吗10. yii2中restful配置好后在nginx下报404错误

网公网安备