Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<html>
<head>
<meta charset=utf-8 />
<title>Four methods of Javascript animation - 3. Clip-rect method</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
  <a href="http://michalbe.blogspot.com">MichalBe's Blog</a>
</body>
</html>
 
var width = 300,
    height = 56,
    frames = 10,
    
    actualFrame = 0,
       
    posX = 100,
    posY = 100;
var canvas = document.createElement('img'),
  canvasStyle = canvas.style;
  
  canvas.src = 'http://images.virtualdesign.pl/images/78335sprite.jpg';
  canvasStyle.position = "absolute";
  canvasStyle.top = posX;
  canvasStyle.left = posY;
  //canvasStyle.width = width;
  //canvasStyle.height = height;
  document.body.appendChild(canvas);
  
var draw = function(){
  var frameTop = height * actualFrame, 
    frameLeft = 0, 
    frameRight = width, 
    frameBottom = frameTop + height;
                                 
  canvasStyle.clip = "rect("
    +frameTop +"px " //top
    +frameRight +"px " //right
    +frameBottom +"px " //bottom
    +frameLeft +"px )"; //left
                                      
  //canvasStyle.left = posX - width * actualAnimation;
  canvasStyle.top = posY - height * actualFrame;
  
    if (actualFrame == frames) {
      actualFrame = 0;
    }
    else {
      actualFrame++;
    }
  
} 
setInterval(draw, 80);
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers