kiten

package module
v0.0.0-...-74b2b6d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2022 License: BSL-1.0 Imports: 8 Imported by: 0

README

kiten

Simple and fast 2D graphics library for Go.

Examples

Create a new canvas

width := 1280
height := 720
canvas := kiten.NewCanvas(width, height, kiten.BlendAdd)

Create a new canvas from an RGBA image

(you can use this later to overlay or draw on images, for example)

canvas := kiten.CanvasFromImageRGBA(image, kiten.BlendAdd)

Real time example

example

Fill the whole canvas with a black color

If you're rendering multiple frames, remember to clear the canvas when drawing a new frame

canvas.Fill(color.RGBA{0, 0, 0, 255})

Draw a red line from 100,100 to 500,700

canvas.Line(100, 100, 500, 700, color.RGBA{255, 0, 0, 255})

Draw a blue circle at 150,150 with the radius of 70

canvas.Circle(150, 150, 70, color.RGBA{0, 0, 255, 255})

Draw a filled blue circle at 150,150 with the radius of 70

canvas.CircleFilled(150, 150, 70, color.RGBA{0, 0, 255, 255})

Draw a filled blue circle with a green outline at 150,150 with the radius of 70

canvas.CircleOutline(150, 150, 70, color.RGBA{0, 0, 255, 255}, color.RGBA{0, 255, 0, 255})

Set the pixel at 500,500 to white

This will do nothing if the coordinates are invalid

canvas.SetPixel(500, 500, color.RGBA{255, 255, 255, 255})

Get the pixel value at 170,300

This will return a transparent color if the coordinates are invalid

canvas.PixelAt(170, 300) // Returns color.RGBA

Draw a green rectangle from 100,100 to 200,200 (not filled)

canvas.Rect(100, 100, 200, 200, color.RGBA{0, 255, 0, 255})

Draw a green rectangle from 100,100 to 200,200 (filled)

canvas.RectFilled(100, 100, 200, 200, color.RGBA{0, 255, 0, 255})

Draw a canvas on top of another canvas at 450,300 and resize it to 100 by 150 pixels

canvas.PutCanvas(450, 300, 100, 150, canvas2)

Connect a path with white lines

import (
    "rand"
    "image"
)

// Generate a path
path := []image.Point{}
for i := 0; i < 100; i++ {
    path = append(path, image.Pt(i*30, canvas.Height/2-rand.Intn(100)))
}

// Draw it
canvas.DrawPath(path, color.RGBA{255, 255, 255, 255})

Draw text at 500,500

import "golang.org/x/image/font/inconsolata"
canvas.Text("Hello World!", 500, 500, inconsolata.Regular8x16, color.RGBA{255, 255, 255, 255})

Export canvas to PNG

import "os"
file, err := os.Create("output.png")
if err != nil {
    panic(err)
}
defer file.Close()

err := canvas.WritePNG(file)
if err != nil {
    panic(err)
}

Draw a white (not filled) triangle (500,500 -> 500,700 -> 600,700)

canvas.Triangle(500, 500, 500, 700, 600, 700, color.RGBA{255, 255, 255, 255})

Draw a white filled triangle (500,500 -> 500,700 -> 600,700)

canvas.TriangleFilled(500, 500, 500, 700, 600, 700, color.RGBA{255, 255, 255, 255})

Draw a white filled triangle with a red outline (500,500 -> 500,700 -> 600,700)

canvas.TriangleOutline(500, 500, 500, 700, 600, 700, color.RGBA{255, 255, 255, 255}, color.RGBA{255, 0, 0, 255})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deg2Rad

func Deg2Rad(degrees float64) float64

Convert degrees to radians

func Rad2Deg

func Rad2Deg(radians float64) float64

Convert radians to degrees

Types

type BlendType

type BlendType int
const (
	BlendAdd      BlendType = 0 // Add pixel values
	BlendMultiply BlendType = 1 // Multiply pixel values
	BlendNone     BlendType = 2 // Overwrite pixel values
)

type Canvas

type Canvas struct {
	Image     *image.RGBA
	Pixels    int
	Width     int
	Height    int
	BlendType BlendType
}

func CanvasFromImageRGBA

func CanvasFromImageRGBA(img *image.RGBA, blendType BlendType) *Canvas

Convert an image into a canvas

func NewCanvas

func NewCanvas(x, y int, blendType BlendType) *Canvas

Create a new canvas with width of x and height of y

func (*Canvas) Circle

func (canvas *Canvas) Circle(cx, cy, r int, color color.RGBA)

Draws a circle (not filled)

func (*Canvas) CircleFilled

func (canvas *Canvas) CircleFilled(cx, cy, r int, color color.RGBA)

Draws a filled circle

func (*Canvas) CircleOutline

func (canvas *Canvas) CircleOutline(cx, cy, r int, insideColor, outlineColor color.RGBA)

Draws a circle with an outline

func (*Canvas) DrawPath

func (canvas *Canvas) DrawPath(path []image.Point, color color.RGBA)

Connects a path of points with lines

func (*Canvas) Fill

func (canvas *Canvas) Fill(color color.RGBA)

Fills the canvas with a color

func (*Canvas) IsPointInCanvas

func (canvas *Canvas) IsPointInCanvas(x, y int) bool

Is the point inside of the canvas?

func (*Canvas) Line

func (canvas *Canvas) Line(x1, y1, x2, y2 int, color color.RGBA)

Draws a line

func (*Canvas) PixelAt

func (canvas *Canvas) PixelAt(x, y int) color.RGBA

Returns the color value at x and y

func (*Canvas) PutCanvas

func (canvas *Canvas) PutCanvas(x, y, w, h int, canvas2 *Canvas)

Draw a canvas on top of this canvas

func (*Canvas) Rect

func (canvas *Canvas) Rect(x1, y1, x2, y2 int, color color.RGBA)

Draws a rectangle (not filled)

func (*Canvas) RectFilled

func (canvas *Canvas) RectFilled(x1, y1, x2, y2 int, color color.RGBA)

Draws a filled rectangle

func (*Canvas) RotatePoint

func (canvas *Canvas) RotatePoint(x, y, degrees float64) (int, int)

Rotate a point in space by degrees

func (*Canvas) SetPixel

func (canvas *Canvas) SetPixel(x, y int, color color.RGBA)

Set a pixel on a canvas

func (*Canvas) Text

func (canvas *Canvas) Text(text string, x, y int, face *basicfont.Face, color color.RGBA)

func (*Canvas) Triangle

func (canvas *Canvas) Triangle(x1, y1, x2, y2, x3, y3 int, color color.RGBA)

Draws a triangle (not filled)

func (*Canvas) TriangleFilled

func (canvas *Canvas) TriangleFilled(x1, y1, x2, y2, x3, y3 int, color color.RGBA)

Draws a filled triangle

func (*Canvas) TriangleOutline

func (canvas *Canvas) TriangleOutline(x1, y1, x2, y2, x3, y3 int, fillColor, outlineColor color.RGBA)

Draws a triangle with an outline

func (*Canvas) WritePNG

func (canvas *Canvas) WritePNG(writer io.Writer) error

Export to PNG

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL