HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

HTML Canvas Graphics

The HTML <canvas> element is used to draw graphics on a web page using JavaScript. It provides a drawing surface where you can use JavaScript to create and manipulate graphics, animations, and interactive content. The <canvas> element is part of the HTML5 specification and is widely supported by modern web browsers.

canvas

What is HTML Canvas?

The HTML <canvas> element is an HTML5 element that provides a drawing surface on a web page. It allows developers to use JavaScript to draw graphics, create animations, and build interactive content directly within the web browser. The canvas is a rectangular area on the page where you can dynamically render graphics, charts, animations, and more

Browser Support

Element Img
Google_Chrome Yes
Firefox Yes
microsoft-edge Yes
opera. Yes
safari Yes

Canvas Examples

Certainly! Here are a few simple examples to demonstrate the usage of the HTML <canvas> element along with JavaScript for drawing graphics:

Example

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas Example 1</title>
</head>
<body>

    <canvas id="rectangleCanvas" width="300" height="200"></canvas>

    <script>
        var canvas = document.getElementById("rectangleCanvas");
        var context = canvas.getContext("2d");

        // Draw a blue rectangle
        context.fillStyle = "blue";
        context.fillRect(50, 50, 200, 100);
    </script>

</body>
You can click on above box to edit the code and run again.

Output

Add a JavaScript

After creating the rectangular canvas area, you must add a JavaScript to do the drawing.

draw

Example

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();
</script>

You can click on above box to edit the code and run again.

Output

Draw a Circle

Circle_black

Example

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
</script>

You can click on above box to edit the code and run again.

Output

Draw a Text

text

Example

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
</script>

You can click on above box to edit the code and run again.

Output

Draw Linear Gradient

gradient

Example

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Create gradient
var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
</script>

You can click on above box to edit the code and run again.

Output

Draw Circular Gradient

radial circle

Example

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Create gradient
var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
</script>

You can click on above box to edit the code and run again.

Output

Draw image

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 10, 10);
</script>

You can click on above box to edit the code and run again.

Output