How To Draw Gradient Colors In Html5 Canvas

This article will tell you how to draw gradient colors using Html5 canvas.

1. Draw Gradient Color In Html5 Canvas Steps.

1.1 Linear Gradient Color.

  1. You can use the Html5 canvas object’s context’s method createLinearGradient(lgStartX, lgStartY, lgEndX, lgEndY) to create a linear gradient object.
    linearGradient = ctx.createLinearGradient(lgStartX, lgStartY, lgEndX, lgEndY);
  2. lgStartX: The linear gradient effect starting point’s x coordinate.
  3.  lgStartY: The linear gradient effect starting point’s y coordinate.
  4. lgEndX: The linear gradient effect ending point’s x coordinate.
  5. lgEndY: The linear gradient effect ending point’s y coordinate.
  6. Then call the linearGradient.addColorStop(offset, color) to set the color offset and value. The offset value range is 0 – 1.
    linearGradient.addColorStop(0, 'red');
  7. Now you should set the linearGradient object to the context object’s fillStyle attribute.
    ctx.fillStyle = linearGradient;
  8. Now you can draw the rectangle using the context object’s fillRect() method.
    ctx.fillRect(leftX, topY, width, height);

1.2 Radial Gradient Color.

  1. Call the Html5 canvas context object’s method createRadialGradient(rgStartX, rgStartY, rgStartRadius, rgEndX, rgEndY, rgEndRadius) to create a radial gradient object.
    radialGradient = ctx.createRadialGradient(rgStartX, rgStartY, rgStartRadius, rgEndX, rgEndY, rgEndRadius);
  2. rgStartX: The center point’s x coordinate of the starting circle in the gradient.
  3. rgStartY: The center point’s y coordinate of the starting circle in the gradient.
  4. rgStartRadius: The starting circle’s radius.
  5. rgEndX: The center point’s x coordinate of the ending circle in the gradient.
  6. rgEndY: The center point’s y coordinate of the ending circle in the gradient.
  7. rgEndRadius: The ending circle’s radius.
  8. Call the radialGradient object’s addColorStop(offset, color) method to set the color and offset.
  9. radialGradient.addColorStop(0.5, 'yellow');
  10. Set the above radialGradient object to the context object’s fillStyle attribute.
    ctx.fillStyle = radialGradient;
  11. Then you can call the context object’s fillRect(leftX, topY, width, height) method to draw the rectangle with radial gradient color.
    ctx.fillRect(leftX, topY, width, height);

2. Html5 Canvas Linear Gradient & Radial Gradient Example.

  1. There are 2 buttons on this example web page.
  2. When you click the first button, it will draw a rectangle with the linear gradient color.
  3. When you click the second button, it will draw a rectangle with the radial gradient color.
    html5-canvas-gradient-color-example
  4. There are 2 source files in this example html5-canvas-gradient.html, html5-canvas-gradient.js.
  5. html5-canvas-gradient.html.
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Html5 Canvas Linear & Radial Gradient Examples</title>
    <script type="text/javascript" src="html5-canvas-gradient.js" charset="utf-8"></script>
    <style>
    canvas{
      display:none;
    }
    </style>
    </head>
    <body>
    <h3>Html5 Canvas Linear Gradient Example.</h3>
    <div>
        <input type="button" id="draw-linear-gradient" value="Linear Gradient Example" onclick="drawLinearGradient('linear-gradient-canvas', this)" />
        <canvas id="linear-gradient-canvas"></canvas>
    </div>
    <h3>Html5 Canvas Radial Gradient Example.</h3>
    <div>
        <input type="button" id="draw-radial-gradient" value="Radial Gradient Example" onclick="drawRadialGradient('radial-gradient-canvas', this)" />
        <canvas id="radial-gradient-canvas"></canvas>
    </div>
    </body>
    </html>
  6. html5-canvas-gradient.js.
    /**
     * 
     */
    
    function drawLinearGradient(id, src){
        
        // Get the html canvas object.
        var canvas = document.getElementById(id);
        
        if(canvas==null){
            
            alert("Can not find the html element with element id " + id);
            
            return false;
            
        }
        
        canvas.style.display = 'block';
        
        ctx = canvas.getContext('2d');
        
        lgStartX = 0;
        lgStartY = 0;
        lgEndX = 300;
        lgEndY = 0;
        
        linearGradient = ctx.createLinearGradient(lgStartX, lgStartY, lgEndX, lgEndY);
        
        linearGradient.addColorStop(0, 'red');
        
        linearGradient.addColorStop(0.5, 'yellow');
        
        linearGradient.addColorStop(1, 'blue');
        
        ctx.fillStyle = linearGradient;
        
        leftX = 10;
        topY = 10;
        width = 500;
        height = 100;
        
        ctx.fillRect(leftX, topY, width, height);
        
        
    }
    
    
    function drawRadialGradient(id, src){
        
        // Get the html canvas object.
        var canvas = document.getElementById(id);
        
        if(canvas==null){
            
            alert("Can not find the html element with element id " + id);
            
            return false;
            
        }
        
            
        canvas.style.display = 'block';
        
        ctx = canvas.getContext('2d');
        
        rgStartX = 100;
        rgStartY = 70;
        rgStartRadius = 5;
        
        rgEndX = 100;
        rgEndY = 70;
        rgEndRadius = 90;
        
        radialGradient = ctx.createRadialGradient(rgStartX, rgStartY, rgStartRadius, rgEndX, rgEndY, rgEndRadius);
        
        radialGradient.addColorStop(0, 'red');
        
        radialGradient.addColorStop(0.5, 'yellow');
        
        radialGradient.addColorStop(1, 'blue');
        
        ctx.fillStyle = radialGradient;
        
        leftX = 0;
        topY = 0;
        width = 200;
        height = 200;
        
        ctx.fillRect(leftX, topY, width, height);
    }

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.