Getting Started

Contributing

Control CommandPost

Plugins

Internationalisation

CommandPost API

Bundled Plugins API

Hammerspoon API

matrix

â€‹

A sub module to

`hs.canvas`

which provides support for basic matrix manipulations which can be used as the values for `transformation`

attributes in the `hs.canvas`

module.For mathematical reasons that are beyond the scope of this document, a 3x3 matrix can be used to represent a series of manipulations to be applied to the coordinates of a 2 dimensional drawing object. These manipulations can include one or more of a combination of translations, rotations, shearing and scaling. Within the 3x3 matrix, only 6 numbers are actually required, and this module represents them as the following keys in a Lua table:

`m11`

, `m12`

, `m21`

, `m22`

, `tX`

, and `tY`

. For those of a mathematical bent, the 3x3 matrix used within this module can be visualized as follows:1

[ m11, m12, 0 ]

2

[ m21, m22, 0 ]

3

[ tX, tY, 1 ]

Copied!

This module allows you to generate the table which can represent one or more of the recognized transformations without having to understand the math behind the manipulations or specify the matrix values directly.

Many of the methods defined in this module can be used both as constructors and as methods chained to a previous method or constructor. Chaining the methods in this manner allows you to combine multiple transformations into one combined table which can then be assigned to an element in your canvas. .

For more information on the mathematics behind these, you can check the web. One site I used for reference (but there are many more which go into much more detail) can be found at http://www.cs.trinity.edu/~jhowland/cs2322/2d/2d/.

API Overview

- Constructors - API calls which return an object, typically one that offers API methods
- Methods - API calls which can only be made on an object returned by a constructor

API Documentation

Constructors

`hs.canvas.matrix.identity() -> matrixObject`

Constructor

Specifies the identity matrix. Resets all existing transformations when applied as a method to an existing matrixObject.

- None

- the identity matrix.

- The identity matrix can be thought of as "apply no transformations at all" or "render as specified".
- Mathematically this is represented as:~~~[ 1, 0, 0 ][ 0, 1, 0 ][ 0, 0, 1 ]~~~

Methods

`hs.canvas.matrix:append(matrix) -> matrixObject`

Method

Appends the specified matrix transformations to the matrix and returns the new matrix. This method cannot be used as a constructor.

`matrix`

- the table to append to the current matrix.

- the new matrix

- Mathematically this method multiples the original matrix by the new one and returns the result of the multiplication.
- You can use this method to "stack" additional transformations on top of existing transformations, without having to know what the existing transformations in effect for the canvas element are.

`hs.canvas.matrix:invert() -> matrixObject`

Method

Generates the mathematical inverse of the matrix. This method cannot be used as a constructor.

- None

- the inverted matrix.

- Inverting a matrix which represents a series of transformations has the effect of reversing or undoing the original transformations.
- This is useful when used with hs.canvas.matrix.append to undo a previously applied transformation without actually replacing all of the transformations which may have been applied to a canvas element.

`hs.canvas.matrix:prepend(matrix) -> matrixObject`

Method

Prepends the specified matrix transformations to the matrix and returns the new matrix. This method cannot be used as a constructor.

`matrix`

- the table to append to the current matrix.

- the new matrix

- Mathematically this method multiples the new matrix by the original one and returns the result of the multiplication.
- You can use this method to apply a transformation
*before*the currently applied transformations, without having to know what the existing transformations in effect for the canvas element are.

`hs.canvas.matrix:rotate(angle) -> matrixObject`

Method

Applies a rotation of the specified number of degrees to the transformation matrix. This method can be used as a constructor or a method.

`angle`

- the number of degrees to rotate in a clockwise direction.

- the new matrix

- The rotation of an element this matrix is applied to will be rotated about the origin (zero point). To rotate an object about another point (its center for example), prepend a translation to the point to rotate about, and append a translation reversing the initial translation.
- e.g.
`hs.canvas.matrix.translate(x, y):rotate(angle):translate(-x, -y)`

`hs.canvas.matrix:scale(xFactor, [yFactor]) -> matrixObject`

Method

Applies a scaling transformation to the matrix. This method can be used as a constructor or a method.

`xFactor`

- the scaling factor to apply to the object in the horizontal orientation.`yFactor`

- an optional argument specifying a different scaling factor in the vertical orientation. If this argument is not provided, the`xFactor`

argument will be used for both orientations.

- the new matrix

`hs.canvas.matrix:shear(xFactor, [yFactor]) -> matrixObject`

Method

Applies a shearing transformation to the matrix. This method can be used as a constructor or a method.

`xFactor`

- the shearing factor to apply to the object in the horizontal orientation.`yFactor`

- an optional argument specifying a different shearing factor in the vertical orientation. If this argument is not provided, the`xFactor`

argument will be used for both orientations.

- the new matrix

`hs.canvas.matrix:translate(x, y) -> matrixObject`

Method

Applies a translation transformation to the matrix. This method can be used as a constructor or a method.

`x`

- the distance to translate the object in the horizontal direction.`y`

- the distance to translate the object in the vertical direction.

- the new matrix

Last modified 6mo ago

Export as PDF

Copy link