Check the code in JSFiddle此代码显示错误。
var img = document.querySelector("img");
var img1 = new Image();
img1.crossOrigin = 'Anonymous';
img1.onload = function() {
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
document.body.appendChild(canvas);
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL();
console.log(dataURL);
};
img1.src = "https://bithin.files.wordpress.com/2012/02/https.jpg";
错误:
来自原作的图片' https://bithin.files.wordpress.com'已被跨源资源共享策略阻止加载:否Access-Control-Allow-Origin
'标头出现在请求的资源上。起源' http://fiddle.jshell.net'因此不允许访问。