ungerik / go-cairo

Go binding for the cairo graphics library
Other
142 stars 32 forks source link

Draw to screen example #3

Closed weberc2 closed 5 years ago

weberc2 commented 11 years ago

Would it be possible to get an example illustrating how to draw to the screen instead of save as a PNG?

ghost commented 8 years ago

I am interested in the above as well. I'd imaging you'd want to use XLib?

bit101 commented 6 years ago

Golang does not have any native UI stuff. Check into gotk3, which includes another port of cairo as well. You can create a full gtk user interface - window, buttons, draw to screen, etc. But their cairo implementation is missing some really important things like matrix operations.

usedbytes commented 6 years ago

I think this is resolved by #20, there's an example in the commit message using SDL (https://github.com/veandco/go-sdl2) to show a Cairo surface in a window:

package main

import (
        "time"
        "github.com/veandco/go-sdl2/sdl"
        "github.com/ungerik/go-cairo"
)

func main() {
        sdl.Init(sdl.INIT_EVERYTHING);
        defer sdl.Quit()

        window, _ := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, 
                        sdl.WINDOWPOS_UNDEFINED, 800, 600,
                        sdl.WINDOW_SHOWN)
        sdlSurface, _ := window.GetSurface()
        sdlSurface.FillRect(nil, 0)

        cairoSurface := cairo.NewSurfaceFromData(sdlSurface.Data(),
                        cairo.FORMAT_ARGB32, int(sdlSurface.W), int(sdlSurface.H),
                        int(sdlSurface.Pitch))
        cairoSurface.SelectFontFace("serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        cairoSurface.SetFontSize(32.0)
        cairoSurface.SetSourceRGB(0.0, 0.0, 1.0)
        cairoSurface.MoveTo(10.0, 50.0)
        cairoSurface.ShowText("Hello World")
        cairoSurface.Finish()

        window.UpdateSurface()

        time.Sleep(5 * time.Second)
}
cellularmitosis commented 4 years ago

Hello, is there any way this example can be included in the repo? This is exactly what I was looking for to get started! I think this could be useful to a lot of people 😄

For some reason, the window wasn't displaying. I got it working by replacing time.Sleep(5 * time.Second) with the final for-loop from the SDL2 example:

    running := true
    for running {
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch event.(type) {
            case *sdl.QuitEvent:
                println("Quit")
                running = false
                break
            }
        }
    }

which worked well 👍.

Edit: I wrote up the completed "getting started" process in a blog post 😎.