By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > How to Make Animation using javascript
JavaScript

How to Make Animation using javascript

Admin
Last updated: 2022/11/23 at 5:40 AM
Admin
Share
2 Min Read
animation using javascript

In this article, we will make and learn to make animation using Javascript. JavaScript does not have a function such as Java‟s sleep method that pauses a task for a specified period of time. However, JavaScript has two functions that can be used to delay the execution of a function.

  • setTimeout – Will execute a function a specific number of milliseconds in the future
  • setInterval – Will execute a function every millisecond 

Both functions take on two arguments:

  • Function – The first argument identifies the function to execute 
  • Time – The number of milliseconds 

setTimeout(someFunction,500); // The function will be executed 500 milliseconds

 setInterval(someFunction,500); // The function will be executed 500 every milliseconds 

The use of the setTimeout is illustrated here by moving a tag across the screen. The int function setups the animation by retrieving a reference to the tag and calling the move function. The function move modifies the position of the tag and recursively schedules itself for future invocation.

function move() {
square.style.left = parseInt(square.style.left)+1+'px';
setTimeout(move,20);
}
function init() {
square = document.getElementById('Square');
square.style.left = '0px';
move();
}
Animation using javascript

The complete page follows:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Javascript Animation</title>

    <style>
        body{
            background-color: black;
        }
        #Square {
            position: absolute;
            left: 0px;
            top: 8em;
            width: 5rem;
            height: 5rem;
            line-height: 3em;
            background: #99ccff;
            border: 1px solid #003366;
            white-space: nowrap;
            padding: 0.5em;
        }
    </style>
</head>

<body>

    <div id="Square">
    </div>



    <script>
        var square = null;

        function move() {
            square.style.left = parseInt(square.style.left) + 1 + 'px';
            setTimeout(move, 20);
        }

        function init() {
            square = document.getElementById('Square');
            square.style.left = '0px';
            move();
        }

        window.onload = init;
    </script>
</body>

</html>

The same effect can be created using the setInterval function.

function move() {
square.style.left = parseInt(square.style.left)+1+'px';
}
function init() {
square = document.getElementById('Square');
square.style.left = '0px';
setInterval(move,20);
}

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: animation using javascript
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article speed typing game using javascript How to Make Speed Typing Game using JavaScript
Next Article Positioning in css What is Positioning in CSS?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?