canvas2D struct

Fields:

  • element (dom.Element)
  • ctx (js.Value)

Methods:

valid


Returns:
  • bool

Show/Hide Method Body
{ return c.ctx.Truthy() }

setSize


Parameters:
  • width float64
  • height float64

Show/Hide Method Body
{
	c.element.Set("width", int(width))
	c.element.Set("height", int(height))
}

drawRect


Parameters:
  • r Rect

References:

  • Rect (v1/game/draw)

Show/Hide Method Body
{
	if r.fillColor != "" {
		c.ctx.Set("fillStyle", r.fillColor)
		c.ctx.Call("fillRect", r.X, r.Y, r.Width, r.Height)
	}
	if r.strokeColor != "" && r.strokeWidth > 0 {
		c.ctx.Set("lineWidth", r.strokeWidth)
		c.ctx.Set("strokeStyle", r.strokeColor)
		c.ctx.Call("strokeRect", r.X, r.Y, r.Width, r.Height)
	}
}

drawCircle


Parameters:
  • circle Circle

References:


Show/Hide Method Body
{
	c.ctx.Call("beginPath")
	c.ctx.Call("arc", circle.X, circle.Y, circle.Radius, 0, math.Pi*2, false)
	if circle.fillColor != "" {
		c.ctx.Set("fillStyle", circle.fillColor)
		c.ctx.Call("fill")
	}
	if circle.strokeColor != "" && circle.strokeWidth > 0 {
		c.ctx.Set("lineWidth", circle.strokeWidth)
		c.ctx.Set("strokeStyle", circle.strokeColor)
		c.ctx.Call("stroke")
	}
}

drawLine


Parameters:
  • line Line

References:

  • Line (v1/game/draw)

Show/Hide Method Body
{
	if line.strokeColor == "" || line.strokeWidth <= 0 {
		return
	}
	c.ctx.Call("beginPath")
	c.ctx.Call("moveTo", line.X1, line.Y1)
	c.ctx.Call("lineTo", line.X2, line.Y2)
	c.ctx.Set("lineWidth", line.strokeWidth)
	c.ctx.Set("strokeStyle", line.strokeColor)
	c.ctx.Call("stroke")
}

NewCanvas function

NewCanvas binds a DOM canvas element to the drawing helpers.

Parameters:

  • el dom.Element

Returns:

  • Canvas
  • bool

References:

Show/Hide Function Body
{
	if el.IsNull() || el.IsUndefined() {
		return Canvas{}, false
	}
	ctx := el.Call("getContext", "2d")
	if !ctx.Truthy() {
		return Canvas{}, false
	}
	return Canvas{impl: &canvas2D{element: el, ctx: ctx}}, true
}

NewCanvas function

NewCanvas is unavailable outside WebAssembly builds.

Parameters:

  • _ any

Returns:

  • Canvas
  • bool

References:

Show/Hide Function Body
{ return Canvas{}, false }

noopCanvas struct

Implements:

  • canvasImpl from draw

Methods:

valid


Returns:
  • bool

Show/Hide Method Body
{ return false }

setSize


Parameters:
  • width float64
  • height float64

Show/Hide Method Body
{}

drawRect


Parameters:
  • Rect

References:

  • Rect (v1/game/draw)

Show/Hide Method Body
{}

drawCircle


Parameters:
  • Circle

References:


Show/Hide Method Body
{}

drawLine


Parameters:
  • Line

References:

  • Line (v1/game/draw)

Show/Hide Method Body
{}

recorder struct

Fields:

  • validState (bool)
  • width (float64)
  • height (float64)
  • rects ([]Rect)
  • circles ([]Circle)
  • lines ([]Line)

Methods:

valid


Returns:
  • bool

Show/Hide Method Body
{ return r.validState }

setSize


Parameters:
  • width float64
  • height float64

Show/Hide Method Body
{
	r.width = width
	r.height = height
}

drawRect


Parameters:
  • rect Rect

References:

  • Rect (v1/game/draw)

Show/Hide Method Body
{ r.rects = append(r.rects, rect) }

drawCircle


Parameters:
  • circle Circle

References:


Show/Hide Method Body
{ r.circles = append(r.circles, circle) }

drawLine


Parameters:
  • line Line

References:

  • Line (v1/game/draw)

Show/Hide Method Body
{ r.lines = append(r.lines, line) }

TestCanvasDrawDelegates function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	impl := &recorder{validState: true}
	canvas := Canvas{impl: impl}

	rect := Rectangle(0, 0, 10, 20).Fill("#111111").Stroke("#ffffff", 2)
	circle := Disc(1, 2, 3).Fill("#eeeeee").Stroke("#000000", 1.5)
	line := Segment(-1, -1, 1, 1).Stroke("#ff00ff", 3)

	canvas.Draw(rect, nil, circle, line)

	if got := len(impl.rects); got != 1 {
		t.Fatalf("expected 1 rect, got %d", got)
	}
	if got := len(impl.circles); got != 1 {
		t.Fatalf("expected 1 circle, got %d", got)
	}
	if got := len(impl.lines); got != 1 {
		t.Fatalf("expected 1 line, got %d", got)
	}

	if impl.rects[0].fillColor != "#111111" || impl.rects[0].strokeColor != "#ffffff" || impl.rects[0].strokeWidth != 2 {
		t.Fatalf("unexpected rect payload: %#v", impl.rects[0])
	}
	if impl.circles[0].fillColor != "#eeeeee" || impl.circles[0].strokeColor != "#000000" || impl.circles[0].strokeWidth != 1.5 {
		t.Fatalf("unexpected circle payload: %#v", impl.circles[0])
	}
	if impl.lines[0].strokeColor != "#ff00ff" || impl.lines[0].strokeWidth != 3 {
		t.Fatalf("unexpected line payload: %#v", impl.lines[0])
	}
}

TestCanvasInvalid function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	canvas := Canvas{}
	if canvas.Valid() {
		t.Fatal("expected zero-value canvas to be invalid")
	}
	canvas.Draw(Rectangle(0, 0, 1, 1))
	canvas.SetSize(10, 20)
}

TestCanvasSetSizeDelegates function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	impl := &recorder{validState: true}
	canvas := Canvas{impl: impl}
	canvas.SetSize(640, 480)
	if impl.width != 640 || impl.height != 480 {
		t.Fatalf("expected setSize delegation, got width=%f height=%f", impl.width, impl.height)
	}
}

TestCanvasValidUsesImpl function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	impl := &recorder{validState: false}
	canvas := Canvas{impl: impl}
	if canvas.Valid() {
		t.Fatal("expected canvas to report invalid when impl invalid")
	}
	impl.validState = true
	if !canvas.Valid() {
		t.Fatal("expected canvas to report valid after impl change")
	}
}

Canvas struct

Canvas represents a 2D drawing surface built on top of a canvas element.

Fields:

  • impl (canvasImpl)

Methods:

Valid

Valid reports whether the canvas is ready for drawing commands.


Returns:
  • bool

Show/Hide Method Body
{
	if c.impl == nil {
		return false
	}
	return c.impl.valid()
}

SetSize

SetSize updates the backing canvas dimensions.


Parameters:
  • width float64
  • height float64

Show/Hide Method Body
{
	if c.impl == nil {
		return
	}
	c.impl.setSize(width, height)
}

Draw

Draw executes the provided commands in order when the canvas is valid.


Parameters:
  • cmds ...Command

Show/Hide Method Body
{
	if !c.Valid() {
		return
	}
	for _, cmd := range cmds {
		if cmd != nil {
			cmd.draw(c)
		}
	}
}

drawRect


Parameters:
  • r Rect

References:

  • Rect (v1/game/draw)

Show/Hide Method Body
{
	if c.impl == nil {
		return
	}
	c.impl.drawRect(r)
}

drawCircle


Parameters:
  • circle Circle

References:


Show/Hide Method Body
{
	if c.impl == nil {
		return
	}
	c.impl.drawCircle(circle)
}

drawLine


Parameters:
  • line Line

References:

  • Line (v1/game/draw)

Show/Hide Method Body
{
	if c.impl == nil {
		return
	}
	c.impl.drawLine(line)
}

canvasImpl interface

Methods:

valid


Returns:
  • bool

setSize


Parameters:
  • width float64
  • height float64

drawRect


Parameters:
  • Rect

drawCircle


Parameters:
  • Circle

drawLine


Parameters:
  • Line

Command interface

Command represents a draw instruction that can be executed on a Canvas.

Methods:

draw


Parameters:
  • Canvas

Rect struct

Rect describes a rectangle drawing command.

Fields:

  • X (float64)
  • Y (float64)
  • Width (float64)
  • Height (float64)
  • fillColor (string)
  • strokeColor (string)
  • strokeWidth (float64)

Methods:

Fill

Fill configures the fill color for the rectangle.


Parameters:
  • color string

Returns:
  • *Rect

Show/Hide Method Body
{
	r.fillColor = color
	return r
}

Stroke

Stroke configures the stroke color and width for the rectangle outline.


Parameters:
  • color string
  • width float64

Returns:
  • *Rect

Show/Hide Method Body
{
	r.strokeColor = color
	r.strokeWidth = width
	return r
}

draw


Parameters:
  • c Canvas

References:


Show/Hide Method Body
{
	c.drawRect(*r)
}

Rectangle function

Rectangle returns a rectangle command builder for the given bounds.

Parameters:

  • x float64
  • y float64
  • width float64
  • height float64

Returns:

  • *Rect
Show/Hide Function Body
{
	return &Rect{X: x, Y: y, Width: width, Height: height}
}

Circle struct

Circle describes a circle drawing command.

Fields:

  • X (float64)
  • Y (float64)
  • Radius (float64)
  • fillColor (string)
  • strokeColor (string)
  • strokeWidth (float64)

Methods:

Fill

Fill configures the fill color for the circle.


Parameters:
  • color string

Returns:
  • *Circle

Show/Hide Method Body
{
	c.fillColor = color
	return c
}

Stroke

Stroke configures the stroke color and width for the circle outline.


Parameters:
  • color string
  • width float64

Returns:
  • *Circle

Show/Hide Method Body
{
	c.strokeColor = color
	c.strokeWidth = width
	return c
}

draw


Parameters:
  • canvas Canvas

References:


Show/Hide Method Body
{
	canvas.drawCircle(*c)
}

Disc function

Disc returns a circle command builder centered at the provided coordinates.

Parameters:

  • x float64
  • y float64
  • radius float64

Returns:

  • *Circle
Show/Hide Function Body
{
	return &Circle{X: x, Y: y, Radius: radius}
}

Line struct

Line describes a line segment drawing command.

Fields:

  • X1 (float64)
  • Y1 (float64)
  • X2 (float64)
  • Y2 (float64)
  • strokeColor (string)
  • strokeWidth (float64)

Methods:

Stroke

Stroke configures the stroke color and width for the line.


Parameters:
  • color string
  • width float64

Returns:
  • *Line

Show/Hide Method Body
{
	l.strokeColor = color
	l.strokeWidth = width
	return l
}

draw


Parameters:
  • c Canvas

References:


Show/Hide Method Body
{
	c.drawLine(*l)
}

Segment function

Segment returns a line command builder for the provided endpoints.

Parameters:

  • x1 float64
  • y1 float64
  • x2 float64
  • y2 float64

Returns:

  • *Line
Show/Hide Function Body
{
	return &Line{X1: x1, Y1: y1, X2: x2, Y2: y2}
}

math import

Import example:

import "math"

github.com/rfwlab/rfw/v1/dom import

Import example:

import "github.com/rfwlab/rfw/v1/dom"

Imported as:

dom

github.com/rfwlab/rfw/v1/js import

Import example:

import "github.com/rfwlab/rfw/v1/js"

Imported as:

js

testing import

Import example:

import "testing"