var img = new Image(); // 关键点,设置crossOriginal属性 img.setAttribute('crossOrigin', 'anonymous'); var canvas = document.getElementById("canvas"); var ctx = canvas.getContext('2d'); img.onload = function () { canvas.width = img.width; canvas.height = img.height + 200; ctx.drawImage(img, 0, 0); document.getElementById('canvasImg').src = canvas.toDataURL("image/jpeg", 1); } img.src = 'http://img.hb.aicdn.com/38d8f519b3f464a80d85ed9632fed904ed0181f41d632-ZHrigO_fw658';
关键的代码是:
img.setAttribute('crossOrigin', 'anonymous');