Animation in JavaScript

Author: Mansoor Ahmed
Introduction

There are three methods to create an animation.

  1. By using the properly named CSS animations.
  2. By using CSS transitions.
  3. By writing code in JavaScript.

In this article, we will take a look at how to do some basic animations in JavaScript.

Description
  • Animation is pretty simple and easy.
  • Draw a varying scene many times per second.
  • We essential data about their location, size, shape, and so on when drawing ships and asteroids.
  • This data would be used to translate and rotate the context as a result all looks in the right place.
  • We update the data every frame and redraw the scene over and over as the data changes to animate a scene.
Importance of Animate Using JavaScript
  • Our browser does a great deal of the actual animating for us when we are animating something in CSS using transitions or animations.
  • All we actually do is just define the starting state and the ending state for both transitions and animations.
  • We define a few intermediate states as well if our animation has keyframes.
  • We have random property values defined at certain points in the animation’s life.
  • It is the exclamation of the values between these points that are very vital to make the animation work.
  • It is precisely this outburst that our browser does for us.
  • We need to be pretty precise about our keyframes the many points in time we want our properties to have a certain value.
  • This accuracy makes animating a lot of realistic scenarios very difficult.
  • Using JavaScript, the door is wide open for us to create any type of animation we want without upsetting about technical feasibility as we have better control over all features of how our animation will behave.
  • For instance, creating rather like the falling snow effect without using JavaScript will be very problematic.
Handling an animation
  • Figures are drawn to the canvas by using the canvas methods directly and by calling custom functions.
  • We only realize these results look as if on the canvas when the script finishes performing in normal circumstances.
  • For example, it isn’t likely to do an animation from inside a for a loop.
  • It means we require a method to execute our drawing functions in excess of a period of time. There are two ways to control an animation like this.
Scheduled updates

For calling a specific function over a period of time, we use following functions;

  • setInterval(),
  • setTimeout(), and
  • requestAnimationFrame()

setInterval ()

Starts frequently performing the function stated by function every delay milliseconds.

setTimeout()

Performs the function stated by function in delay milliseconds.

requestAnimationFrame (callback)

A Moving Circle
  • Let’s begin with a very simple scene as a single moving circle.
  • The circle has an x- and y-coordinates position on the canvas that we will move.
  • Create a new folder in the normal way.
  • Copy over the drawing.js library and stylesheet.
  • Make a new file exercise6.html with the below code.
Animation Animation var context = document.getElementById("asteroids"). getContext("2d"); context.strokeStyle = "white"; context.lineWidth = 1.5; var x = 0, y = context.canvas.height / 2; function frame() { context.clearRect(0, 0, context.canvas.width, context. canvas.height); draw(context); update(); } function update() { x += 1; } function draw(ctx) { draw_grid(ctx); ctx.beginPath(); ctx.arc(x, y, 40, 0, 2 * Math.PI); ctx.fill(); ctx.stroke(); } setInterval(frame, 1000.0/60.0); // 60 fps
  • The code is generally acquainted, however, there are a few new things to notice.
  • Main, we’re keeping the x- and y-coordinates as global variables.
  • We’ve similarly updated our code into a sequence of functions as a frame, update, and draw.
  • The frame function prepares three separate things.
  • It frees the canvas using the context.clearRect method.
  • Formerly it calls the draw function that draws a circle at positions x, y.
  • It calls the update function that moves the x-coordinate one pixel to the right.
  • The last new thing is the call to setInterval that schedules the frame function to be called 60 times per second.
  • Each time the frame function is called as a result.
  • It clears the canvas, draws a grid, draws a circle at the current position, and moves the position to the right.
  • The call to setInterval passes in the function to be called frame
  • The time interval in milliseconds between calls is 1000.0 / 60.0.
  • Therefore, the frame function is called every sixtieth of a second.
  • The circle moves to the right at 60 pixels per second.
  • It’s not extended visible, then the value of x continues to increment once the circle moves beyond the end of the canvas.
For more details visit: https://www.technologiesinindustry4.com/2021/10/animation-in-javascript.html