Signals
Last updated
Last updated
Note: These docs were adopted from the original docs
Signals represent a value that may change over time. They can be used to define dependencies between the state of the animation. This way, when a value changes, all other values that depend on it get automatically updated.
Signals for primitive types are created using the createSignal()
function, where the first argument specifies their initial value:
Additionally, each complex type has a static createSignal()
method that can be used to create a signal for said type:
Properties of every node are also represented by signals:
Once created, signals can be invoked to perform one of the three possible actions (The action is chosen based on the number of arguments):
retrieve the value:
update the value:
create a for the value:
Instead of the actual value, a signal can be provided with a function that computes the value dynamically. Consider the following example:
Here, the area
signal uses the radius
signal to compute its value.
To better understand how signals work, let's modify the example from before to see when exactly the area is calculated:
This demonstrates three important aspects of signals:
Signals are only calculated when their value is requested. The first "area recalculated!"
message is logged to console only after area()
is called.
Once the signal is calculated, its value is saved and then returned during subsequent calls to area()
. That's why nothing is logged to the console during the second call. This aspect of signals makes them perfect for caching computationally heavy operations. In fact, Motion Canvas uses signals internally to cache things such as matrices.
The area
signal keeps track of other signals it depends on. When we change the radius
signal, the area
signal is notified about that. But it doesn't get recalculated immediately - laziness is still at play. We can modify the radius however many times we want, but the area
will be recalculated only once its value is requested again by calling area()
.
Signals keep track of the initial values specified during creation. At any time, we can reset a signal to its initial value by passing the DEFAULT
symbol to it:
We can also use the DEFAULT
symbol for tweening:
Resetting to the default value is especially useful with node properties. In the example below, we set the lineHeight
of the Txt
node to 150%
. This will override its default value, which would be simply inherited from its parent:
If we want to reset the lineHeight
back to the default, inherited value, we can do so with DEFAULT
:
We can use the fact that properties of nodes are represented by signals to construct scenes that automatically update when the data changes. Following the previous example, let's create a visualization for the area of the circle:
With this setup, all we need to do is animate the radius
signal, and the rest of the scene will adjust accordingly: