Creating components
A component is an entity that owns primitive children. Initialize its parent and
container properties first, declare kind = 'group' for a primitive container, then construct children with self as their parent.
Expose useful children as named attributes so callers can animate them directly.
Children inherit placement and opacity; an outer position uses parent units.
The complete project below defines a meter with a track and fill. It demonstrates
both composition (nudge) and the Animation.timeline() protocol (Slide).
Composition should be your default: it already implements time conversion and
property keyframes. A protocol implementation may delegate to built-in animations
as shown, or return a custom Timeline. Treat returned timeline structures as
immutable after insertion.
project.json
{
"version": 2,
"packages": {
"Video2D": "1.0.0",
"Neutral": "1.0.0"
},
"assets": {},
"fonts": {
"JetBrains Mono": [
{
"path": "assets/fonts/jetbrains-mono/JetBrainsMono-Regular.ttf",
"weight": 400,
"style": "normal"
}
]
}
}
main.py
"""A compound component and two ways to define reusable animations."""
from manifest import Video2d, EntityWithChildren, Position, Rectangle, Move, Sequence, Interpolation
from manifest import container_properties
class Meter(EntityWithChildren):
"""A bar meter whose track and fill can be animated independently."""
kind = 'group'
def __init__(self, parent, position, color):
super().__init__(parent, container_properties(position))
self.track = Rectangle(self, Position(0, 0), Position(220, 32), fill=(55, 60, 70))
self.fill = Rectangle(self, Position(4, 4), Position(100, 24), fill=color)
def nudge(entity):
"""Compose existing animations into a reusable one-second gesture."""
return Sequence(Move(entity, Position(40, 0), time=0.5), Move(entity, Position(-40, 0), time=0.5))
class Slide:
"""Implement the Animation protocol by returning a relative timeline."""
def __init__(self, entity, delta):
self.animation = Move(entity, delta, time=1, interpolation=Interpolation.Linear)
def timeline(self):
return self.animation.timeline()
def build(seq, frame):
meter = Meter(frame, Position(80, 100), (80, 160, 240))
seq.show(nudge(meter))
seq.show(Slide(meter, Position(100, 0)))
seq.show(0.5)
def make_video():
return Video2d(build, resolution=(640, 360), fps=30)```
The first gesture returns the meter to its original position after one second.
The second moves it 100 units right over one second. The last half-second is a hold.
Moving the parent moves both rectangles together without writing each child's
position. To animate only the fill, pass `meter.fill` to an animation. Use a [custom property](custom-properties.md) to give the meter its own animatable value. See [Property inheritance](property-inheritance.md) for child layout and [Creating animations](creating-animations.md) for reusable gestures.
## Define a component property
Pass a default to `container_properties` when constructing the component. For example, replacing the meter’s base constructor call with the following line gives each meter a local `level` property:
```python
super().__init__(parent, container_properties(position, level=0.5))
Read it with meter.props.level() or assign a new value with meter.props.level = 0.75. To make the fill respond to changes, bind its size to the level as shown in Defining custom properties.
Reusable subsequences
branch = seq.build_sequence(callback, component) invokes the callback with a
child builder and component. Insert the captured branch with seq.show(branch).
Sequence(a, b) plays sequentially; Bundle(a, b) or seq.show(a, b) plays in
parallel. Avoid simultaneous animations writing the same property. Construct
entities in a stable order because sequence structure contributes to generated IDs.