Files
8bitMyGoUI/fx/rain.go
T
ZacharyZhang-NY 8be66c09f2 Add fx components 291-320 and the home scene; Kit.Shot, Kit.Toward
Palette cycling, sky, sun and moon, clouds, starfield, rain, snow,
petals, leaves, fireflies, waterfall, ocean, campfire, lava, aurora,
lightning, mountains and a season scene; scanlines, vignette, screen
shake, flash, dither fades, iris wipes, dissolves, mosaics, glitches,
sparkles, explosions and confetti. Home opens on a live scene in the
palette's season and a strip of all eight palettes.

pixel adds Canvas.Cycled, Sprite.At, SpriteOf, Kit.Toward and
Kit.Shot, which spends a cue under Reduce Motion so it never replays;
display exports Reduce. Gallery grids and the sidebar are one Tab
stop each.
2026-10-12 04:05:25 -04:00

45 lines
1.0 KiB
Go

package fx
import (
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RainOptions struct {
// Width and Height are dots; zeros mean 160 by 96.
Width, Height int
Label string
}
// Rain slants streaks down; each splashes as it lands.
func Rain(c *ui.Context, o RainOptions) *ui.Element {
k := pixel.K(c)
w, h := size("Rain", o.Width, o.Height, 160, 96)
return box(c, k, o.Label, w, h).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Clip(0, 0, g.W, g.H, func() { rain(g, k, g.Step(12)) })
})
}
// rain drops fall three rows, a dot left, each step.
func rain(g pixel.Canvas, k pixel.Kit, step int) {
s := pick(k, pixel.Faint, pixel.Info)
fall := (g.H+4)/3 + 1
for i := range g.W * g.H / 160 {
p := wrap(step+rnd(i, 30), fall+3)
x := wrap(rnd(i, 31)-p, g.W)
switch {
case p < fall:
y := 3*p - 4
g.Line(x+1, y, x, y+3, s)
case p == fall:
g.Dot(x-1, g.H-2, s)
g.Dot(x+1, g.H-2, s)
case p == fall+1:
g.Dot(x-2, g.H-3, s)
g.Dot(x+2, g.H-3, s)
}
}
}