Open In App

HTML canvas fillStyle Property

The canvas fillStyle property is used to set or return the color, gradient, or pattern used to fill the drawing

Style:



context.fillStyle=color|gradient|pattern;

Property Value:

Example 1: 






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

Output: 

Example 2: 




<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas fillStyle property
    </title>
</head>
  
<body>
    <canvas id="GFG" 
            width="500"
            height="300">
  </canvas>
  
    <script>
        var x = 
            document.getElementById("GFG");
        var context = 
            x.getContext("2d");
        var gr = 
            context.createLinearGradient(50, 0, 350, 0);
        gr.addColorStop(0, "green");
        gr.addColorStop(1, "white");
        context.fillStyle = gr;
        context.fillRect(50, 50, 350, 200);
        context.stroke();
    </script>
</body>
  
</html>

Output: 

Example 3: 




<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas fillStyle property
    </title>
</head>
  
<body>
    <canvas id="GFG"
            width="500"
            height="300">
  </canvas>
  
    <script>
        var x = 
            document.getElementById("GFG");
        
        var context =
            x.getContext("2d");
        
        var gr =
            context.createLinearGradient(0, 100, 0, 200);
        gr.addColorStop(0, "green");
        gr.addColorStop(1, "yellow");
        context.fillStyle = gr;
        context.fillRect(50, 50, 350, 200);
        context.stroke();
    </script>
</body>
  
</html>

Output: 

Supported Browsers:


Article Tags :