kamera

package module
v2.98.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: CC0-1.0 Imports: 5 Imported by: 8

README

GoDoc

Kamera

Camera package for Ebitengine.

import "github.com/setanarut/kamera/v2"

https://github.com/user-attachments/assets/58787fbe-5788-43d4-9560-482dcc965efb

Features

  • Camera shake effect with fastnoise library noise types.
  • Smooth camera movement with three interpolation modes:
    • None: Direct camera movement without smoothing
    • Lerp: Linear interpolation for smooth transitions
    • SmoothDamp: Spring-like motion with acceleration and deceleration and maximum speed.
  • Rotate/Zoom

Usage

A pseudo code:

func (g *Game) Update() error {
  MainCamera.LookAt(playerCenter.X, playerCenter.Y)
  return nil
 }

func (g *Game) Draw(screen *ebiten.Image) {
  playerDrawImageOptions.GeoM.Reset() // GeoM must be reset
  playerDrawImageOptions.GeoM.Translate(target.X, target.Y) // Move player
  MainCamera.Draw(playerImage, playerDrawImageOptions, screen)
 }

Examples

  1. Clone this repository
  2. In the terminal, change directory to the examples folder cd examples
  3. Run a demo with go run ./folder_name (go run ./platformer, go run ./director).

Documentation

Overview

Package kamera provides a camera object for Ebitengine v2.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Camera

type Camera struct {
	// Top-left X position of camera
	X float64
	// Top-left Y position of camera
	Y float64
	// Width is camera's width
	Width float64
	// Height is camera's height
	Height float64
	// Amgle is camera angle (without the angle of trauma shaking).
	//
	// The unit is radian.
	Angle float64
	// ActualAngle is camera angle (including the angle of trauma shaking).
	//
	// The unit is radian.
	ActualAngle float64
	// ZoomFactor is the camera zoom (scaling) factor. Default is 1.
	ZoomFactor float64
	// SmoothType is the camera movement smoothing type.
	SmoothType SmoothType
	// Trauma factor. Factor is in the range [0-1]. Use AddTrauma() function
	Trauma float64
	// SmoothOptions holds the camera movement smoothing settings
	SmoothOptions *SmoothOptions
	// ShakeOptions holds the camera shake options.
	ShakeOptions *ShakeOptions
	// If ShakeEnabled is false, AddTrauma() has no effect and shake is always 0.
	//
	// The default value is false
	ShakeEnabled bool
	// XAxisSmoothingDisabled disables the smoothing of the X axis if it's true.
	XAxisSmoothingDisabled bool
	// YAxisSmoothingDisabled disables the smoothing of the Y axis if it's true.
	YAxisSmoothingDisabled bool
	// Internal camera values. Do not change directly.
	Tick, ZoomFactorShake float64
	// Internal camera values. Do not change directly.
	TempTargetX, CenterOffsetX, TraumaOffsetX, CurrentVelocityX float64
	// Internal camera values. Do not change directly.
	TempTargetY, CenterOffsetY, TraumaOffsetY, CurrentVelocityY float64
}

Camera object.

Use the `Camera.LookAt()` to align the center of the camera to the target.

func NewCamera

func NewCamera(lookAtX, lookAtY, w, h float64) *Camera

NewCamera returns new Camera

func (*Camera) AddTrauma

func (cam *Camera) AddTrauma(factor float64)

AddTrauma adds trauma. Factor is in the range [0-1]

func (*Camera) ApplyCameraTransform

func (cam *Camera) ApplyCameraTransform(g *ebiten.GeoM)

ApplyCameraTransform applies geometric transformation to given geoM

func (*Camera) ApplyCameraTransformToPoint added in v2.9.0

func (cam *Camera) ApplyCameraTransformToPoint(x, y float64) (float64, float64)

ApplyCameraTransformToPoint applies camera transformation to given point

func (*Camera) Bottom added in v2.95.1

func (cam *Camera) Bottom() float64

Bottom returns the bottom edge position of the camera in world-space.

func (*Camera) Center added in v2.8.0

func (cam *Camera) Center() (X float64, Y float64)

Center returns center point of the camera in world-space

func (*Camera) CenterX added in v2.98.1

func (cam *Camera) CenterX() float64

CenterX returns X axis center of the camera in world-space

func (*Camera) CenterY added in v2.98.1

func (cam *Camera) CenterY() float64

CenterY returns Y axis center of the camera in world-space

func (*Camera) Draw

func (cam *Camera) Draw(worldObject *ebiten.Image, worldObjectOps *ebiten.DrawImageOptions, screen *ebiten.Image)

Draw applies the Camera's geometric transformation then draws the object on the screen with drawing options.

func (*Camera) DrawWithColorM added in v2.93.0

func (cam *Camera) DrawWithColorM(worldObject *ebiten.Image, cm colorm.ColorM, worldObjectOps *colorm.DrawImageOptions, screen *ebiten.Image)

DrawWithColorM applies the Camera's geometric transformation then draws the object on the screen with colorm package drawing options.

func (*Camera) LookAt

func (cam *Camera) LookAt(targetX, targetY float64)

LookAt aligns the midpoint of the camera viewport to the target.

Camera motion smoothing is only applied with this method. Use this function only once in Update() and change only the (targetX, targetY)

func (*Camera) Reset

func (cam *Camera) Reset()

Reset resets rotation and zoom factor to zero

func (*Camera) Right added in v2.95.1

func (cam *Camera) Right() float64

Right returns the right edge position of the camera in world-space.

func (*Camera) ScreenToWorld

func (cam *Camera) ScreenToWorld(screenX, screenY int) (worldX float64, worldY float64)

ScreenToWorld converts screen-space coordinates to world-space

func (*Camera) SetCenter added in v2.95.2

func (cam *Camera) SetCenter(x, y float64)

SetCenter sets center position of the camera in world-space.

Unlike the LookAt() method, the position is set directly (teleport).

Can be used to cancel follow camera and teleport to target.

func (*Camera) SetSize

func (cam *Camera) SetSize(w, h float64)

SetSize sets camera rectangle size

func (*Camera) SetTopLeft added in v2.95.1

func (cam *Camera) SetTopLeft(x, y float64)

SetTopLeft sets top-left position of the camera in world-space.

Unlike the LookAt() method, the position is set directly (teleport).

func (*Camera) String

func (cam *Camera) String() string

String returns camera values as string

type ShakeOptions added in v2.93.0

type ShakeOptions struct {
	// Noise generator for noise types and settings.
	Noise         *fastnoise.NoiseState[float64]
	MaxX          float64 // Maximum X-axis shake. 0 means disabled
	MaxY          float64 // Maximum Y-axis shake. 0 means disabled
	MaxAngle      float64 // Max shake angle (radians). 0 means disabled
	MaxZoomFactor float64 // Zoom factor strength [1-0]. 0 means disabled
	TimeScale     float64 // Noise time domain speed
	Decay         float64 // Decay for trauma
}

func DefaultCameraShakeOptions

func DefaultCameraShakeOptions() *ShakeOptions

type SmoothOptions added in v2.9.0

type SmoothOptions struct {
	// LerpSpeedX is the  X-axis linear interpolation speed every frame.
	// Value is in the range [0-1]. Default value is 0.09
	//
	// A smaller value will reach the target slower.
	LerpSpeedX float64
	// LerpSpeedY is the Y-axis linear interpolation speed every frame. Value is in the range [0-1].
	//
	// A smaller value will reach the target slower.
	LerpSpeedY float64

	// SmoothDampTimeX is the X-Axis approximate time it will take to reach the target.
	//
	// A smaller value will reach the target faster. Default value is 0.2
	SmoothDampTimeX float64
	// SmoothDampTimeY is the Y-Axis approximate time it will take to reach the target.
	//
	// A smaller value will reach the target faster. Default value is 0.2
	SmoothDampTimeY float64

	// SmoothDampMaxSpeedX is the maximum speed the camera can move while smooth damping in X-Axis
	//
	// Default value is 1000
	SmoothDampMaxSpeedX float64
	// SmoothDampMaxSpeedY is the maximum speed the camera can move while smooth damping in Y-Axis
	//
	// Default value is 1000
	SmoothDampMaxSpeedY float64
}

SmoothOptions is the camera movement smoothing options.

func DefaultSmoothOptions added in v2.9.0

func DefaultSmoothOptions() *SmoothOptions

type SmoothType added in v2.93.0

type SmoothType int

SmoothType is the camera movement smoothing type.

const (
	// None is instant movement to the target. No smoothing.
	None SmoothType = iota
	// Lerp is Lerp() function.
	Lerp
	// SmoothDamp is SmoothDamp() function.
	SmoothDamp
)

Jump to

Keyboard shortcuts

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