Kamis, 25 Desember 2025

DHTML - Animasi Path

 



<html>

<body>

<div id=Item1 style="position: absolute; left: 130; top: 90;width:20;height:10;background-color:yellow">1</div>

<div id=Item2 style="position: absolute; left: 90; top: 130;width:20;height:10;background-color:blue">2</div>

<div id=Item3 style="position: absolute; left: 110; top: 70;width:20;height:10;background-color:pink">3</div>

<div id=Item4 style="position: absolute; left: 50; top: 110;width:20;height:10;background-color:tan">4</div>

<div id=Item5 style="position: absolute; left: 170; top: 90;width:20;height:10;background-color:cyan">5</div>

<div id=Item6 style="position: absolute; left: 200; top: 170;width:20;height:10;background-color:#cccccc">6</div>


<input type=button value="Start" onclick="runWaypoint(Item1, 0, 1); runWaypoint(Item2, 0, 1);">


<div id=Debug>Generation</div>


<script>

var tickDuration;

tickDuration = 50;


var activeObjectCount;

var activeObjects;

var itemDeactivated;


var tickGeneration;


activeObjects = new Array();

activeObjectCount = 0;

timerRefcount = 0;

itemDeactivated = false;


tickGeneration = 0;


function initializePath(e) {

 e.waypointX = new Array();

 e.waypointY = new Array();

 e.duration = new Array();

}


function addWaypoint(e, number, x, y, duration) {

 e.waypointX[number] = x;

 e.waypointY[number] = y;

 e.duration[number] = duration;

}


function compact() {

  var i, n, c;


  n = new Array();

  c = 0;

  itemDeactivated = false;


  for (i=0; i<activeObjectCount; i++)  {

     if (activeObjects[i].active == true) {

        n[c] = activeObjects[i];

        c++;

     }

  }


  activeObjects = n;

  activeObjectCount = c;

}


function tick(generation) {

  if (generation < tickGeneration) {

    // alert("Error "+generation);

     return;

  }


  //alert("tick: "+generation);


  if (itemDeactivated)

     compact();


  if (activeObjectCount == 0) {

     return;

  }

  else {

    for (i=0; i<activeObjectCount; i++) {

      moveElement(activeObjects[i]);

    }


    window.setTimeout("tick("+generation+");", tickDuration);

  }

}


function start(e) {

  if (itemDeactivated)

     compact();


  activeObjects[activeObjectCount] = e;

  activeObjectCount++;


  if (activeObjectCount == 1) { 

    tickGeneration++;

    tick(tickGeneration);

  }

}


function runWaypoint(e, startPoint, endPoint) {

  var startX, startY, endX, endY, duration;


  if (e.waypointX == null)  

    return;   


  startX = e.waypointX[startPoint];

  startY = e.waypointY[startPoint];

  endX = e.waypointX[endPoint];

  endY = e.waypointY[endPoint];


  duration = e.duration[endPoint];

  e.ticks = duration / tickDuration;


  e.endPoint = endPoint;

  e.active = true;

  e.currTick = 0;

  e.dx = (endX - startX) / e.ticks;

  e.dy = (endY - startY) / e.ticks;

  e.style.posLeft = startX;

  e.style.posTop = startY;


  start(e);

}  


function moveElement(e) {

  e.style.posLeft += e.dx;

  e.style.posTop += e.dy;

  e.currTick++;


  if (e.currTick > e.ticks) {

    e.active = false;

    itemDeactivated = true;


    if (e.onpathcomplete != null) {

       window.pathElement = e;

       e.onpathcomplete()

    }

  }

}


</script>


<script>

// need to call initializePath on all objects that will be moved with this mechanism

initializePath(Item1);

initializePath(Item2);

initializePath(Item3);

initializePath(Item4);

initializePath(Item5);

initializePath(Item6);


// the 0th waypoint is the initial position for waypoint #1

// syntax is item, waypoint, endx, endy, duration in msecs

addWaypoint(Item1, 0, 0, 0, 0);

addWaypoint(Item1, 1, 200, 200, 2000);


addWaypoint(Item2, 0, 100, 100, 0);

addWaypoint(Item2, 1, 400, 100, 4000);


addWaypoint(Item3, 0, 400, 400, 0);

addWaypoint(Item3, 1, 200, 100, 1000);


addWaypoint(Item4, 0, 0, 0, 0);

addWaypoint(Item4, 1, 200, 200, 2000);


addWaypoint(Item5, 0, 100, 100, 0);

addWaypoint(Item5, 1, 400, 100, 4000);


addWaypoint(Item6, 0, 400, 400, 0);

addWaypoint(Item6, 1, 200, 100, 1000);


function endfunction() {

  // syntax for runWaypoint is Item, start point, end point

  runWaypoint(Item3, 0, 1);

  runWaypoint(Item4, 0, 1);

  runWaypoint(Item5, 0, 1);

  runWaypoint(Item6, 0, 1);

}


function endfunction2() {

  runWaypoint(Item1, 0, 1);

}


Item1.onpathcomplete = endfunction;

Item6.onpathcomplete = endfunction2;


</script>

</body>

</html>


Tidak ada komentar: