Open In App

HTML canvas strokeRect() Method

The strokeRect() method is used to draw the rectangle in a given color. The default color of the stroke is black. 

Syntax:



context.strokeRect(x, y, width, height)

Parameters:

Example 1: 






<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas strokeRect() Method
    </title>
</head>
  
<body>
    <canvas id="GFG"
            width="500" 
            height="300">
  </canvas>
  
    <script>
        var x =
            document.getElementById("GFG");
        var context = x.getContext("2d");
        context.strokeRect(50, 50, 350, 200);
        context.stroke();
    </script>
  
</body>
  
</html>

Output: 

Example 2: 




<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas 
      strokeRect() Method
    </title>
</head>
  
<body>
    <canvas id="GFG" 
            width="500"
            height="300">
  </canvas>
  
    <script>
        var x =
            document.getElementById("GFG");
        var context =
            x.getContext("2d");
        context.strokeStyle = "green";
        context.strokeRect(50, 50, 350, 200);
  
        context.strokeStyle = "red";
        context.strokeRect(100, 100, 250, 100);
        context.stroke();
    </script>
  
</body>
  
</html>

Output: 

Supported Browsers:


Article Tags :