Skip to main content

utils

                            
                              
                                import
                                
                                {
                                ...
                                }
                                
                                from
                                
                                "@motion-canvas/core/lib/utils"
                                ;
                                

General utilities and helper functions.

Classes

  • DetailedError

Interfaces

  • Reference
  • ReferenceReceiver

Type Aliases

RefsProperty

                            
                              
                                type 
                                RefsProperty
                                
                                  
                                    
                                      TValue
                                    
                                  
                                
                                 = 
                                TValue
                                 extends 
                                public 
                                
                                () ? 
                                TReference
                                 : 
                                never
                              
                              

Type Parameters

Functions

beginSlide

                            
                              
                                public 
                                beginSlide
                                
                                  
                                    
                                      name
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                ThreadGenerator
                              
                              

Parameters


capitalize

                            
                              
                                public 
                                capitalize
                                
                                  
                                    
                                      T
                                       extends 
                                      string
                                    
                                  
                                
                                
                                  
                                    
                                      value
                                      : 
                                      T
                                    
                                  
                                
                                : 
                                Capitalize
                                
                                  
                                    
                                      T
                                    
                                  
                                
                              
                              

Type Parameters

  • T extends string

Parameters


createRef

                            
                              
                                public 
                                createRef
                                
                                  
                                    
                                      T
                                    
                                  
                                
                                (): 
                                Reference
                                
                                  
                                    
                                      T
                                    
                                  
                                
                              
                              

Type Parameters


debug

                            
                              
                                public 
                                debug
                                
                                  
                                    
                                      payload
                                      : 
                                      any
                                    
                                  
                                
                                : 
                                void
                              
                              

Logs a debug message with an arbitrary payload.

This method is a shortcut for calling useLogger().debug() which allowsyou to more easily log non-string values as well.

Examples

Parameters


deprecate

                            
                              
                                public 
                                deprecate
                                
                                  
                                    
                                      TArgs
                                       extends 
                                      any
                                      []
                                    
                                    
                                      TReturn
                                    
                                  
                                
                                
                                  
                                    
                                      fn
                                      : 
                                      public 
                                      
                                      ()
                                    
                                    
                                      message
                                      : 
                                      string
                                    
                                    
                                      remarks
                                      ?: 
                                      string
                                    
                                  
                                
                                : 
                                public 
                                
                                ()
                              
                              

Mark the given function as deprecated.

Type Parameters

Parameters

  • fn : public ()

    The function to deprecate.

  • message : string

    The log message.

  • remarks ?: string

    The optional log remarks.


endPlayback

                            
                              
                                public 
                                endPlayback
                                
                                  
                                    
                                      playback
                                      : 
                                      PlaybackStatus
                                    
                                  
                                
                                : 
                                void
                              
                              

Parameters


endScene

                            
                              
                                public 
                                endScene
                                
                                  
                                    
                                      scene
                                      : 
                                      Scene
                                      
                                        
                                          
                                            unknown
                                          
                                        
                                      
                                    
                                  
                                
                                : 
                                void
                              
                              

Parameters


endThread

                            
                              
                                public 
                                endThread
                                
                                  
                                    
                                      thread
                                      : 
                                      Thread
                                    
                                  
                                
                                : 
                                void
                              
                              

Parameters


errorToLog

                            
                              
                                public 
                                errorToLog
                                
                                  
                                    
                                      error
                                      : 
                                      any
                                    
                                  
                                
                                : 
                                LogPayload
                              
                              

Parameters


finishScene

                            
                              
                                public 
                                finishScene
                                (): 
                                void
                              
                              

Mark the current scene as ready to transition out.

Usually used together with transitions. When a scene is marked as finished,the transition will start but the scene generator will continue running.


getContext

                            
                              
                                public 
                                getContext
                                
                                  
                                    
                                      options
                                      ?: 
                                      CanvasRenderingContext2DSettings
                                    
                                    
                                      canvas
                                      : 
                                      HTMLCanvasElement
                                       = 
                                      ...
                                    
                                  
                                
                                : 
                                CanvasRenderingContext2D
                              
                              

Parameters


isProxyEnabled

                            
                              
                                public 
                                isProxyEnabled
                                (): 
                                boolean
                              
                              

Check if the proxy is enabled via the plugin by checkingfor import.meta.env.VITE_MC_PROXY_ENABLED

The value can either be 'true' of 'false'(as strings) if present, or be undefined if not runfrom a vite context or run without the MC Plugin.


makeRef

                            
                              
                                public 
                                makeRef
                                
                                  
                                    
                                      TObject
                                    
                                    
                                      TKey
                                       extends 
                                      
                                        
                                          
                                            string
                                          
                                          
                                            number
                                          
                                          
                                            symbol
                                          
                                        
                                      
                                    
                                  
                                
                                
                                  
                                    
                                      object
                                      : 
                                      TObject
                                    
                                    
                                      key
                                      : 
                                      TKey
                                    
                                  
                                
                                : 
                                ReferenceReceiver
                                
                                  
                                    
                                      TObject
                                      [
                                      TKey
                                      ]
                                    
                                  
                                
                              
                              

Type Parameters

Parameters


makeRefs

                            
                              
                                public 
                                makeRefs
                                
                                  
                                    
                                      T
                                       extends 
                                      public 
                                      
                                      ()
                                    
                                  
                                
                                (): 
                                RefsProperty
                                
                                  
                                    
                                      T
                                    
                                  
                                
                              
                              

Type Parameters

  • T extends public ()

range

                            
                              
                                public 
                                range
                                
                                  
                                    
                                      length
                                      : 
                                      number
                                    
                                  
                                
                                : 
                                number
                                []
                              
                              
                            
                              
                                public 
                                range
                                
                                  
                                    
                                      from
                                      : 
                                      number
                                    
                                    
                                      to
                                      : 
                                      number
                                    
                                  
                                
                                : 
                                number
                                []
                              
                              
                            
                              
                                public 
                                range
                                
                                  
                                    
                                      from
                                      : 
                                      number
                                    
                                    
                                      to
                                      : 
                                      number
                                    
                                    
                                      step
                                      : 
                                      number
                                    
                                  
                                
                                : 
                                number
                                []
                              
                              

Create an array containing a range of numbers.

Examples

Parameters

  • length : number

    The length of the array.


startPlayback

                            
                              
                                public 
                                startPlayback
                                
                                  
                                    
                                      playback
                                      : 
                                      PlaybackStatus
                                    
                                  
                                
                                : 
                                void
                              
                              

Parameters


startScene

                            
                              
                                public 
                                startScene
                                
                                  
                                    
                                      scene
                                      : 
                                      Scene
                                      
                                        
                                          
                                            unknown
                                          
                                        
                                      
                                    
                                  
                                
                                : 
                                void
                              
                              

Parameters


startThread

                            
                              
                                public 
                                startThread
                                
                                  
                                    
                                      thread
                                      : 
                                      Thread
                                    
                                  
                                
                                : 
                                void
                              
                              

Parameters


useContext

                            
                              
                                public 
                                useContext
                                
                                  
                                    
                                      callback
                                      : 
                                      public 
                                      
                                      ()
                                    
                                  
                                
                                : 
                                public 
                                
                                ()
                              
                              

Provide a function to access the Context2D before the scene is rendered.

Parameters

  • callback : public ()

    The function that will be provided the context before render.


useContextAfter

                            
                              
                                public 
                                useContextAfter
                                
                                  
                                    
                                      callback
                                      : 
                                      public 
                                      
                                      ()
                                    
                                  
                                
                                : 
                                public 
                                
                                ()
                              
                              

Provide a function to access the Context2D after the scene is rendered.

Parameters

  • callback : public ()

    The function that will be provided the context after render.


useDuration

                            
                              
                                public 
                                useDuration
                                
                                  
                                    
                                      name
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                number
                              
                              

Register a time event and get its duration in seconds.

This can be used to better specify when an animation should startas well as how long this animation should take

Examples

Parameters

  • name : string

    The name of the event.


useLogger

                            
                              
                                public 
                                useLogger
                                (): 
                                
                                  
                                    
                                      Console
                                    
                                    
                                      Logger
                                    
                                  
                                
                              
                              

usePlayback

                            
                              
                                public 
                                usePlayback
                                (): 
                                PlaybackStatus
                              
                              

Get a reference to the playback status.


useRandom

                            
                              
                                public 
                                useRandom
                                (): 
                                Random
                              
                              
                            
                              
                                public 
                                useRandom
                                
                                  
                                    
                                      seed
                                      : 
                                      number
                                    
                                    
                                      fixed
                                      ?: 
                                      boolean
                                    
                                  
                                
                                : 
                                Random
                              
                              

Get the random number generator for the current scene.


useScene

                            
                              
                                public 
                                useScene
                                (): 
                                Scene
                              
                              

Get a reference to the current scene.


useThread

                            
                              
                                public 
                                useThread
                                (): 
                                Thread
                              
                              

Get a reference to the current thread.


useTime

                            
                              
                                public 
                                useTime
                                (): 
                                number
                              
                              

Get the real time since the start of the animation.

The returned value accounts for offsets caused by functions such as waitFor .

Examples


viaProxy

                            
                              
                                public 
                                viaProxy
                                
                                  
                                    
                                      url
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                string
                              
                              

Route the given url through a local proxy.

Examples

Parameters