package widget import ( "image" "math" ) const ( halfPi = math.Pi / 2 ) type Transitioner interface { Transition(float64) float64 } type TransitionFunc func(float64) float64 func (f TransitionFunc) Transition(x float64) float64 { return f(x) } var ( Linear = TransitionFunc(func(x float64) float64 { return x }) Exponential = TransitionFunc(func(x float64) float64 { return math.Pow(2, 8*(x-1)) }) Circular = TransitionFunc(func(x float64) float64 { return 1 - math.Sin(math.Acos(x)) }) Sine = TransitionFunc(func(x float64) float64 { return 1 - math.Sin(x*halfPi) }) Elastic = TransitionFunc(func(x float64) float64 { return math.Pow(2, 10*(x-1)) * math.Cos(20*x*math.Pi/3) }) Bounce = TransitionFunc(bounce) ) func bounce(x float64) (v float64) { for a, b := 0.0, 1.0; true; a, b = a+b, b/2 { if x >= (7-4*a)/11 { v = b*b - math.Pow((11-6*a-11*x)/4, 2) break } } return } type PointTransition struct { start, final image.Point t Transitioner dx, dy float64 step, steps int } func NewPointTransition(final image.Point, t Transitioner) *PointTransition { return &PointTransition{ final: final, t: t, } } // Start a new transition for point at start to final. func (p *PointTransition) Start(start image.Point, steps int) { p.start = start p.dx = float64(p.final.X-start.X) / float64(steps) p.dy = float64(p.final.Y-start.Y) / float64(steps) } // Next step. func (p *PointTransition) Next() (at image.Point, done bool) { if p.step >= p.steps { return p.final, true } p.step++ tx := 1.0 if p.t != nil { tx = p.t.Transition(float64(p.step) / float64(p.steps)) } at = p.start at.X += int(p.dx * float64(p.step) * tx) at.Y += int(p.dy * float64(p.step) * tx) return }