Last active
April 18, 2019 09:02
-
-
Save blackmiaool/8469eb8a3600aff5b92a28ca1e670f02 to your computer and use it in GitHub Desktop.
compress image
This file contains hidden or 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 compress(canvasDom, size){ | |
let ratio = 1; | |
let dataurl; | |
do { | |
dataurl = canvasDom.toDataURL("image/jpeg", ratio); | |
ratio -= 0.1; | |
if (ratio < 0.3) { | |
return null; | |
} | |
} while (dataurl.length > size/3*4); | |
return dataurl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment