strokeRect() and fillRect() accept the coordinates of the upper-left corner of a rectangle and its width and height.
The strokeRect() method draws an outline of the rectangle.
The fillRect() draws a filled rectangle.
The following code shows how these methods are used.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML5 Canvas</title> <script type="text/javascript"> window.onload = function(){/* ww w .j a va 2 s. c o m*/ let canvas = document.getElementById("myCanvas"); let context = canvas.getContext("2d"); // draw stuff here context.lineWidth = 5; context.fillRect(10,10,180,50); context.strokeRect(10,80,180,50); }; </script> </head> <body> <canvas id="myCanvas" width="300" height="200"></canvas> </body> </html>
You can also use rect() method to draw rectangles.
However, you also need to call stroke() or fill() to actually draw the rectangle.