Files
8bitMyGoUI/fx/screenshake.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

28 lines
828 B
Go

package fx
import (
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ScreenShakeOptions struct {
// Cue shakes the content each time it changes.
Cue int
// Amount is the widest jolt in dots; zero means 3.
Amount int
}
// quakes are the jolts, a step each, in Amount thirds.
var quakes = [...][2]int{{3, -2}, {-3, 2}, {2, 3}, {-3, -2}, {2, 1}, {-1, -2}, {1, 1}, {-1, 0}, {0, 0}}
// ScreenShake jolts its content both ways, settling as it goes.
func ScreenShake(c *ui.Context, o ScreenShakeOptions, content func()) *ui.Element {
k := pixel.K(c)
a := pixel.Or("ScreenShake Amount", o.Amount, 3)
e := ui.Box(c)
q := quakes[k.Shot(c, e, o.Cue, 15, len(quakes)-1)]
dx, dy := q[0]*a/3, q[1]*a/3
return e.Padding(k.D(a+dy), k.D(a-dx), k.D(a-dy), k.D(a+dx)).Children(content)
}