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
- func CursorPosition() vector.Vector
- func FPS() float64
- func Fullscreen(enable bool)
- func IsFullscreen() bool
- func IsKeyPressed(k Key) bool
- func IsMouseButtonPressed(k Key) bool
- func MonitorSize() vector.Vector
- func Run(screenWidth, screenHeight int, sketch Sketchable, opts ...Option) error
- func Scroll() (dx, dy float64)
- func TPS() float64
- type DrawOptions
- type Environment
- type Image
- func ImageFromFS(fs fs.FS, path string) (*Image, error)
- func ImageFromFile(path string) (*Image, error)
- func ImageFromStdImage(src image.Image) *Image
- func NewBlackImage(dim vector.Vector) *Image
- func NewBlankImage(dim vector.Vector) *Image
- func NewFilledImage(dim vector.Vector, c color.Color) *Image
- func NewWhiteImage(dim vector.Vector) *Image
- func (i *Image) Arc(v vector.Vector, radius, startAngle, endAngle, strokeWidth float64, ...)
- func (i *Image) At(v vector.Vector) color.Color
- func (i *Image) Circle(v vector.Vector, radius, strokeWidth float64, c color.Color)
- func (i *Image) Clear()
- func (i *Image) Clone() *Image
- func (i *Image) CopyTo(dst *Image, srcPos, size, dstPos vector.Vector)
- func (i *Image) Draw(dst *Image, opts *DrawOptions)
- func (i *Image) DrawAt(dst *Image, pos vector.Vector)
- func (i *Image) Fill(c color.Color)
- func (i *Image) FillArc(v vector.Vector, radius, startAngle, endAngle float64, c color.Color)
- func (i *Image) FillCircle(v vector.Vector, radius, strokeWidth float64, c color.Color)
- func (i *Image) FillRectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)
- func (i *Image) FillShape(points []vector.Vector, close bool, c color.Color)
- func (i *Image) Height() float64
- func (i *Image) Line(v1, v2 vector.Vector, strokeWidth float64, c color.Color)
- func (i *Image) Rectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)
- func (i *Image) Set(v vector.Vector, c color.Color)
- func (i *Image) Shape(points []vector.Vector, close bool, strokeWidth float64, c color.Color)
- func (i *Image) Size() vector.Vector
- func (i *Image) SubImage(pos, size vector.Vector) *Image
- func (i *Image) Width() float64
- type Key
- type MouseButton
- type Option
- type Scene
- func (s *Scene) Arc(v vector.Vector, radius, startAngle, endAngle, strokeWidth float64, ...)
- func (s *Scene) At(v vector.Vector) color.Color
- func (s *Scene) Circle(v vector.Vector, radius, strokeWidth float64, c color.Color)
- func (s *Scene) Clear()
- func (s *Scene) DrawImage(img *Image, pos, size vector.Vector)
- func (s *Scene) Fill(c color.Color)
- func (s *Scene) FillArc(v vector.Vector, radius, startAngle, endAngle float64, c color.Color)
- func (s *Scene) FillCircle(v vector.Vector, radius, strokeWidth float64, c color.Color)
- func (s *Scene) FillRectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)
- func (s *Scene) FillShape(points []vector.Vector, close bool, c color.Color)
- func (s *Scene) Height() float64
- func (s *Scene) Line(v1, v2 vector.Vector, strokeWidth float64, c color.Color)
- func (s *Scene) Pull()
- func (s *Scene) Push()
- func (s *Scene) Rectangle(v vector.Vector, width, height, strokeWidth float64, c color.Color)
- func (s *Scene) Rotate(angle float64)
- func (s *Scene) Scale(rate float64)
- func (s *Scene) ScaleX(rate float64)
- func (s *Scene) ScaleY(rate float64)
- func (s *Scene) Shape(points []vector.Vector, close bool, strokeWidth float64, c color.Color)
- func (s *Scene) Size() vector.Vector
- func (s *Scene) Transform(v vector.Vector)
- func (s *Scene) TransformX(dx float64)
- func (s *Scene) TransformY(dy float64)
- func (s *Scene) Width() float64
- type Sketch
- type Sketchable
Constants ¶
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 ¶
CursorPosition returns the current position of the mouse cursor.
func IsFullscreen ¶
func IsFullscreen() bool
IsFullscreen returns true if the sketch is currently in fullscreen.
func IsKeyPressed ¶
IsKeyPressed returns true if the given key is currently pressed.
func IsMouseButtonPressed ¶
IsMouseButtonPressed returns true if the given mouse button is currently pressed.
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.
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 ¶
ImageFromFS reads an image file from the given filesystem path and returns it as an *Image. Supports GIF, JPEG, PNG.
func ImageFromFile ¶
ImageFromFile reads an image file from the given path and returns it as an *Image. Supports GIF, JPEG, PNG.
func ImageFromStdImage ¶
ImageFromStdImage reads an image from the given io.Reader and returns it as an *Image. Supports GIF, JPEG, PNG.
func NewBlackImage ¶
NewBlackImage creates a new image with the given dimensions, filled with black. Width and height must be positive.
func NewBlankImage ¶
NewBlankImage creates a new blank (transparent) image with the given dimensions. Width and height must be positive.
func NewFilledImage ¶
NewFilledImage creates a new image with the given dimensions, filled with the given color. Width and height must be positive.
func NewWhiteImage ¶
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) Circle ¶
Circle draws a circle at the given position with the given radius, stroke width and color.
func (*Image) Clone ¶
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 ¶
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) FillArc ¶
FillArc draws a filled arc (pie slice) at the given position with the given radius, start angle, end angle and color.
func (*Image) FillCircle ¶
FillCircle draws a filled circle at the given position with the given radius, stroke width and color.
func (*Image) FillRectangle ¶
FillRectangle draws a filled rectangle at the given position with the given width, height, stroke width and color.
func (*Image) FillShape ¶
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) Rectangle ¶
Rectangle draws a rectangle at the given position with the given width, height, stroke width and color.
func (*Image) Shape ¶
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.
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.
const ( MouseButtonLeft MouseButton = MouseButton(ebiten.MouseButtonLeft) MouseButtonRight MouseButton = MouseButton(ebiten.MouseButtonRight) MouseButtonMiddle MouseButton = MouseButton(ebiten.MouseButtonMiddle) MouseButton0 MouseButton = MouseButton(ebiten.MouseButton0) MouseButton1 MouseButton = MouseButton(ebiten.MouseButton1) MouseButton2 MouseButton = MouseButton(ebiten.MouseButton2) MouseButton3 MouseButton = MouseButton(ebiten.MouseButton3) MouseButton4 MouseButton = MouseButton(ebiten.MouseButton4) )
Mouse buttons.
type Option ¶
type Option func(sketchBuildParams)
Option is a function that configures a sketch.
func WithAntyaliasing ¶
WithAntyaliasing enables or disables antialiasing (enabled by default).
func WithBackgroundColor ¶
WithBackgroundColor sets the background color of the sketch ([240, 240, 240, 255] by default).
func WithResizing ¶
WithResizing enables or disables resizing (enabled by default).
func WithRunnableOnUnfocused ¶
WithRunnableOnUnfocused enables or disables the game to be runnable on unfocused (enabled by default).
func WithTerminationKeys ¶
WithTerminationKeys sets the keys which will terminate the game.
func WithWindowTitle ¶
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) Circle ¶
Circle draws a circle at the given position with the given radius, stroke width and color.
func (*Scene) DrawImage ¶
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) FillArc ¶
FillArc draws a filled arc (pie slice) at the given position with the given radius, start angle, end angle and color.
func (*Scene) FillCircle ¶
FillCircle draws a filled circle at the given position with the given radius, stroke width and color.
func (*Scene) FillRectangle ¶
FillRectangle draws a filled rectangle at the given position with the given width, height, stroke width and color.
func (*Scene) FillShape ¶
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) 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 ¶
Rectangle draws a rectangle at the given position with the given width, height, stroke width and color.
func (*Scene) Shape ¶
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) TransformX ¶
TransformX moves the origin of the coordinate system along the X axis.
func (*Scene) TransformY ¶
TransformY moves the origin of the coordinate system along the Y axis.
type Sketch ¶
type Sketch struct{}
Sketch is a noop Sketchable which can be embedded and added to Sketch.
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.