Text Animation Program Using C Programming

🎯 Introduction

Text animation is a captivating technique used to add visual appeal to various applications and projects. It involves displaying text in a dynamic and moving manner, making it more engaging for the viewer. In this blog post, we will explore how to create a text animation program using the C programming language. The program will allow users to input a text, initial coordinates, and target coordinates to create an animation effect by smoothly moving the text from the initial position to the target position on the screen.

🎯 Concept

The core concept behind this text animation program is to use the Graphics Library in C to display and manipulate graphical elements on the screen. The graphics.h library provides functions to handle graphics-related tasks like drawing shapes, displaying text, and controlling graphical output. The animation effect is achieved by incrementally moving the text from the initial coordinates to the target coordinates using interpolation.

🎯 Text Animation Example Program

#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>


#define round(val) (int)(val+0.5)


void main() {

   int gd = DETECT, gm, sx, sy, tx, ty;


   char text[50];


   void move(int, int, int, int, char[]);


   printf("Enter the text:");

   scanf("%s", text);

   printf("Enter the initial points:");

   scanf("%d%d", &sx, &sy);

   printf("Enter the TARGET points:");

   scanf("%d%d", &tx, &ty);


   initgraph(&gd, &gm, "");

   outtextxy(sx, sy, text);


   move(sx, sy, tx, ty, text);

   getch();

   closegraph();


}


void move(int sx, int sy, int tx, int ty, char text[50]) {


   int dx = tx - sx, dy = ty - sy, steps, k;

   float xin, yin, x = sx, y = sy;


   getch();


   if (abs(dx) > abs(dy))

      steps = abs(dy);

   else

      steps = abs(dy);


   xin = dx / (float) steps;

   yin = dy / (float) steps;


   for (k = 0; k < steps; k++) {

      cleardevice();

      x += xin;

      y += yin;

      setcolor(15);

      outtextxy(round(x), round(y), text);

      delay(50);

   }

}

🎯 Detailed Explanation

Let's dive into the details of the program and its components:

Header Files:

The necessary header files for the program are stdio.h, math.h, conio.h, and graphics.h. The stdio.h provides input/output functions, math.h provides mathematical functions, conio.h provides console input/output functions, and graphics.h provides graphics-related functions.

Main Function:

The main() function initializes the graphics mode, takes input for the text to be animated, initial coordinates (sx and sy), and target coordinates (tx and ty). It then calls the initgraph() function to initialize the graphics system and displays the input text at the initial coordinates using outtextxy() function. Afterward, it calls the move() function to create the animation effect and moves the text smoothly to the target coordinates.

Move Function:

The move() function calculates the distance (dx and dy) between the initial and target coordinates. It determines the number of steps required for the animation based on the greater of the absolute values of dx and dy. The xin and yin variables are calculated to determine the amount of incremental movement required for each step in the x and y directions, respectively.

Animation Loop:

The animation is achieved by running a loop that updates the position of the text by incrementing x and y with xin and yin, respectively, for each step. The cleardevice() function clears the screen, and the outtextxy() function is used to display the text at the updated coordinates during each iteration. The setcolor() function sets the color of the text to white (color code 15), and the delay() function introduces a small delay to control the animation speed.

🎯 Summary

In this blog post, we have explored the concept of text animation using the C programming language and the Graphics Library. The program allows users to input a text, initial coordinates, and target coordinates and then creates an animation effect by smoothly moving the text from its initial position to the target position on the screen. The animation is achieved by calculating incremental steps and updating the position of the text in each iteration, creating a visually appealing and dynamic display.

🎯 Key Points