/gena

Generative art in go, finally useful application for native complex numbers

Primary LanguageGo

gena

Go Go Report Card

gena is a Go package to create generative art.

How to use

Hello, Circle!

See example code in cmd/circle.go.

Creating Contexts

There are a few ways of creating a context.

NewContext(size V2) *Context
NewContextFromRGBA(im *image.RGBA) *Context

Drawing Functions

DrawPoint(pos V2, r float64)
DrawLine(from, to V2)
DrawRectangle(topLeft, size V2)
DrawRoundedRectangle(topLeft, size V2, r float64)
DrawCircle(center V2, r float64)
DrawArc(center V2, r, angle1, angle2 float64)
DrawEllipse(center, r V2)
DrawEllipticalArc(center, r V2, angle1, angle2 float64)
DrawRegularPolygon(n int, center V2, r, rotation float64)
DrawImage(im image.Image, pos V2)
DrawImageAnchored(im image.Image, pos, a V2)
SetPixel(pos V2)

MoveTo(pos V2)
LineTo(pos V2)
QuadraticTo(v1, v2 V2)
CubicTo(v1, v2, v3 V2)
ClearPath()
NewSubPath()
ClosePath()

Clear()
Stroke()
Fill()
StrokePreserve()
FillPreserve()

It is often desired to center an image at a point. Use DrawImageAnchored with a set to 0.5 to do this. Use 0 to left or top align. Use 1 to right or bottom align. DrawStringAnchored does the same for text, so you don't need to call MeasureString yourself.

Text Functions

It will even do word wrap for you!

DrawString(s string, v V2)
DrawStringAnchored(s string, v, a V2)
DrawStringWrapped(s string, v, a V2, width, lineSpacing float64, align Align)
MeasureString(s string) V2
MeasureMultilineString(s string, lineSpacing float64) V2
WordWrap(s string, w float64) []string
SetFontFace(fontFace font.Face)
LoadFontFace(path string, points float64) error

Color Functions

ColorRGB(r, g, b float64) color.Color
ColorRGBA(r, g, b, a float64) color.Color
ColorRGB255(r, g, b int) color.Color
ColorRGBA255(r, g, b, a int) color.Color
ColorHex(x string) color.Color

Stroke & Fill Options

SetLineWidth(lineWidth float64)
SetLineCap(lineCap LineCap)
SetLineJoin(lineJoin LineJoin)
SetDash(dashes ...float64)
SetDashOffset(offset float64)
SetFillRule(fillRule FillRule)

Gradients & Patterns

Linear, radial and conic gradients are supported. You can also use surface patterns or implement your own.

// apply patterns
SetFillStyle(pattern Pattern)
SetStrokeStyle(pattern Pattern)

// builtin patterns
PatternSolid(color color.Color) Pattern
PatternGradientLinear(v0, v1 V2) Pattern
PatternGradientRadial(v0 V2, r0 float64, v1 V2, r1 float64) Pattern
PatternGradientConic(c V2, deg float64) Pattern
PatternPatternSurface(im image.Image, op RepeatOp) Pattern

Transformation Functions

Identity()
Translate(v V2)
Scale(v V2)
Rotate(angle float64)
Shear(v V2)
ScaleAbout(s, v V2)
RotateAbout(angle float64, v V2)
ShearAbout(s, v V2)
TransformPoint(v V2) V2
InvertY()

It is often desired to rotate or scale about a point that is not the origin. The functions RotateAbout, ScaleAbout, ShearAbout are provided as a convenience.

InvertY is provided in case Y should increase from bottom to top. The default is top to bottom.

Clipping Functions

Use clipping regions to restrict drawing operations to an area that you defined using paths.

Clip()
ClipPreserve()
ResetClip()
AsMask() *image.Alpha
SetMask(mask *image.Alpha)
InvertMask()

Helper Functions

Radians(degrees float64) float64
Degrees(radians float64) float64
LoadImage(path string) image.Image
SavePNG(path string, im image.Image)
SaveJPG(path string, im image.Image, quality uint8)

Examples

Black Hole

Circle Grid

Circle Line

Circle Loop

Circle Loop2

Circle Move

Circle Noise

Color Canva

Color Circle

Color Circle2

Contour Line

Domain Warp

Source: https://iquilezles.org/articles/warp/

Dot Line

Dots Wave

Gradient Conic

Gradient Linear

Gradient Radial

Noise Line

Janus

Julia Set

Maze

Ocean Fish

Source: https://openprocessing.org/sketch/1102157

Perlin Perls

Pixel Hole

Point Ribbon

Random Circle

Random Shapes

Silk Sky

Silk Smoke

Spiral Square

Square Grid

Stars

Swirl

Yarn