/** * 將圖片轉換爲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); });