/** * 将图片转换为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); });