Skip to content

Instantly share code, notes, and snippets.

@ljmotta
Last active October 13, 2020 19:34
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 ljmotta/803688c34ccdaa23926ef28535312941 to your computer and use it in GitHub Desktop.
Save ljmotta/803688c34ccdaa23926ef28535312941 to your computer and use it in GitHub Desktop.
Base64Png Editor Get Initial Content
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