var canvas=document.getElementById("canvas"),//获取canvas ctx = canvas.getContext("2d"), //对应的CanvasRenderingContext2D对象(画笔) img = new Image(),//创建新的图片对象 base64 = '' ;//base64 img.src = 'http://www.xxxx.png'; img.setAttribute("crossOrigin",'Anonymous') img.onload = function(){//图片加载完,再draw 和 toDataURL ctx.drawImage(img,0,0); base64 = canvas.toDataURL("image/png"); };