本文主要是介绍canvas使用toDataUrl()报错,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
页面要做一个截图的功能,本地生成了一张图片,然后转成canvas,再从canvas变成png,但是canvas.toDataUrl()一直报错:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
代码如下:
var url = DOMURL.createObjectURL(svg);var canvas = document.createElement('canvas');var ctx = canvas.getContext('2d');canvas.width = window.innerWidth;canvas.height = window.innerHeight*1.25;cacheImg(url,function(){ctx.drawImage(this , 0 , 0);var image = canvas.toDataURL("image/png").replace("image/png", 'image/octet-stream');DOMURL.revokeObjectURL(url);});function cacheImg(url , callback){var img = new Image()img.setAttribute('crossOrigin', 'Anonymous');//img.crossOrigin = "Anonymous"; //或者加这行代码img.src = url;img.onload = function() {callback.call(img)}}
这篇关于canvas使用toDataUrl()报错的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!