-
Notifications
You must be signed in to change notification settings - Fork 21
/
shimmer.go
107 lines (90 loc) · 2.56 KB
/
shimmer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// +build js,wasm
package shimmer
import (
"bytes"
"image"
"strconv"
"syscall/js"
"time"
"github.com/anthonynsimon/bild/imgio"
)
type Shimmer struct {
inBuf []uint8
outBuf bytes.Buffer
onImgLoadCb, shutdownCb js.Func
brightnessCb, contrastCb js.Func
hueCb, satCb js.Func
sourceImg image.Image
console js.Value
done chan struct{}
}
// New returns a new instance of shimmer
func New() *Shimmer {
return &Shimmer{
console: js.Global().Get("console"),
done: make(chan struct{}),
}
}
// Start sets up all the callbacks and waits for the close signal
// to be sent from the browser.
func (s *Shimmer) Start() {
// Setup callbacks
s.setupOnImgLoadCb()
js.Global().Set("loadImage", s.onImgLoadCb)
s.setupBrightnessCb()
js.Global().Get("document").
Call("getElementById", "brightness").
Call("addEventListener", "change", s.brightnessCb)
s.setupContrastCb()
js.Global().Get("document").
Call("getElementById", "contrast").
Call("addEventListener", "change", s.contrastCb)
s.setupHueCb()
js.Global().Get("document").
Call("getElementById", "hue").
Call("addEventListener", "change", s.hueCb)
s.setupSatCb()
js.Global().Get("document").
Call("getElementById", "sat").
Call("addEventListener", "change", s.satCb)
s.setupShutdownCb()
js.Global().Get("document").
Call("getElementById", "close").
Call("addEventListener", "click", s.shutdownCb)
<-s.done
s.log("Shutting down app")
s.onImgLoadCb.Release()
s.brightnessCb.Release()
s.contrastCb.Release()
s.hueCb.Release()
s.satCb.Release()
s.shutdownCb.Release()
}
// updateImage writes the image to a byte buffer and then converts it to base64.
// Then it sets the value to the src attribute of the target image.
func (s *Shimmer) updateImage(img *image.RGBA, start time.Time) {
enc := imgio.JPEGEncoder(90)
err := enc(&s.outBuf, img)
if err != nil {
s.log(err.Error())
return
}
dst := js.Global().Get("Uint8Array").New(len(s.outBuf.Bytes()))
n := js.CopyBytesToJS(dst, s.outBuf.Bytes())
s.console.Call("log", "bytes copied:", strconv.Itoa(n))
js.Global().Call("displayImage", dst)
s.console.Call("log", "time taken:", time.Now().Sub(start).String())
s.outBuf.Reset()
}
// utility function to log a msg to the UI from inside a callback
func (s *Shimmer) log(msg string) {
js.Global().Get("document").
Call("getElementById", "status").
Set("innerText", msg)
}
func (s *Shimmer) setupShutdownCb() {
s.shutdownCb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
s.done <- struct{}{}
return nil
})
}