View Source Scenic.Primitive.Transform behaviour (Scenic v0.11.2)

Change the position, rotation, scale and more of a primitive.

Unlike html, which uses auto-layout to position items on the screen, Scenic moves primitives around using matrix transforms. This is common in video games and provides powerful control of your primitives.

A matrix is an array of numbers that can be used to change the positions, rotations, scale and more of locations.

Don't worry! You will not need to look at any matrices unless you want to get fancy. In Scenic, you will rarely (if ever) create matrices on your own (you can if you know what you are doing!), and will instead use the transform helpers.

Multiple transforms can be applied to any primitive. Transforms combine down the graph to create a very flexible way to manage your scene.

There are a fixed set of transform helpers that create matrices for you.

  • Matrix hand specify a matrix.
  • Pin set a pin to rotate or scale around. Most primitives define a sensible default pin.
  • Rotate rotate around the pin.
  • Scale scale larger or smaller. Centered around the pin.
  • Translate move/translate horizontally and veritcally.

specifying-transforms

Specifying Transforms

You apply transforms to a primitive the same way you specify styles.

graph =
  Graph.build
  |> circle( 100, fill: {:color, :green}, translate: {200, 200} )
  |> ellipse( {40, 60, fill: {:color, :red}, rotate: 0.4, translate: {100, 100} )

Don't worry about the order you apply transforms to a single object. Scenic will multiply them together in the correct way when it comes time to render them.

Link to this section Summary

Functions

Given a Map describing the transforms on a primitive, calculate the combined matrix that should be applied.

Link to this section Callbacks

@callback validate(data :: any()) :: {:ok, data :: any()} | {:error, String.t()}

Link to this section Functions

Given a Map describing the transforms on a primitive, calculate the combined matrix that should be applied.

This is trickier than just multiplying them together. Rotations, translations and scale, need to be done in the right order, which is why this function is provided.

You will not normally need to use this function. It is used internally by the input system.