@theatre/core
getProject(id, config)
If you import and initialize Studio, you can call getProject()
without an explicit state.
In this case, the state of the project will be managed by Studio.
In production, however, you'd pass a state object, which is exported from Studio in the config argument.
Project
Project returned by getProject
.
To read about Projects, check out the Projects Manual!
project.ready
Promise
that resolves when Theatre.js is loaded. If @theatre/studio
is used, this Promise
would resolve when Studio has loaded the state of the project into memory. If @theatre/studio
is not used, this Promise
is already resolved.
project.isReady
Indicates whether the project is ready to be used.
It is better to use Project.ready, which is a Promise
that resolves when the project is ready.
project.address
The Project's unique address in Theatre.js.
It is a JS object that contains a single property projectId
.
project.sheet(name, instanceId?)
Creates or returns a Sheet under the Project. If a Sheet with the given name is in the Project's state then the existing Sheet is returned; otherwise, a new Sheet is created and returned. You can optionally give a second argument: a sheet instance id that allows you to create multiple instances of the same Sheet (this makes it possible to have multiple instances of the same animation). By default, the instance id is the same as the Sheet id.
Sheet
Sheet returned by Project.sheet
.
sheet.object(key, config, options?)
Creates or returns an Object with given props under this Sheet. If an Object with the given name is in the Project's state then the existing Object is returned; otherwise, a new Object is created and returned.
Objects can also be reconfigured on the fly. Learn more here.
sheet.detatchObject(key)Since 0.5.1
Detaches a previously created child object from the sheet.
If you call sheet.object(key)
again with the same key
, the values of the object's props WILL NOT be reset to their initial values.
sheet.sequence
The Sequence of this sheet. Sequences are JS objects that hold animation data such as Keyframes and current position.
sheet.project
The Project this Sheet belongs to.
sheet.address
The Sheet's unique address in Theatre.js.
It is an object containing a projectId
, sheetId
, and sheetInstanceId
.
Sequence
A JS object that holds animation data such as Keyframes and current position.
sequence.play(opts?)
Starts playback of a sequence. Returns a Promise
that either resolves to true
when the playback completes or resolves
to false
if playback gets interrupted (for example by calling Sequence.pause
)
sequence.pause()
Pauses the currently playing animation.
sequence.position
The current position of the playhead. In a time-based sequence, this represents the current time in seconds.
sequence.pointer
A Pointer to the Sequence's inner state.
As with any Pointer, you can use this with onChange()
to listen to its value changes or with val()
to read its current value.
sequence.attachAudio(opts)
Attaches an audio source to the sequence. Playing the sequence automatically plays the audio source and the audio and animation times are kept in sync.
Returns a Promise
that resolves once the audio source is loaded and decoded.
Learn more from the Using Audio manual.
Note: It's better to provide the audioContext
rather than allow Theatre.js to create it. That's because some browsers
suspend the audioContext
unless it's initiated by a user
gesture, like a click. If that happens, Theatre.js will wait for a user gesture to resume the audioContext
. But that's
probably not an optimal user experience. It is better to provide a button or some other UI element to communicate to
the user that they have to initiate the animation.
To read about Audio, check out the Using Audio Manual!
Object
A Sheet Object returned by sheet.object
with some given props.
To read about Objects, check out the Objects Manual.
To read about the props of Objects, check out the Prop Types Manual or the Prop types API docs below!
object.value
The current values of the props of the Object.
object.props
A Pointer to the props of the Object.
object.sheet
The instance of Sheet that the Object belongs to.
object.project
The Project this object belongs to.
object.address
An Object address is a JS object representing the unique address of the Object in Theatre.js.
It contains a projectId
, sheetId
, sheetInstanceId
, and objectKey
.
object.initialValue
Sets the initial value of the Object. This value overrides the default values defined in the Object's prop types. And, it can then be overridden if the user overrides it in the Studio UI with a static or sequenced value.
object.onValuesChange(callback)
Calls a given callback every time any of the Object's prop values change.
Returns an unsubscribe function that can be called to stop listening.
Prop types
You can define the types of props when creating an Object through sheet.object
using the types
object.
Many prop types allow you to omit the type, and the type is inferred from the initial prop values you provide.
For example, in the following code snippet, the "My Object" Object has a single prop x
with an inferred prop type types.number
.
In cases where you want more control over your Object's props, you can specify the type explicitly. Prop types accept options that alter the way the prop behaves when sequenced or displayed in the Studio UI.
For stringLiteral
, string
, and boolean
types, we can define a custom interpolator as an option, see the code
below for an example.
To read about Prop types, check out the Prop Types Manual!
types.compound(props, opts?)
Compound props are analogous to JavaScript objects in that they enable the nesting of props.
In the example below, position
has an inferred prop type of types.compound
.
You can pass additional options when specifying compound types explicitly.
types.number(default, opts?)
A number prop type.
types.rgba(default?)
An RGBA prop type. All color channels are normalized between 0 and 1.
types.boolean(default)
A boolean prop type. This prop type may take a custom interpolator as an option.
types.string(default, opts?)
A string prop type. This prop type may take a custom interpolator as an option.
types.stringLiteral(default, choices, opts?)
A stringLiteral prop type, useful for building menus or radio buttons. This prop type may take a custom interpolator as an option.
String literals can be presented as radio buttons.
Or as menus.
Pointers
Pointers basically point to values that you can read, observe, and in some cases change.
onChange(pointer, callback)
Takes a Pointer as the first argument and a callback as the second argument.
Calls the callback every time the value pointed to by pointer
changes.
Returns an unsubscribe function.
val(pointer)
Takes a Pointer and returns the value it points to.
Was this article helpful to you?
Last edited on December 01, 2022.
Edit this page