解决html2canvas图片跨域合成失败的问题

      /**
      * 将图片转换为base64
      * 解决html2canvas跨域合成失败的问题
      */
      var getBase64Image = function(src, cb) {
        var img = document.createElement('img'),
            dataURL = '';
        img.crossOrigin = '';
        img.src = src;
        img.onload = () => {
            var canvas = document.createElement("canvas");
            canvas.width = img.width;
            canvas.height = img.height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width, img.height);
            dataURL = canvas.toDataURL();
            cb(dataURL);
        };
      }
      getBase64Image('http://imgsrc.png', function(url) {
        $('.img').attr('src', url);
      });

 

posted @ 2019-05-25 17:23  一像素boy  阅读(5079)  评论(0编辑  收藏  举报