sketch

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 15 Imported by: 0

README

PkgGoDev Build Status codecov

sketch

A simplified, opinionated wrapper around Ebiten Go's 2D game engine designed for creative coding and quick visual experiments. Instead of managing the full Ebiten game loop yourself, you implement a single Sketchable interface and call Run. The library handles window setup, frame scheduling, and coordinate-system transforms so you can focus on drawing.

Sketch gives you a Scene with a high-level drawing API: lines, rectangles, circles, arcs, polygons, all with both stroked and filled version and automatic transform support (translate, rotate, scale, push/pull). Keyboard and mouse input are exposed as simple package-level functions as well as through the Environment passed to each frame. A vector sub-package provides 2D Cartesian and polar vector types for convenient geometry math.

Getting started

Implementing your own Sketch

Embed sketch.Sketch into a struct and override whichever methods you need:

package main

import (
	"image/color"
    "log"

	"github.com/michalmitros/sketch"
	"github.com/michalmitros/sketch/vector"
)

type BouncingBall struct {
    // Embedding the Sketch struct makes the BouncingBall a noop Sketchable which can be added to Run.
    // It is not required, but makes implementation easier as it provides default noop implementations for required functions.
	sketch.Sketch

	pos      vector.Vector
	velocity vector.Vector
	radius   float64
}

// Setup is called once before first Update() call.
// It's the best way of initializing the sketch if its parameters require data from the Environment, like screen size.
func (b *BouncingBall) Setup(env *sketch.Environment) error {
	b.pos = env.ScreenSize().Scale(0.5) // center of the screen, screen width / 2 and screen height / 2
	b.velocity = vector.New(3, 2)
	b.radius = 30
	return nil
}

// Update is called every frame.
func (b *BouncingBall) Update(env *sketch.Environment) error {
	// move the ball
	b.pos = b.pos.Add(b.velocity)

    // check if the ball is out of bounds
    w, h := env.ScreenSize().Values() // Values() returns the vector's components for easy access
	if b.pos.X-b.radius < 0 || b.pos.X+b.radius > w {
		b.velocity.X *= -1
	}
	if b.pos.Y-b.radius < 0 || b.pos.Y+b.radius > h {
		b.velocity.Y *= -1
	}

    // terminate the sketch if the escape key is pressed,
    // can be also done by passing WithTerminationKeys(sketch.KeyEscape) to Run
	if env.IsKeyPressed(sketch.KeyEscape) {
		return sketch.Termination
	}
	return nil
}

// Draw is used each frame to render it.
func (b *BouncingBall) Draw(scene *sketch.Scene) {
    // fill red circle at b.pos with radius b.radius and stroke width 2
	scene.FillCircle(b.pos, b.radius, 2, color.RGBA{255, 0, 0, 255})
}

func main() {
    // run the BouncingBall sketch with 800x600 resolution, a black background and the title "Bouncing Ball"
	if err := sketch.Run(800, 600, new(BouncingBall),
		sketch.WithWindowTitle("Bouncing Ball"),
		sketch.WithBackgroundColor(color.Black),
	); err != nil {
		log.Fatal(err)
	}
}
Options

Run accepts optional configuration via functional options:

Option Default Description
WithWindowTitle(title) "" Sets the window title
WithBackgroundColor(c) [240,240,240,255] Background color
WithResizing(enable) disabled Allows the window to be resized
WithAntyaliasing(enable) enabled Toggles anti-aliased rendering
WithRunnableOnUnfocused(enable) enabled Keeps running when unfocused
WithTerminationKeys(keys...) none Keys that terminate the sketch

Input

Keyboard

All standard keys are available as Key constants:

sketch.IsKeyPressed(sketch.KeySpace)
Mouse

Three package-level functions cover mouse input:

sketch.IsMouseButtonPressed(sketch.MouseButtonLeft)    // bool
sketch.CursorPosition()                                // vector.Vector
sketch.Scroll()                                        // (dx, dy float64)

Available button constants: MouseButtonLeft, MouseButtonRight, MouseButtonMiddle, and MouseButton0-MouseButton4.

Utility
sketch.FPS()           // current frames per second
sketch.TPS()           // current ticks per second
sketch.Fullscreen(b)   // toggle fullscreen
sketch.IsFullscreen()  // check fullscreen state
sketch.MonitorSize()   // size (as vector.Vector) of the primary monitor

Environment

Environment is passed to both Setup and Update. It carries:

env.ScreenSize()           // vector.Vector - current width and height
env.DeltaTime()            // time.Duration - time since the last tick; 0 in Setup() and first Update() tick

When resizing of the window is enabled, always read dimensions from Environment instead of caching them from Setup.


Scene

Scene is the drawing surface passed to Draw. Every shape method accepts screen-space coordinates and a color.Color.

Transforms

Push/pull a transformation stack to isolate coordinate changes:

scene.Push()			  // push a new transformation layer to easily isolate changes
scene.Translate(v)       // move origin
scene.Rotate(angle)      // rotate axes (radians)
scene.Scale(rate)        // uniform scale
scene.ScaleX(rate)       // scale only X
scene.ScaleY(rate)       // scale only Y
// ... draw shapes ...
scene.Pull()			  // pop the transformation layer to restore previous layer
Shapes

Stroked methods take a strokeWidth and color.Color; filled methods take only color.Color:

scene.Line(v1, v2, strokeWidth, color)
scene.Rectangle(pos, w, h, strokeWidth, color)
scene.FillRectangle(pos, w, h, color)
scene.Circle(center, radius, strokeWidth, color)
scene.FillCircle(center, radius, color)
scene.Arc(center, radius, startAngle, endAngle, strokeWidth, color)
scene.FillArc(center, radius, startAngle, endAngle, color)
scene.Shape(points, close, strokeWidth, color)
scene.FillShape(points, close, color)

Plus:

scene.Clear()            // fill with background color
scene.Fill(c)            // fill with arbitrary color
scene.At(x, y)           // sample pixel color
scene.Width() / scene.Height() / scene.Size()

Image

Image is a drawable 2D image type. Load from files, create blank canvases, or draw images onto each other with transform support.

Loading & Creating
img, err := sketch.ImageFromFile("path/to/image.png")   // GIF, JPEG, PNG
img, err := sketch.ImageFromFS(fsys, "path/to/image.png")
img := sketch.ImageFromStdImage(stdImg)
img := sketch.NewBlankImage(dim)          // transparent
img := sketch.NewFilledImage(dim, color)  // filled with color
img := sketch.NewWhiteImage(dim)          // white
img := sketch.NewBlackImage(dim)          // black
Drawing

Draw an image onto another using DrawOptions:

opts := sketch.DefaultDrawOptions()
opts.Pos = vector.New(100, 100)
opts.Anchor = vector.New(0.5, 0.5)  // center
opts.Scale = vector.New(2, 2)
opts.Rotation = math.Pi / 4
opts.Tint = color.RGBA{255, 200, 200, 255}
opts.Opacity = 0.8
img.Draw(dst, &opts)

img.DrawAt(dst, pos)  // shorthand

Plus:

img.Width() / img.Height() / img.Size()
img.At(v) / img.Set(v, c)
img.Fill(c) / img.Clear()
img.Clone()
img.SubImage(pos, size)
img.CopyTo(dst, srcPos, size, dstPos)
Drawing on Scene
scene.DrawImage(img, pos, size)
Example
img, _ := sketch.ImageFromFile("sprite.png")
opts := sketch.DefaultDrawOptions()
opts.Pos = vector.New(200, 150)
opts.Anchor = vector.New(0.5, 0.5)
opts.Scale = vector.New(2, 2)
opts.Rotation = math.Pi / 4
img.Draw(canvas, &opts)

Vector & Polar

The vector sub-package provides two types for 2D geometry.

Vector (vector.Vector) - Cartesian coordinates:

v := vector.New(x, y)
zero := vector.Zero()
x, y := v.Values()
v.Add(other)   | v.Sub(other)   | v.Mul(other)
v.Scale(s)     | v.Mag()        | v.SetMag(m)
v.Dist(other)  | v.Rotate(rad)  | v.Angle()
v.Polar()      // conversion to Polar

Polar (vector.Polar) - polar coordinates (angle in radians):

p := vector.NewPolar(radius, angle)
r, a := p.Values()
p.Rotate(rad)  | p.Add(other)  | p.Dist(other)
p.Vector()     // conversion to Cartesian

Documentation

Overview

Package sketch is a simplified wrapper around Ebiten Go's 2D game engine designed for creative coding and quick visual experiments. Instead of managing the full Ebiten game loop yourself, you implement a single `Sketchable` interface and call `Run`.

Index

Constants

View Source
const (
	// Termination is returned when the sketch is terminated.
	Termination sketchErr = "sketch terminated"
	// ErrNilSketch is returned when the sketch is not provided.
	ErrNilSketch sketchErr = "sketch cannot be nil"
	// ErrInvalidScreenDimensions is returned when the screen dimensions are not positive.
	ErrInvalidScreenDimensions sketchErr = "screen dimensions must be positive"
	// ErrSketchAlreadyRunning is returned when the sketch is already running.
	ErrSketchAlreadyRunning sketchErr = "another sketch is already running"
)

Variables

This section is empty.

Functions

func CursorPosition

func CursorPosition() vector.Vector

CursorPosition returns the current position of the mouse cursor.

func FPS

func FPS() float64

FPS returns the current frames per second.

func Fullscreen

func Fullscreen(enable bool)

Fullscreen sets the sketch to fullscreen.

func IsFullscreen

func IsFullscreen() bool

IsFullscreen returns true if the sketch is currently in fullscreen.

func IsKeyPressed

func IsKeyPressed(k Key) bool

IsKeyPressed returns true if the given key is currently pressed.

func IsMouseButtonPressed

func IsMouseButtonPressed(k Key) bool

IsMouseButtonPressed returns true if the given mouse button is currently pressed.

func MonitorSize

func MonitorSize() vector.Vector

MonitorSize returns the size of the monitor.

func Run

func Run(
	screenWidth, screenHeight int,
	sketch Sketchable,
	opts ...Option,
) error

Run runs the sketch. It returns ErrNilSketch if sketch is nil. It returns ErrInvalidScreenDimensions if screenWidth or screenHeight is less than or equal to 0. It returns ErrSketchAlreadyRunning if another sketch is already running.

func Scroll

func Scroll() (dx, dy float64)

Scroll returns the current scroll amount of the mouse wheel.

func TPS

func TPS() float64

TPS returns the current ticks per second.

Types

type DrawOptions

type DrawOptions struct {
	// Pos is the position where the image is drawn (defaults to origin).
	Pos vector.Vector
	// Anchor is the anchor point within the image.
	// (0,0) means top-left, (0.5,0.5) means center, (1,1) means bottom-right.
	// Defaults to (0,0) – top-left.
	Anchor vector.Vector
	// Scale factors for X and Y axes. (1,1) = original size.
	// Negative values flip the image. Defaults to (1,1).
	Scale vector.Vector
	// Rotation in radians around the anchor point (counter-clockwise).
	// Defaults to 0.
	Rotation float64
	// Tint is an optional color multiplier. A value of white means no tint
	// (original colors preserved). nil means no tint.
	Tint color.Color
	// Opacity controls transparency: 0 = fully transparent, 1 = fully opaque.
	// Defaults to 1.
	Opacity float64
}

DrawOptions controls how an image is drawn. Zero values result in sensible defaults: position at (0,0), original scale, top-left anchor, no rotation, full opacity, no tint.

func DefaultDrawOptions

func DefaultDrawOptions() DrawOptions

DefaultDrawOptions returns draw options with sensible defaults: position at origin, original scale, top-left anchor, no rotation, full opacity, no tint.

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

Environment is the environment of the sketch used to provide information about the current state of the sketch to Update().

func (*Environment) DeltaTime

func (e *Environment) DeltaTime() time.Duration

DeltaTime returns time since the last tick Returns 0 in Setup() and first Update() tick.

func (*Environment) ScreenSize

func (e *Environment) ScreenSize() vector.Vector

ScreenSize returns the width and height of the screen.

type Image

type Image struct {
	// contains filtered or unexported fields
}

Image represents a drawable 2D image.

func ImageFromFS

func ImageFromFS(fs fs.FS, path string) (*Image, error)

ImageFromFS reads an image file from the given filesystem path and returns it as an *Image. Supports GIF, JPEG, PNG.

func ImageFromFile

func ImageFromFile(path string) (*Image, error)

ImageFromFile reads an image file from the given path and returns it as an *Image. Supports GIF, JPEG, PNG.

func ImageFromStdImage

func ImageFromStdImage(src image.Image) *Image

ImageFromStdImage reads an image from the given io.Reader and returns it as an *Image. Supports GIF, JPEG, PNG.

func NewBlackImage

func NewBlackImage(dim vector.Vector) *Image

NewBlackImage creates a new image with the given dimensions, filled with black. Width and height must be positive.

func NewBlankImage

func NewBlankImage(dim vector.Vector) *Image

NewBlankImage creates a new blank (transparent) image with the given dimensions. Width and height must be positive.

func NewFilledImage

func NewFilledImage(dim vector.Vector, c color.Color) *Image

NewFilledImage creates a new image with the given dimensions, filled with the given color. Width and height must be positive.

func NewWhiteImage

func NewWhiteImage(dim vector.Vector) *Image

NewWhiteImage creates a new image with the given dimensions, filled with white. Width and height must be positive.

func (*Image) Arc

func (i *Image) Arc(v vector.Vector, radius, startAngle, endAngle, strokeWidth float64, c color.Color)

Arc draws an arc at the given position with the given radius, start angle, end angle, stroke width and color.

func (*Image) At

func (i *Image) At(v vector.Vector) color.Color

At returns the color of the pixel at the given coordinates.

func (*Image) Circle

func (i *Image) Circle(v vector.Vector, radius, strokeWidth float64, c color.Color)

Circle draws a circle at the given position with the given radius, stroke width and color.

func (*Image) Clear

func (i *Image) Clear()

Clear clears the image to fully transparent black.

func (*Image) Clone

func (i *Image) Clone() *Image

Clone returns a new Image that is an independent pixel-by-pixel copy of this image. Changes to the clone do not affect the original.

func (*Image) CopyTo

func (i *Image) CopyTo(dst *Image, srcPos, size, dstPos vector.Vector)

CopyTo copies a rectangular region from this image onto another image. srcX, srcY specify the top-left corner of the source rectangle in this image. dstX, dstY specify where to place it on the destination image.

func (*Image) Draw

func (i *Image) Draw(dst *Image, opts *DrawOptions)

Draw draws the image onto the destination image using the given options.

func (*Image) DrawAt

func (i *Image) DrawAt(dst *Image, pos vector.Vector)

DrawAt draws the image at the given position of the destination image.

func (*Image) Fill

func (i *Image) Fill(c color.Color)

Fill fills the entire image with the given color.

func (*Image) FillArc

func (i *Image) FillArc(v vector.Vector, radius, startAngle, endAngle float64, c color.Color)

FillArc draws a filled arc (pie slice) at the given position with the given radius, start angle, end angle and color.

func (*Image) FillCircle

func (i *Image) FillCircle(v vector.Vector, radius, strokeWidth float64, c color.Color)

FillCircle draws a filled circle at the given position with the given radius, stroke width and color.

func (*Image) FillRectangle

func (i *Image) FillRectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)

FillRectangle draws a filled rectangle at the given position with the given width, height, stroke width and color.

func (*Image) FillShape

func (i *Image) FillShape(points []vector.Vector, close bool, c color.Color)

FillShape draws a filled polygon through the given points with the given color. If close is true, the shape is closed. If len(points) < 2, nothing is drawn. If len(points) == 2, a line is drawn.

func (*Image) Height

func (i *Image) Height() float64

Height returns the height of the image in pixels.

func (*Image) Line

func (i *Image) Line(v1, v2 vector.Vector, strokeWidth float64, c color.Color)

Line draws a line from v1 to v2 with the given stroke width and color.

func (*Image) Rectangle

func (i *Image) Rectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)

Rectangle draws a rectangle at the given position with the given width, height, stroke width and color.

func (*Image) Set

func (i *Image) Set(v vector.Vector, c color.Color)

Set sets the color of the pixel at the given coordinates.

func (*Image) Shape

func (i *Image) Shape(points []vector.Vector, close bool, strokeWidth float64, c color.Color)

Shape draws a polygon through the given points with the given stroke width and color. If close is true, the shape is closed. If len(points) < 2, nothing is drawn. If len(points) == 2, a line is drawn.

func (*Image) Size

func (i *Image) Size() vector.Vector

Size returns the dimensions of the image as a vector (width, height).

func (*Image) SubImage

func (i *Image) SubImage(pos, size vector.Vector) *Image

SubImage returns a new Image that represents a rectangular sub-region of the original image. The returned image shares the same underlying pixel data - modifying pixels in one affects the other. Use Clone() if you need an independent copy.

func (*Image) Width

func (i *Image) Width() float64

Width returns the width of the image in pixels.

type Key

type Key int

Key is a key on the keyboard.

const (
	KeyA              Key = Key(ebiten.KeyA)
	KeyB              Key = Key(ebiten.KeyB)
	KeyC              Key = Key(ebiten.KeyC)
	KeyD              Key = Key(ebiten.KeyD)
	KeyE              Key = Key(ebiten.KeyE)
	KeyF              Key = Key(ebiten.KeyF)
	KeyG              Key = Key(ebiten.KeyG)
	KeyH              Key = Key(ebiten.KeyH)
	KeyI              Key = Key(ebiten.KeyI)
	KeyJ              Key = Key(ebiten.KeyJ)
	KeyK              Key = Key(ebiten.KeyK)
	KeyL              Key = Key(ebiten.KeyL)
	KeyM              Key = Key(ebiten.KeyM)
	KeyN              Key = Key(ebiten.KeyN)
	KeyO              Key = Key(ebiten.KeyO)
	KeyP              Key = Key(ebiten.KeyP)
	KeyQ              Key = Key(ebiten.KeyQ)
	KeyR              Key = Key(ebiten.KeyR)
	KeyS              Key = Key(ebiten.KeyS)
	KeyT              Key = Key(ebiten.KeyT)
	KeyU              Key = Key(ebiten.KeyU)
	KeyV              Key = Key(ebiten.KeyV)
	KeyW              Key = Key(ebiten.KeyW)
	KeyX              Key = Key(ebiten.KeyX)
	KeyY              Key = Key(ebiten.KeyY)
	KeyZ              Key = Key(ebiten.KeyZ)
	KeyAltLeft        Key = Key(ebiten.KeyAltLeft)
	KeyAltRight       Key = Key(ebiten.KeyAltRight)
	KeyArrowDown      Key = Key(ebiten.KeyArrowDown)
	KeyArrowLeft      Key = Key(ebiten.KeyArrowLeft)
	KeyArrowRight     Key = Key(ebiten.KeyArrowRight)
	KeyArrowUp        Key = Key(ebiten.KeyArrowUp)
	KeyBackquote      Key = Key(ebiten.KeyBackquote)
	KeyBackslash      Key = Key(ebiten.KeyBackslash)
	KeyBackspace      Key = Key(ebiten.KeyBackspace)
	KeyBracketLeft    Key = Key(ebiten.KeyBracketLeft)
	KeyBracketRight   Key = Key(ebiten.KeyBracketRight)
	KeyCapsLock       Key = Key(ebiten.KeyCapsLock)
	KeyComma          Key = Key(ebiten.KeyComma)
	KeyContextMenu    Key = Key(ebiten.KeyContextMenu)
	KeyControlLeft    Key = Key(ebiten.KeyControlLeft)
	KeyControlRight   Key = Key(ebiten.KeyControlRight)
	KeyDelete         Key = Key(ebiten.KeyDelete)
	KeyDigit0         Key = Key(ebiten.KeyDigit0)
	KeyDigit1         Key = Key(ebiten.KeyDigit1)
	KeyDigit2         Key = Key(ebiten.KeyDigit2)
	KeyDigit3         Key = Key(ebiten.KeyDigit3)
	KeyDigit4         Key = Key(ebiten.KeyDigit4)
	KeyDigit5         Key = Key(ebiten.KeyDigit5)
	KeyDigit6         Key = Key(ebiten.KeyDigit6)
	KeyDigit7         Key = Key(ebiten.KeyDigit7)
	KeyDigit8         Key = Key(ebiten.KeyDigit8)
	KeyDigit9         Key = Key(ebiten.KeyDigit9)
	KeyEnd            Key = Key(ebiten.KeyEnd)
	KeyEnter          Key = Key(ebiten.KeyEnter)
	KeyEqual          Key = Key(ebiten.KeyEqual)
	KeyEscape         Key = Key(ebiten.KeyEscape)
	KeyF1             Key = Key(ebiten.KeyF1)
	KeyF2             Key = Key(ebiten.KeyF2)
	KeyF3             Key = Key(ebiten.KeyF3)
	KeyF4             Key = Key(ebiten.KeyF4)
	KeyF5             Key = Key(ebiten.KeyF5)
	KeyF6             Key = Key(ebiten.KeyF6)
	KeyF7             Key = Key(ebiten.KeyF7)
	KeyF8             Key = Key(ebiten.KeyF8)
	KeyF9             Key = Key(ebiten.KeyF9)
	KeyF10            Key = Key(ebiten.KeyF10)
	KeyF11            Key = Key(ebiten.KeyF11)
	KeyF12            Key = Key(ebiten.KeyF12)
	KeyF13            Key = Key(ebiten.KeyF13)
	KeyF14            Key = Key(ebiten.KeyF14)
	KeyF15            Key = Key(ebiten.KeyF15)
	KeyF16            Key = Key(ebiten.KeyF16)
	KeyF17            Key = Key(ebiten.KeyF17)
	KeyF18            Key = Key(ebiten.KeyF18)
	KeyF19            Key = Key(ebiten.KeyF19)
	KeyF20            Key = Key(ebiten.KeyF20)
	KeyF21            Key = Key(ebiten.KeyF21)
	KeyF22            Key = Key(ebiten.KeyF22)
	KeyF23            Key = Key(ebiten.KeyF23)
	KeyF24            Key = Key(ebiten.KeyF24)
	KeyHome           Key = Key(ebiten.KeyHome)
	KeyInsert         Key = Key(ebiten.KeyInsert)
	KeyIntlBackslash  Key = Key(ebiten.KeyIntlBackslash)
	KeyMetaLeft       Key = Key(ebiten.KeyMetaLeft)
	KeyMetaRight      Key = Key(ebiten.KeyMetaRight)
	KeyMinus          Key = Key(ebiten.KeyMinus)
	KeyNumLock        Key = Key(ebiten.KeyNumLock)
	KeyNumpad0        Key = Key(ebiten.KeyNumpad0)
	KeyNumpad1        Key = Key(ebiten.KeyNumpad1)
	KeyNumpad2        Key = Key(ebiten.KeyNumpad2)
	KeyNumpad3        Key = Key(ebiten.KeyNumpad3)
	KeyNumpad4        Key = Key(ebiten.KeyNumpad4)
	KeyNumpad5        Key = Key(ebiten.KeyNumpad5)
	KeyNumpad6        Key = Key(ebiten.KeyNumpad6)
	KeyNumpad7        Key = Key(ebiten.KeyNumpad7)
	KeyNumpad8        Key = Key(ebiten.KeyNumpad8)
	KeyNumpad9        Key = Key(ebiten.KeyNumpad9)
	KeyNumpadAdd      Key = Key(ebiten.KeyNumpadAdd)
	KeyNumpadDecimal  Key = Key(ebiten.KeyNumpadDecimal)
	KeyNumpadDivide   Key = Key(ebiten.KeyNumpadDivide)
	KeyNumpadEnter    Key = Key(ebiten.KeyNumpadEnter)
	KeyNumpadEqual    Key = Key(ebiten.KeyNumpadEqual)
	KeyNumpadMultiply Key = Key(ebiten.KeyNumpadMultiply)
	KeyNumpadSubtract Key = Key(ebiten.KeyNumpadSubtract)
	KeyPageDown       Key = Key(ebiten.KeyPageDown)
	KeyPageUp         Key = Key(ebiten.KeyPageUp)
	KeyPause          Key = Key(ebiten.KeyPause)
	KeyPeriod         Key = Key(ebiten.KeyPeriod)
	KeyPrintScreen    Key = Key(ebiten.KeyPrintScreen)
	KeyQuote          Key = Key(ebiten.KeyQuote)
	KeyScrollLock     Key = Key(ebiten.KeyScrollLock)
	KeySemicolon      Key = Key(ebiten.KeySemicolon)
	KeyShiftLeft      Key = Key(ebiten.KeyShiftLeft)
	KeyShiftRight     Key = Key(ebiten.KeyShiftRight)
	KeySlash          Key = Key(ebiten.KeySlash)
	KeySpace          Key = Key(ebiten.KeySpace)
	KeyTab            Key = Key(ebiten.KeyTab)
	KeyAlt            Key = Key(ebiten.KeyAlt)
	KeyControl        Key = Key(ebiten.KeyControl)
	KeyShift          Key = Key(ebiten.KeyShift)
)

Keyboard keys.

type MouseButton

type MouseButton int

MouseButton is a mouse button.

type Option

type Option func(sketchBuildParams)

Option is a function that configures a sketch.

func WithAntyaliasing

func WithAntyaliasing(enable bool) Option

WithAntyaliasing enables or disables antialiasing (enabled by default).

func WithBackgroundColor

func WithBackgroundColor(c color.Color) Option

WithBackgroundColor sets the background color of the sketch ([240, 240, 240, 255] by default).

func WithResizing

func WithResizing(enable bool) Option

WithResizing enables or disables resizing (enabled by default).

func WithRunnableOnUnfocused

func WithRunnableOnUnfocused(enable bool) Option

WithRunnableOnUnfocused enables or disables the game to be runnable on unfocused (enabled by default).

func WithTerminationKeys

func WithTerminationKeys(k ...Key) Option

WithTerminationKeys sets the keys which will terminate the game.

func WithWindowTitle

func WithWindowTitle(title string) Option

WithWindowTitle sets the window title of the sketch (no title by default).

type Scene

type Scene struct {
	// contains filtered or unexported fields
}

Scene is the scene used to draw to.

func (*Scene) Arc

func (s *Scene) Arc(v vector.Vector, radius, startAngle, endAngle, strokeWidth float64, c color.Color)

Arc draws an arc at the given position with the given radius, start angle, end angle, stroke width and color.

func (*Scene) At

func (s *Scene) At(v vector.Vector) color.Color

At returns the color at the given position.

func (*Scene) Circle

func (s *Scene) Circle(v vector.Vector, radius, strokeWidth float64, c color.Color)

Circle draws a circle at the given position with the given radius, stroke width and color.

func (*Scene) Clear

func (s *Scene) Clear()

Clear clears the scene.

func (*Scene) DrawImage

func (s *Scene) DrawImage(img *Image, pos, size vector.Vector)

DrawImage draws an image onto the scene at the given position, scaled to the given size. The image is drawn with respect to the current transformation stack (scale, rotate, translate). Passing a nil image is a no-op.

func (*Scene) Fill

func (s *Scene) Fill(c color.Color)

Fill fills the scene with the given color.

func (*Scene) FillArc

func (s *Scene) FillArc(v vector.Vector, radius, startAngle, endAngle float64, c color.Color)

FillArc draws a filled arc (pie slice) at the given position with the given radius, start angle, end angle and color.

func (*Scene) FillCircle

func (s *Scene) FillCircle(v vector.Vector, radius, strokeWidth float64, c color.Color)

FillCircle draws a filled circle at the given position with the given radius, stroke width and color.

func (*Scene) FillRectangle

func (s *Scene) FillRectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)

FillRectangle draws a filled rectangle at the given position with the given width, height, stroke width and color.

func (*Scene) FillShape

func (s *Scene) FillShape(points []vector.Vector, close bool, c color.Color)

FillShape draws a filled polygon through the given points with the given color. If close is true, the shape is closed. If len(points) < 2, nothing is drawn. If len(points) == 2, a line is drawn.

func (*Scene) Height

func (s *Scene) Height() float64

Height returns the height of the scene.

func (*Scene) Line

func (s *Scene) Line(v1, v2 vector.Vector, strokeWidth float64, c color.Color)

Line draws a line from v1 to v2 with the given stroke width and color.

func (*Scene) Pull

func (s *Scene) Pull()

Pull removes the most recently pushed transformation layer. Pulling the initial layer has no effect.

func (*Scene) Push

func (s *Scene) Push()

Push adds an identity transformation layer to the stack.

func (*Scene) Rectangle

func (s *Scene) Rectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)

Rectangle draws a rectangle at the given position with the given width, height, stroke width and color.

func (*Scene) Rotate

func (s *Scene) Rotate(angle float64)

Rotate rotates the coordinate system by angle radians.

func (*Scene) Scale

func (s *Scene) Scale(rate float64)

Scale scales both coordinate axes by rate.

func (*Scene) ScaleX

func (s *Scene) ScaleX(rate float64)

ScaleX scales the X coordinate axis by rate.

func (*Scene) ScaleY

func (s *Scene) ScaleY(rate float64)

ScaleY scales the Y coordinate axis by rate.

func (*Scene) Shape

func (s *Scene) Shape(points []vector.Vector, close bool, strokeWidth float64, c color.Color)

Shape draws a polygon through the given points with the given stroke width and color. If close is true, the shape is closed. If len(points) < 2, nothing is drawn. If len(points) == 2, a line is drawn.

func (*Scene) Size

func (s *Scene) Size() vector.Vector

Size returns the width and height of the scene.

func (*Scene) Transform

func (s *Scene) Transform(v vector.Vector)

Transform moves the origin of the coordinate system by v.

func (*Scene) TransformX

func (s *Scene) TransformX(dx float64)

TransformX moves the origin of the coordinate system along the X axis.

func (*Scene) TransformY

func (s *Scene) TransformY(dy float64)

TransformY moves the origin of the coordinate system along the Y axis.

func (*Scene) Width

func (s *Scene) Width() float64

Width returns the width of the scene.

type Sketch

type Sketch struct{}

Sketch is a noop Sketchable which can be embedded and added to Sketch.

func (*Sketch) Draw

func (s *Sketch) Draw(scene *Scene)

Draw is used each frame to render it.

func (*Sketch) Setup

func (s *Sketch) Setup(env *Environment) error

Setup is called once before first Update() call.

func (*Sketch) Update

func (s *Sketch) Update(env *Environment) error

Update is called every frame.

type Sketchable

type Sketchable interface {
	// Update is called every frame.
	Update(*Environment) error
	// Draw is used each frame to render it.
	Draw(*Scene)
	// Setup is called once before first Update() call.
	Setup(env *Environment) error
}

Sketchable is a single sketchable object.

Directories

Path Synopsis
Package vector provides a simple 2D vector type.
Package vector provides a simple 2D vector type.

Jump to

Keyboard shortcuts

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