Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:51:22+00:00 2026-06-18T09:51:22+00:00

I am struggling to create line drawing animation using request animation frame. While I

  • 0

I am struggling to create line drawing animation using request animation frame. While I was using setInterval function everything was working fine, but I read somewhere that request ani frame is much more optimized and that all functions in interval should be written in this manner.

So here is my code:

var topMinX = 3;
var topMaxX = 75;
var topMinY = 2;
var topMaxY = 2;
var min;
var max;

//request animation frame
   (function animate(){
        var t = setTimeout(function(){
            requestAnimFrame(animate);
            var d = render(topMinX,topMaxX,topMinY,topMaxY,true);
        },20);

    })();

     function render(xMin,xMax,yMin,yMax,direction){
        if(direction){
            min = xMin;
            max = xMax
         }else{
            min = yMin;
            max = yMax;
         }      

        if(min<max){
             context.beginPath();
             if(direction){
                context.moveTo(xMin,yMin);
                xMin = xMin+2;
                alert(xMin);
                context.lineTo(xMin,yMax);
             }else{
                context.moveTo(xMin,yMin);
                yMin = yMin+2;
                context.lineTo(xMax,yMin);
             }                   
             context.lineWidth = 4;
             context.stroke();              
        }

     }  

The problem is that xMin value won’t increment. It will always alert 5 and I expected it to increment by 2 in every iteration. Basically I just want to draw a square, so I don’t need any diagonal moves, that’s why I’ve added the direction parameter.

I am new to canvas and request ani frame, so any help would be more then useful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:51:23+00:00Added an answer on June 18, 2026 at 9:51 am

    I don’t know about square drawing, but the problem here seems to be in variable passing to function.

    Those variables you have defined at the beginning does not get changed when passing furher into functions

    You can check how it works here:

    function swap(a, b) {
       var tmp = a;
       a = b;
       b = tmp; //assign tmp to b
    }
    
    var x = 1, y = 2;
    swap(x, y);
    
    alert("x is " + x + " y is " + y); // "x is 1 y is 2"
    

    And your example on FIDDLE

    it does draw a line and topMinX is incremented

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using mysql to create an hotel booking system, but i am struggling a
I am using mvc webapi to create a REST API and struggling to find
I'm struggling to create SQL tables using email addresses as the table name. The
I'm trying to create a league table but I am struggling to align things
I created a multiple-line chart using nvd3 , but was unable to modify it
I am attempting to get migrations working with my project using fluent migrator. But
I'm struggling to create a custom UITableViewCell (in IB) that has a UIImageView on
I'm really struggling to create a valid multidimensional JavaScript array with the following basic
I'm struggling to create a custom build for a Range Slider. My slider works
I'm struggling to create a little Blackjack console game in C++. I've written almost

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.