CanvasView

Canvas shapes example

Canvas shapes

Canvas text example

Canvas text

Type of a view that allows to draw on it using a custom drawing function. This is useful for creating custom visual elements or components with complex shapes and designs. The "draw-function" property sets a drawing function that gets called every time the view needs to be redrawn. This function receives a Canvas object as an argument, which provides various methods for customizing styles, text, and drawing. The coordinates and sizes are specified in pixels only

Create from source

func NewCanvasView(session Session, params Params) CanvasView

Create the new custom draw view and returns its interface

Create from resource

CanvasView {
    id = canvasView,
    width = 100%,
    height = 100%,
}

Interface description

Inherit methods, properties and events from View

Redraw()

Force CanvasView to redraw its content

Properties

"draw-function"

Property sets the draw function of CanvasView

Constant: DrawFunction

Types: func(Canvas)

Examples

var image rui.Image

func imageCanvasDemo(canvas rui.Canvas) {
    if image != nil {
        canvas.DrawImage(50, 20, image)
    } else {
        image = rui.LoadImage("image.svg", func(img rui.Image) {
            if img.LoadingStatus() == rui.ImageReady {
                canvas.View().Redraw()
            }
        }, canvas.View().Session())
    }
}
canvas := rui.NewCanvasView(session, rui.Params{
    rui.Width:        rui.Percent(100),
    rui.Height:       rui.Percent(100),
    rui.DrawFunction: imageCanvasDemo,
})
func CanvasViewByID(rootView View, id string, ids ...string) CanvasView

Returns the child CanvasView, path to which is specified using the arguments id, ids. Example: view := CanvasViewByID(rootView, "id1", "id2", "id3"), view := CanvasViewByID(rootView, "id1/id2/id3"). These two function calls are equivalent. If a View with this path is not found or View is not CanvasView, the function will return nil.

func RedrawCanvasView(rootView View, canvasViewID string)

Finds CanvasView with canvasViewID and redraws it

func LoadImage(url string, onLoaded func(Image), session Session) Image

Starts the async image loading by url