Simple Animation with View/Draw
Author: Carl Read
Animating the various shapes you can add to a REBOL face with View's Draw dialect is quite simple using a feel function and a few variables. (Feel is used to check for window events.)
As an example, we'll start with a layout that displays three circles in a horizontal row on a black box...
To make the animation of these circles possible, we'll first add two variables, POS and SIZE. These we'll use in each circle's arguments to allow us to later change their position and radius. Our script now looks like this...
If you run that it'll still look like the first script and won't be animated. However, with POS and SIZE now part of the circles' arguments, when the face is updated using SHOW any changes to POS or SIZE will be reflected in the circles' positions or sizes.
As mentioned above, we'll use a feel function to animate these circles. Two events will be responded to - a timed event to smoothly alter the size and vertical position of the circles and the press of a cursor key to allow the user to change the direction the circles are moving.
So, here's our final, animated script...
You'll notice two extra variables have been added - INC and DIR. These are either positive or negative and control whether the size of the circles are increasing or decreasing and their direction of movement.
Then, (apart from a bit of self-explanatory text), the FEEL block has been added to the face's definition. First, the RATE 30 ensures the engage function is called 30 times a second (if your computer is up to it.) That's the TIME event the SWITCH function responds to, it first changing the size and position of the circles and then changing INC and DIR if the circles have reached the limits set for them. The changes to the circles are then SHOWn.
SWITCH also checks for the cursor keys being pressed, setting the circles' directions if up or down are pressed. No SHOW is really needed here, given one is hit 30 times a second by the TIME event.
And that's it. Of course, this method of changing Draw values on the fly can be used on colors, points, transparences and so on.
For more about the Draw dialect: http://www.rebol.com/docs/draw.html And for more about Feel: http://www.rebol.com/how-to/feel.html