Skip to content

Instantly share code, notes, and snippets.

@john92paul
Last active August 27, 2020 20:08
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 john92paul/c9371bf54a36d9714422b3549f085727 to your computer and use it in GitHub Desktop.
Save john92paul/c9371bf54a36d9714422b3549f085727 to your computer and use it in GitHub Desktop.
<script>
reader.onloadend = function (e) {
var myImage = new Image(); // Creates image object
myImage.src = e.target.result; // Assigns converted image to image object
myImage.onload = function(ev) {
var myCanvas = document.getElementById("myCanvas"); // Creates a canvas object
var myContext = myCanvas.getContext("2d"); // Creates a contect object
myCanvas.width = myImage.width; // Assigns image's width to canvas
myCanvas.height = myImage.height; // Assigns image's height to canvas
myContext.drawImage(myImage,0,0); // Draws the image on canvas
let imgData = myCanvas.toDataURL("image/jpeg",0.75); // Assigns image base64 string in jpeg format to a variable
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment