canvas2d

package
v1.16.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0 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 canvas2d is the last renderer of the web (SPEC §9.6): it draws each frame with the software renderer (internal/gpu/soft) and puts the image into the page's 2D canvas with putImageData, so that an app shows even where neither WebGPU nor WebGL 2 works. A frame crosses into JavaScript in one js.CopyBytesToJS.

The drawing and the composition over the background are plain Go (renderer.go) and run under scripts/test-wasm.sh and the normal tests; the canvas is touched only in the files that carry the js tag.

Flutter: no counterpart (Flutter web's last resort is CanvasKit or Skwasm).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Background is what shows where the scene draws nothing. A canvas of the page is
	// opaque, so only the color's red, green and blue count; the zero value is black.
	Background ui.Color
}

Options are the optional parameters of NewRenderer.

Flutter: no counterpart

type Presenter

type Presenter interface {
	// Present shows img, an opaque image, in the canvas that the handle names, and sets
	// the canvas's size to the image's if it differs. It must not keep img.
	Present(h gpu.SurfaceHandle, img *image.RGBA) error
	// Release forgets the canvas of the handle.
	Release(h gpu.SurfaceHandle)
}

Presenter shows a finished frame in the canvas of a surface. The js build's Presenter puts it there with putImageData; tests give one that records the frames.

Flutter: no counterpart

type Renderer

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

Renderer is a gpu.Renderer that draws with soft and presents through a Presenter.

Flutter: no counterpart

func NewRenderer

func NewRenderer(p Presenter, opts ...Options) *Renderer

NewRenderer makes a renderer that presents through p. More than one options value panics with MultipleOptions.

Flutter: no counterpart

func (*Renderer) Attach

func (r *Renderer) Attach(h gpu.SurfaceHandle) (gpu.Surface, error)

Attach starts drawing into the canvas that the handle names, which must be of kind gpu.SurfaceKindWeb. It fails with SurfaceKindUnsupported for another kind and with RendererClosed after Close.

Flutter: no counterpart

func (*Renderer) Close

func (r *Renderer) Close() error

Close stops the renderer; its surfaces are destroyed. It is safe to call twice.

Flutter: no counterpart

func (*Renderer) Render

func (r *Renderer) Render(s gpu.Surface, scene *ui.Scene, size ui.Size, dpr float32) error

Render draws the scene into the surface at the size in device pixels and presents it. A nil scene draws the background only. The scene's coordinates are already in device pixels, so dpr is not used.

Flutter: no counterpart

func (*Renderer) RenderCapture

func (r *Renderer) RenderCapture(s gpu.Surface, scene *ui.Scene, size ui.Size, _ float32) (*image.RGBA, error)

RenderCapture is Render that also returns the frame it presented, top row first. The image belongs to the caller.

Flutter: no counterpart

func (*Renderer) Snapshot

func (r *Renderer) Snapshot(scene *ui.Scene, size ui.Size) (*image.RGBA, error)

Snapshot draws the scene into a new image of the size, without a canvas.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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