Documentation ¶
Overview ¶
Package gog is a Go Drawing Library
Example ¶
150-frame rotating cubic bezier APNG animation
package main import ( "fmt" "image/color" "github.com/setanarut/gog" ) func main() { ctx := gog.New(250, 250) curve := gog.CubicBezier(100, 95, 50, 300, 190, 88, 140, 200) curve.SetPos(ctx.Center) for i := 0; i < 150; i++ { ctx.Clear(color.Gray{30}) curve.Rotate((gog.Pi * 2) / 150) ctx.DebugDraw(curve) ctx.AppendAnimationFrame() } // ctx.SaveAPNG("anim.png", 3) fmt.Println(len(ctx.AnimationFrames)) }
Output: 150
Index ¶
- Variables
- func Linspace(start, stop float64, num int) (res []float64)
- func New(width, height int) *context
- func Radians(degree float64) float64
- func TangentAngle(start, end Point) float64
- type CapMode
- type DrawMode
- type JoinMode
- type Path
- func BBox(min, max Point) *Path
- func Circle(origin Point, radius float64) *Path
- func CubicBezier(x0, y0, x1, y1, x2, y2, x3, y3 float64) *Path
- func Ellipse(origin Point, xRadius, yRadius float64) *Path
- func Line(start, end Point) *Path
- func NewPath(points []Point) *Path
- func Rect(topLeft Point, w, h float64) *Path
- func RegularPolygon(origin Point, n int, radius float64) *Path
- func Square(topLeft Point, side float64) *Path
- func (p *Path) AppendPoints(points ...Point) *Path
- func (p *Path) Bounds() (Point, Point)
- func (p *Path) Centroid() Point
- func (p *Path) Clone() *Path
- func (p *Path) Close() *Path
- func (p *Path) DebugDraw(c *context) *Path
- func (p *Path) DeleteAtIndex(index int) *Path
- func (p *Path) DeleteEnd() *Path
- func (p *Path) End() Point
- func (p *Path) Fill(c *context) *Path
- func (p *Path) FillStroke(c *context) *Path
- func (p *Path) GetPoints() []Point
- func (p *Path) InsertAtIndex(pnt Point, index int) *Path
- func (p *Path) InsertAtLength(length float64)
- func (p *Path) InsertAtTime(t float64)
- func (p *Path) IsClosed() bool
- func (p Path) Len() int
- func (p Path) Length() float64
- func (p *Path) Open() *Path
- func (p *Path) Perpendicular(t float64, length float64) (p1 Point, p2 Point)
- func (p *Path) PointAngleAtLength(length float64) (Point, float64)
- func (p *Path) PointAngleAtTime(t float64) (Point, float64)
- func (p *Path) PointAtIndex(index int) Point
- func (p *Path) PrintPoints()
- func (p *Path) RemoveDoubles() *Path
- func (p *Path) ResetAnchor() *Path
- func (p *Path) Reverse() *Path
- func (p *Path) Rotate(angle float64) *Path
- func (p *Path) Rotated(angle float64) *Path
- func (p *Path) Scale(factor Point) *Path
- func (p *Path) SetAnchor(pt Point) *Path
- func (p *Path) SetFill(c color.Color) *Path
- func (p *Path) SetLineWidth(w float64) *Path
- func (p *Path) SetPos(position Point) *Path
- func (p *Path) SetStroke(c color.Color) *Path
- func (p *Path) SetStyle(s Style) *Path
- func (p *Path) Start() Point
- func (p *Path) Stroke(c *context) *Path
- func (p *Path) StrokeFill(c *context) *Path
- func (p *Path) Translate(x, y float64) *Path
- type Point
- func (p Point) Add(q Point) Point
- func (a Point) Distance(other Point) float64
- func (p Point) Div(k float64) Point
- func (a Point) Fixed() fixed.Point26_6
- func (a Point) Lerp(other Point, t float64) Point
- func (p Point) Mul(factor Point) Point
- func (a Point) Rotate(angle float64, o Point) Point
- func (p Point) Sub(q Point) Point
- type Style
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Black = color.Black
var Gray = color.Gray{128}
var Gray40 = color.Gray{40}
var Pi float64 = math.Pi
var White = color.White
Functions ¶
func TangentAngle ¶
TangentAngle return tangent angle of two points
Types ¶
type Path ¶
type Path struct { // Style holds the fill color, line color, thickness and DrawMode options. Style Style // Anchor point Anchor Point // contains filtered or unexported fields }
Path object
func CubicBezier ¶
CubicBezier returns a cubic-bezier Path.
func Line ¶
Line returns a line Path.
Example ¶
Creates new line and prints start and end point
package main import ( "fmt" "github.com/setanarut/gog" ) func main() { line := gog.Line(gog.Point{X: 0, Y: 0}, gog.Point{X: 25, Y: 80}) fmt.Println(line.Start(), line.End()) }
Output: {0 0} {25 80}
func RegularPolygon ¶
RegularPolygon returns regular polygon shaped Path.
func (*Path) AppendPoints ¶ added in v0.5.1
AppendPoints appends points to the end of the Path
func (*Path) DeleteAtIndex ¶ added in v0.5.1
DeleteAtIndex removes point from Path at index if the number of points is more than two.
func (*Path) DeleteEnd ¶ added in v0.5.1
DeleteEnd removes end point of the Path if the number of points is more than two.
func (*Path) FillStroke ¶
FillStroke fills then strokes path
func (*Path) InsertAtIndex ¶ added in v0.5.1
InsertAtIndex inserts point to the Path at index
Example ¶
Insert point to path points at index
package main import ( "github.com/setanarut/gog" ) func main() { line := gog.NewPath([]gog.Point{{0, 0}, {10, 10}}) line.InsertAtIndex(gog.Point{66, 66}, 1) line.PrintPoints() }
Output: [{0 0} {66 66} {10 10}]
func (*Path) InsertAtLength ¶ added in v0.5.1
InsertAtLength inserts point at length if coord is empty
Example ¶
package main import ( "github.com/setanarut/gog" ) func main() { line := gog.NewPath([]gog.Point{{0, 0}, {0, 10}, {0, 20}}) line.InsertAtLength(10.5) line.PrintPoints() }
Output: [{0 0} {0 10} {0 10.5} {0 20}]s
func (*Path) InsertAtTime ¶ added in v0.5.1
InsertAtTime inserts point at time
func (*Path) Perpendicular ¶
Returns Perpendicular line points at time t with length
func (*Path) PointAngleAtLength ¶ added in v0.5.1
PointAngleAtLength Returns point and tangent angle at length
Example ¶
Get point and tangent angle at length
package main import ( "fmt" "github.com/setanarut/gog" ) func main() { line := gog.NewPath([]gog.Point{{X: 0, Y: 0}, {X: 10, Y: 10}}) point, angle := line.PointAngleAtLength(line.Length() / 2) fmt.Println(point, angle) }
Output: {5 5} 0.7853981633974483
func (*Path) PointAngleAtTime ¶ added in v0.5.1
PointAngleAtTime Returns point and tangent angle at time t
Example ¶
Get point and tangent angle at time t
package main import ( "fmt" "github.com/setanarut/gog" ) func main() { line := gog.NewPath([]gog.Point{{X: 0, Y: 0}, {X: 10, Y: 10}}) point, angle := line.PointAngleAtTime(0.5) fmt.Println(point, angle) }
Output: {5 5} 0.7853981633974483
func (*Path) PointAtIndex ¶ added in v0.5.1
PointAtIndex returns point at index
func (*Path) PrintPoints ¶ added in v0.5.1
func (p *Path) PrintPoints()
PrintPoints prints path points to standard output.
func (*Path) RemoveDoubles ¶ added in v0.5.1
RemoveDoubles removes double points
Example ¶
package main import ( "fmt" "github.com/setanarut/gog" ) func main() { path := gog.NewPath([]gog.Point{{0, 0}, {77, 77}, {77, 77}, {0, 0}, {0, 0}}) path.RemoveDoubles() fmt.Println(path.GetPoints()) }
Output: [{0 0} {77 77} {0 0}]
func (*Path) ResetAnchor ¶
ResetAnchor sets anchor point to centroid
func (*Path) Reverse ¶
Reverse reverses Path. The starting point becomes the end and the end becomes the beginning.
func (*Path) SetAnchor ¶
SetAnchor Sets Path's anchor point
Example ¶
package main import ( "fmt" "github.com/setanarut/gog" ) func main() { line := gog.NewPath([]gog.Point{{X: 0, Y: 0}, {X: 10, Y: 10}}) fmt.Println(line.Anchor) // Centroid of Path line.SetAnchor(gog.Point{X: 3, Y: 3}) fmt.Println(line.Anchor) line.ResetAnchor() fmt.Println(line.Anchor) fmt.Println(line.Centroid() == line.Anchor) }
Output: {5 5} {3 3} {5 5} true
func (*Path) SetLineWidth ¶
SetLineWidth sets line thickness of Path.
func (*Path) SetPos ¶
SetPos Aligns the Path with the anchor point to the desired point. In other words, it sets the position.
func (*Path) StrokeFill ¶
StrokeFill stroke then fill path
type Point ¶
type Point struct {
X, Y float64
}
A Point is an X, Y coordinate pair. The axes increase right and down.
type Style ¶
type Style struct { Fill color.Color Stroke color.Color LineWidth float64 // Line cap style constant // // 0=ButtCap 1=SquareCap 2=RoundCap 3=CubicCap 4=QuadraticCap Cap CapMode // Line join style // // 0=MiterJoin 1=RoundJoin 2=BevelJoin Join JoinMode }
Style of path
Example ¶
package main import ( "fmt" "image/color" "github.com/setanarut/gog" ) func main() { myStyle := gog.NewStyle(color.RGBA{255, 0, 0, 255}, color.Gray{128}, 10, gog.RoundCap, gog.RoundJoin) myStyle2 := gog.Style{ Fill: color.RGBA{255, 255, 0, 255}, Stroke: color.RGBA{255, 0, 255, 255}, LineWidth: 7, Cap: gog.CubicCap, Join: gog.BevelJoin, } square := gog.Square(gog.P(10, 10), 50).SetStyle(myStyle) square2 := gog.Square(gog.P(10, 10), 50) square2.Style = myStyle2 square2.SetFill(color.RGBA{0, 255, 255, 255}) fmt.Printf("%+v\n%+v", square.Style, square2.Style) }
Output: {Fill:{R:255 G:0 B:0 A:255} Stroke:{Y:128} LineWidth:10 Cap:2 Join:1} {Fill:{R:0 G:255 B:255 A:255} Stroke:{R:255 G:0 B:255 A:255} LineWidth:7 Cap:3 Join:2}