Example #1
0
func XMain(callbacks Callbacks) {
	dpy := C.XOpenDisplay(nil)

	w := C.XCreateSimpleWindow(dpy, C.XDefaultRootWindow(dpy),
		0, 0, 600, 400,
		0, 0, 0)
	C.XSelectInput(dpy, w, C.StructureNotifyMask|C.SubstructureNotifyMask|C.ExposureMask)
	C.XMapWindow(dpy, w)

	win := Window{dpy: dpy, xw: w}
	visual := C.XDefaultVisual(dpy, 0)

	surf := cairo.XlibSurfaceCreate(unsafe.Pointer(dpy), uint64(win.xw), unsafe.Pointer(visual), 10, 10)

	for {
		var e C.XEvent
		C.XNextEvent(dpy, &e)
		typ := XEventType(*(*C.int)(unsafe.Pointer(&e)))
		// log.Printf("X event: %s", typ)
		switch typ {
		case C.ConfigureNotify:
			e := (*C.XConfigureEvent)(unsafe.Pointer(&e))
			surf.SetSize(int(e.width), int(e.height))
		case C.Expose:
			cr := cairo.Create(surf.Surface)
			callbacks.Draw(cr, surf)
		default:
			// log.Printf("unknown X event %s", typ)
		}
	}
}
Example #2
0
func main() {
	surf := cairo.ImageSurfaceCreate(cairo.FormatRGB24, 640, 480)
	cr := cairo.Create(surf.Surface)

	cr.Arc(320, 240, 40, 0, math.Pi)
	path := cr.CopyPath()
	iter := path.Iter()
	for seg := iter.Next(); seg != nil; seg = iter.Next() {
		log.Printf("seg %s %#v", seg.Type, seg)
	}
}
Example #3
0
func main() {
	size := 320
	margin := 10
	innerSize := size - (margin * 2)
	step := 15

	surf := cairo.ImageSurfaceCreate(cairo.FormatARGB32, size, size)
	cr := cairo.Create(surf.Surface)

	cr.SetSourceRGB(1, 1, 1)
	cr.Paint()

	cr.SetAntialias(cairo.AntialiasBest)
	// Offset by 0.5 to get pixel-aligned lines.
	cr.Translate(float64(margin)+0.5, float64(margin)+0.5)
	cr.SetSourceRGB(0, 0, 0)
	cr.SetLineWidth(1)
	for i := 0; i <= innerSize; i += step {
		cr.MoveTo(0, float64(i))
		cr.LineTo(float64(i), float64(innerSize))
		cr.Stroke()

		cr.MoveTo(float64(i), 0)
		cr.LineTo(float64(innerSize), float64(i))
		cr.Stroke()
	}
	surf.Flush()

	f, err := os.Create("example.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	err = surf.WriteToPNG(f)
	if err != nil {
		panic(err)
	}
}