canvas

package
v0.0.0-...-f3d8a94 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

The canvas package provides some a facility for managing independently updating objects inside a graphics window.

The principal type is Canvas, which displays a z-ordered set of objects. New objects may be added, deleted and change their appearance: the Canvas manages any necessary re-drawing.

Any object that implements the Item interface may be placed onto a Canvas, including a Canvas itself, allowing more complex objects to be built relatively easily.

Index

Constants

View Source
const (
	N = Anchor(1 << iota)
	S
	E
	W
	Baseline
)

Variables

This section is empty.

Functions

func BorderOp

func BorderOp(dst draw.Image, r image.Rectangle, w int, src image.Image, sp image.Point, op draw.Op)

Border aligns r.Min in dst with sp in src and then replaces pixels in a w-pixel border around r in dst with the result of the Porter-Duff compositing operation “src over dst.” If w is positive, the border extends w pixels inside r. If w is negative, the border extends w pixels outside r.

func Box

func Box(width, height int, col image.Image, border int, borderCol image.Image) image.Image

Box creates a rectangular image of the given size, filled with the given colour, with a border-size border of colour borderCol.

func DrawOp

func DrawOp(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point, op draw.Op)

func ImageOf

func ImageOf(it Item) *image.RGBA

func NewPainter

func NewPainter(dst draw.Image, src image.Image, op draw.Op) (p raster.Painter)

NewPainter returns a Painter that will draw from src onto dst using the Porter-Duff composition operator op.

func SliceImage

func SliceImage(width, height int, r image.Rectangle, img draw.Image, p image.Point) draw.Image

SliceImage returns an image which is a view onto a portion of img. The returned image has the specified width and height, but all draw operations are clipped to r. The origin of img is aligned with p. Where img overlaps with r, it will be used for drawing operations.

func SpanBbox

func SpanBbox(ss []raster.Span) image.Rectangle

Types

type Anchor

type Anchor int

type Background

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

A Background is the base layer on which other objects can be layered. It implements the Backing interface and displays a single object only.

func NewBackground

func NewBackground(img draw.Image, bg image.Image, flush func(r image.Rectangle)) *Background

NewBackground creates a new Background object that draws to img, and draws the actual background with bg. The flush function, if non-nil, will be called to whenever changes are to be made visible externally (for example when Flush() is called.

Note that bg is drawn with the draw.Src operation, so it is possible to create images with a transparent background.

func (*Background) Atomically

func (b *Background) Atomically(f func(FlushFunc))

func (*Background) Flush

func (b *Background) Flush()

Flush flushes all pending changes, and makes them visible.

func (*Background) Rect

func (b *Background) Rect() image.Rectangle

func (*Background) SetItem

func (b *Background) SetItem(item Drawer)

SetItem sets the item to draw on top of the background.

type Backing

type Backing interface {
	Flusher
	Atomically(func(f FlushFunc))
	Rect() image.Rectangle
}

A Backer represents a graphical area containing a number of Drawer objects. To change its appearance, one of those objects may call Atomically, passing it a function which will be called once to make the changes. The function will be passed a FlushFunc that can be used to inform the Backer of any changes that are made. Rect should return the rectangle that is available for items within the Backing to draw into.

func NullBacking

func NullBacking() Backing

NullBacking returns an object that satisfies the Backing interface but has no actual image associated with it.

type Canvas

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

A Canvas represents a z-ordered set of drawable Items. As a Canvas itself implements Item and Backing, Canvas's can be nested indefinitely.

func NewCanvas

func NewCanvas(background color.Color, r image.Rectangle) *Canvas

NewCanvas returns a new Canvas object that is inside backing. The background image, if non-nil, must be opaque, and will used to draw the background. r gives the extent of the canvas.

func (*Canvas) AddItem

func (c *Canvas) AddItem(item Item)

func (*Canvas) Atomically

func (c *Canvas) Atomically(f func(FlushFunc))

Atomically calls f, which can then make changes to the appearance of items in the canvas. See the Backing interface for details

func (*Canvas) Bbox

func (c *Canvas) Bbox() image.Rectangle

func (*Canvas) Delete

func (c *Canvas) Delete(it Item)

DeleteItem deletes a single item from the canvas.

func (*Canvas) Draw

func (c *Canvas) Draw(dst draw.Image, clipr image.Rectangle)

func (*Canvas) Flush

func (c *Canvas) Flush()

func (*Canvas) HandleMouse

func (c *Canvas) HandleMouse(_ Flusher, m ui.MouseEvent, ec <-chan interface{}) bool

HandleMouse delivers the mouse events to the top-most item that that is hit by the mouse point.

func (*Canvas) HitTest

func (c *Canvas) HitTest(p image.Point) (hit bool)

func (*Canvas) Opaque

func (c *Canvas) Opaque() bool

func (*Canvas) Raise

func (c *Canvas) Raise(it, nextto Item, above bool)

Raise moves the Item it adjacent to nextto in the canvas z-ordering. If above is true, the item will be placed just above nextto, otherwise it will be placed just below. If nextto is nil, then the item will be placed at the very top (above==true) or the very bottom (above==false).

func (*Canvas) Rect

func (c *Canvas) Rect() image.Rectangle

Rect returns the rectangle that is available for items to draw into.

func (*Canvas) Replace

func (c *Canvas) Replace(it, it1 Item) (replaced bool)

func (*Canvas) SetContainer

func (c *Canvas) SetContainer(b Backing)

type Drawer

type Drawer interface {
	Draw(dst draw.Image, clipr image.Rectangle)
	SetContainer(b Backing)
}

The Drawer interface represents the basic level of functionality for a drawn object. SetContainer is called when the object is placed inside another; b will be nil if the object is removed from its container. SetContainer can be called twice in a row with the same object if some ancestor has changed.

The Draw method should draw a representation of the object onto dst. No pixels outside clipr should be changed. It is legitimate for the Drawer object to retain the dst image until SetContainer is called (for instance, to perform its own direct manipulation of the image).

Neither method in Drawer should interact with any object outside its direct control (for example by modifying the appearance of another object using the same Backer)

type Ellipse

type Ellipse struct {
	Item
	// contains filtered or unexported fields
}

A ellipse object represents an ellipse centered in cr with radiuses ra and rb

func NewEllipse

func NewEllipse(col image.Image, cr image.Point, ra, rb int, width float64) *Ellipse

color, center, radius a radius b and width

func (*Ellipse) Move

func (obj *Ellipse) Move(delta image.Point)

func (*Ellipse) SetCentre

func (obj *Ellipse) SetCentre(p image.Point)

SetEndPoints changes the center of the ellipse

func (*Ellipse) SetContainer

func (obj *Ellipse) SetContainer(b Backing)

func (*Ellipse) SetFill

func (obj *Ellipse) SetFill(fill image.Image)

SetColor changes the colour of the ellipse

type FlushFunc

type FlushFunc func(r image.Rectangle, drawn Drawer)

A FlushFunc can be used to inform a Backing object of a changed area of pixels. r specifies the rectangle that has changed; if drawn is non-nil, it indicates that the rectangle has already been redrawn (only appropriate if all pixels in the rectangle have been non-transparently overwritten); its value gives the item that has changed, which must be a direct child of the Backing object.

type Flusher

type Flusher interface {
	Flush()
}

The Flush method is used to flush any pending changes to the underlying image, usually the screen.

type HandleMouser

type HandleMouser interface {
	HandleMouse(f Flusher, m ui.MouseEvent, ec <-chan interface{}) bool
}

HandleMouse can be implemented by any object that might wish to handle mouse events. It is called with an initial mouse event, and a channel from which further events can be read. It returns true if the initial mouse event was absorbed. mc should not be used after HandleMouse returns.

type HandlerItem

type HandlerItem interface {
	Item
	HandleMouser
}

type Image

type Image struct {
	Item
	// contains filtered or unexported fields
}

An Image represents an rectangular (but possibly transparent) image.

func NewImage

func NewImage(img image.Image, opaque bool, p image.Point) *Image

Image returns a new Image which will be drawn using img, with p giving the coordinate of the image's top left corner.

func (*Image) SetCentre

func (obj *Image) SetCentre(p image.Point)

func (*Image) SetContainer

func (obj *Image) SetContainer(c Backing)

type ImageItem

type ImageItem struct {
	R        image.Rectangle
	Image    image.Image
	IsOpaque bool
}

An ImageItem is an Item that uses an image to draw itself. It is intended to be used as a building block for other Items.

func (*ImageItem) Bbox

func (obj *ImageItem) Bbox() image.Rectangle

func (*ImageItem) Draw

func (obj *ImageItem) Draw(dst draw.Image, clip image.Rectangle)

func (*ImageItem) HitTest

func (obj *ImageItem) HitTest(p image.Point) bool

func (*ImageItem) Opaque

func (obj *ImageItem) Opaque() bool

func (*ImageItem) SetContainer

func (obj *ImageItem) SetContainer(c Backing)

type Item

type Item interface {
	Drawer
	Bbox() image.Rectangle
	HitTest(p image.Point) bool
	Opaque() bool
}

Values that implement the Item interface may be added to the canvas. They should adhere to the following rules: - No calls to the canvas should be made while in the Draw, Bbox or HitTest methods. - All changes to the appearance of the object should be made using the Atomically function.

type Line

type Line struct {
	Item
	// contains filtered or unexported fields
}

A line object represents a single straight line.

func NewLine

func NewLine(fill image.Image, p0, p1 image.Point, width float64) *Line

Line returns a new Line, coloured with col, from p0 to p1, of the given width.

func (*Line) SetContainer

func (obj *Line) SetContainer(b Backing)

func (*Line) SetEndPoints

func (obj *Line) SetEndPoints(p0, p1 image.Point)

SetEndPoints changes the end coordinates of the Line.

func (*Line) SetFill

func (obj *Line) SetFill(fill image.Image)

SetColor changes the colour of the line

type MoveableItem

type MoveableItem interface {
	Item
	SetCentre(p image.Point)
}

A MoveableItem is an item that may be moved by calling SetCentre, where the centre is the central point of the item's bounding box.

func Draggable

func Draggable(it MoveableItem) MoveableItem

Draggable makes any MoveableItem into an object that may be dragged by the mouse.

func Moveable

func Moveable(item Item) MoveableItem

type Polygon

type Polygon struct {
	Item
	// contains filtered or unexported fields
}

A Polygon represents a filled polygon.

func NewPolygon

func NewPolygon(fill image.Image, points []image.Point) *Polygon

Polygon returns a new PolyObject, using col for its fill colour, and using points for its vertices.

func (*Polygon) SetContainer

func (obj *Polygon) SetContainer(c Backing)

type RasterItem

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

A RasterItem is a low level canvas object that can be used to build higher level primitives. It implements Item, and will calculate (and remember) its bounding box on request.

Otherwise it can be used as a raster.Rasterizer.

func (*RasterItem) Add1

func (obj *RasterItem) Add1(p raster.Point)

func (*RasterItem) Add2

func (obj *RasterItem) Add2(p0, p1 raster.Point)

func (*RasterItem) Add3

func (obj *RasterItem) Add3(p0, p1, p2 raster.Point)

func (*RasterItem) Bbox

func (obj *RasterItem) Bbox() image.Rectangle

func (*RasterItem) CalcBbox

func (obj *RasterItem) CalcBbox()

CalcBbox calculates the current bounding box of all the pixels in the current path.

func (*RasterItem) Clear

func (obj *RasterItem) Clear()

func (*RasterItem) Draw

func (obj *RasterItem) Draw(dst draw.Image, clipr image.Rectangle)

func (*RasterItem) HitTest

func (obj *RasterItem) HitTest(p image.Point) bool

func (*RasterItem) Opaque

func (obj *RasterItem) Opaque() bool

func (*RasterItem) SetContainer

func (obj *RasterItem) SetContainer(b Backing)

func (*RasterItem) SetFill

func (obj *RasterItem) SetFill(fill image.Image)

func (*RasterItem) Start

func (obj *RasterItem) Start(p raster.Point)

type Slider

type Slider struct {
	Item
	// contains filtered or unexported fields
}

func NewSlider

func NewSlider(r image.Rectangle, fg, bg color.Color, value values.Value) (obj *Slider)

A Slider shows a mouse-adjustable slider bar. NewSlider returns the Slider item. The value is used to set and get the current slider value; its Type() should be float64; the slider's value is in the range [0, 1].

func (*Slider) HandleMouse

func (obj *Slider) HandleMouse(f Flusher, m ui.MouseEvent, ec <-chan interface{}) bool

func (*Slider) SetContainer

func (obj *Slider) SetContainer(c Backing)

type Text

type Text struct {
	Item
	// contains filtered or unexported fields
}

func NewText

func NewText(p image.Point, where Anchor, s string, font *truetype.Font, size float64, val values.Value) *Text

NewText creates a new item to display a line of text. If val is non-nil, it should be a string-typed Value, and the Value's text will be displayed instead of s.

func (*Text) SetCentre

func (t *Text) SetCentre(cp image.Point)

func (*Text) SetContainer

func (t *Text) SetContainer(c Backing)

func (*Text) SetFill

func (t *Text) SetFill(fill image.Image)

func (*Text) SetFontSize

func (t *Text) SetFontSize(size float64)

func (*Text) SetPoint

func (t *Text) SetPoint(p0 image.Point)

func (*Text) SetText

func (t *Text) SetText(s string)

type TextItem

type TextItem struct {
	*freetype.Context
	Text string
	Pt   raster.Point
	// contains filtered or unexported fields
}

func (*TextItem) Bbox

func (d *TextItem) Bbox() image.Rectangle

func (*TextItem) CalcBbox

func (d *TextItem) CalcBbox()

func (*TextItem) Draw

func (d *TextItem) Draw(dst draw.Image, clip image.Rectangle)

func (*TextItem) HitTest

func (d *TextItem) HitTest(p image.Point) bool

func (*TextItem) Init

func (d *TextItem) Init() *TextItem

func (*TextItem) Opaque

func (d *TextItem) Opaque() bool

func (*TextItem) SetContainer

func (d *TextItem) SetContainer(_ Backing)

func (*TextItem) SetFill

func (d *TextItem) SetFill(fill image.Image)

Jump to

Keyboard shortcuts

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