Skip to content

Commit ea4556c

Browse files
committed
ToolKit: added PlayImageUntil and now Animation uses PlayImageUntil
1 parent 985a3c8 commit ea4556c

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

examples/animation/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"flag"
55
"image"
6+
"image/color"
67
"time"
78

89
"github.com/fogleman/gg"
@@ -57,16 +58,16 @@ func NewAnimation(sz image.Point) *Animation {
5758
}
5859
}
5960

60-
func (a *Animation) Next() (image.Image, time.Duration, error) {
61+
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
6162
defer a.updatePosition()
6263

63-
a.ctx.SetRGB(0, 0, 0)
64+
a.ctx.SetColor(color.Black)
6465
a.ctx.Clear()
6566

6667
a.ctx.DrawCircle(float64(a.position.X), float64(a.position.Y), float64(a.stroke))
67-
a.ctx.SetRGB(1, 0, 0)
68+
a.ctx.SetColor(color.RGBA{255, 0, 0, 0})
6869
a.ctx.Fill()
69-
return a.ctx.Image(), time.Millisecond * 50, nil
70+
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
7071
}
7172

7273
func (a *Animation) updatePosition() {

toolkit.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ func (tk *ToolKit) PlayImage(i image.Image, delay time.Duration) error {
4444
}
4545

4646
type Animation interface {
47-
Next() (image.Image, time.Duration, error)
47+
Next() (image.Image, <-chan time.Time, error)
4848
}
4949

5050
// PlayAnimation play the image during the delay returned by Next, until an err
5151
// is returned, if io.EOF is returned, PlayAnimation finish without an error
5252
func (tk *ToolKit) PlayAnimation(a Animation) error {
5353
var err error
5454
var i image.Image
55-
var d time.Duration
55+
var n <-chan time.Time
5656

5757
for {
58-
i, d, err = a.Next()
58+
i, n, err = a.Next()
5959
if err != nil {
6060
break
6161
}
6262

63-
if err := tk.PlayImage(i, d); err != nil {
63+
if err := tk.PlayImageUntil(i, n); err != nil {
6464
return err
6565
}
6666
}
@@ -72,6 +72,20 @@ func (tk *ToolKit) PlayAnimation(a Animation) error {
7272
return err
7373
}
7474

75+
// PlayImageUntil draws the given image until is notified to stop
76+
func (tk *ToolKit) PlayImageUntil(i image.Image, notify <-chan time.Time) error {
77+
defer func() {
78+
<-notify
79+
}()
80+
81+
if tk.Transform != nil {
82+
i = tk.Transform(i)
83+
}
84+
85+
draw.Draw(tk.Canvas, tk.Canvas.Bounds(), i, image.ZP, draw.Over)
86+
return tk.Canvas.Render()
87+
}
88+
7589
// PlayImages draws a sequence of images during the given delays, the len of
7690
// images should be equal to the len of delay. If loop is true the function
7791
// loops over images until a true is sent to the returned chan

0 commit comments

Comments
 (0)