Animation in ES6

Animation in ES6

In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct complicated animation such as Fireworks, Roll-in and Roll-out, Fade effects. We can easily shift the DOM (Document Object Model) elements like <div>, <img/> and many others.

We can create the animation with the help of the following two methods in ES6.

  • With the help of requestAnimationFrame() method: This method executes the function if the screen is ready for the next iteration. It accepts the single line function. We can create animated effects when we invoke this function in a repeated way. The process of creating animation is done frame by frame. The browser invokes the frames.
  • With the help of setInterval() method: This method is used to accept two arguments in the form of an integer and a function. We can also terminate this method with the help of clearInterval() method.

Now, we will discuss each method in detail.

requestAnimationFrame() method

The requestAnimationFrame() method is easy to establish but complicated to terminate. It is used to create gentle animation. We can create the loops and timings required to set up manually. This method cannot be used with some specific intervals. This method contains the loop at 60fps (frames per second) to create a gentle and smooth animation. We cannot execute it in the background.

Here, we have an illustration of the same.

Example1

<html>  
   <head>  
      <script type = "text/javascript">  
            var image = null;  
            function init(){   
               image = document.getElementById('myimage'); 
               image.style.position = 'relative';      
               image.style.left = '50px';    
            }      
            function moveRight(){   
               img.style.left = parseInt( 
               img.style.left) + 100 + 'px';   
            }   
            window.onload = init;   
      </script>  
   </head>  
   <body>  
      <form>  
         <image id = "myimage" src = "train1.png" />  
         <center> 
            <p>Click on the below button to move the image left</p>  
         <input type = "button" value = "Click Me" onclick = "moveLeft();" /> 
      </center>  
      </form> 
   </body> 
</html> 

Output

After the execution of the above code, we got the following output:

Animation in ES6

In the above example, we tried to create a simple animation with the help of DOM object properties and JavaScript functions. First, we tried to get the DOM object with the help of getElementById(), then stored the object into a global variable.

Here, we also have another example of creating the animation with the help of setTimeout(), and clearTimeout() functions.

Example2

<html>  
   <head>  
      <title>JavaScript Animation</title>  
      <script type = "text/javascript">  
            var animation ;  
            function init(){   
               img = document.getElementById('myimg');      
               img.style.position = 'relative';     
               img.style.left = '0px';  
            }   
            function towardRight(){   
               img.style.left = parseInt(img.style.left) + 10 + 'px';     
               animation = setTimeout(towardRight,50);  
            }   
            function stop() {      
               clearTimeout(animation);     
              img.style.left = '0px';    
            }   
            window.onload = init;   
      </script>  
   </head>  
   <body>  
      <form>  
         <img id = "myimg" src = "train1.png" />  
         <center> 
         <h2 style="color:darkred;">Click on the following buttons to manage animation</h2>  
         <input type="button" value="Start" onclick = "towardRight();" />  
         <input type = "button" value="Stop" onclick = "stop();" />  
         <center> 
      </form>     
   </body>  
</html> 

Output

After the successful execution of the above example, we got the following output:

Animation in ES6

setInterval() method

In ES6, this method is used as a classical method, which helps to create animation effects. The setInterval() method depends upon the recursive execution of the source code. It helps us to modify the frame of the element by frame.

Syntax

animation = setInterval(display, t);   // To invoked the function after t milliseconds
clearInterval(animation);                  // For terminate the process

In the above syntax, when the display function modifies the element style, the setInterval(display, t) method can be used for step-by-step modifications in element style after a time interval. If we have a small-time interval, then the animation looks regular.

Rollover with a Mouse Event

In the rollover image, the event refers to an image on which when we shift the mouse pointer, then the HTTP (Hypertext transfer protocol) modifies the image with another image. But, when we move the mouse pointer from the image, then it shows the previous image.

Here, we have an example to understand the above concept.

Example

<html> 
   <head> 
      <script type = "text/javascript">
            if(document.images) { 
               var img1 = new Image();      
               img1.src = "cat.jpg"; 
               var img2 = new Image();      
               img2.src = "tiger.jpg"; 
            } 
      </script> 
   </head> 
   <body> 
   <center>  
      <a href = "#" onMouseOver = "document.myImg.src = img2.src;" 
         onMouseOut = "document.myImg.src = img1.src;"> 
         <img name = "myImg" src = "train.jpg" /> 
      </a><br><br> 
      <h2>Shift your mouse over the image to see the modification</h2> 
   </center> 
   </body> 
</html>

Output

After the execution of the code, we got the following output:

Animation in ES6

Page Printing in ES6

The user often requires a print button on the web page which is used to print the data on the web page with the help of a printer. JavaScript facilitates us to perform printing a web page. In JavaScript, we have window.print() function, which is used to print the current web page. Window.print() function can be used with the onclick event.

Syntax

window.print();

Example

We have an example to elaborate the above function.

<html>  
<head>   
</head>  
<body>  
    <center>  
        <h2>Hello World </h2>  
        <h3>Welcome to ES6</h3>  
        <h3>Click on the print button to see the modifications</h3>  
        <input type = "button" value = "Print" onclick = "window.print()"/> 
    </center>  
</body>  
</html>

Output

After the execution of the code, we got the following output:

Animation in ES6

When we click on the print button, then we got the following output:

Animation in ES6

Related Topics

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.