Drawing canvas after dom loaded
<!DOCTYPE html> <html> <head> <title>BasicExample</title> <script> function changeCanvasColor () { let canvas = document.getElementById("mycanvas"); let context = canvas.getContext("2d"); context.fillStyle = "blue"; context.fillRect(0, 0, canvas.width, canvas.height); }/* w w w .j av a 2 s .com*/ document.addEventListener('DOMContentLoaded', changeCanvasColor); </script> </head> <body> <div id="gameArea"> <canvas id="mycanvas" width="800" height="480"></canvas> </div> </body> </html>