Animation and Interaction#

Modern computer graphics is not limited to displaying static images, shapes, and objects. Graphics can also move, respond to user input, and change their appearance based on different events.

Two important concepts that make computer graphics more dynamic are animation and interaction.

Animation

Animation is the process of creating the illusion of movement by displaying a sequence of images or changing the properties of graphical objects over time.

In computer graphics, animation is commonly achieved by continuously updating the position, size, rotation, color, or other properties of an object.

Example

  • A ball bouncing up and down.

  • A character walking across the screen.

  • A car moving along a road.

  • A logo rotating continuously.

  • A game character jumping when a button is pressed.

Interaction

Interaction refers to the ability of a computer graphics application to respond to actions performed by the user.

Unlike static graphics, interactive graphics allow users to control or influence what happens on the screen.

Examples

  • Clicking a button to change an object’s color.

  • Moving the mouse to control an object.

  • Pressing keyboard keys to move a character.

  • Dragging an object from one location to another.

  • Clicking a shape to display information.

Difference between Animation and Interaction
Animation

Creates movement over time.

Can operate automatically.

Uses time-based updates.

Example

A rotating circle.

Interaction

Responds to user actions.

Usually requries user input.

Uses events and input handling.

Example

Clicking a circle changes its color.

Types of Animation#

There are several types of animation used in computer graphics and multimedia applications.

2D Animation

2D animation involves creating movement using objects that exist in a two-dimensional environment.

Objects are represented using:

  • X-coordinate – horizontal position

  • Y-coordinate – vertical position

Examples

  • Cartoon animations.

  • Animated icons.

  • Moving objects in 2D games.

  • Animated advertisements.

3D Animation

3D animation involves creating movement using objects with three dimensions:

  • X-axis – horiznotal position

  • Y-axis – vertical position

  • Z-axis – depth

Unlike 2D animation, 3D animation provides a sense of depth and volume.

Examples

  • Animated movies

  • 3D video games

  • Architectural walkthroughs

  • Product demonstrations

Frame-by-Frame Animation

Frame-by-frame animation creates movement by displaying a sequence of individually created images. Each image represents a slightly different position or appearance of an object. Virtual reality environments.

Keyframe Animation

Keyframe animation defines important positions or states of an object at specific points in time.

The computer generates the intermediate frames between the keyframes. THis process is called tweening or in-betweening.

Examples

  • At 0 seconds: A ball is on the left.

  • At 2 seconds: A ball is at the center.

  • At 4 seconds: A ball is on the right.

Commonly Used In

  • Adobe Animate

  • Blender

  • Adobe After Effects

  • CSS animations

Motion Graphics

Motion graphics involve animating text, shapes, icons, and graphical elements to communicate information or create visual effects.

Example

  • Animated presentation titles

  • Logo animations

  • Animated infographics

  • Video introductions

  • Promotional advertisements

Principles of Animation#

The principles of animation help develoeprs create movements that appear natural, realistic, and visually appealing.

Squash and Stretch

Squash and stretch give objects a sense of flexibility, weight, and movement.

Example

When a ball hits the ground, it becomes slightly flattened. When it moves upward, it stretches.

Anticipation

Anticipation prepares the viewer for an upcoming action.

Example

Before a character jumps, it bends it knees. This movement signals that a jump is about to happen.

Timing

Timing refers to the amount of time required to perform an action. It determines how fast or slow an object moves.

Examples

  • A fast-moving ball may appear lightweight.

  • A slowly moving heavy object may appear to have more weight.

Slow In and Slow Out

Objects do not always move at a constant speed. Slow in means an object gradually accelerates from a stationary position. Slow out means an object gradually decelerates before stopping. This creates more natural movement.

Follow Through and Overlapping Action

Follow through refers to the continuation of movement after the main action has stopped.

Example

When a character stops running, its hair or clothing may continue moving briefly.

Overlapping action occurs when different parts of an object move at different rates.

Arc

Most natural movmeents follow curved paths instead of perfectly straight lines.

Example

When a person throws a ball, the ball generally follows a curved trajectory.

Exaggeration

Exaggeration makes actions more notiveable to improve visual communication.

Example

A cartoon character may open its eyes extremely wide when surprised.

Animation Techniques in JavaScript#

JavaScript allows developers to create animations directly in a web browser.

One of the most common technologies used for 2D animation is the HTML Canvas API.

HTML Canvas

HTML Canvas is an HTML element that provides a drwaing area where developers can create graphics using JavaScript.

It can be used to draw lines, rectangles, circles, ellipses, images, text, and animated objects.

Example

<canvas id="myCanvas" width="600" height="400"></canvas>

JavaScript can access the canvas using:

const canvas = document.querySelector("#myCanvas");
const ctx = canvas.getContext("2d");
The requestAnimationFrame() Method

The requestAnimationFrame() method tells the browser to execute a function before the next screen repaint. It is commonly used to create smooth animations.

Example

function animate() {
  // Animation code goes here
  requestAnimationFrame(animate);
}

Explanation:

  1. The animate() function contains the animation instructions.

  2. requestAnimationFrame(animate) schedules another execution of the function.

  3. The function repeatedly executes, producing continuous animation.

Clearing the Canvas

Before drawing the next animation frame, developers usually clear the previous frame.

ctx.clearRect(0, 0, canvas.width, canvas.height);

This removes previously drawn content from the canvas. Without clearing the canvas, moving objects may leave visible trails.

Updating Object Position

To animate an object, its coordinates must change over time.

Example

let x = 50;

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  ctx.beginPath();
  ctx.arc(x, 100, 25, 0, Math.PI * 2);
  ctx.fillStyle = "blue";
  ctx.fill();

  x += 2;

  requestAnimationFrame(animate);
}

animate();

User Interaction in JavaScript#

JavaScript allows graphics applications to respond to user actions through events. An event is an action or occurence that the browser can detect.

Mouse Events

Mouse events allow users to interact with graphical objects using a mouse.

Event

Description

click

Occurs when an element is clicked.

dblclick

Occurs when an element is double-clicked.

mousemove

Occurs when the mouse moves.

mousedown

Occurs when a mouse button is pressed.

mouseup

Occurs when a mouse button is released.

mouseenter

Occurs when the mouse enters an element.

mouseleave

Occurs when the mouse leaves an element.

Keyboard Events

Keyboard events allow users to control objects using keyboard keys.

Event

Description

keydown

Occurs when a key is pressed.

keyup

Occurs when a key is released.

Drag and Drop Interaction

Drag and drop allows users to move graphical objects from one location to another.

Examples

  • Moving shapes around a canvas.

  • Rearranging objects in a drawing application.

  • Moving game objects.

  • Organizing items in a visual interface.

Interactive Buttons

Buttons allow users to perform specific ations.

Example

<button onclick="changeColor();">Change Color</button>

Combining Animation and Interaction#

One of the most important applications of animation and interaction is the development of interactive graphics and games.

Consider a simple game where a player controls a ball.

The application should have the following features:

  • A ball is displayed on the canvas.

  • The ball continuously moves horizontally.

  • The user can control the ball using arrow keys.

  • The ball changes color when clicked.

  • The ball bounces when it reaches the canvas boundaries.

This example combines:

  • Animation through continuous movement.

  • Keyboard interaction through arrow keys.

  • Mouse interaction through clicking.

  • Boundary detection through conditional statements.

  • Grpahics rendering through the Canvas API.