Skip to content

Instantly share code, notes, and snippets.

@csszen
Last active May 17, 2021 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csszen/0f7b07d59fdafdc4856213133e4d42fb to your computer and use it in GitHub Desktop.
Save csszen/0f7b07d59fdafdc4856213133e4d42fb to your computer and use it in GitHub Desktop.
function toDataURL (src, callback, outputFormat) {
const img = new Image()
img.crossOrigin = 'Anonymous'
img.onload = function () {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.height = this.naturalHeight
canvas.width = this.naturalWidth
ctx.drawImage(this, 0, 0)
const dataURL = canvas.toDataURL(outputFormat)
callback(dataURL)
console.log('toDataURL', 'DONE', dataURL.length)
}
img.src = src
if (img.complete || img.complete === undefined) {
img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=='
img.src = src
}
}
@csszen
Copy link
Author

csszen commented Apr 22, 2021

use it like toDataURL('s/czg.svg', copy) in browser F12 devtools. It copies the base64 encoded image to the clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment