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

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.

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.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 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.

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.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

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.

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.

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.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

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.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 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.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 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.