Custom Transformers
Due to the lack of sufficient experience and practical verification in this section, the API here may change at any time.
In practice, we find that not all properties are atomic. Some properties may be composed of smaller ones, for example:
- Color: Intuitively, the RGB space contains the R, G, B axes; the YUV space contains the Y, U, V axes. Different spaces require different handling methods.
- Coordinates: In 2D, composed of X and Y.
Additionally, some properties change in highly nonlinear ways or depend on specific contexts or data structures (such as path animations), making it difficult to achieve with simple linear interpolation.
We may also find it cumbersome to insert many identical handleFunction
s repeatedly for a particular type of animation in .Milease()
. Therefore,
we need custom transformers to define how properties interpolate or map between states, achieving more precise, natural, and convenient animation effects.
Usage
Writing a Transformer
public class MyTransformation : ITransformation
{
public bool CanTranslate<E>()
{
var type = typeof(E);
if (type == typeof(int))
{
// Assume I only handle int types here
return true;
}
return false;
}
public MileaseHandleFunction<T, E> MakeTransformation<T, E>(BlendingMode blendingMode)
{
// Assume I actually do nothing here
return e =>
{
var progress = e.Progress;
var ani = e.Animation;
var startValue = (ani.StartValue as int?)!;
var toValue = (ani.ToValue as int?)!;
var result = startValue + (toValue - startValue) * progress;
e.Animation.ValueSetter.Invoke(e.Target, (E)(object)result);
};
}
}
Registering a Transformer
TransformationManagerHolder.TransformationManager.Register(new MyTransformation());
After this, during Milease's animation building process, whenever no handleFunction
is specified, it will try to use your registered ITransformation
here.
Built-in State Transformers
For convenience, Milease comes with a set of built-in transformers.
Color Transformer: Milease includes a color interpolation scheme in the OKLCH space to provide visually smoother color transition animations.