wasm

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wasm embeds Kite and Kitex applications in an HTML canvas. Build the application with GOOS=js GOARCH=wasm and load it with the matching Go wasm_exec.js runtime after the canvas exists. Only New and Backend require the browser target; grid operations and input translation run natively.

For a page containing <canvas id="terminal"></canvas>:

package main

import (
	"context"
	"log"
	"syscall/js"

	"github.com/masterkeysrd/kite"
	"github.com/masterkeysrd/kite/backend/wasm"
	"github.com/masterkeysrd/kite/element"
)

func main() {
	canvas := js.Global().Get("document").Call("getElementById", "terminal")
	b, err := wasm.New(canvas, wasm.WithSize(60, 18))
	if err != nil {
		log.Fatal(err)
	}

	app := kite.New(b, kite.WithMouseMode(kite.MouseModeTrack))
	app.Mount(element.Box("Hello, canvas!"))
	if err := app.Run(context.Background()); err != nil {
		log.Print(err)
	}
}

Hosts can call Restore when unmounting to release listeners and close input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TranslateKeyEvent

func TranslateKeyEvent(in DOMKeyInput) (*backend.RawKeyEvent, bool)

TranslateKeyEvent translates named keys and single printable characters.

func TranslateMouseEvent

func TranslateMouseEvent(in DOMMouseInput, m CellMetrics, mode backend.MouseSupport) (*backend.RawMouseEvent, bool)

TranslateMouseEvent maps button transitions and filters motion by tracking mode.

func TranslateWheelEvent

func TranslateWheelEvent(in DOMWheelInput, m CellMetrics, mode backend.MouseSupport) (*backend.RawMouseEvent, bool)

TranslateWheelEvent converts each moving axis into a signed wheel tick.

Types

type CellMetrics

type CellMetrics struct {
	PaddingX, PaddingY    float64
	CellWidth, CellHeight float64
	Cols, Rows            int
}

CellMetrics describes the grid in canvas-local CSS pixels.

func (CellMetrics) PixelToCell

func (m CellMetrics) PixelToCell(x, y float64) (col, row int, inside bool)

PixelToCell returns clamped coordinates and whether the point hits the grid.

type DOMKeyInput

type DOMKeyInput struct {
	Key, Code string
	Mod       key.Mod
	Repeat    bool
}

DOMKeyInput describes a browser keydown, including its physical code.

type DOMMouseInput

type DOMMouseInput struct {
	Type            string
	X, Y            float64
	Button, Buttons int
	Mod             key.Mod
}

DOMMouseInput describes a mouse event with local CSS coordinates and DOM button values.

type DOMWheelInput

type DOMWheelInput struct {
	X, Y, DeltaX, DeltaY float64
	Mod                  key.Mod
}

DOMWheelInput describes a wheel gesture over the canvas.

type Option

type Option func(*config)

Option configures a canvas backend before its first frame.

func WithCursorColor

func WithCursorColor(cursor color.Color) Option

WithCursorColor sets the default hardware cursor color.

func WithDefaultColors

func WithDefaultColors(fg, bg color.Color) Option

WithDefaultColors sets fallback cell colors. Nil leaves a color unchanged.

func WithFont

func WithFont(family string, sizePx float64) Option

WithFont sets the CSS font family list and size in CSS pixels.

func WithLineHeight

func WithLineHeight(factor float64) Option

WithLineHeight sets the cell height as a multiple of the font size.

func WithPadding

func WithPadding(px float64) Option

WithPadding sets the space around the grid in CSS pixels.

func WithSize

func WithSize(cols, rows int) Option

WithSize sets the initial grid size in cells.

type Surface

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

Surface stores a terminal grid or an absolute-coordinate view of one.

func NewSurface

func NewSurface(size geom.Size) *Surface

NewSurface allocates a blank terminal grid.

func (*Surface) At

func (s *Surface) At(x, y int) backend.Cell

At returns a zero cell for points outside this surface.

func (*Surface) CellAt

func (s *Surface) CellAt(x, y int) backend.Cell

CellAt reads a cell using the backend.Surface convention.

func (*Surface) Clear

func (s *Surface) Clear()

Clear resets only the cells visible through this surface.

func (*Surface) Clip

func (s *Surface) Clip(rect geom.Rect) *Surface

Clip restricts reads and writes without translating coordinates.

func (*Surface) Diff

func (s *Surface) Diff(previous *Surface, damage []geom.Point, visit func(int, int, backend.Cell))

Diff visits changed glyph origins in row order. A nil previous frame forces a full repaint. Damage points invalidate their entire containing glyph.

func (*Surface) Resize

func (s *Surface) Resize(size geom.Size)

Resize replaces this surface with a blank, independently sized grid.

func (*Surface) Set

func (s *Surface) Set(x, y int, cell backend.Cell)

Set writes a cell using the backend.Surface convention.

func (*Surface) SetCell

func (s *Surface) SetCell(x, y int, cell backend.Cell)

SetCell writes within the clip, truncating wide cells at its right edge.

func (*Surface) Size

func (s *Surface) Size() geom.Size

Size returns the dimensions of the visible region.

Jump to

Keyboard shortcuts

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