Drawing lines using RequestAnimationFrame()





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to draw a horizontal line when the game launches and was able to come up with something that drew the line using requestAnimationFrame() and ctx.stroke().



The problem is that when I attempt to edit the length of the line by changing the values of the end point on the line, the line remains the same length.



If anyone could take a look at it and explain what is going on I would really appreciate it.






var canvasTop = document.getElementById('top');
var ctxTop = canvasTop.getContext('2d');

var frameCountTop = 0;
var fpsTop, fpsIntervalTop, startTimeTop, nowTop, thenTop, elapsedTop;

function startAnimatingTop(fpsTop) {
fpsIntervalTop = 1000 / fpsTop;
thenTop = Date.now();
startTimeTop = thenTop;
drawTop();
}

var topLinePointA = [32, 80];
var topLinePointB = [280, 80];
var topLineStart = topLinePointA[0];
var topLineIncrement = topLineStart + 1;
var topLineEnd = topLinePointB[0];

function drawTop() {
var i = 32;
while (i < topLineEnd) {
requestAnimationFrame(drawTop);
i++;
nowTop = Date.now();
elapsedTop = nowTop - thenTop;
if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
thenTop = nowTop - (elapsedTop % fpsIntervalTop);
ctxTop.lineWidth = 20;
ctxTop.strokeStyle = "black";
ctxTop.moveTo(topLineStart, 80);
ctxTop.lineTo(topLineIncrement, 80);
ctxTop.stroke();
topLineStart += 1;
} else {
cancelAnimationFrame(drawTop);
return;
}
}
}

startAnimatingTop(100);

/*HANGMAN STYLES*/

/*CLASS SELECTORS*/
.lineContainer {
/*border-style: solid;
border-color: blue;*/
}

#top {
position: absolute;
height: 5%;
width: 45%;
left: 30%;
}

#side {
position: absolute;
bottom: 20%;
left: 70%;
height: 78%;
width: 5%;
}

#bottom {
position: absolute;
bottom: 35%;
height: 5%;
width: 56%;
left: 20%;
}

#hangman {
position: absolute;
left: 30%;
height: 40%;
width: 10%;
}

<canvas id='top' class='lineContainer'></canvas>
<canvas id='side' class='lineContainer'></canvas>
<canvas id='bottom' class='lineContainer'></canvas>
<canvas id='hangman' class='lineContainer'></canvas>
<canvas id='puzzle'></canvas>
<div id='scorecard'>

</div>












share|improve this question































    0















    I want to draw a horizontal line when the game launches and was able to come up with something that drew the line using requestAnimationFrame() and ctx.stroke().



    The problem is that when I attempt to edit the length of the line by changing the values of the end point on the line, the line remains the same length.



    If anyone could take a look at it and explain what is going on I would really appreciate it.






    var canvasTop = document.getElementById('top');
    var ctxTop = canvasTop.getContext('2d');

    var frameCountTop = 0;
    var fpsTop, fpsIntervalTop, startTimeTop, nowTop, thenTop, elapsedTop;

    function startAnimatingTop(fpsTop) {
    fpsIntervalTop = 1000 / fpsTop;
    thenTop = Date.now();
    startTimeTop = thenTop;
    drawTop();
    }

    var topLinePointA = [32, 80];
    var topLinePointB = [280, 80];
    var topLineStart = topLinePointA[0];
    var topLineIncrement = topLineStart + 1;
    var topLineEnd = topLinePointB[0];

    function drawTop() {
    var i = 32;
    while (i < topLineEnd) {
    requestAnimationFrame(drawTop);
    i++;
    nowTop = Date.now();
    elapsedTop = nowTop - thenTop;
    if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
    thenTop = nowTop - (elapsedTop % fpsIntervalTop);
    ctxTop.lineWidth = 20;
    ctxTop.strokeStyle = "black";
    ctxTop.moveTo(topLineStart, 80);
    ctxTop.lineTo(topLineIncrement, 80);
    ctxTop.stroke();
    topLineStart += 1;
    } else {
    cancelAnimationFrame(drawTop);
    return;
    }
    }
    }

    startAnimatingTop(100);

    /*HANGMAN STYLES*/

    /*CLASS SELECTORS*/
    .lineContainer {
    /*border-style: solid;
    border-color: blue;*/
    }

    #top {
    position: absolute;
    height: 5%;
    width: 45%;
    left: 30%;
    }

    #side {
    position: absolute;
    bottom: 20%;
    left: 70%;
    height: 78%;
    width: 5%;
    }

    #bottom {
    position: absolute;
    bottom: 35%;
    height: 5%;
    width: 56%;
    left: 20%;
    }

    #hangman {
    position: absolute;
    left: 30%;
    height: 40%;
    width: 10%;
    }

    <canvas id='top' class='lineContainer'></canvas>
    <canvas id='side' class='lineContainer'></canvas>
    <canvas id='bottom' class='lineContainer'></canvas>
    <canvas id='hangman' class='lineContainer'></canvas>
    <canvas id='puzzle'></canvas>
    <div id='scorecard'>

    </div>












    share|improve this question



























      0












      0








      0








      I want to draw a horizontal line when the game launches and was able to come up with something that drew the line using requestAnimationFrame() and ctx.stroke().



      The problem is that when I attempt to edit the length of the line by changing the values of the end point on the line, the line remains the same length.



      If anyone could take a look at it and explain what is going on I would really appreciate it.






      var canvasTop = document.getElementById('top');
      var ctxTop = canvasTop.getContext('2d');

      var frameCountTop = 0;
      var fpsTop, fpsIntervalTop, startTimeTop, nowTop, thenTop, elapsedTop;

      function startAnimatingTop(fpsTop) {
      fpsIntervalTop = 1000 / fpsTop;
      thenTop = Date.now();
      startTimeTop = thenTop;
      drawTop();
      }

      var topLinePointA = [32, 80];
      var topLinePointB = [280, 80];
      var topLineStart = topLinePointA[0];
      var topLineIncrement = topLineStart + 1;
      var topLineEnd = topLinePointB[0];

      function drawTop() {
      var i = 32;
      while (i < topLineEnd) {
      requestAnimationFrame(drawTop);
      i++;
      nowTop = Date.now();
      elapsedTop = nowTop - thenTop;
      if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
      thenTop = nowTop - (elapsedTop % fpsIntervalTop);
      ctxTop.lineWidth = 20;
      ctxTop.strokeStyle = "black";
      ctxTop.moveTo(topLineStart, 80);
      ctxTop.lineTo(topLineIncrement, 80);
      ctxTop.stroke();
      topLineStart += 1;
      } else {
      cancelAnimationFrame(drawTop);
      return;
      }
      }
      }

      startAnimatingTop(100);

      /*HANGMAN STYLES*/

      /*CLASS SELECTORS*/
      .lineContainer {
      /*border-style: solid;
      border-color: blue;*/
      }

      #top {
      position: absolute;
      height: 5%;
      width: 45%;
      left: 30%;
      }

      #side {
      position: absolute;
      bottom: 20%;
      left: 70%;
      height: 78%;
      width: 5%;
      }

      #bottom {
      position: absolute;
      bottom: 35%;
      height: 5%;
      width: 56%;
      left: 20%;
      }

      #hangman {
      position: absolute;
      left: 30%;
      height: 40%;
      width: 10%;
      }

      <canvas id='top' class='lineContainer'></canvas>
      <canvas id='side' class='lineContainer'></canvas>
      <canvas id='bottom' class='lineContainer'></canvas>
      <canvas id='hangman' class='lineContainer'></canvas>
      <canvas id='puzzle'></canvas>
      <div id='scorecard'>

      </div>












      share|improve this question
















      I want to draw a horizontal line when the game launches and was able to come up with something that drew the line using requestAnimationFrame() and ctx.stroke().



      The problem is that when I attempt to edit the length of the line by changing the values of the end point on the line, the line remains the same length.



      If anyone could take a look at it and explain what is going on I would really appreciate it.






      var canvasTop = document.getElementById('top');
      var ctxTop = canvasTop.getContext('2d');

      var frameCountTop = 0;
      var fpsTop, fpsIntervalTop, startTimeTop, nowTop, thenTop, elapsedTop;

      function startAnimatingTop(fpsTop) {
      fpsIntervalTop = 1000 / fpsTop;
      thenTop = Date.now();
      startTimeTop = thenTop;
      drawTop();
      }

      var topLinePointA = [32, 80];
      var topLinePointB = [280, 80];
      var topLineStart = topLinePointA[0];
      var topLineIncrement = topLineStart + 1;
      var topLineEnd = topLinePointB[0];

      function drawTop() {
      var i = 32;
      while (i < topLineEnd) {
      requestAnimationFrame(drawTop);
      i++;
      nowTop = Date.now();
      elapsedTop = nowTop - thenTop;
      if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
      thenTop = nowTop - (elapsedTop % fpsIntervalTop);
      ctxTop.lineWidth = 20;
      ctxTop.strokeStyle = "black";
      ctxTop.moveTo(topLineStart, 80);
      ctxTop.lineTo(topLineIncrement, 80);
      ctxTop.stroke();
      topLineStart += 1;
      } else {
      cancelAnimationFrame(drawTop);
      return;
      }
      }
      }

      startAnimatingTop(100);

      /*HANGMAN STYLES*/

      /*CLASS SELECTORS*/
      .lineContainer {
      /*border-style: solid;
      border-color: blue;*/
      }

      #top {
      position: absolute;
      height: 5%;
      width: 45%;
      left: 30%;
      }

      #side {
      position: absolute;
      bottom: 20%;
      left: 70%;
      height: 78%;
      width: 5%;
      }

      #bottom {
      position: absolute;
      bottom: 35%;
      height: 5%;
      width: 56%;
      left: 20%;
      }

      #hangman {
      position: absolute;
      left: 30%;
      height: 40%;
      width: 10%;
      }

      <canvas id='top' class='lineContainer'></canvas>
      <canvas id='side' class='lineContainer'></canvas>
      <canvas id='bottom' class='lineContainer'></canvas>
      <canvas id='hangman' class='lineContainer'></canvas>
      <canvas id='puzzle'></canvas>
      <div id='scorecard'>

      </div>








      var canvasTop = document.getElementById('top');
      var ctxTop = canvasTop.getContext('2d');

      var frameCountTop = 0;
      var fpsTop, fpsIntervalTop, startTimeTop, nowTop, thenTop, elapsedTop;

      function startAnimatingTop(fpsTop) {
      fpsIntervalTop = 1000 / fpsTop;
      thenTop = Date.now();
      startTimeTop = thenTop;
      drawTop();
      }

      var topLinePointA = [32, 80];
      var topLinePointB = [280, 80];
      var topLineStart = topLinePointA[0];
      var topLineIncrement = topLineStart + 1;
      var topLineEnd = topLinePointB[0];

      function drawTop() {
      var i = 32;
      while (i < topLineEnd) {
      requestAnimationFrame(drawTop);
      i++;
      nowTop = Date.now();
      elapsedTop = nowTop - thenTop;
      if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
      thenTop = nowTop - (elapsedTop % fpsIntervalTop);
      ctxTop.lineWidth = 20;
      ctxTop.strokeStyle = "black";
      ctxTop.moveTo(topLineStart, 80);
      ctxTop.lineTo(topLineIncrement, 80);
      ctxTop.stroke();
      topLineStart += 1;
      } else {
      cancelAnimationFrame(drawTop);
      return;
      }
      }
      }

      startAnimatingTop(100);

      /*HANGMAN STYLES*/

      /*CLASS SELECTORS*/
      .lineContainer {
      /*border-style: solid;
      border-color: blue;*/
      }

      #top {
      position: absolute;
      height: 5%;
      width: 45%;
      left: 30%;
      }

      #side {
      position: absolute;
      bottom: 20%;
      left: 70%;
      height: 78%;
      width: 5%;
      }

      #bottom {
      position: absolute;
      bottom: 35%;
      height: 5%;
      width: 56%;
      left: 20%;
      }

      #hangman {
      position: absolute;
      left: 30%;
      height: 40%;
      width: 10%;
      }

      <canvas id='top' class='lineContainer'></canvas>
      <canvas id='side' class='lineContainer'></canvas>
      <canvas id='bottom' class='lineContainer'></canvas>
      <canvas id='hangman' class='lineContainer'></canvas>
      <canvas id='puzzle'></canvas>
      <div id='scorecard'>

      </div>





      var canvasTop = document.getElementById('top');
      var ctxTop = canvasTop.getContext('2d');

      var frameCountTop = 0;
      var fpsTop, fpsIntervalTop, startTimeTop, nowTop, thenTop, elapsedTop;

      function startAnimatingTop(fpsTop) {
      fpsIntervalTop = 1000 / fpsTop;
      thenTop = Date.now();
      startTimeTop = thenTop;
      drawTop();
      }

      var topLinePointA = [32, 80];
      var topLinePointB = [280, 80];
      var topLineStart = topLinePointA[0];
      var topLineIncrement = topLineStart + 1;
      var topLineEnd = topLinePointB[0];

      function drawTop() {
      var i = 32;
      while (i < topLineEnd) {
      requestAnimationFrame(drawTop);
      i++;
      nowTop = Date.now();
      elapsedTop = nowTop - thenTop;
      if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
      thenTop = nowTop - (elapsedTop % fpsIntervalTop);
      ctxTop.lineWidth = 20;
      ctxTop.strokeStyle = "black";
      ctxTop.moveTo(topLineStart, 80);
      ctxTop.lineTo(topLineIncrement, 80);
      ctxTop.stroke();
      topLineStart += 1;
      } else {
      cancelAnimationFrame(drawTop);
      return;
      }
      }
      }

      startAnimatingTop(100);

      /*HANGMAN STYLES*/

      /*CLASS SELECTORS*/
      .lineContainer {
      /*border-style: solid;
      border-color: blue;*/
      }

      #top {
      position: absolute;
      height: 5%;
      width: 45%;
      left: 30%;
      }

      #side {
      position: absolute;
      bottom: 20%;
      left: 70%;
      height: 78%;
      width: 5%;
      }

      #bottom {
      position: absolute;
      bottom: 35%;
      height: 5%;
      width: 56%;
      left: 20%;
      }

      #hangman {
      position: absolute;
      left: 30%;
      height: 40%;
      width: 10%;
      }

      <canvas id='top' class='lineContainer'></canvas>
      <canvas id='side' class='lineContainer'></canvas>
      <canvas id='bottom' class='lineContainer'></canvas>
      <canvas id='hangman' class='lineContainer'></canvas>
      <canvas id='puzzle'></canvas>
      <div id='scorecard'>

      </div>






      javascript animation html5-canvas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 19:09









      ggorlen

      7,9633926




      7,9633926










      asked Nov 23 '18 at 18:55









      LarryYoungLarryYoung

      207




      207
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are many problems with your code.



          You need to read up on using the canvas. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D will help.




          1. Only call requestAnimationFrame once per frame. Putting it inside a while loop will just start 1000's of frames fighting for display refresh.

          2. Start paths with ctx.beginPath();

          3. Clear the canvas using ctx.clearRect(0,0,width,height)

          4. Set canvas size via attributes not via style properties.

          5. The first argument to the function called by requestAnimationFrame is the time. eg drawTop(time)


          See comments for more info.



          ctx.canvas.width = 100;  // << set the canvas size via canvas attributes not style
          ctx.canvas.height = 100;

          var thenTop = performance.now(); // to get time.

          requestAnimationFrame(drawTop); // <<< start the render loop with request, dont call direct

          function drawTop(nowTop) { // << time passed to render call
          requestAnimationFrame(drawTop); // <<< put call here not in loop
          ctxTop.clearRect(0, 0, ctxTop.canvas.width, ctxTop.canvas.height); // <<< clear the canvas
          var i = 32;
          while (i < topLineEnd) {
          // requestAnimationFrame(drawTop); // <<<<<<< Not here
          i++;

          elapsedTop = nowTop - thenTop;
          if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
          thenTop = nowTop - (elapsedTop % fpsIntervalTop);
          ctxTop.lineWidth = 20;
          ctxTop.strokeStyle = "black";
          ctxTop.beginPath(); // <<<<<< To start a new path
          ctxTop.moveTo(topLineStart, 80);
          ctxTop.lineTo(topLineIncrement, 80);
          ctxTop.stroke();
          topLineStart += 1;
          } else {
          return;
          }
          }
          }





          share|improve this answer
























          • Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

            – LarryYoung
            Nov 23 '18 at 22:35













          • good explanations and visuals. Easy to understand. Thanks again

            – LarryYoung
            Nov 23 '18 at 23:17












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451736%2fdrawing-lines-using-requestanimationframe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There are many problems with your code.



          You need to read up on using the canvas. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D will help.




          1. Only call requestAnimationFrame once per frame. Putting it inside a while loop will just start 1000's of frames fighting for display refresh.

          2. Start paths with ctx.beginPath();

          3. Clear the canvas using ctx.clearRect(0,0,width,height)

          4. Set canvas size via attributes not via style properties.

          5. The first argument to the function called by requestAnimationFrame is the time. eg drawTop(time)


          See comments for more info.



          ctx.canvas.width = 100;  // << set the canvas size via canvas attributes not style
          ctx.canvas.height = 100;

          var thenTop = performance.now(); // to get time.

          requestAnimationFrame(drawTop); // <<< start the render loop with request, dont call direct

          function drawTop(nowTop) { // << time passed to render call
          requestAnimationFrame(drawTop); // <<< put call here not in loop
          ctxTop.clearRect(0, 0, ctxTop.canvas.width, ctxTop.canvas.height); // <<< clear the canvas
          var i = 32;
          while (i < topLineEnd) {
          // requestAnimationFrame(drawTop); // <<<<<<< Not here
          i++;

          elapsedTop = nowTop - thenTop;
          if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
          thenTop = nowTop - (elapsedTop % fpsIntervalTop);
          ctxTop.lineWidth = 20;
          ctxTop.strokeStyle = "black";
          ctxTop.beginPath(); // <<<<<< To start a new path
          ctxTop.moveTo(topLineStart, 80);
          ctxTop.lineTo(topLineIncrement, 80);
          ctxTop.stroke();
          topLineStart += 1;
          } else {
          return;
          }
          }
          }





          share|improve this answer
























          • Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

            – LarryYoung
            Nov 23 '18 at 22:35













          • good explanations and visuals. Easy to understand. Thanks again

            – LarryYoung
            Nov 23 '18 at 23:17
















          1














          There are many problems with your code.



          You need to read up on using the canvas. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D will help.




          1. Only call requestAnimationFrame once per frame. Putting it inside a while loop will just start 1000's of frames fighting for display refresh.

          2. Start paths with ctx.beginPath();

          3. Clear the canvas using ctx.clearRect(0,0,width,height)

          4. Set canvas size via attributes not via style properties.

          5. The first argument to the function called by requestAnimationFrame is the time. eg drawTop(time)


          See comments for more info.



          ctx.canvas.width = 100;  // << set the canvas size via canvas attributes not style
          ctx.canvas.height = 100;

          var thenTop = performance.now(); // to get time.

          requestAnimationFrame(drawTop); // <<< start the render loop with request, dont call direct

          function drawTop(nowTop) { // << time passed to render call
          requestAnimationFrame(drawTop); // <<< put call here not in loop
          ctxTop.clearRect(0, 0, ctxTop.canvas.width, ctxTop.canvas.height); // <<< clear the canvas
          var i = 32;
          while (i < topLineEnd) {
          // requestAnimationFrame(drawTop); // <<<<<<< Not here
          i++;

          elapsedTop = nowTop - thenTop;
          if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
          thenTop = nowTop - (elapsedTop % fpsIntervalTop);
          ctxTop.lineWidth = 20;
          ctxTop.strokeStyle = "black";
          ctxTop.beginPath(); // <<<<<< To start a new path
          ctxTop.moveTo(topLineStart, 80);
          ctxTop.lineTo(topLineIncrement, 80);
          ctxTop.stroke();
          topLineStart += 1;
          } else {
          return;
          }
          }
          }





          share|improve this answer
























          • Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

            – LarryYoung
            Nov 23 '18 at 22:35













          • good explanations and visuals. Easy to understand. Thanks again

            – LarryYoung
            Nov 23 '18 at 23:17














          1












          1








          1







          There are many problems with your code.



          You need to read up on using the canvas. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D will help.




          1. Only call requestAnimationFrame once per frame. Putting it inside a while loop will just start 1000's of frames fighting for display refresh.

          2. Start paths with ctx.beginPath();

          3. Clear the canvas using ctx.clearRect(0,0,width,height)

          4. Set canvas size via attributes not via style properties.

          5. The first argument to the function called by requestAnimationFrame is the time. eg drawTop(time)


          See comments for more info.



          ctx.canvas.width = 100;  // << set the canvas size via canvas attributes not style
          ctx.canvas.height = 100;

          var thenTop = performance.now(); // to get time.

          requestAnimationFrame(drawTop); // <<< start the render loop with request, dont call direct

          function drawTop(nowTop) { // << time passed to render call
          requestAnimationFrame(drawTop); // <<< put call here not in loop
          ctxTop.clearRect(0, 0, ctxTop.canvas.width, ctxTop.canvas.height); // <<< clear the canvas
          var i = 32;
          while (i < topLineEnd) {
          // requestAnimationFrame(drawTop); // <<<<<<< Not here
          i++;

          elapsedTop = nowTop - thenTop;
          if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
          thenTop = nowTop - (elapsedTop % fpsIntervalTop);
          ctxTop.lineWidth = 20;
          ctxTop.strokeStyle = "black";
          ctxTop.beginPath(); // <<<<<< To start a new path
          ctxTop.moveTo(topLineStart, 80);
          ctxTop.lineTo(topLineIncrement, 80);
          ctxTop.stroke();
          topLineStart += 1;
          } else {
          return;
          }
          }
          }





          share|improve this answer













          There are many problems with your code.



          You need to read up on using the canvas. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D will help.




          1. Only call requestAnimationFrame once per frame. Putting it inside a while loop will just start 1000's of frames fighting for display refresh.

          2. Start paths with ctx.beginPath();

          3. Clear the canvas using ctx.clearRect(0,0,width,height)

          4. Set canvas size via attributes not via style properties.

          5. The first argument to the function called by requestAnimationFrame is the time. eg drawTop(time)


          See comments for more info.



          ctx.canvas.width = 100;  // << set the canvas size via canvas attributes not style
          ctx.canvas.height = 100;

          var thenTop = performance.now(); // to get time.

          requestAnimationFrame(drawTop); // <<< start the render loop with request, dont call direct

          function drawTop(nowTop) { // << time passed to render call
          requestAnimationFrame(drawTop); // <<< put call here not in loop
          ctxTop.clearRect(0, 0, ctxTop.canvas.width, ctxTop.canvas.height); // <<< clear the canvas
          var i = 32;
          while (i < topLineEnd) {
          // requestAnimationFrame(drawTop); // <<<<<<< Not here
          i++;

          elapsedTop = nowTop - thenTop;
          if (elapsedTop > fpsIntervalTop && i < topLineEnd) {
          thenTop = nowTop - (elapsedTop % fpsIntervalTop);
          ctxTop.lineWidth = 20;
          ctxTop.strokeStyle = "black";
          ctxTop.beginPath(); // <<<<<< To start a new path
          ctxTop.moveTo(topLineStart, 80);
          ctxTop.lineTo(topLineIncrement, 80);
          ctxTop.stroke();
          topLineStart += 1;
          } else {
          return;
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 19:34









          Blindman67Blindman67

          27.3k52764




          27.3k52764













          • Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

            – LarryYoung
            Nov 23 '18 at 22:35













          • good explanations and visuals. Easy to understand. Thanks again

            – LarryYoung
            Nov 23 '18 at 23:17



















          • Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

            – LarryYoung
            Nov 23 '18 at 22:35













          • good explanations and visuals. Easy to understand. Thanks again

            – LarryYoung
            Nov 23 '18 at 23:17

















          Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

          – LarryYoung
          Nov 23 '18 at 22:35







          Thanks I'll go back and do some more studying on the mdn and maybe post an update. I really appreciate you taking the time to do the rewrite. it will help alot!

          – LarryYoung
          Nov 23 '18 at 22:35















          good explanations and visuals. Easy to understand. Thanks again

          – LarryYoung
          Nov 23 '18 at 23:17





          good explanations and visuals. Easy to understand. Thanks again

          – LarryYoung
          Nov 23 '18 at 23:17




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451736%2fdrawing-lines-using-requestanimationframe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy