Animation: wriggly : Animation : GUI Components : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP
JavaScript DHTML Home »  GUI Components   » [  Animation  ]   
 



Animation: wriggly

Please note that some example is only working under IE or Firefox.

//Wriggly by Kurt Grigg - http://www.btinternet.com/~kurt.grigg/javascript

/*
Paste this link as the last thing on your page just before </body></html>

<script type="text/javascript" src="wriggly.js"></script>

To edit the colours, right click on the wriggly.js file icon and choose edit.

Make sure the wriggly.js file is in/uploaded to the same directory/folder as the 
web page using it!
 

*/


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<title>Wriggly</title>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="content-script-type" content="text/javascript">
<meta http-equiv="content-style-type" content="text/css">

<style type="text/css">
<!--
body{
background-color:#000000;
}
//-->
</style>

</head>
<body>



<script type="text/javascript">
//Wriggly by Kurt Grigg - http://www.btinternet.com/~kurt.grigg/javascript

if  ((document.getElementById&& 
window.addEventListener || window.attachEvent){

(function(){

//Configure here....

var colours = new Array('#ff0000','#00ff00','#ffa500','#fff000');
var num = 10;    //number of segments.
var icen = 2;    //next segment is times 'icen' previous segment size.
var rep = 40;    //setTimeout speed.
var min = 0;     //slowest speed.
var max = 10;    //fastest speed.

//End.

var temp1 = new Array();
var temp2 = new Array();
if (icen%!= 0icen++;
var fcen = icen/2;
var d = document;
var idx = d.getElementsByTagName('div').length;

var dims;

for (i = 0; i < num; i++){
var randcol = colours[Math.floor(Math.random()*colours.length)];
var dims = (i+1* icen;
document.write('<div id="worm'+(idx+i)+'" style="position:absolute;top:0px;left:0px;width:'
+dims+'px;height:'+dims+'px;font-size:1px;border: 1px solid '+randcol+';overflow:hidden">.<\/div>');
}

var h,w,r;
var y = 0;
var x = 0;
var dir = 45;   //direction.
var acc = 1;    //acceleration.
var newacc = new Array(1,0,1);
var vel = 1;    //initial speed.
var sev = 0;
var newsev = new Array(8,-8,5,-5,3,-3,1,-1,0);

//counters.
var c1 = 0;    //time between changes.
var c2 = 0;    //new time between changes.

var pix = "px";

var strictmod = ((document.compatMode&& 
document.compatMode.indexOf("CSS"!= -1);


var domWw = (typeof window.innerWidth == "number");
var domSy = (typeof window.pageYOffset == "number");

if (domWwr = window;
else
  if (d.documentElement && 
  typeof d.documentElement.clientWidth == "number" && 
  d.documentElement.clientWidth != 0)
  r = d.documentElement;
 else
  if (d.body && 
  typeof d.body.clientWidth == "number")
  r = d.body;
 }
}


function winsize(){
var oh,sy,ow,sx,rh,rw;
if (domWw){
  if (d.documentElement && d.defaultView && 
  typeof d.defaultView.scrollMaxY == "number"){
  oh = d.documentElement.offsetHeight;
  sy = d.defaultView.scrollMaxY;
  ow = d.documentElement.offsetWidth;
  sx = d.defaultView.scrollMaxX;
  rh = oh-sy;
  rw = ow-sx;
 }
 else{
  rh = r.innerHeight;
  rw = r.innerWidth;
 }
h = rh - (dims+fcen+1);
w = rw - (dims+fcen+1);
}
else{
h = r.clientHeight - (dims+fcen+1);
w = r.clientWidth - (dims+fcen+1);
}
}


function scrl(yx){
var y,x;
if (domSy){
 y = r.pageYOffset;
 x = r.pageXOffset;
 }
else{
 y = r.scrollTop;
 x = r.scrollLeft;
 }
return (yx == 0)?y:x;
}


function followleader(){
 for (i = 0; i < num; i++){
  if (i < num-1){
   temp1[i].top = parseFloat(temp2[i].top)  + fcen + pix;
   temp1[i].left = parseFloat(temp2[i].left+ fcen + pix;
  
  else{
   temp1[i].top = y  + scrl(0+ pix;
   temp1[i].left = x + scrl(1+ pix;
  }
 }
}


function newpath(){
sev = newsev[Math.floor(Math.random()*newsev.length)];
acc = newacc[Math.floor(Math.random()*newacc.length)];
c2 = Math.floor(10+Math.random()*50);
}


function animate(){
var vb,hb,dy,dx,curr;
if (acc == 1vel +=0.05;
if (acc == 0vel -=0.05;
if (vel >= maxvel = max;
if (vel <= minvel = min;
c1++;
if (c1 >= c2){
 newpath();
 c1=0;
}
curr = dir+=sev;

dy = vel * Math.sin(curr*Math.PI/180);
dx = vel * Math.cos(curr*Math.PI/180);

y+=dy;
x+=dx;

//horizontal-vertical bounce.
vb = 180-dir;
hb = 0-dir;

//Corner rebounds?
if ((y < 1&& (x < 1)){y = 1; x = 1; dir = 45;}
if ((y < 1&& (x > w)){y = 1; x = w; dir = 135;}
if ((y > h&& (x < 1)){y = h; x = 1; dir = 315;}
if ((y > h&& (x > w)){y = h; x = w; dir = 225;}

//edge rebounds.
if (y < 1) {y = 1; dir = hb;}  
if (y > h) {y = h; dir = hb;}  
if (x < 1) {x = 1; dir = vb;
if (x > w) {x = w; dir = vb;

followleader();
setTimeout(animate,rep);
}


function init(){
for (i=0; i < num; i++){
 temp1[i= document.getElementById("worm"+(idx+i)).style;
 if (i < num-1)
 temp2[i= document.getElementById("worm"+(idx+(i+1))).style; 
}
winsize();

var iniafter = Math.floor(500+Math.random()*2000);
setTimeout(animate,iniafter);
}


if (window.addEventListener){
 window.addEventListener("resize",winsize,false);
 window.addEventListener("load",init,false);
}  
else if (window.attachEvent){
 window.attachEvent("onresize",winsize);
 window.attachEvent("onload",init);


})();
}//End.
</script>

</body>
</html>

           
       
Related examples in the same category
1.  Attack animationHas Download File
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.  Animation along a Circle
8.  Dancing Text (IE)
9.  Type Writer effect (IE)
10.  Type Writer Effect 1.1 (IE)
11.  JavaScript Ticker 1.2 (IE)
12.  Animation: Lottery Number Picker and Statistics
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 eyesHas Download File
24.  Animation: three eyesHas Download File
25.  Animation: eyesHas Download File
26.  Spot lightHas Download File
27.  Big static eyesHas Download File
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 animationHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.