trying to do image work

pull/3/head
nathan 1 year ago
parent 2e6d88de2a
commit 336a5c2bf6

@ -1,6 +1,9 @@
package main
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"flag"
"fmt"
"image"
@ -21,6 +24,10 @@ import (
// contents of struct mostly don't matter for toolkit.
type incomingImage struct {
Image string `json:"image"`
}
type Animation struct {
ctx *gg.Context
position image.Point
@ -133,6 +140,7 @@ func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation {
// what happens each frame, at an interval of 50 milliseconds
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
incoming := incomingImage{}
defer a.updatePosition()
a.ctx.SetColor(color.Black)
a.ctx.Clear()
@ -144,10 +152,18 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
a.ctx.SetColor(color.White)
select {
case msg := <-a.mqmsg:
json.Unmarshal([]byte(string(msg.Payload())), &incoming)
if incoming.Image == "" {
a.msg = string(msg.Payload())
a.ctx.DrawString(a.msg, 5, 9)
} else {
baseImage, _ := b64.StdEncoding.DecodeString(incoming.Image)
bigImage, _, _ := image.Decode(bytes.NewReader(baseImage))
littleImage := imaging.Resize(bigImage, 32, 32, imaging.Lanczos)
a.ctx.DrawImageAnchored(littleImage, 0, 0, 0, 0)
}
default:
}
a.ctx.DrawString(a.msg, 5, 9)
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
}
@ -194,9 +210,9 @@ func main() {
log.Println("making animator")
go animator(tk, mqMessages)
log.Println("I guess I'm at the end")
sigs := make(chan os.Signal, -1)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, -1)
done := make(chan bool, 1)
go func() {
sig := <-sigs
fmt.Println()

Loading…
Cancel
Save