Javascript to represent progress along irregular path

Share

The goal is to simulate a motion by filling this path drawn over a background image using JavaScript only.
Will consider a simple way, cut in solid color, with transparent background. In real situation will be, say, a meaningful path with respect to some landscape.

In the image below to create a move in the first section of the path will use a second smaller image – the yellow block – of such shape so that one edge is as much as possible perpendicular to the path. This image will move in the background (smaller z-index) of the main image creating the desired fill effect.
  

    <div>
        <img src="block1.png" />
        <img alt="" src="bkg.png" />
    </div>

First some variables that will allow us to initially position the small image and to control a timer:

  var nTick = 100; // miliseconds
  var nStartY = 106.7;
  var timer;
  var dblStep;
  var nPosition = 0;
  var nTarget;
  // img for section 1
  var block1;
  var nBlock1X = 49;
  var nBlock1Y = 86;

The timer will repeat the function call until the path is filled or the target is reached. This example covers only the first section of the path, the rest being pretty much similar.

  function window_onload() {
    nTarget = document.getElementById("Target").value; ;
    dblStep = .5; //target / 40;
    // init
    block1 = document.getElementById("imgblock1");
    block1.style.left = nBlock1X + "%";
    block1.style.top = nBlock1Y + "%";
    // start timer
    timer = window.setInterval(time, nTick);
    return true;
    }
 
  function time() {
    if (nPosition <= 33)
      ShowPosition1();
    else if (nPosition > 33)  && nPosition <= 67)
      ShowPosition2();
    else
      ShowPosition3();     
   nPosition = RoundNumber(nPosition + dblStep, 2);
   if (nPosition > nTarget)
     window.clearInterval(timer);
   }

Since the path is irregular, some adjustments to the moving image block will be needed to create a perpendicular fill. To avoid flickering the image block is hidden and displayed again after the new position is determined.

  function ShowPosition1() {
    var nClimb1RightTrans;
 
    block1.style.display = "none";
    if (block1.style.pixelTop < 492 && block1.style.pixelTop > 475) {
      window.clearInterval(timer);
      timer = window.setInterval(time, nTick * 2);
      nClimb1RightTrans = .8;
      }
    else {
      window.clearInterval(timer);
      timer = window.setInterval(time, nTick);
      nClimb1RightTrans = .2;
      }
    block1.style.left = (parseFloat(block.style.left) + nClimb1RightTrans) + "%";
    block1.style.top = (((nStartY + ((nPosition / 33) * (nBlock1Y  - nStartY)))) - 30) + "%";
    block1.style.display = "block";
    }

A fine tuning situation will be to handle the small portion where the path changes direction. For this will alter two parameters of the moving: the speed and right shifting.
We can use constant values for the pixelTop since it’s all relative to the background image.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close