YouTip LogoYouTip

Met Canvas Rect

# HTML canvas rect() Method [![Image 9: Canvas Object Reference](#) Canvas Object](#) ## Example Draw a 150*100 pixel rectangle: YourbrowserdoesnotsupporttheHTML5canvastag. JavaScript: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.rect(20,20,150,100); ctx.stroke(); [Try it Β»](#) * * * ## Browser Support ![Image 10: Internet Explorer](#)![Image 11: Firefox](#)![Image 12: Opera](#)![Image 13: Google Chrome](#)![Image 14: Safari](#) Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the rect() method. **Note:** Internet Explorer 8 and earlier versions do not support the element. * * * ## Definition and Usage The rect() method creates a rectangle. **Tip:** Use the [stroke()](#) or [fill()](#) methods to actually draw the rectangle on the canvas. | JavaScript syntax: | _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. | * * * ![Image 15: Example](#) ## More Examples ## Example Create three rectangles using the rect() method: Your browser does not support the canvas tag. JavaScript: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); // Red rectangle ctx.beginPath(); ctx.lineWidth="6"; ctx.strokeStyle="red"; ctx.rect(5,5,290,140); ctx.stroke(); // Green rectangle ctx.beginPath(); ctx.lineWidth="4"; ctx.strokeStyle="green"; ctx.rect(30,30,50,50); ctx.stroke(); // Blue rectangle ctx.beginPath(); ctx.lineWidth="10"; ctx.strokeStyle="blue"; ctx.rect(50,50,150,80); ctx.stroke(); [Try it Β»](#) * * Canvas Object](#)
← Met Canvas FillrectProp Canvas Miterlimit β†’