What are some best practices for ensuring that an image is fully loaded before drawing it in a canvas?

When drawing an image on a canvas, it is important to ensure that the image is fully loaded before attempting to draw it. This can be achieved by using the "onload" event handler to wait for the image to load completely before drawing it on the canvas. By doing so, you can prevent any potential issues such as the image not being fully loaded or appearing partially on the canvas.

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="100"></canvas>

<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');

var img = new Image();
img.onload = function() {
  ctx.drawImage(img, 0, 0);
};
img.src = 'image.jpg';
</script>

</body>
</html>