Last active
October 13, 2020 19:34
-
-
Save ljmotta/803688c34ccdaa23926ef28535312941 to your computer and use it in GitHub Desktop.
Base64Png Editor Get Initial Content
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
useEffect(() => { | |
const ctx = canvasRef.current?.getContext("2d")!; | |
/** | |
* Initial canvas size | |
*/ | |
canvasRef.current!.width = 0; | |
canvasRef.current!.height = 0; | |
imageRef.current!.onload = () => { | |
/** | |
* Update the canvas size | |
*/ | |
canvasRef.current!.width = imageRef.current!.width; | |
canvasRef.current!.height = imageRef.current!.height; | |
/** | |
* Draw the image on the canvas | |
*/ | |
ctx.drawImage(imageRef.current!, 0, 0); | |
/** | |
* The toDataUrl() returns the base64 information in the format "<base64 header>,<base64 data>" | |
* We just need the base64 data. | |
*/ | |
setEditorContent(canvasRef.current!.toDataURL().split(",")[1]); | |
}; | |
return () => { | |
imageRef.current!.onload = null; | |
}; | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment