js代码:
var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); var img = new Image(); img.crossOrigin = ''; //图片转canvas img.onload = function () { context.drawImage(this, 0, 0); context.getImageData(0, 0, this.width, this.height); document.body.appendChild(canvas); }; img.src = "https://pic4.zhimg.com/869cf812c5a1beb827d723e6332748a1_m.jpg";
在图片onload以后生成并添加到body里面