fill canvas with linear gradient - Javascript Canvas

Javascript examples for Canvas:Gradient

Description

fill canvas with linear gradient

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.js"></script> 
      <style id="compiled-css" type="text/css">

#chart {/*from  ww w  .  ja  v a2 s .co m*/
   width:80px;
   height:80px;
   position:fixed;
   top:20px;
   left:20px;
}
.yellow {
   background-color: #FFBF00;
}
.orange {
   background-color: #FF8000;
}
#myCanvas {
   width:200px;
   height:150px;
   position:fixed;
   top:250px;
   left:200px;
   border:1px solid black;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var $swatch = $(".swatch");
$swatch.click(function (e) {
    color = $(this).css("background-color");
    var canvas = document.getElementById('myCanvas');
    var ctx = canvas.getContext('2d');
    ctx.rect(0, 0, canvas.width, canvas.height);
    var grd = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
    grd.addColorStop(0, color);
    grd.addColorStop(1, '#fff');
    ctx.fillStyle = grd;
    ctx.fill();
});
    });

      </script> 
   </head> 
   <body> 
      <table id="chart"> 
         <tbody>
            <tr> 
               <td class="swatch orange"></td> 
               <td class="swatch yellow"></td> 
            </tr> 
         </tbody>
      </table> 
      <canvas id="myCanvas"></canvas>  
   </body>
</html>

Related Tutorials