const image = new Image(); image.onload = () => { console.log(image.width); // 构建canvas节点 const canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; const context = canvas.getContext('2d'); context.drawImage(image, 0, 0, image.width, image.height); // 转换 const imgBase64 = canvas.toDataURL(); console.log(imgBase64); }; image.src = 'https://cdns.jsrun.net/css/img/logo@2x.png';