Last active
May 17, 2021 03:16
-
-
Save csszen/0f7b07d59fdafdc4856213133e4d42fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use it like
toDataURL('s/czg.svg', copy)
in browserF12
devtools. It copies the base64 encoded image to the clipboard.