简体   繁体   中英

How to get base64 code by an img tag from DOM?

I have an image link (./kcaptcha/) that gives me random image with numbers on each query. So, I want to get base64 code from DOM, not from image src/url. Is there any options to achieve it? image here

Let's say you work with a tag that looks like this:

 const myBtn = document.getElementById("myBtn"); myBtn.addEventListener('click', () => { const c = document.createElement('canvas'); const ctx = c.getContext("2d"); const img = document.getElementById("myImg"); ctx.drawImage(img, 0, 0); alert(c.toDataURL("image/png")); })
 <img id="myImg" src="https://picsum.photos/200" crossorigin="Anonymous"/> <br> <button id="myBtn">Get B64</button>

crossorigin="Anonymous" is mandatory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM