-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (65 loc) · 1.76 KB
/
Copy pathmain.go
File metadata and controls
86 lines (65 loc) · 1.76 KB
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
package main
import (
"bytes"
"encoding/base64"
"image"
"image/color"
"image/color/palette"
"image/draw"
"image/gif"
"image/png"
"syscall/js"
)
var colors color.Palette = append(palette.WebSafe, image.Transparent)
func main() {
loop := make(chan bool)
// Err-lang style callback hell FTW.
save := js.FuncOf(func(this js.Value, steps []js.Value) interface{} {
fig := gif.GIF{LoopCount: 0}
buf := new(bytes.Buffer)
bob := js.FuncOf(func(this js.Value, funcs []js.Value) interface{} {
resolve, reject := funcs[0], funcs[1]
for i, b64 := range steps {
b, err := base64.StdEncoding.DecodeString(b64.String())
// Jump to the next frame.
if err != nil {
reject.Invoke(js.Global().Get("Error").New(err.Error()))
continue
}
img, err := png.Decode(bytes.NewReader(b))
// Ditto.
if err != nil {
reject.Invoke(js.Global().Get("Error").New(err.Error()))
continue
}
rec := img.Bounds()
dst := image.NewPaletted(rec, colors)
draw.Draw(dst, rec, img, rec.Min, draw.Src)
fig.Image = append(fig.Image, dst)
fig.Delay = append(fig.Delay, 0)
e := js.Global().Get("CustomEvent").New("splashing#step", map[string]interface{}{
"detail": map[string]interface{}{
"total": len(steps),
"index": i,
},
})
js.Global().Call("dispatchEvent", e)
}
// Fail.
if err := gif.EncodeAll(buf, &fig); err != nil {
reject.Invoke(js.Global().Get("Error").New(err.Error()))
return nil
}
// Win.
resolve.Invoke(map[string]interface{}{
"data": base64.StdEncoding.EncodeToString(buf.Bytes()),
"size": buf.Len(),
})
return nil
})
return js.Global().Get("Promise").New(bob)
})
js.Global().Set("Save", save)
// Block party (to keep from exiting).
<-loop
}