Documentation ¶
Overview ¶
package golottie renders Lottie animations generated by BodyMovin using headless browser instance through chromedp.
For examples checkout go-lottie
Index ¶
- Variables
- func NewContext(ctx context.Context) (context *gContext, cancel context.CancelFunc)
- type Animation
- type AnimationData
- func (a *AnimationData) Close()
- func (a *AnimationData) GetFramesTotal() int
- func (a *AnimationData) GetURL() (url string)
- func (a *AnimationData) WithCustomTemplate(templ *template.Template, data map[string]interface{}) (_ *AnimationData, err error)
- func (a *AnimationData) WithDefaultTemplate() (animation *AnimationData, err error)
- type Context
- type Renderer
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func NewContext ¶
func NewContext(ctx context.Context) (context *gContext, cancel context.CancelFunc)
NewContext wraps a new chromedp context created from parent ctx.
Types ¶
type Animation ¶
type Animation interface { // GetURL is called by renderer to retrive animation HTML. GetURL() string // Close is needed in a case of animation data being served localy. Close() // GetFramesTotal returns number of frames to be rendered. GetFramesTotal() int }
Animation interface is used by renderer to get animation data.
type AnimationData ¶
type AnimationData struct { // Template field contains pointer to animation template // to be reused when parsing multiple animations. // Equals to nil if animation template hasn't been initialized. Template *template.Template // contains filtered or unexported fields }
AnimationData implements Animation interface to serve animation data localy.
func NewAnimation ¶
func NewAnimation(data []byte) *AnimationData
NewAnimation creates a new animation with data argument as animation data. Template function should be called on resulting animation for it to be completely initialized.
Example:
data, _ := os.ReadFile("animation.json") animation, err := golottie.NewAnimation(data).WithDefaultTemplate() if err != nil { log.Fatal(err) } renderer.SetAnimation(animation)
func (*AnimationData) Close ¶
func (a *AnimationData) Close()
Close closes the local server if it exists.
func (*AnimationData) GetFramesTotal ¶
func (a *AnimationData) GetFramesTotal() int
GetFramesTotal is used by renderer to get the amount of frames to render.
func (*AnimationData) GetURL ¶
func (a *AnimationData) GetURL() (url string)
GetURL serves an animation data localy and returns an URL to be used by renderer.
func (*AnimationData) WithCustomTemplate ¶
func (a *AnimationData) WithCustomTemplate(templ *template.Template, data map[string]interface{}) (_ *AnimationData, err error)
WithCustomTemplate initializes animation data using provided custom template. Can be called to reuse animation.Template multiple times.
Example:
data, _ := os.ReadFile("animation.json") animation, _ := golottie.NewAnimation(data).WithDefaultTemplate() data, _ = os.ReadFile("animation2.json") animationTwo, err := golottie.NewAnimation(data).WithCustomTemplate(animation.Template, nil) if err != nil { log.Fatal(err.Error) }
The data arguments map can be provided to be available inside the template. If data map is nil, a new map will be created with "animationData" key containing initial animation data as unescaped JS string. If data map isn't nil, "animationData" key will be added to it.
func (*AnimationData) WithDefaultTemplate ¶
func (a *AnimationData) WithDefaultTemplate() (animation *AnimationData, err error)
WithDefaultTemplate initializes animation data using embedded default template. Returns an error if the initial data is nil or has 0 length.
type Context ¶
Context interface is a custom context which implements context.Context and adds custom error method to contain an error stack.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
func (*Renderer) NextFrame ¶
NextFrame advances the current animation frame by one. Returns EOF if there aren't any frames left.
func (*Renderer) RenderFrame ¶
RenderFrame renders current frame as PNG and writes the resulting bytes to the provided frame buffer.
func (*Renderer) RenderFrameSVG ¶
RenderFrameSVG renders current frame as SVG and writes the resulting SVG string to the provided frame buffer.
func (*Renderer) SetAnimation ¶
SetAnimation sets renderer animation. Renderer calls AnimationData.GetFramesTotal and AnimationData.GetURL to update the animation.