resizing image on thread instead of ui thread
This commit is contained in:
32
main.go
32
main.go
@@ -29,16 +29,17 @@ type incomingImage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Animation struct {
|
type Animation struct {
|
||||||
ctx *gg.Context
|
ctx *gg.Context
|
||||||
position image.Point
|
position image.Point
|
||||||
dir image.Point
|
dir image.Point
|
||||||
height int
|
height int
|
||||||
width int
|
width int
|
||||||
stroke int
|
stroke int
|
||||||
image []image.Image
|
image []image.Image
|
||||||
updown int
|
updown int
|
||||||
mqmsg chan mqtt.Message
|
mqmsg chan mqtt.Message
|
||||||
msg string
|
msg string
|
||||||
|
|
||||||
countDown int
|
countDown int
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,6 +141,12 @@ func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendImage(img string, a *Animation) {
|
||||||
|
baseImage, _ := b64.StdEncoding.DecodeString(img)
|
||||||
|
bigImage, _, _ := image.Decode(bytes.NewReader(baseImage))
|
||||||
|
a.image = append(a.image, imaging.Resize(bigImage, 32, 32, imaging.Lanczos))
|
||||||
|
}
|
||||||
|
|
||||||
// what happens each frame, at an interval of 50 milliseconds
|
// what happens each frame, at an interval of 50 milliseconds
|
||||||
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
||||||
incoming := incomingImage{}
|
incoming := incomingImage{}
|
||||||
@@ -168,10 +175,7 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
|||||||
a.msg = string(msg.Payload())
|
a.msg = string(msg.Payload())
|
||||||
a.ctx.DrawString(a.msg, 5, 9)
|
a.ctx.DrawString(a.msg, 5, 9)
|
||||||
} else {
|
} else {
|
||||||
baseImage, _ := b64.StdEncoding.DecodeString(incoming.Image)
|
go appendImage(incoming.Image, a)
|
||||||
bigImage, _, _ := image.Decode(bytes.NewReader(baseImage))
|
|
||||||
a.image = append(a.image, imaging.Resize(bigImage, 32, 32, imaging.Lanczos))
|
|
||||||
a.ctx.DrawImageAnchored(a.image[2], 0, 0, 0, 0)
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user