Add text components 021-040 with demos and behavior tests

This commit is contained in:
ZacharyZhang-NY committed 2026-10-11 18:20:12 -04:00
1 parent 43098065bf
commit 59fde4096d
30 files changed
+1702 -7

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
package text
import (
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ShakeTextOptions struct {
Slot pixel.Slot
Scale int
Seed int
}
// ShakeText jitters each character a dot, seeded, at 12 fps.
func ShakeText(c *ui.Context, s string, o ShakeTextOptions) *ui.Element {
k, sc := pixel.K(c), scale(o.Scale)
return block(c, k, s, pixel.Measure(s)*sc+2, pixel.LineHeight*sc+2, func(g pixel.Canvas) {
step := g.Step(12)
each(s, sc, func(i int, r rune, x int) {
dx, dy := shake(k.Reduce(), o.Seed, i, step)
g.Text(1+x+dx, 1+dy, string(r), o.Slot, sc)
})
})
}
// shake is a −1…1 offset, or none under Reduce Motion.
func shake(reduce bool, seed, i, step int) (dx, dy int) {
if reduce {
return 0, 0
}
n := noise(seed, i, step)
return int(n%3) - 1, int(n/3%3) - 1
}