/** * 根据图片链接下载图片 */ const downloadImg = () => { const canvas = document.createElement('canvas'); const img = document.createElement('img'); img.onload = () => { canvas.width = img.width; canvas.height = img.height; const context = canvas.getContext('2d'); context.drawImage(img, 0, 0, img.width, img.height); canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height); canvas.toBlob(b => { const link = document.createElement('a'); link.href = window.URL.createObjectURL(b); link.download = new Date().getTime().toString(); // 下载图片名称 link.click(); }, "image/jpeg"); } img.setAttribute('crossOrigin', 'Anonymous'); img.src = imgSrc; }
——网上抄的,保留备用。
标签:canvas,const,img,height,width,地址,link,链接,图片 From: https://www.cnblogs.com/atao24/p/17028394.html