Canvas How to - Hover to change fill style








Question

We would like to know how to hover to change fill style.

Answer


<!--from   w  w w .ja v a  2 s  .c o  m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
    var ctx = document.getElementById('canvas').getContext('2d');
    ctx.stroke = "red";
    ctx.beginPath();
    ctx.moveTo(30, 30);
    ctx.lineTo(150, 150);
    ctx.bezierCurveTo(60, 70, 60, 70, 70, 150);
    ctx.lineTo(30, 30);
    ctx.fillStyle = 'black';
    ctx.fill();
    $('#canvas').hover( function() {
            ctx.fillStyle = 'red';
            ctx.fill();
        },
        function() {
            ctx.fillStyle = 'black';
            ctx.fill();         
    });
});//]]>  
</script>
</head>
<body>
  <canvas id='canvas'></canvas>
</body>
</html>

The code above is rendered as follows: