-
Notifications
You must be signed in to change notification settings - Fork 0
/
golottie.go
93 lines (85 loc) · 2.71 KB
/
golottie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// package golottie renders [Lottie]
// animations generated by [BodyMovin]
// using headless browser instance through [chromedp].
//
// For examples checkout [go-lottie]
//
// [Lottie]: https://airbnb.design/lottie/
// [BodyMovin]: https://aescripts.com/bodymovin/
// [chromedp]: https://github.com/chromedp/chromedp
// [go-lottie]: https://github.com/icyrogue/go-lottie
package golottie
import (
"fmt"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp"
)
type Renderer struct {
framesDone int
framesTotal int
ctx Context
}
// New creates a new renderer instance with parent context.
func New(ctx Context) (renderer *Renderer) {
return &Renderer{ctx: ctx}
}
// SetAnimation sets renderer animation.
// Renderer calls [AnimationData.GetFramesTotal] and [AnimationData.GetURL]
// to update the animation.
func (r *Renderer) SetAnimation(animation Animation) error {
r.framesTotal = animation.GetFramesTotal()
if err := chromedp.Run(r.ctx,
//TODO: pass WxH and BG with the animation
emulation.SetDefaultBackgroundColorOverride().WithColor(&cdp.RGBA{0, 0, 0, 0}),
chromedp.EmulateViewport(1920, 1080),
chromedp.Navigate(animation.GetURL()),
chromedp.WaitReady(`//*[@id="lottie"]`),
); err != nil {
return err
}
return nil
}
// NextFrame advances the current animation frame by one.
// Returns [EOF] if there aren't any frames left.
func (r *Renderer) NextFrame() bool {
if r.framesDone >= r.framesTotal {
r.ctx.Error(EOF)
return false
}
if err := chromedp.Run(r.ctx,
chromedp.Evaluate(fmt.Sprintf("anim.goToAndStop(%d, true)", r.framesDone), nil),
); err != nil {
r.ctx.Error(err)
return false
}
r.framesDone++
return true
}
// RenderFrame renders current frame as PNG and writes the resulting
// bytes to the provided frame buffer.
func (r *Renderer) RenderFrame(frameBuf *[]byte) error {
return chromedp.Run(r.ctx,
// emulation.SetEmulatedMedia().WithMedia("screen"),
chromedp.CaptureScreenshot(frameBuf))
}
// RenderFrameSVG renders current frame as SVG and writes the resulting
// SVG string to the provided frame buffer.
func (r *Renderer) RenderFrameSVG(frameBuf *string) error {
return chromedp.Run(r.ctx,
chromedp.OuterHTML("svg", frameBuf, chromedp.ByQuery))
}
// func (r *Renderer) RenderAll(frames *[]*[]byte) error {
// return chromedp.Run(r.ctx, chromedp.ActionFunc(func(ctx context.Context) error {
// var err error
// for i, frame := range *frames {
// if err = chromedp.Screenshot("#lottie", frame, chromedp.ByQuery).Do(ctx); err != nil {
// return err
// }
// if err = chromedp.Evaluate(fmt.Sprintf("anim.goToAndStop(%d, true)", i), nil).Do(ctx); err != nil {
// return err
// }
// }
// return nil
// }))
// }