html2canvas截屏跨域图片解决办法

//html2canvas.js需要修改1215行代码如下更改
function ImageContainer(src, cors) {
    this.src = src;
    this.image = new Image();
    var self = this;
    this.tainted = null;
    this.promise = new Promise(function(resolve, reject) {
        self.image.onload = resolve;
        self.image.onerror = reject;
        if (cors) {
            //self.image.crossOrigin = "anonymous";//1.去掉此处的anonymous代码改为空
            self.image.crossOrigin = "";
        }
        //self.image.src = src;//2.在源src后面添加一个随机数如时间戳+"?"+new Date().getTime();
        self.image.src = src+"?"+new Date().getTime();
        if (self.image.complete === true) {
            resolve(self.image);
        }
    });
}




//javascript调用html2canvas时续添加useCORS:true    案例如下:
 html2canvas(document.querySelector("#jiequ"), {
    canvas: canvas,
    onrendered: function(canvas) {
    //$("#droppable").hide()
    var imgPath = canvas.toDataURL("image/png");  
    $("#ceshi>img").attr("src",imgPath);
    /*$.ajax({
    type:"post",
    url:configUrl+"/electronicContract/signContract.do",
    async:true,
    data:{
    data:imgPath
    },
    success:function(data){
    alert(data)
    }
    });*/
       //var pHtml = "";  
       //$("canvas").html(pHtml);
       //document.body.appendChild(canvas)
    },useCORS:true
   })

你可能感兴趣的:(技术)