Skip to content
Related Articles

Related Articles

HTML | canvas addColorStop() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 10 Oct, 2019

The addColorStop() method is used to specify the color and its position in the gradient object.


gradient.addColorStop(stop, color);


  • stop: It is a value between 0.0 and 1.0 that represents the position to be hold between start and end in a gradient.
  • color: It is the color value to be displayed at the stop position.


<!DOCTYPE html>
        HTML canvas addColorStop() Method
<body style="text-align:left;">
    <h2>HTML canvas addColorStop() Method</h2>
    <canvas id="GFG" width="500" height="200"
    style="border:2px solid" >
        var doc_id = document.getElementById("GFG");
        var context = doc_id.getContext("2d");
// Create gradient
var grad = context.createLinearGradient(0, 0, doc_id.width, 0);
grad.addColorStop("0", "green");
grad.addColorStop("0.5", "white");
grad.addColorStop("1.0", "green");
// Fill with gradient
context.fillStyle = grad;
context.fillRect(100, 50, 100, 100);


Supported Browsers: The browser supported by HTML canvas addColorStop() Method are listed below:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Safari
  • Opera

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!