The Wayback Machine - https://web.archive.org/web/20240118172350/https://www.geeksforgeeks.org/html-canvas-tutorial/
Open In App
Related Articles

HTML Canvas Tutorial

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

HTML Canvas facilitates the element <canvas> to draw graphics on Canvas with the help of JavaScript. HTML Canvas offers various methods for drawing different shapes and lines. The HTML Canvas is a rectangular area defined via height and width on an HTML page. By default, HTML Canvas has no border and no content.

Note: To specify the canvas area, canvas provides two attributes: height and width. By default, the width and height of the value are 300px and 150px respectively.

What is HTML Canvas

HTML Canvas uses <canvas> element to draw the graphics. JavaScript is essential for drawing graphics on HTML Canvas. We can draw different types of shapes and lines including Canvas Lines, Canvas Rectangle, Canvas Circle, Canvas Curves, Canvas Gradients, Canvas Text, Canvas Images, etc.

 

The following list provides a comprehensive guide for drawing on the canvas.

Canvas Basics

Canvas Properties

Canvas Methods

Line Styles Properties

Rectangles Methods

Paths Methods

Transformations Methods

Text Properties and Methods

Property

Method

Pixel Manipulation

Property

Methods 

Compositing & Image Drawing

Properties

Method

Reasons to Learn HTML Canvas

  • Canvas is used mostly in game development, it is essential to learn for creating web-based games.
  • HTML Canvas allows you to create responsive graphics and visuals for different screen sizes.
  • Canvas can also be used for visualizing data through charts, graphs, and diagrams.

Usage of HTML Canvas

  • Drawing Graphics: HTML Canvas is used for drawing 2D graphics and render it directly in the web browser.
  • Game Development: HTML Canvas provides platform for building interactive and dynamic 2D games on the web.
  • Chart and Data Visualization: HTML Canvas is used to draw charts, graphs, and different types visual representations of data.

FAQs on HTML Canvas

Q1. What is HTML Canvas?

Ans. The HTML Canvas provides a drawing surface and using JavaScript we can create interactive and visually appealing content.

Q2. What is the size of HTML Canvas by default?

Ans. The HTML Canvas has a default width of 300px and a height of 150px.

Q3. How can I draw shapes on the canvas?

Ans. To draw rectangles, lines, and arcs we can use methods like fillRect(), strokeRect(), and arc() respectively.

Q4. How do I make a transparent canvas in HTML5?

Ans. The HTML Canvas are transparent by default.

Q5. How do I clear the entire canvas?

Ans. The method clearRect() is responsible to clear a specified rectangular area from coordinates (0, 0) to the canvas width and height.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 10 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Similar Reads
Complete Tutorials