9. Animated scaling

9.1. Problem

You want to animate scaling of an actor. Example use cases:

  • To animate zooming in/out of a texture in an image viewer application.

  • To add an animated "bounce" effect (quick scale up followed by scale down) to a UI element to indicate it has received focus.

9.2. Solution

Animate the actor's scale-x and scale-y properties to change the scaling on the x and y axes respectively.

For example, to animate an actor to twice its current scale with implicit animations:

gdouble scale_x;
gdouble scale_y;

/* get the actor's current scale */
clutter_actor_get_scale (actor, &scale_x, &scale_y);

/* animate to twice current scale on both axes */
clutter_actor_animate (actor, CLUTTER_LINEAR, 1000,
                       "scale-x", scale_x * 2,
                       "scale-y", scale_y * 2);

Alternatively, ClutterAnimator or ClutterState can be used to animate an actor's scale properties. See this example which uses ClutterState to animate scaling.

9.3. Discussion

Scaling an actor is done through its scale-x and scale-y properties, each of which takes a double value. A value of less than 1.0 for an axis scales an actor down on that axis, reducing its apparent size; values greater than 1.0 scale an actor up, increasing its apparent size.

Why "apparent" size? Because scaling applies a transform to an actor which changes how it appears on the stage, without changing its "real" size. Similarly, scaling an actor may transform its position: it could appear to move to a different position within its container, although it is "really" at its original position. Run the example to see how size and position are transformed by scaling.

It can be useful to know an actor's transformed position and size after scaling: for example, if you were implementing a reflowing layout manager which used scaling as part of its allocation algorithm. Here's an example of how to get these properties for an actor:

gfloat transformed_x;
gfloat transformed_y;

gfloat transformed_width;
gfloat transformed_height;

clutter_actor_get_transformed_position (actor, &transformed_x, &transformed_y);
clutter_actor_get_transformed_size (actor, &transformed_width, &transformed_height);

Note that you can scale an actor on both axes by the same amount (uniform scaling), or by a different amount on each axis (differential scaling).

Use clutter_actor_is_scaled() to determine whether scaling has been applied to an actor: this function returns FALSE if both scale-x and scale-y are 1.0; otherwise, it returns TRUE.

9.3.1. Scaling vs. resizing

Scaling changes the apparent size of an actor, while leaving its real size unchanged. By contrast, resizing changes the real size of the actor, by modifying its width and height properties.

Resizing and scaling produce the same visual effect, as both make an actor appear to be larger or smaller. Therefore, for most purposes, they are interchangeable if you just want to change an actor's apparent size.

So why would you scale an actor rather than resize it?

  • If you've scaled an actor, you can easily reset it to its original size, by setting its scale back to 1.0 on both axes. By contrast, to reset a resized actor to its original size, you would have to track the original size manually: the actor doesn't make its original size accessible.

  • Scaling can easily change the apparent size of multiple actors inside a container. For example, say you wanted to shrink multiple actors inside a container to half their original size. There are two ways you could do this:

    1. The hard way would be to resize each actor individually. You couldn't just resize the container, as resizing a container doesn't resize its children: usually they will be clipped so that they are either partially or wholly hidden.

    2. The easy way would be to set the container's scale to half its initial value: the actors in the container would retain their original sizes, but would appear at half size.

9.3.2. Scaling, layouts and containers

It is possible to scale actors inside containers. For example, if you were using a ClutterBox which has a ClutterBoxLayout layout manager, you could scale the children of that layout.

However, you should remain aware that layout managers don't take account of the scale of their children, only their size. So if you scale up an actor inside a layout manager, it may overlap other actors in the layout: the size allocated by the layout manager doesn't increase as an actor's scale increases.

Similarly, scaling an actor down doesn't reduce the space it will be allocated by a layout.

9.3.3. Setting the scale center

An actor's scale center is the point around which scaling occurs: when you scale the actor, it will "shrink" into (if scale < 1.0) or "expand" out of (if scale > 1.0) its scale center.

You can change an actor's scale center using either gravity (a named position on the actor; for example, the middle of the top edge of the actor is CLUTTER_GRAVITY_NORTH); or x,y coordinates relative to the actor's anchor point (by default, the anchor point for an actor is at 0,0).

Setting scale gravity has the same consequences as setting both the scale-center-x and scale-center-y properties for an actor. For example, CLUTTER_GRAVITY_NORTH_EAST sets the scale center to <width of the actor>, 0, relative to the actor's anchor point (defaults to the top-right corner of the actor). However, the advantage of scale gravities is that they change with the actor: so if the actor is resized, you don't have to manually reset the scale center. This means that CLUTTER_GRAVITY_NORTH_EAST will always represent the top-right corner of the actor, regardless of how it is scaled or resized. The same is true of each of the other scale gravities.

If you're animating an actor's scale but want a different scale center, set it before the animation begins. One way to do this is to leave the actor's scale unchanged, but with a different scale center:

gdouble scale_x;
gdouble scale_y;

/* get the actor's current scale */
clutter_actor_get_scale (actor, &scale_x, &scale_y);

/* set scale center using x,y coordinates, leaving scale unchanged;
 * the actor's size here is assumed to be 200x200
 */
clutter_actor_set_scale_full (actor,
                              scale_x,
                              scale_y,
                              100.0,  /* center x */
                              100.0   /* center y */);

/* set scale center using gravity, leaving scale unchanged */
clutter_actor_set_scale_with_gravity (actor,
                                      scale_x,
                                      scale_y,
                                      CLUTTER_GRAVITY_CENTER);

Another approach is to set scale center properties via GObject, which doesn't require you to figure out the actor's scale first:

/* set scale center using x,y coordinates */
g_object_set (actor,
              "scale-center-x", 100.0, /* center x */
              "scale-center-y", 100.0, /* center y */
              NULL);

/* set scale center using gravity */
g_object_set (actor,
              "scale-gravity", CLUTTER_GRAVITY_CENTER,
              NULL);

Once the scale center is set, you can animate the scaling as per usual.

It is even possible to animate the scale-center-* properties, which can produce interesting, though slightly unpredictable, effects. It's usually better to change the scale center before the animation starts.

The example cycles through the available scale gravities, showing the effect on the animation of each of the scale centers.

The second example shows how to combine scaling in and out on a texture, in response to mouse button presses. In this case, the scale gravity remains at CLUTTER_GRAVITY_NORTH_WEST (i.e. at the anchor point of the actor). However, the anchor point is moved to the coordinates of each double click on button 1 (usually the left mouse button) or button 3 (usually the right mouse button); which in turn automatically moves the scale center before the texture is scaled. As a result, the texture "expands" or "contracts" around the clicked point, while the point remains still.

Warning

One final caveat about scale centers: if an actor is already scaled, the scale center coordinates are relative to the real size of the actor, rather than its transformed size. This can result in a "jumping" effect if you change the scale center on a scaled actor.

For example, you might set the scale gravity of an actor to CLUTTER_GRAVITY_WEST, then scale the actor to 0.5 on both axes. Later, you change the actor's scale gravity to CLUTTER_GRAVITY_EAST. The effect of this is to "jump" the actor to the right, so its right-hand edge is aligned with where it was at scale 1.0.

If this isn't desirable, you can just retain the scale center on a scaled actor, and only change it when the actor is unscaled.

9.4. Full examples

Example 5.14. Animated scaling of an actor using each of the scale gravities. Press any key to start the animation.

#include <stdlib.h>
#include <clutter/clutter.h>

typedef struct
{
  ClutterState *transitions;
  ClutterActor *actor;
  ClutterActor *props_display;
  guint         scale_gravity;
  gboolean      transitions_running;
} State;

static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff };
static const ClutterColor yellow_color = { 0xff, 0xff, 0x00, 0xff };

static void
show_scale_properties_cb (ClutterActor *actor,
                          gpointer      user_data)
{
  State *state = (State *) user_data;

  gfloat transformed_x, transformed_y;
  gfloat transformed_width, transformed_height;
  gfloat scale_center_x, scale_center_y;

  gchar *message;

  clutter_actor_get_transformed_position (state->actor,
                                          &transformed_x,
                                          &transformed_y);

  clutter_actor_get_transformed_size (state->actor,
                                      &transformed_width,
                                      &transformed_height);

  g_object_get (G_OBJECT (actor),
                "scale-center-x", &scale_center_x,
                "scale-center-y", &scale_center_y,
                NULL);

  /* draw cross on the scale center */
  cogl_set_source_color4ub (255, 255, 0, 255);

  cogl_path_move_to (scale_center_x, scale_center_y);
  cogl_path_rel_line_to (10, 10);
  cogl_path_rel_line_to (-20, -20);
  cogl_path_move_to (scale_center_x, scale_center_y);
  cogl_path_rel_line_to (10, -10);
  cogl_path_rel_line_to (-20, 20);

  cogl_path_stroke ();

  /* show actor properties */
  message = g_strdup_printf ("Scale center: %.0f, %.0f\n"
                             "Transformed position: %.2f, %.2f\n"
                             "Transformed size: %.2f, %.2f",
                             scale_center_x, scale_center_y,
                             transformed_x, transformed_y,
                             transformed_width, transformed_height);

  clutter_text_set_text (CLUTTER_TEXT (state->props_display), message);

  g_free (message);
}

static void
next_transition_cb (ClutterState *transitions,
                    gpointer      user_data)
{
  State *state = (State *) user_data;

  if (clutter_actor_is_scaled (state->actor))
    clutter_state_set_state (state->transitions, "not-scaled");
  else if (state->scale_gravity > 9)
    {
      /* gravity is at center, so reset ready for next key press */
      state->scale_gravity = CLUTTER_GRAVITY_NORTH;

      state->transitions_running = FALSE;
    }
  else
    {
      g_object_set (G_OBJECT (state->actor),
                    "scale-gravity", state->scale_gravity,
                    NULL);

      state->scale_gravity++;

      clutter_state_set_state (state->transitions, "scaled-down");
    }
}

static gboolean
key_pressed_cb (ClutterActor *actor,
                ClutterEvent *event,
                gpointer      user_data)
{
  State *state = (State *) user_data;

  if (!state->transitions_running)
    {
      state->transitions_running = TRUE;
      next_transition_cb (NULL, state);
    }

  return TRUE;
}

int
main (int   argc,
      char *argv[])
{
  State *state = g_new0 (State, 1);
  ClutterActor *stage;

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;

  stage = clutter_stage_new ();
  clutter_actor_set_size (stage, 350, 350);
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);

  state->scale_gravity = CLUTTER_GRAVITY_NORTH;
  state->transitions_running = FALSE;

  state->props_display = clutter_text_new ();
  clutter_actor_set_size (state->props_display, 340, 80);
  clutter_actor_set_position (state->props_display, 5, 280);
  clutter_text_set_color (CLUTTER_TEXT (state->props_display), &yellow_color);

  state->actor = clutter_rectangle_new_with_color (&red_color);
  clutter_actor_set_size (state->actor, 200, 200);
  clutter_actor_set_position (state->actor, 75, 50);

  g_object_set (G_OBJECT (state->actor),
                "scale-gravity", state->scale_gravity,
                NULL);

  state->transitions = clutter_state_new ();
  clutter_state_set_duration (state->transitions, NULL, NULL, 400);

  clutter_state_set (state->transitions, NULL, "not-scaled",
                     state->actor, "scale-x", CLUTTER_LINEAR, 1.0,
                     state->actor, "scale-y", CLUTTER_LINEAR, 1.0,
                     NULL);

  clutter_state_set (state->transitions, NULL, "scaled-down",
                     state->actor, "scale-x", CLUTTER_LINEAR, 0.25,
                     state->actor, "scale-y", CLUTTER_LINEAR, 0.25,
                     NULL);

  clutter_state_warp_to_state (state->transitions, "not-scaled");

  g_signal_connect (stage,
                    "key-press-event",
                    G_CALLBACK (key_pressed_cb),
                    state);

  g_signal_connect (state->transitions,
                    "completed",
                    G_CALLBACK (next_transition_cb),
                    state);

  g_signal_connect_after (state->actor,
                          "paint",
                          G_CALLBACK (show_scale_properties_cb),
                          state);

  clutter_container_add (CLUTTER_CONTAINER (stage),
                         state->actor,
                         state->props_display,
                         NULL);

  clutter_actor_show (stage);

  clutter_main ();

  g_object_unref (state->transitions);
  g_free (state);

  return EXIT_SUCCESS;
}


Example 5.15. Animated scaling (up and down) of a texture in response to button presses. Call with the path to an image as the first argument.

/*
 * Load an image into a texture, which can then be zoomed in/out
 * (double click on button 1, double click on button 3 respectively);
 * also resets the texture to the stage center when a key is pressed
 * (better would be to prevent drags taking the actor off-stage,
 * but the implementation is much more complicated)
 */
#include <stdlib.h>
#include <clutter/clutter.h>

#define STAGE_SIDE 400.0

static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };

/* on key press, center the actor on the stage;
 * useful if you drag it off-stage accidentally
 */
static gboolean
key_press_cb (ClutterActor *actor,
              ClutterEvent *event,
              gpointer      user_data)
{
  gfloat width, height;

  clutter_actor_get_size (actor, &width, &height);

  clutter_actor_set_anchor_point (actor, width / 2, height / 2);

  clutter_actor_set_position (actor,
                              STAGE_SIDE / 2,
                              STAGE_SIDE / 2);

  return TRUE;
}

/* on double click, zoom in on the clicked point;
 * also keeps scale in the range 0.1 to 20
 */
static gboolean
clicked_cb (ClutterActor *actor,
            ClutterEvent *event,
            gpointer      user_data)
{
  gdouble scale;
  gfloat click_x, click_y;
  gfloat click_target_x, click_target_y;
  guint32 button;

  /* don't do anything unless there was a double click */
  if (clutter_event_get_click_count (event) < 2)
    return TRUE;

  /* work out new scale */
  button = clutter_event_get_button (event);

  clutter_actor_get_scale (actor, &scale, NULL);

  if (button == CLUTTER_BUTTON_PRIMARY)
    scale *= 1.2;
  else if (button == CLUTTER_BUTTON_SECONDARY)
    scale /= 1.2;

  /* don't do anything if scale is outside bounds */
  if (scale < 0.1 || scale > 20.0)
    return TRUE;

  /* get the location of the click on the scaled actor */
  clutter_event_get_coords (event, &click_x, &click_y);
  clutter_actor_transform_stage_point (actor,
                                       click_x, click_y,
                                       &click_target_x, &click_target_y);

  /* anchor the actor on the clicked point on its surface */
  clutter_actor_set_anchor_point (actor, click_target_x, click_target_y);

  /* set the actor's position to the click coords: it won't move,
   * because the anchor point is already there; but
   * the scale will now be centered on these coords (as the
   * scale center defaults to the anchor point); so the anchor point
   * on the actor won't move from under the pointer
   */
  clutter_actor_set_position (actor, click_x, click_y);

  clutter_actor_animate (actor, CLUTTER_LINEAR, 500,
                         "scale-x", scale,
                         "scale-y", scale,
                         NULL);

  return TRUE;
}

int
main (int   argc,
      char *argv[])
{
  ClutterActor *stage;
  ClutterActor *texture;
  gchar *image_path;
  GError *error = NULL;

  if (argc < 2)
    {
      g_print ("Usage: %s <path to image file>\n", argv[0]);
      exit (EXIT_FAILURE);
    }

  image_path = argv[1];

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;

  stage = clutter_stage_new ();
  clutter_actor_set_size (stage, STAGE_SIDE, STAGE_SIDE);
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);

  texture = clutter_texture_new ();
  clutter_actor_set_reactive (texture, TRUE);
  clutter_actor_set_width (texture, STAGE_SIDE);
  clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (texture), TRUE);

  clutter_actor_add_action (texture, clutter_drag_action_new ());

  g_object_set (G_OBJECT (texture),
                "scale-gravity", CLUTTER_GRAVITY_NORTH_WEST,
                NULL);

  clutter_texture_set_from_file (CLUTTER_TEXTURE (texture), image_path, &error);

  if (error != NULL)
    {
      g_warning ("Error loading %s\n%s", image_path, error->message);
      g_error_free (error);
      exit (EXIT_FAILURE);
    }

  clutter_actor_set_y (texture, (STAGE_SIDE - clutter_actor_get_height (texture)) * 0.5);

  g_signal_connect (texture,
                    "button-release-event",
                    G_CALLBACK (clicked_cb),
                    NULL);

  g_signal_connect_swapped (stage,
                            "key-press-event",
                            G_CALLBACK (key_press_cb),
                            texture);

  clutter_container_add_actor (CLUTTER_CONTAINER (stage), texture);

  clutter_actor_show (stage);

  clutter_main ();

  return EXIT_SUCCESS;
}