glfw

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 4 Imported by: 0

README

Tests Static Analysis Go Reference

glfw-js

Package glfw experimentally provides a glfw-like API with desktop (via glfw) and browser (via HTML5 canvas) backends.

It is used for creating a GL context and receiving events.

Note: This package is currently in development. The API is incomplete and may change.

Installation

go get github.com/trendvidia/glfw-js

Directories

Path Synopsis
test/events events hooks every available callback and outputs their arguments.

License

Documentation

Overview

Package glfw experimentally provides a glfw-like API with desktop (via glfw) and browser (via HTML5 canvas) backends.

It is used for creating a GL context and receiving events.

Note: This package is currently in development. The API is incomplete and may change.

Index

Constants

View Source
const (
	True     int = 1
	False    int = 0
	DontCare int = -1
)
View Source
const (
	MouseButton1 MouseButton = 0
	MouseButton2 MouseButton = 2 // Web MouseEvent has middle and right mouse buttons in reverse order.
	MouseButton3 MouseButton = 1 // Web MouseEvent has middle and right mouse buttons in reverse order.

	MouseButtonLeft   = MouseButton1
	MouseButtonRight  = MouseButton2
	MouseButtonMiddle = MouseButton3
)
View Source
const (
	CursorNormal = iota
	CursorHidden
	CursorDisabled
)
View Source
const NoAPI int = 0

NoAPI is the ClientAPI value requesting a context-less window: CreateWindow then skips WebGL context creation so the bare canvas can host a WebGPU context instead (a canvas holds exactly one context type). Mirrors glfw.NoAPI on desktop. Presence in the hints map (not this zero value) is what selects it, so an unset ClientAPI still defaults to the WebGL path. (trendvidia: WebGPU.)

Variables

View Source
var (
	ErrInvalidParameter = errors.New("invalid parameter")
	ErrInvalidValue     = errors.New("invalid value")
)

Functions

func DefaultWindowHints

func DefaultWindowHints()

func DetachCurrentContext

func DetachCurrentContext()

func GetClipboardString

func GetClipboardString() string

GetClipboardString returns the contents of the system clipboard, if it contains or is convertible to a UTF-8 encoded string.

This function may only be called from the main thread.

func Init

func Init(cw ContextWatcher) error

func Open

func Open(name string) (io.ReadCloser, error)

Open opens a named asset. It's the caller's responsibility to close it when done.

func PollEvents

func PollEvents() error

func PostEmptyEvent

func PostEmptyEvent()

func SetClipboardString

func SetClipboardString(str string)

SetClipboardString sets the system clipboard to the specified UTF-8 encoded string.

This function may only be called from the main thread.

func SwapInterval

func SwapInterval(interval int) error

func Terminate

func Terminate() error

func WaitEvents

func WaitEvents()

func WindowHint

func WindowHint(target Hint, hint int)

Types

type Action

type Action int
const (
	Release Action = 0
	Press   Action = 1
	Repeat  Action = 2
)

type CharCallback

type CharCallback func(w *Window, char rune)

type CharModsCallback

type CharModsCallback func(w *Window, char rune, mods ModifierKey)

type CloseCallback

type CloseCallback func(w *Window)

type ContextWatcher

type ContextWatcher interface {
	// OnMakeCurrent is called after a context is made current.
	// context is is a platform-specific representation of the context, or nil if unavailable.
	OnMakeCurrent(context any)

	// OnDetach is called after the current context is detached.
	OnDetach()
}

ContextWatcher is a general mechanism for being notified when context is made current or detached.

type CursorEnterCallback

type CursorEnterCallback func(w *Window, entered bool)

type CursorPosCallback

type CursorPosCallback func(w *Window, xpos float64, ypos float64)

type DropCallback

type DropCallback func(w *Window, names []string)

type FocusCallback

type FocusCallback func(w *Window, focused bool)

type FramebufferSizeCallback

type FramebufferSizeCallback func(w *Window, width int, height int)

type GamepadAxis

type GamepadAxis int
const (
	AxisLeftX GamepadAxis = iota
	AxisLeftY
	AxisRightX
	AxisRightY
	AxisLeftTrigger
	AxisRightTrigger

	AxisLast = AxisRightTrigger
)

type GamepadButton

type GamepadButton int
const (
	ButtonA GamepadButton = iota
	ButtonB
	ButtonX
	ButtonY
	ButtonLeftBumper
	ButtonRightBumper
	ButtonBack
	ButtonStart
	ButtonGuide
	ButtonLeftThumb
	ButtonRightThumb
	ButtonDpadUp
	ButtonDpadRight
	ButtonDpadDown
	ButtonDpadLeft

	ButtonLast     = ButtonDpadLeft
	ButtonCross    = ButtonA
	ButtonCircle   = ButtonB
	ButtonSquare   = ButtonX
	ButtonTriangle = ButtonY
)

type Hint

type Hint int
const (
	AlphaBits Hint = iota
	DepthBits
	StencilBits
	Samples

	Focused
	Iconified
	Maximized
	Visible
	Hovered
	Resizable
	Decorated
	Floating
	AutoIconify
	CenterCursor
	TransparentFramebuffer
	FocusOnShow
	ScaleToMonitor

	ClientAPI
	ContextVersionMajor
	ContextVersionMinor
	ContextRobustness
	ContextReleaseBehavior
	OpenGLForwardCompatible
	OpenGLDebugContext
	OpenGLProfile

	// goxjs/glfw-specific hints for WebGL.
	PremultipliedAlpha
	PreserveDrawingBuffer
	PreferLowPowerToHighPerformance
	FailIfMajorPerformanceCaveat
)

type IconifyCallback

type IconifyCallback func(w *Window, iconified bool)

type InputMode

type InputMode int
const (
	CursorMode InputMode = iota
	StickyKeysMode
	StickyMouseButtonsMode
	LockKeyMods
	RawMouseMotion
)

type Joystick

type Joystick int
const (
	Joystick1 Joystick = iota
	Joystick2
	Joystick3
	Joystick4
	Joystick5
	Joystick6
	Joystick7
	Joystick8
	Joystick9
	Joystick10
	Joystick11
	Joystick12
	Joystick13
	Joystick14
	Joystick15
	Joystick16

	JoystickLast = Joystick16
)

func (Joystick) GetAxes

func (joy Joystick) GetAxes() []float32

func (Joystick) GetButtons

func (joy Joystick) GetButtons() []Action

func (Joystick) GetGamepadName

func (joy Joystick) GetGamepadName() string

func (Joystick) IsPresent

func (joy Joystick) IsPresent() bool

type Key

type Key int
const (
	KeyUnknown      Key = -1
	KeySpace        Key = 32
	KeyApostrophe   Key = 222
	KeyComma        Key = 188
	KeyMinus        Key = 189
	KeyPeriod       Key = 190
	KeySlash        Key = 191
	Key0            Key = 48
	Key1            Key = 49
	Key2            Key = 50
	Key3            Key = 51
	Key4            Key = 52
	Key5            Key = 53
	Key6            Key = 54
	Key7            Key = 55
	Key8            Key = 56
	Key9            Key = 57
	KeySemicolon    Key = 186
	KeyEqual        Key = 187
	KeyA            Key = 65
	KeyB            Key = 66
	KeyC            Key = 67
	KeyD            Key = 68
	KeyE            Key = 69
	KeyF            Key = 70
	KeyG            Key = 71
	KeyH            Key = 72
	KeyI            Key = 73
	KeyJ            Key = 74
	KeyK            Key = 75
	KeyL            Key = 76
	KeyM            Key = 77
	KeyN            Key = 78
	KeyO            Key = 79
	KeyP            Key = 80
	KeyQ            Key = 81
	KeyR            Key = 82
	KeyS            Key = 83
	KeyT            Key = 84
	KeyU            Key = 85
	KeyV            Key = 86
	KeyW            Key = 87
	KeyX            Key = 88
	KeyY            Key = 89
	KeyZ            Key = 90
	KeyLeftBracket  Key = 219
	KeyBackslash    Key = 220
	KeyRightBracket Key = 221
	KeyGraveAccent  Key = 192
	KeyWorld1       Key = -iota - 2
	KeyWorld2       Key = -iota - 2
	KeyEscape       Key = 27
	KeyEnter        Key = 13
	KeyTab          Key = 9
	KeyBackspace    Key = 8
	KeyInsert       Key = -iota - 2
	KeyDelete       Key = 46
	KeyRight        Key = 39
	KeyLeft         Key = 37
	KeyDown         Key = 40
	KeyUp           Key = 38
	KeyPageUp       Key = -iota - 2
	KeyPageDown     Key = -iota - 2
	KeyHome         Key = -iota - 2
	KeyEnd          Key = -iota - 2
	KeyCapsLock     Key = 20
	KeyScrollLock   Key = -iota - 2
	KeyNumLock      Key = -iota - 2
	KeyPrintScreen  Key = -iota - 2
	KeyPause        Key = -iota - 2
	KeyF1           Key = 112
	KeyF2           Key = 113
	KeyF3           Key = 114
	KeyF4           Key = 115
	KeyF5           Key = 116
	KeyF6           Key = 117
	KeyF7           Key = 118
	KeyF8           Key = 119
	KeyF9           Key = 120
	KeyF10          Key = 121
	KeyF11          Key = 122
	KeyF12          Key = 123
	KeyF13          Key = -iota - 2
	KeyF14          Key = -iota - 2
	KeyF15          Key = -iota - 2
	KeyF16          Key = -iota - 2
	KeyF17          Key = -iota - 2
	KeyF18          Key = -iota - 2
	KeyF19          Key = -iota - 2
	KeyF20          Key = -iota - 2
	KeyF21          Key = -iota - 2
	KeyF22          Key = -iota - 2
	KeyF23          Key = -iota - 2
	KeyF24          Key = -iota - 2
	KeyF25          Key = -iota - 2
	KeyKP0          Key = -iota - 2
	KeyKP1          Key = -iota - 2
	KeyKP2          Key = -iota - 2
	KeyKP3          Key = -iota - 2
	KeyKP4          Key = -iota - 2
	KeyKP5          Key = -iota - 2
	KeyKP6          Key = -iota - 2
	KeyKP7          Key = -iota - 2
	KeyKP8          Key = -iota - 2
	KeyKP9          Key = -iota - 2
	KeyKPDecimal    Key = -iota - 2
	KeyKPDivide     Key = -iota - 2
	KeyKPMultiply   Key = -iota - 2
	KeyKPSubtract   Key = -iota - 2
	KeyKPAdd        Key = -iota - 2
	KeyKPEnter      Key = -iota - 2
	KeyKPEqual      Key = -iota - 2
	KeyLeftShift    Key = 340
	KeyLeftControl  Key = 341
	KeyLeftAlt      Key = 342
	KeyLeftSuper    Key = 91
	KeyRightShift   Key = 344
	KeyRightControl Key = 345
	KeyRightAlt     Key = 346
	KeyRightSuper   Key = 93
	KeyMenu         Key = -iota - 2
)

TODO: Keys defined as -iota-2 need to be set to a valid positive value that matches the keyCode

generated by browsers. -iota-2 is used as a temporary solution to have unique but invalid values.
See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode.

type KeyCallback

type KeyCallback func(w *Window, key Key, scancode int, action Action, mods ModifierKey)

type ModifierKey

type ModifierKey int
const (
	ModShift ModifierKey = (1 << iota)
	ModControl
	ModAlt
	ModSuper
)

type Monitor

type Monitor struct{}

func GetPrimaryMonitor

func GetPrimaryMonitor() *Monitor

func (*Monitor) GetVideoMode

func (m *Monitor) GetVideoMode() *VidMode

type MouseButton

type MouseButton int

type MouseButtonCallback

type MouseButtonCallback func(w *Window, button MouseButton, action Action, mods ModifierKey)

type MouseMovementCallback

type MouseMovementCallback func(w *Window, xpos float64, ypos float64, xdelta float64, ydelta float64)

type PosCallback

type PosCallback func(w *Window, xpos int, ypos int)

type RefreshCallback

type RefreshCallback func(w *Window)

type ScrollCallback

type ScrollCallback func(w *Window, xoff float64, yoff float64)

type SizeCallback

type SizeCallback func(w *Window, width int, height int)

type VidMode

type VidMode struct {
	Width       int // The width, in pixels, of the video mode.
	Height      int // The height, in pixels, of the video mode.
	RedBits     int // The bit depth of the red channel of the video mode.
	GreenBits   int // The bit depth of the green channel of the video mode.
	BlueBits    int // The bit depth of the blue channel of the video mode.
	RefreshRate int // The refresh rate, in Hz, of the video mode.
}

VidMode describes a single video mode.

type Window

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

func CreateWindow

func CreateWindow(_, _ int, title string, monitor *Monitor, share *Window) (*Window, error)

func GetCurrentContext

func GetCurrentContext() *Window

func (*Window) Destroy

func (w *Window) Destroy()

func (*Window) GetCanvas

func (w *Window) GetCanvas() js.Value

GetCanvas returns the underlying HTML5 canvas element as a js.Value. The WebGPU backend uses it to build a wgpu surface (canvas.getContext("webgpu")) for a window created with WindowHint(ClientAPI, NoAPI). (trendvidia: WebGPU.)

func (*Window) GetClipboardString

func (w *Window) GetClipboardString() string

func (*Window) GetCursorPos

func (w *Window) GetCursorPos() (x, y float64)

func (*Window) GetFramebufferSize

func (w *Window) GetFramebufferSize() (width, height int)

func (*Window) GetInputMode

func (w *Window) GetInputMode(mode InputMode) int

func (*Window) GetKey

func (w *Window) GetKey(key Key) Action

func (*Window) GetMouseButton

func (w *Window) GetMouseButton(button MouseButton) Action

func (*Window) GetPos

func (w *Window) GetPos() (x, y int)

func (*Window) GetSize

func (w *Window) GetSize() (width, height int)

func (*Window) Hide

func (w *Window) Hide()

func (*Window) MakeContextCurrent

func (w *Window) MakeContextCurrent()

func (*Window) SetAttrib

func (w *Window) SetAttrib(attrib Hint, value int)

func (*Window) SetCharCallback

func (w *Window) SetCharCallback(cbfun CharCallback) (previous CharCallback)

func (*Window) SetCharModsCallback

func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback)

func (*Window) SetClipboardString

func (w *Window) SetClipboardString(str string)

func (*Window) SetCloseCallback

func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback)

func (*Window) SetCursorEnterCallback

func (w *Window) SetCursorEnterCallback(cbfun CursorEnterCallback) (previous CursorEnterCallback)

func (*Window) SetCursorPosCallback

func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorPosCallback)

func (*Window) SetDropCallback

func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback)

func (*Window) SetFocusCallback

func (w *Window) SetFocusCallback(cbfun FocusCallback) (previous FocusCallback)

func (*Window) SetFramebufferSizeCallback

func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback)

func (*Window) SetIcon

func (w *Window) SetIcon(images any)

func (*Window) SetIconifyCallback

func (w *Window) SetIconifyCallback(cbfun IconifyCallback) (previous IconifyCallback)

func (*Window) SetInputMode

func (w *Window) SetInputMode(mode InputMode, value int)

func (*Window) SetKeyCallback

func (w *Window) SetKeyCallback(cbfun KeyCallback) (previous KeyCallback)

func (*Window) SetMonitor

func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int)

func (*Window) SetMouseButtonCallback

func (w *Window) SetMouseButtonCallback(cbfun MouseButtonCallback) (previous MouseButtonCallback)

func (*Window) SetMouseMovementCallback

func (w *Window) SetMouseMovementCallback(cbfun MouseMovementCallback) (previous MouseMovementCallback)

func (*Window) SetPos

func (w *Window) SetPos(xpos, ypos int)

func (*Window) SetPosCallback

func (w *Window) SetPosCallback(cbfun PosCallback) (previous PosCallback)

func (*Window) SetRefreshCallback

func (w *Window) SetRefreshCallback(cbfun RefreshCallback) (previous RefreshCallback)

func (*Window) SetScrollCallback

func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback)

func (*Window) SetShouldClose

func (w *Window) SetShouldClose(value bool)

func (*Window) SetSize

func (w *Window) SetSize(width, height int)

func (*Window) SetSizeCallback

func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback)

func (*Window) SetTitle

func (w *Window) SetTitle(title string)

func (*Window) ShouldClose

func (w *Window) ShouldClose() bool

func (*Window) Show

func (w *Window) Show()

func (*Window) SwapBuffers

func (w *Window) SwapBuffers() error

Directories

Path Synopsis
test
events command
events hooks every available callback and outputs their arguments.
events hooks every available callback and outputs their arguments.

Jump to

Keyboard shortcuts

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