codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

15 / 59 topics
11HTML Semantic Elements12HTML5 New Elements13HTML5 Forms14HTML5 Media Elements15HTML5 Canvas16HTML5 Web Storage
Tutorials/HTML & CSS/HTML5 Canvas
🎨HTML & CSS

HTML5 Canvas

Updated 2026-04-20
3 min read

Introduction to HTML5 Canvas

HTML5 introduced the <canvas> element, which provides a way to draw graphics on a web page using JavaScript. Unlike other HTML elements that rely on external resources or plugins, the canvas is a bitmap and can be manipulated programmatically. This makes it ideal for creating dynamic visualizations, games, animations, and interactive charts.

In this tutorial, we'll explore the basics of using the <canvas> element, including setting up the canvas, drawing shapes, handling user input, and applying styles. By the end of this section, you'll have a solid understanding of how to use HTML5 Canvas in your web projects.

Setting Up the Canvas

To start using the canvas, you need to add it to your HTML document. The <canvas> element is similar to other HTML elements like <div> or <img>, but it doesn't display anything unless you provide JavaScript code to draw on it.

<canvas id="myCanvas" width="500" height="300"></canvas>

In this example, we've created a canvas with an ID of myCanvas and dimensions of 500 pixels wide by 300 pixels high. The width and height attributes define the size of the canvas in pixels.

Drawing on the Canvas

To draw on the canvas, you need to access its drawing context using JavaScript. The most commonly used context is the 2D rendering context, which allows you to draw shapes, text, images, and other graphical elements.

<canvas id="myCanvas" width="500" height="300"></canvas>
<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');

  // Drawing a rectangle
  ctx.fillStyle = 'blue';
  ctx.fillRect(10, 10, 100, 50);

  // Drawing a circle
  ctx.beginPath();
  ctx.arc(200, 150, 30, 0, Math.PI * 2);
  ctx.fillStyle = 'red';
  ctx.fill();

  // Drawing text
  ctx.font = '30px Arial';
  ctx.fillText('Hello Canvas', 300, 150);
</script>

In this example:

  • We access the canvas element using document.getElementById.
  • We get the 2D rendering context with canvas.getContext('2d').
  • We draw a blue rectangle using fillRect(x, y, width, height).
  • We draw a red circle using arc(x, y, radius, startAngle, endAngle) and fill it with ctx.fill().
  • We draw text using fillText(text, x, y).

Handling User Input

The canvas can respond to user input such as mouse clicks or movements. You can add event listeners to the canvas element to handle these events.

<canvas id="myCanvas" width="500" height="300"></canvas>
<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');

  // Drawing a rectangle
  ctx.fillStyle = 'blue';
  ctx.fillRect(10, 10, 100, 50);

  // Handling mouse click
  canvas.addEventListener('click', function(event) {
    const rect = canvas.getBoundingClientRect();
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;

    ctx.beginPath();
    ctx.arc(x, y, 5, 0, Math.PI * 2);
    ctx.fillStyle = 'green';
    ctx.fill();
  });
</script>

In this example:

  • We add a click event listener to the canvas.
  • When the user clicks on the canvas, we calculate the click position relative to the canvas using getBoundingClientRect().
  • We draw a green circle at the click position.

Best Practices

  1. Use CSS for Styling: While you can apply styles directly to the canvas element using CSS, it's generally better to use JavaScript for drawing and CSS for layout and styling.
  2. Optimize Performance: Drawing complex graphics or animations on the canvas can be resource-intensive. Use techniques like requestAnimationFrame for smooth animations and consider off-screen rendering when possible.
  3. Handle Different Screen Sizes: Ensure your canvas scales well across different devices by using responsive design techniques.

Conclusion

The HTML5 Canvas element provides a powerful toolset for creating dynamic graphics and visualizations on the web. By understanding how to set up the canvas, draw shapes, handle user input, and apply best practices, you can create engaging and interactive experiences for your users. Whether you're building games, data visualizations, or simple animations, the canvas is an essential part of modern web development.

In the next section, we'll dive deeper into advanced topics such as working with images, transformations, and more complex animations. Stay tuned!


PreviousHTML5 Media ElementsNext HTML5 Web Storage

Recommended Gear

HTML5 Media ElementsHTML5 Web Storage