Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update










0















Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').



This is what I have so far but I can't get the ball to bounce to the left.



var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");

setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;

function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";

if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;

requestAnimationFrame(update);










share|improve this question
























  • Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)

    – Paul-Jan
    Nov 14 '18 at 16:23











  • you probably don't want to be calling the update function with both setInterval and requestAnimationFrame...

    – Punit
    Nov 14 '18 at 17:40












  • also, the ball's x coordinate should correspond with its left property, and the y coordinate should correspond with its top property

    – Punit
    Nov 14 '18 at 17:42
















0















Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').



This is what I have so far but I can't get the ball to bounce to the left.



var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");

setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;

function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";

if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;

requestAnimationFrame(update);










share|improve this question
























  • Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)

    – Paul-Jan
    Nov 14 '18 at 16:23











  • you probably don't want to be calling the update function with both setInterval and requestAnimationFrame...

    – Punit
    Nov 14 '18 at 17:40












  • also, the ball's x coordinate should correspond with its left property, and the y coordinate should correspond with its top property

    – Punit
    Nov 14 '18 at 17:42














0












0








0








Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').



This is what I have so far but I can't get the ball to bounce to the left.



var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");

setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;

function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";

if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;

requestAnimationFrame(update);










share|improve this question
















Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').



This is what I have so far but I can't get the ball to bounce to the left.



var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");

setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;

function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";

if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;

requestAnimationFrame(update);







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:21









Alfredo A.

98011633




98011633










asked Nov 14 '18 at 16:20









seanstacksseanstacks

61




61












  • Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)

    – Paul-Jan
    Nov 14 '18 at 16:23











  • you probably don't want to be calling the update function with both setInterval and requestAnimationFrame...

    – Punit
    Nov 14 '18 at 17:40












  • also, the ball's x coordinate should correspond with its left property, and the y coordinate should correspond with its top property

    – Punit
    Nov 14 '18 at 17:42


















  • Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)

    – Paul-Jan
    Nov 14 '18 at 16:23











  • you probably don't want to be calling the update function with both setInterval and requestAnimationFrame...

    – Punit
    Nov 14 '18 at 17:40












  • also, the ball's x coordinate should correspond with its left property, and the y coordinate should correspond with its top property

    – Punit
    Nov 14 '18 at 17:42

















Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)

– Paul-Jan
Nov 14 '18 at 16:23





Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)

– Paul-Jan
Nov 14 '18 at 16:23













you probably don't want to be calling the update function with both setInterval and requestAnimationFrame...

– Punit
Nov 14 '18 at 17:40






you probably don't want to be calling the update function with both setInterval and requestAnimationFrame...

– Punit
Nov 14 '18 at 17:40














also, the ball's x coordinate should correspond with its left property, and the y coordinate should correspond with its top property

– Punit
Nov 14 '18 at 17:42






also, the ball's x coordinate should correspond with its left property, and the y coordinate should correspond with its top property

– Punit
Nov 14 '18 at 17:42













1 Answer
1






active

oldest

votes


















1














The solution is in the question itself:




When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').




if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
x = 0; // return it to the far left of the screen (a 'left' property of '0px')






share|improve this answer






















    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%2f53304604%2fcreate-an-application-with-one-dom-element-that-is-always-moving-right-at-a-spe%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














    The solution is in the question itself:




    When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').




    if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
    x = 0; // return it to the far left of the screen (a 'left' property of '0px')






    share|improve this answer



























      1














      The solution is in the question itself:




      When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').




      if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
      x = 0; // return it to the far left of the screen (a 'left' property of '0px')






      share|improve this answer

























        1












        1








        1







        The solution is in the question itself:




        When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').




        if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
        x = 0; // return it to the far left of the screen (a 'left' property of '0px')






        share|improve this answer













        The solution is in the question itself:




        When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').




        if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
        x = 0; // return it to the far left of the screen (a 'left' property of '0px')







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 17:49









        PunitPunit

        96749




        96749





























            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%2f53304604%2fcreate-an-application-with-one-dom-element-that-is-always-moving-right-at-a-spe%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3