Home > Resources > Making Animation with Scilab > 3. Animation of a moving point
Making Animation with Scilab

## 3. Animation of a moving point

Code 1 below is an example Scilab script that produces an animation of a point moving along x-axis.

```// animation_point.sce

clear; xdel(winsid());

// Create motion data -- (1)
t = 0:0.001:1;    // Time data
x = sin(2*%pi*t); // Position data

// Draw initial figure -- (2)
h_fig = figure;
h_fig.background = 8;
h_point = plot(x(1), 0, 'Marker', 'o', 'MarkerSize', 20,..
'MarkerEdgeColor', 'blue', 'MarkerFaceColor', 'blue');
h_axes = gca();
h_axes.data_bounds = [-1.5, -1.5; 1.5, 1.5];

// Animation Loop -- (3)
for i = 1:length(x)
drawlater();
h_point.data = [x(i), 0];
drawnow();
end
```
Code 1: Example Scilab script that produces an animation of a moving point

First at (1), the motion data of the point are created. Next at (2), the initial point is drawn by the `plot` command and the handle of the Polyline object is saved in the variable `h_point`. Finally in the loop at (3), the value of `data` property of the Polyline object is replaced and the figure is updated with `drawnow()` command resulting in animation. This is the basic code to make an animation in Scilab.

The animation produced by this script is shown in Movie 1. Movie 1: Animation of a moving point

In order to control the speed of the animation, `realtime()` can be used. Code 2 below creates an animation of a moving point as in Code 1, but controls the speed of the animation by calling `realtime()` in the loop.

```// animation_point_with_realtime.sce

clear; xdel(winsid());

// Create motion data -- (1)
t = 0:0.01:1;     // Time data
x = sin(2*%pi*t); // Position data

// Draw initial figure -- (2)
h_fig = figure;
h_fig.background = 8;
h_point = plot(x(1), 0, 'Marker', 'o', 'MarkerSize', 20,..
'MarkerEdgeColor', 'blue', 'MarkerFaceColor', 'blue');
h_axes = gca();
h_axes.data_bounds = [-1.5, -1.5; 1.5, 1.5];

// Animation Loop -- (3)
realtimeinit(0.1); // set time unit duration, in seconds
realtime(0);       // set current time to 0
for i = 1:length(x)
drawlater();
h_point.data = [x(i), 0];
realtime(i); // wait until i time units (0.1*i seconds)
drawnow();
end
```
Code 2: Example Scilab script that produces an animation of a moving point - the speed of the animation is control by `realtime()`