Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!doctype html>
<html>
  <head>
    <title> Fill Rectangle Canvas API </title>
    <link rel="stylesheet" href="fillRect.css" type="text/css" />
  </head>
  
  <body>
    <canvas id="canvas"(>  </canvas>
    <script type="text/javascript" src="fillRect.js"></script>
  </body>
</html>
 
#canvas {
  width: 600px;
  height: 300px;
  border: 1px dashed black;
  margin: auto;
  display: block;
  margin-top: 100px;
}
 
(function() {
  
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');
  context.fillStyle = '#ccc';
  context.fillRect(0, 0, 100, 50); //x,y,w,h
  
  // translate(canvas, context, 0, 0, 100, 50, 'x', 5);
  
  //context.fillStyle = 'skyblue';
  //context.fillRect(canvas.width-100, canvas.height-50, 100, 50); //x,y,w,h
  
  translate(canvas, context, 0, 0, 100, 50, 'x', 5);
  
  // translate(canvas, context, canvas.width-100, canvas.height-50, 100, 50, 'x', -5);
  
  function translate(canvas, context, x, y, w, h, direction, interval) {
    context.fillRect(x, y, w, h);
    
    if (direction == 'x') {
      if ((x + interval + w >= canvas.width) || (x + interval < 0)) interval = -1 * interval;
      
      setTimeout(function() {
        context.clearRect(x, y, w, h);
        translate(canvas, context, x + interval, y, w, h, direction, interval);
      }, 1000);
    }
  }
  
}());
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
blunderboypro
0viewers