Page 1 of 1

"canvas" type graphical library?

Posted: Tue Mar 17, 2020 11:30 am
by riceman0
Hello, I'm just getting started with Raspberry Pi 4, starting to learn my way around the Raspbian desktop and compiling my first programs in C (I prefer C and C++).

I'd like to get started with graphical programming, and the paradigm I prefer is the "Canvas" model... I just create a window and then I can select brushes and pens and draw lines, circles, rectangles, polygons within the bounds of the windows. (I am coming from windows, and their GDI subsystem provides this nicely and ends up being pretty powerful). I prefer this approach because you can truly customize your UIs and I don't mind the extra work involved.

Most of the C/C++ GUI support I've seen so far has been "widget" based, just gives you a bunch of buttons and controls. This is fine as long as they also give you access to the "canvas", but I haven't seen that yet.

Also, I don't care if I have to operate outside the desktop, it doesn't have to be a "window". As long as I can access the screen with some primitive graphical support and fonts, from a C or C++ program and still can access the RPi hardware such as SPI and GPIO, I'm all set.

Thanks for any thoughts.

Re: "canvas" type graphical library?

Posted: Tue Mar 17, 2020 12:09 pm
by PeterO
You could look at Gtk+ and it's "drawing area" widget which is how you can use the underlying Cairo 2D drawing library.

This will get you started : http://zetcode.com/gfx/cairo/

PeterO

Re: "canvas" type graphical library?

Posted: Tue Mar 17, 2020 12:47 pm
by PiGraham
I suspect this is pretty much ubiquitous.

Pygame has Surface and Draw https://www.pygame.org/docs/ref/draw.html
tkinter has Canvas with create_line etc https://kite.com/python/docs/Tkinter.Canvas
The same is in SDL2
pi3D, which I think can be used for 2D, has canvas https://pi3d.github.io/html/_modules/pi ... anvas.html
Ditto Qt

If you look into most GUI libraries chances are you'll find a Canvas and drawing primitives supported.

Re: "canvas" type graphical library?

Posted: Tue Mar 17, 2020 10:12 pm
by paddyg
pi3d canvas is a bit of a cheat really. pi3d uses OpenGL functionality which is very fast but the 2Dness is simply rendering 3D points, lines and triangles with no perspective. Which is 2D I suppose but things like filled polygons, line ends and corners, anti-aliasing and bezier curves have to be constructed with a bit of effort. For C/C++ applications Gtk, SDL2 and Qt are pretty standard and work fine on RPi

Re: "canvas" type graphical library?

Posted: Wed Mar 18, 2020 12:58 am
by Paeryn
PiGraham wrote:
Tue Mar 17, 2020 12:47 pm
tkinter has Canvas with create_line etc https://kite.com/python/docs/Tkinter.Canvas
Be aware that the lines, circles etc. created on Tk's Canvas aren't just drawn on to a bitmap that you can forget about, they are objects in their own right and will still be there and drawn even if subsequent elements are added that cover them.

If you treat Tk's Canvas as a bitmap and constantly draw over elements (like drawing a filled rectangle to 'erase' part of the Canvas) you will end up slowing everything down as the whole list of objects on a Canvas gets redrawn even if they are obscured. It does mean however that you can do things like move, resize or delete a line and it will correctly update.

Re: "canvas" type graphical library?

Posted: Wed Mar 18, 2020 1:42 am
by DarkElvenAngel
Check out raylib that's beginner friendly and can run on the pi with or without a desktop.

https://www.raylib.com