animation

package
v0.49.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCurve = LinearCurve{}
View Source
var DefaultDirection = DirectionNormal
View Source
var DefaultFillMode = FillModeForwards{}
View Source
var DefaultOrigin = Origin{
	X: Percentage{0.5},
	Y: Percentage{0.5},
}
View Source
var DefaultRounding = Round{}
View Source
var DirectionAlternate = DirectionImpl{Alternate: true, Reverse: false}
View Source
var DirectionAlternateReverse = DirectionImpl{Alternate: true, Reverse: true}
View Source
var DirectionNormal = DirectionImpl{Alternate: false, Reverse: false}
View Source
var DirectionReverse = DirectionImpl{Alternate: false, Reverse: true}
View Source
var EaseIn = CubicBezierCurve{0.3, 0, 1, 1}
View Source
var EaseInOut = CubicBezierCurve{0.65, 0, 0.35, 1}

TODO: figure out if what curve to use here. unless we're going back to Ivo's curve (0.3, 0, 0, 1), make sure to update the unit tests

var EaseInOut = CubicBezierCurve{0.3, 0, 0, 1}

View Source
var EaseOut = CubicBezierCurve{0, 0, 0, 1}
View Source
var RotateDefault = Rotate{0.0}
View Source
var ScaleDefault = Scale{Vec2f{1.0, 1.0}}
View Source
var TranslateDefault = Translate{Vec2f{0.0, 0.0}}

Functions

func Lerp

func Lerp(from, to, t float64) float64

func Rescale

func Rescale(fromMin, fromMax, toMin, toMax, v float64) float64

Types

type AnimatedPositioned

type AnimatedPositioned struct {
	Child    render.Widget `starlark:"child,required"`
	XStart   int           `starlark:"x_start"`
	XEnd     int           `starlark:"x_end"`
	YStart   int           `starlark:"y_start"`
	YEnd     int           `starlark:"y_end"`
	Duration int           `starlark:"duration,required"`
	Curve    Curve         `starlark:"curve,required"`
	Delay    int           `starlark:"delay"`
	Hold     int           `starlark:"hold"`
}

Animate a widget from start to end coordinates.

**DEPRECATED**: Please use `animation.Transformation` instead.

DOC(Child): Widget to animate DOC(XStart): Horizontal start coordinate DOC(XEnd): Horizontal end coordinate DOC(YStart): Vertical start coordinate DOC(YEnd): Vertical end coordinate DOC(Duration): Duration of animation in frames DOC(Curve): Easing curve to use, default is 'linear' DOC(Delay): Delay before animation in frames DOC(Hold): Delay after animation in frames

func (AnimatedPositioned) FrameCount

func (o AnimatedPositioned) FrameCount(bounds image.Rectangle) int

func (AnimatedPositioned) Paint

func (o AnimatedPositioned) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int)

func (AnimatedPositioned) PaintBounds

func (o AnimatedPositioned) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle

type CubicBezierCurve

type CubicBezierCurve struct {
	// contains filtered or unexported fields
}

Bezier curve defined by a, b, c and d.

func (CubicBezierCurve) Transform

func (cb CubicBezierCurve) Transform(t float64) float64

type Curve

type Curve interface {
	Transform(t float64) float64
}

func ParseCurve

func ParseCurve(str string) (Curve, error)

type CustomCurve

type CustomCurve struct {
	Function *starlark.Function
}

Custom curve implemented as a starlark function

func (CustomCurve) Transform

func (cc CustomCurve) Transform(t float64) float64

type Direction

type Direction interface {
	FrameCount(delay, duration int) int
	Progress(delay, duration int, fill float64, frameIdx int) float64
}

type DirectionImpl

type DirectionImpl struct {
	Alternate bool
	Reverse   bool
}

func (DirectionImpl) FrameCount

func (self DirectionImpl) FrameCount(delay, duration int) int

func (DirectionImpl) Progress

func (self DirectionImpl) Progress(delay, duration int, fill float64, frameIdx int) (progress float64)

type FillMode

type FillMode interface {
	Value() float64
}

type FillModeBackwards

type FillModeBackwards struct{}

func (FillModeBackwards) Value

func (self FillModeBackwards) Value() float64

type FillModeForwards

type FillModeForwards struct{}

func (FillModeForwards) Value

func (self FillModeForwards) Value() float64

type Keyframe

type Keyframe struct {
	Percentage Percentage  `starlark:"percentage,required"`
	Transforms []Transform `starlark:"transforms,required"`
	Curve      Curve       `starlark:"curve"`
}

A keyframe defining specific point in time in the animation.

The keyframe _percentage_ can is expressed as a floating point value between `0.0` and `1.0`.

DOC(Percentage): Percentage of the time at which this keyframe occurs through the animation. DOC(Transforms): List of transforms at this keyframe to interpolate to or from. DOC(Curve): Easing curve to use, default is 'linear'

type LinearCurve

type LinearCurve struct{}

Linear curve moving from 0 to 1 (wait for it...) linearly

func (LinearCurve) Transform

func (lc LinearCurve) Transform(t float64) float64

type Origin

type Origin struct {
	X Percentage `starlark:"x,required"`
	Y Percentage `starlark:"y,required"`
}

An relative anchor point to use for scaling and rotation transforms.

DOC(X): Horizontal anchor point DOC(Y): Vertical anchor point

func (Origin) Transform

func (self Origin) Transform(bounds image.Rectangle) Vec2f

type Percentage

type Percentage struct {
	Value float64
}

type Rotate

type Rotate struct {
	Angle float64 `starlark:"angle,required"`
}

Transform by rotating by a given angle in degrees.

DOC(Angle): Angle to rotate by in degrees

func (Rotate) Apply

func (self Rotate) Apply(ctx *gg.Context, origin Vec2f, rounding Rounding)

func (Rotate) Interpolate

func (self Rotate) Interpolate(other Transform, progress float64) (result Transform, ok bool)

type Round

type Round struct{}

func (Round) Apply

func (self Round) Apply(v float64) float64

type RoundCeil

type RoundCeil struct{}

func (RoundCeil) Apply

func (self RoundCeil) Apply(v float64) float64

type RoundFloor

type RoundFloor struct{}

func (RoundFloor) Apply

func (self RoundFloor) Apply(v float64) float64

type RoundNone

type RoundNone struct{}

func (RoundNone) Apply

func (self RoundNone) Apply(v float64) float64

type Rounding

type Rounding interface {
	Apply(v float64) float64
}

type Scale

type Scale struct {
	Vec2f
}

Transform by scaling by a given factor.

DOC(X): Horizontal scale factor DOC(Y): Vertical scale factor

func (Scale) Apply

func (self Scale) Apply(ctx *gg.Context, origin Vec2f, rounding Rounding)

func (Scale) Interpolate

func (self Scale) Interpolate(other Transform, progress float64) (result Transform, ok bool)

type Transform

type Transform interface {
	Apply(ctx *gg.Context, origin Vec2f, rounding Rounding)
	Interpolate(other Transform, progress float64) (result Transform, ok bool)
}

func ExtendTransforms

func ExtendTransforms(lhs []Transform, rhs []Transform) []Transform

func InterpolateTransforms

func InterpolateTransforms(lhs, rhs []Transform, progress float64) (result []Transform, ok bool)

See: https://www.w3.org/TR/css-transforms-1/#interpolation-of-transforms

type Transformation

type Transformation struct {
	Child        render.Widget `starlark:"child,required"`
	Keyframes    []Keyframe    `starlark:"keyframes,required"`
	Duration     int           `starlark:"duration,required"`
	Delay        int           `starlark:"delay"`
	Width        int           `starlark:"width"`
	Height       int           `starlark:"height"`
	Origin       Origin        `starlark:"origin"`
	Direction    Direction     `starlark:"direction"`
	FillMode     FillMode      `starlark:"fill_mode"`
	Rounding     Rounding      `starlark:"rounding"`
	WaitForChild bool          `starlark:"wait_for_child"`
}

Transformation makes it possible to animate a child widget by transitioning between transforms which are applied to the child wiget.

It supports animating translation, scale and rotation of its child.

If you have used CSS transforms and animations before, some of the following concepts will be familiar to you.

Keyframes define a list of transforms to apply at a specific point in time, which is given as a percentage of the total animation duration.

A keyframe is created via `animation.Keyframe(percentage, transforms, curve)`.

The `percentage` specifies its point in time and can be expressed as a floating point number in the range `0.0` to `1.0`.

In case a keyframe at percentage 0% or 100% is missing, a default keyframe without transforms and with a "linear" easing curve is inserted.

As the animation progresses, transforms defined by the previous and next keyframe will be interpolated to determine the transform to apply at the current frame.

The `duration` and `delay` of the animation are expressed as a number of frames.

By default a transform `origin` of `animation.Origin(0.5, 0.5)` is used, which defines the anchor point for scaling and rotation to be exactly the center of the child widget. A different `origin` can be specified by providing a custom `animation.Origin`.

The animation `direction` defaults to `normal`, playing the animation forwards. Other possible values are `reverse` to play it backwards, `alternate` to play it forwards, then backwards or `alternate-reverse` to play it backwards, then forwards.

The animation `fill_mode` defaults to `forwards`, and controls which transforms will be applied to the child widget after the animation finishes. A value of `forwards` will retain the transforms of the last keyframe, while a value of `backwards` will rever to the transforms of the first keyframe.

When translating the child widget on the X- or Y-axis, it often is desireable to round to even integers, which can be controlled via `rounding`, which defaults to `round`. Possible values are `round` to round to the nearest integer, `floor` to round down, `ceil` to round up or `none` to not perform any rounding. Rounding only is applied for translation transforms, but not to scaling or rotation transforms.

If `wait_for_child` is set to `True`, the animation will finish and then wait for all child frames to play before restarting. If it is set to `False`, it will not wait.

DOC(Child): Widget to animate DOC(Keyframes): List of animation keyframes DOC(Duration): Duration of animation (in frames) DOC(Delay): Duration to wait before animation (in frames) DOC(Width): Width of the animation canvas DOC(Height): Height of the animation canvas DOC(Origin): Origin for transforms, default is '50%, 50%' DOC(Direction): Direction of the animation, default is 'normal' DOC(FillMode): Fill mode of the animation, default is 'forwards' DOC(Rounding): Rounding to use for interpolated translation coordinates (not used for scale and rotate), default is 'round' DOC(WaitForChild): Wait for all child frames to play after finishing

EXAMPLE BEGIN

animation.Transformation(
    child = render.Box(render.Circle(diameter = 6, color = "#0f0")),
    duration = 100,
    delay = 0,
    origin = animation.Origin(0.5, 0.5),
    direction = "alternate",
    fill_mode = "forwards",
    keyframes = [
        animation.Keyframe(
            percentage = 0.0,
            transforms = [animation.Rotate(0), animation.Translate(-10, 0), animation.Rotate(0)],
            curve = "ease_in_out",
        ),
        animation.Keyframe(
            percentage = 1.0,
            transforms = [animation.Rotate(360), animation.Translate(-10, 0), animation.Rotate(-360)],
        ),
    ],
),

EXAMPLE END

func (*Transformation) FrameCount

func (self *Transformation) FrameCount(bounds image.Rectangle) int

func (*Transformation) Init

func (self *Transformation) Init(*starlark.Thread) error

func (*Transformation) Paint

func (self *Transformation) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int)

func (*Transformation) PaintBounds

func (self *Transformation) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle

type Translate

type Translate struct {
	Vec2f
}

Transform by translating by a given offset.

DOC(X): Horizontal offset DOC(Y): Vertical offset

func (Translate) Apply

func (self Translate) Apply(ctx *gg.Context, origin Vec2f, rounding Rounding)

func (Translate) Interpolate

func (self Translate) Interpolate(other Transform, progress float64) (result Transform, ok bool)

type Vec2f

type Vec2f struct {
	X float64 `starlark:"x,required"`
	Y float64 `starlark:"y,required"`
}

func (Vec2f) Lerp

func (lhs Vec2f) Lerp(rhs Vec2f, progress float64) Vec2f

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL