I'm trying to "draw" a rectangle in a black canvas but it doesn't work. This is my code:
window.onload = function() {
canvas = document.getElementById('canvas');
canvasContext = canvas.getContext('2d');
draw();
}
function draw() {
canvasContext.fillstyle='black';
canvasContext.fillRect(0,0,800,600);
canvasContext.fillstyle='white';
canvasContext.fillRect(0,0,10,10);
}
<canvas id="canvas" width="800" height="600"></canvas>
Why doesn't it work?
onloadhandler and instead just put all of your code in a scoping function in ascriptat the end of the HTML, right before the closing</body>tag.fillstyleshould befillStyle(capital S). Voting to close as typo/non-repro. JavaScript is case-sensitive.