Change an image every time a link is pressed - Javascript Canvas

Javascript examples for Canvas:image

Description

Change an image every time a link is pressed

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.8.3.js"></script> 
      <style id="compiled-css" type="text/css">

#myCanvas {/*w  w  w.ja v  a  2 s .co m*/
   width: 300px;
   height: 140px;
   margin: 10px auto;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = 70;
function drawCircle(colour) {
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    ctx.fillStyle = colour;
    ctx.fill();
}
$("#online").click( function() { drawCircle("green"); });
$("#offline").click( function() { drawCircle("grey"); });
$("#connecting").click( function() { drawCircle("orange"); });
drawCircle("grey");
    });

      </script> 
   </head> 
   <body> 
      <canvas id="myCanvas"></canvas> 
      <a id="online">Online</a> 
      <a id="offline">Offline</a> 
      <a id="connecting">Connecting</a>  
   </body>
</html>

Related Tutorials