Open dayton-outar opened 9 hours ago
For creating a graphical animation project in a GUI for Ubuntu, similar to what is possible with the Canvas API in browsers, you can use libraries or frameworks in languages like C++, Python, or JavaScript that offer rich graphical and UI capabilities. Below are a few approaches:
GTK is a popular toolkit for creating graphical user interfaces on Linux, and Cairo is a 2D graphics library that can be used with GTK for drawing.
Steps:
sudo apt install libgtk-3-dev libcairo2-dev
Use Cairo to draw on a GTK window, similar to how you'd use the Canvas API.
Example:
Write a simple C program to draw shapes:
#include <gtk/gtk.h>
#include <cairo.h>
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer user_data) {
cairo_set_source_rgb(cr, 0.2, 0.2, 0.2);
cairo_paint(cr);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_rectangle(cr, 20, 20, 100, 100);
cairo_fill(cr);
return FALSE;
}
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), darea);
g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(on_draw_event), NULL);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
gcc -o gtk_canvas_example gtk_canvas_example.c `pkg-config --cflags --libs gtk+-3.0`
./gtk_canvas_example
PyQt5 or PySide2 allows you to create graphical applications in Python, and the QPainter
class offers similar drawing capabilities to the Canvas API.
Steps:
pip install PyQt5
Create a simple window and draw on it using QPainter
.
Example:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class CanvasWidget(QWidget):
def __init__(self):
super().__init__()
def paintEvent(self, event):
qp = QPainter(self)
self.drawShapes(qp)
def drawShapes(self, qp):
qp.setPen(QPen(QColor(255, 0, 0), 8, Qt.SolidLine))
qp.drawRect(20, 20, 100, 100)
app = QApplication(sys.argv)
window = CanvasWidget()
window.resize(400, 300)
window.show()
sys.exit(app.exec_())
SDL (Simple DirectMedia Layer) and SFML (Simple and Fast Multimedia Library) are powerful libraries for creating 2D/3D graphics applications and games.
sudo apt install libsdl2-dev libsfml-dev
If you're familiar with JavaScript, you can use Electron to create a desktop application and embed HTML5 Canvas in the window.
Steps:
npm install -g electron
Let me know if you'd like a more detailed example for any specific approach!
Build a terminal application using C++ to show a donut wobbling in a rain of characters. Green on black.