golottie

package module
v0.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 13 Imported by: 0

README

golottie

Coverage Go Reference
Render Lottie animations from Bodymovin using headless browser instance.

Basically a simpler transitive-bullshit/puppeteer-lottie rewrite but in GO and with the ability to render frames concurrently

Logo

Quick start

Add the following import in your Go module

import "github.com/icyrogue/golottie"

Add the dependency explicitly if you need to

$ go get -u github.com/icyrogue/golottie

For examples checkout the go-reference and examples directory or just use the...

CLI

gollotie provides a simple experimental CLI to render animations localy. It renders frames by retrieving the SVG data and converting it to PNG using librsvg so install it first.

Note
Some package managers distribute the required rsvg-convert CLI separately

Install it with

$ go install github.com/icyrogue/golottie/cmd/golottie@latest
Usage of golottie:

-b --bufsize	frame buffer size
		(default: 16)
-c --count	worker count (goroutines) to be created for concurrent rendering
		(default: 1)
-h --height	height of the output
		(default: 1080)
-i --input	input file name
-o --output	output sprintf pattern
-q --quiet	should I have a mouth to scream?
		(default: false)
-w --width	width of the output
		(default: 1920)

This CLI is proof of concept that animation can be rendered by multiple concurrent workers specified by --count option.

Note
The width and height have to be specified manually if differ from defaults.

Warning
Changes and optimizations are coming, use it if you dare!

The most obvious optimization is to use memory arena allocation strategy
Rust rewrite?

License

Released under the MIT License

Documentation

Overview

package golottie renders Lottie animations generated by BodyMovin using headless browser instance through chromedp.

For examples checkout go-lottie

Index

Constants

This section is empty.

Variables

View Source
var (
	EOF                 = errors.New("EOF")
	ErrNilAnimationData = errors.New("animation data is nil")
	ErrNilTemplate      = errors.New("custom template is nil")
)

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

type Context interface {
	context.Context
	Error(err error)
}

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 New

func New(ctx Context) (renderer *Renderer)

New creates a new renderer instance with parent context.

func (*Renderer) NextFrame

func (r *Renderer) NextFrame() bool

NextFrame advances the current animation frame by one. Returns EOF if there aren't any frames left.

func (*Renderer) RenderFrame

func (r *Renderer) RenderFrame(frameBuf *[]byte) error

RenderFrame renders current frame as PNG and writes the resulting bytes to the provided frame buffer.

func (*Renderer) RenderFrameSVG

func (r *Renderer) RenderFrameSVG(frameBuf *string) error

RenderFrameSVG renders current frame as SVG and writes the resulting SVG string to the provided frame buffer.

func (*Renderer) SetAnimation

func (r *Renderer) SetAnimation(animation Animation) error

SetAnimation sets renderer animation. Renderer calls AnimationData.GetFramesTotal and AnimationData.GetURL to update the animation.

Directories

Path Synopsis
cmd
examples
png
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL