Add Rect.AnchorTo()
This commit is contained in:
parent
2c79e7325a
commit
50e1d8c96b
12
geometry.go
12
geometry.go
|
@ -548,7 +548,7 @@ func (r Rect) Edges() [4]Line {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnchorPos enumerates available anchor positions, such as `Center`, `Top`, `TopRight`, etc.
|
// AnchorPos enumerates available anchor positions, such as `Center`, `Top`, `TopRight`, etc.
|
||||||
type AnchorPos struct{ v Vec }
|
type AnchorPos struct{ Val Vec }
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Center = AnchorPos{V(0.5, 0.5)}
|
Center = AnchorPos{V(0.5, 0.5)}
|
||||||
|
@ -590,8 +590,14 @@ func (anchorPos AnchorPos) String() string {
|
||||||
|
|
||||||
// AnchorPos returns the position of the given anchor of the Rect.
|
// AnchorPos returns the position of the given anchor of the Rect.
|
||||||
func (r *Rect) AnchorPos(anchor AnchorPos) Vec {
|
func (r *Rect) AnchorPos(anchor AnchorPos) Vec {
|
||||||
// func (u Vec) ScaledXY(v Vec) Vec {
|
return r.Min.Add(r.Size().ScaledXY(anchor.Val))
|
||||||
return r.Min.Add(r.Size().ScaledXY(anchor.v))
|
}
|
||||||
|
|
||||||
|
// AnchorTo updates the Rect position to align it on the given anchor.
|
||||||
|
func (r *Rect) AnchorTo(anchor AnchorPos) {
|
||||||
|
size := r.Size()
|
||||||
|
r.Min = r.Min.Sub(size.ScaledXY(anchor.Val))
|
||||||
|
r.Max = r.Max.Sub(size.ScaledXY(anchor.Val))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Center returns the position of the center of the Rect.
|
// Center returns the position of the center of the Rect.
|
||||||
|
|
Loading…
Reference in New Issue