Composition
A Composition
object manages the entire state of a video project and serves as the foundation for any video editing tasks in Diffusion Studio. This guide will walk you through setting up and manipulating a Composition
object.
Setting Up a Composition
To begin, import the Composition
class from @diffusionstudio/core
and create a new instance:
When constructing a Composition
, you can pass various options. Here are the default values:
If WebGPU is unavailable, the Composition
will automatically fall back to using WebGL as the rendering backend. The specified height
and width
define the canvas size, but these do not dictate the final video resolution, which can be adjusted later.
Note: Changing the aspect ratio of the
Composition
after its creation is currently unsupported.
Visualizing the Composition
To display the Composition
within your application, you can attach it to a DOM element. The example below shows how to center the player inside a container and scale it according to the available space:
This setup ensures that the player is centered and scales to fit within the player-container
while maintaining the aspect ratio of the Composition
. For production environments, consider using a ResizeObserver to handle dynamic resizing based on your specific needs.
Note: Since the canvas cannot be added as a child of two different elements, you must remove it from the player before attaching it to another element:
Changing the Composition State
Here are some common state manipulations you might perform on a Composition
:
Setting the Composition Duration
By default, a Composition
's duration is determined by the last visible clip. However, you can assign a fixed duration using the duration setter:
Adding Tracks
For efficient usage, manually manipulate the Tracks
in your Composition
:
Alternatively, you can create a Track
instance beforehand and then prepend it:
You can remove the track again by calling
composition.removeTrack(track)
Adding Clips
The Composition
offers a convenient method to create a Track
and append a Clip
to it:
add
is always an asynchronous action because it waits until the Clip
can be visualized.
Finding Tracks and Clips
If you need to search for specific tracks or clips and no direct reference is available, use the following methods:
There are also shorthand notations for finding specific types of tracks and clips:
Taking Screenshots
To capture a screenshot of the current frame:
Playback Controls
The most essential playback features are easily accessible through the Composition
:
Hint: Use the
composition.time()
function to conveniently display the current time of theComposition
.