Animation along a Circle : Animation « GUI Components « JavaScript DHTML






Animation along a Circle

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->
<html>
<head>
<title>Circle Animation </title>
<style rel="stylesheet" id="mainStyle" type="text/css">
html {background-color:#cccccc}
body {background-color:#eeeeee; font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:12px;
    margin-left:15%; margin-right:15%; border:3px groove darkred; padding:15px}
h1 {text-align:right; font-size:1.5em; font-weight:bold}
h2 {text-align:left; font-size:1.1em; font-weight:bold; text-decoration:underline}
.buttons {margin-top:10px}


</style>

<script language="JavaScript" type="text/javascript">
// animation object holds numerous properties related to motion
var anime = new Object();

// initialize default anime object
function initAnime() {
    anime = {elemID:"", 
             xStart:0, 
             yStart:0, 
             xCurr:0, 
             yCurr:0, 
             next:1,
             pts:1,
             radius:1,
             interval:null
            };
}

// stuff animation object with necessary explicit and calculated values
function initCircAnime(elemID, startX, startY, pts, radius) {
    initAnime(); 
    anime.elemID = elemID;
    anime.xCurr = anime.xStart = startX;
    anime.yCurr = anime.yStart = startY;
    anime.pts = pts;
    anime.radius = radius;
    // set element's start position
    document.getElementById(elemID).style.left = startX + "px";
    document.getElementById(elemID).style.top = startY + "px";
    // start the repeated invocation of the animation
    anime.interval = setInterval("doCircAnimation()", 10);
}

function doCircAnimation() {
    if (anime.next < anime.pts) {
        var x = anime.xCurr + 
           Math.round(Math.cos(anime.next * (Math.PI/(anime.pts/2))) * anime.radius);
        var y = anime.yCurr + 
           Math.round(Math.sin(anime.next * (Math.PI/(anime.pts/2))) * anime.radius);
        document.getElementById(anime.elemID). style.left = x + "px";
        document.getElementById(anime.elemID). style.top = y + "px";
        anime.xCurr = x;
        anime.yCurr = y;
        anime.next++;
    } else {
        document.getElementById(anime.elemID).style.left = anime.xStart + "px";
        document.getElementById(anime.elemID).style.top = anime.yStart + "px";
        clearInterval(anime.interval);
    }
}

</script>
</head>
<body style="height:400px;"  onload="initAnime()">
<h1>Circular Animation</h1>
<hr />
<form>
<input type="button" value="Circle" 
onclick="initCircAnime('block', 400, 200, 36, 20)">
</form>

<div id="block" style="position:absolute; top:200px; left:400px; height:200px; width:200px; background-color:orange"></div>

</body>
</html>


           
         
  








Related examples in the same category

1.Attack animation
2.Circle Animation
3.Right to left animation
4.Flash animation in Javascript
5.Flash animation in JavaScript: Changing style Properties
6.Animation along Straight Line
7.Dancing Text (IE)
8.Type Writer effect (IE)
9.Type Writer Effect 1.1 (IE)
10.JavaScript Ticker 1.2 (IE)
11.Animation: Lottery Number Picker and Statistics
12.Animation: wriggly
13.Animation: welcome message
14.Animation: trio
15.Auto lotto dip
16.Animation: snow
17.Animation: star
18.Animation: mouse doodle
19.Animation: fireworks
20.Animation: pretty
21.Animation: Random Movement
22.Lotto number draw
23.Following eyes
24.Animation: three eyes
25.Animation: eyes
26.Spot light
27.Big static eyes
28.Animation based on DIV with color flash
29.Framework for creating CSS-based animations
30.Animate dynamic element h1 tag
31.Popup window animation (fly across screen)
32.Animation on several images
33.Using the onFilterChange Event Handler
34.JavaScript Animation
35.Periodically Updating the Text Displayed by an HTML Element
36.Moving an Airplane Across a Web Page
37.Link Hint Scroller 2.0 (IE)
38.Snow animation
39.Animation with JSTween