canvas节点无法导出图片_画布到图像无法在'HTMLCanvasElement'上执行'toDataURL':可能无法导出污染的画布...

本文讨论了在尝试从Facebook API获取图片并将其绘制到canvas后,由于跨域限制导致的canvas无法使用toDataURL()方法导出为数据URL的问题。安全策略规定,如果canvas中包含了跨域资源,它将被标记为污染(tainted),不允许进行读取操作。解决方案是寻找允许跨域请求的替代方法或者在服务器端处理图片导出。
摘要由CSDN通过智能技术生成

I am getting a picture from facebook api with $scope.picture.picture.data.url it correctly renders it to canvas but when I want to change it to dataUrl it get me this error

canvas to image Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

canvas is rendered like this

var canv=document.getElementById("mainCanvasD");

var ctx = canv.getContext('2d');

ctx.clearRect(0,0,540,320);

// $scope.picture used here

// draw games background image here

// console.log($scope.picture.picture.data.url);

drawUrlImage($scope.picture.picture.data.url,10,130,180,180,"mainCanvasD");

drawUrlImage("http://localhost/ezone/public/game/"+$scope.gameName+"/right/"+generateRandom(6)+".jpg",240,130,180,180,"mainCanvasD");

and am using this code to chnage it to dataUrl

var canv=document.getElementById("mainCanvasD");

var dataURL = canv.toDataURL('image/jpg');

documentData={"image":dataURL,"gameName":$scope.gameName,"userId":$scope.userId,"gameId":$scope.gameId};

解决方案

For security reasons you cannot access the content of a canvas if you draw something onto it which comes outside of your domain, in this case an image from facebook.

The canvas becomes tainted in this case. You can still draw other stuff to it, the user sees it, but you are not allowed to programatically read it anymore.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值