
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to draw a rectangle on HTML5 Canvas?
The HTML5 <canvas> tag is used to draw graphics, animations, etc. using scripting. It is a new tag introduced in HTML5. The canvas element has a DOM method called getContext, which obtains rendering context and its drawing functions. This function takes one parameter, the type of context 2d.
To draw a rectangle with HTML5 canvas, use the fillRect(x, y, width, height) method:
You can try to run the following code to learn how to draw a rectangle with HTML5 Canvas
Example
<!DOCTYPE html> <html> <head> <title>HTML5 Canvas Tag</title> </head> <body> <canvas id="newCanvas" width="200" height="100" style="border:1px solid #000000;"></canvas> <script> var c = document.getElementById('newCanvas'); var ctx = c.getContext('2d'); ctx.fillStyle = '#7cce2b'; ctx.fillRect(0,0,300,100); </script> </body> </html>
Output
- Related Articles
- How to draw a rounded Rectangle on HTML Canvas?
- How to draw a quadratic curve on HTML5 Canvas?
- How to draw large font on HTML5 Canvas?
- Update HTML5 canvas rectangle on hover
- How to draw an SVG file on an HTML5 canvas?
- How to draw a rectangle in HTML5 SVG?
- How to draw lines using HTML5 Canvas?
- How to draw a Bezier Curve with HTML5 Canvas?
- How to draw a star by using canvas HTML5?
- How to draw an oval in HTML5 canvas?
- Draw a shadow with HTML5 Canvas
- Draw a circle filled with random color squares on HTML5 canvas
- Draw Bezier Curve with HTML5 Canvas
- How to draw grid using HTML5 and canvas or SVG?
- How to draw a line on a Tkinter canvas?

Advertisements