Visualize.IR.Transform (Visualize v0.2.35)

Copy Markdown View Source

Backend-agnostic transformation representation.

Stores a sequence of transformation operations that can be applied to elements when rendering.

Examples

iex> alias Visualize.IR.Transform
iex> Transform.new()
...> |> Transform.translate(100, 50)
...> |> Transform.rotate(45)
...> |> Transform.scale(2, 2)
%Visualize.IR.Transform{operations: [{:translate, 100, 50}, {:rotate, 45}, {:scale, 2, 2}]}

Summary

Types

An affine matrix {a, b, c, d, e, f} for [a c e; b d f; 0 0 1]: a point maps as x' = a·x + c·y + e, y' = b·x + d·y + f.

t()

Functions

Applies an affine matrix to a point.

Combines two transforms by appending the operations.

Returns whether the transform is empty (has no operations).

Adds a matrix transformation. The matrix is specified as (a, b, c, d, e, f) which corresponds to

The product m1 · m2 of two affine matrices, so that applying the result to a point is applying m2 first and then m1.

Creates a new empty transform.

Creates a transform with a single rotate operation. Angle is in degrees.

Adds a rotate operation. Angle is in degrees.

Adds a rotate operation around a specific center point. Angle is in degrees.

Creates a transform with a single uniform scale operation.

With two numbers, creates a transform with a single scale operation. With a transform and one number, adds a uniform scale operation to it.

Adds a scale operation with separate x and y factors.

Adds a skewX operation. Angle is in degrees.

Adds a skewY operation. Angle is in degrees.

Folds the operations into one affine matrix {a, b, c, d, e, f}.

Converts the transform to an SVG transform attribute string.

Creates a transform with a single translate operation.

Adds a translate operation.

Types

matrix()

@type matrix() :: {number(), number(), number(), number(), number(), number()}

An affine matrix {a, b, c, d, e, f} for [a c e; b d f; 0 0 1]: a point maps as x' = a·x + c·y + e, y' = b·x + d·y + f.

operation()

@type operation() ::
  {:translate, number(), number()}
  | {:rotate, number()}
  | {:rotate, number(), number(), number()}
  | {:scale, number(), number()}
  | {:skew_x, number()}
  | {:skew_y, number()}
  | {:matrix, number(), number(), number(), number(), number(), number()}

t()

@type t() :: %Visualize.IR.Transform{operations: [operation()]}

Functions

apply_matrix(arg1, arg2)

@spec apply_matrix(matrix(), {number(), number()}) :: {number(), number()}

Applies an affine matrix to a point.

concat(transform1, transform2)

@spec concat(t(), t()) :: t()

Combines two transforms by appending the operations.

empty?(transform)

@spec empty?(t()) :: boolean()

Returns whether the transform is empty (has no operations).

matrix(t, a, b, c, d, e, f)

@spec matrix(t(), number(), number(), number(), number(), number(), number()) :: t()

Adds a matrix transformation. The matrix is specified as (a, b, c, d, e, f) which corresponds to:

| a c e |
| b d f |
| 0 0 1 |

multiply(arg1, arg2)

@spec multiply(matrix(), matrix()) :: matrix()

The product m1 · m2 of two affine matrices, so that applying the result to a point is applying m2 first and then m1.

new()

@spec new() :: t()

Creates a new empty transform.

rotate(angle)

@spec rotate(number()) :: t()

Creates a transform with a single rotate operation. Angle is in degrees.

rotate(t, angle)

@spec rotate(t(), number()) :: t()

Adds a rotate operation. Angle is in degrees.

rotate(t, angle, cx, cy)

@spec rotate(t(), number(), number(), number()) :: t()

Adds a rotate operation around a specific center point. Angle is in degrees.

scale(s)

@spec scale(number()) :: t()

Creates a transform with a single uniform scale operation.

scale(sx, sy)

@spec scale(number(), number()) :: t()
@spec scale(t(), number()) :: t()

With two numbers, creates a transform with a single scale operation. With a transform and one number, adds a uniform scale operation to it.

scale(t, sx, sy)

@spec scale(t(), number(), number()) :: t()

Adds a scale operation with separate x and y factors.

skew_x(t, angle)

@spec skew_x(t(), number()) :: t()

Adds a skewX operation. Angle is in degrees.

skew_y(t, angle)

@spec skew_y(t(), number()) :: t()

Adds a skewY operation. Angle is in degrees.

to_matrix(transform)

@spec to_matrix(t()) :: matrix()

Folds the operations into one affine matrix {a, b, c, d, e, f}.

Composition follows SVG: M = Op₁ · Op₂ · … · Opₙ, so the last operation applies to a point first, exactly as the transform attribute string would. rotate(a, cx, cy) is translate(cx, cy) rotate(a) translate(-cx, -cy); angles are degrees.

Examples

iex> alias Visualize.IR.Transform
iex> Transform.translate(10, 20) |> Transform.to_matrix()
{1.0, 0.0, 0.0, 1.0, 10.0, 20.0}

to_string(transform)

@spec to_string(t()) :: String.t()

Converts the transform to an SVG transform attribute string.

Examples

iex> alias Visualize.IR.Transform
iex> Transform.translate(100, 50) |> Transform.to_string()
"translate(100,50)"

translate(x, y)

@spec translate(number(), number()) :: t()

Creates a transform with a single translate operation.

translate(t, x, y)

@spec translate(t(), number(), number()) :: t()

Adds a translate operation.