Draw a 150*100 pixels rectangle:
<!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="300" height="180" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas> <script> var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.rect(20, 20, 150, 100);/*from w w w . j av a 2s.c om*/ ctx.stroke(); </script> </body> </html>
The rect()
method creates a rectangle.
Use the stroke()
or the fill()
method to actually draw the rectangle on the canvas.
context.rect(x, y, width, height);
Parameter Values
Parameter | Description |
---|---|
x | The x-coordinate of the upper-left corner of the rectangle |
y | The y-coordinate of the upper-left corner of the rectangle |
width | The width of the rectangle, in pixels |
height | The height of the rectangle, in pixels |