【已解决】canvas跨域问题

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');

 

posted @ 2020-02-11 20:01  沉迷编程的程序员  阅读(924)  评论(0编辑  收藏  举报