Images and Canvases

Create moustache.vala

Copy/paste this into a file called moustache.vala:

using Clutter;

void
main(string[] args)
{
    Clutter.init(ref args);
    
    // note: the Clutter.Texture API is deprecrated, but the API that
    // replaced it is very cumbersome, so we just stick with
    // Clutter.Texture
    var linus = new Texture.from_file("Linus_Torvalds.jpeg");
    
    int width, height;
    linus.get_base_size(out width, out height);
    
    var stage = new Stage() {
        width = width,
        height = height};
    
    stage.add_child(linus);
    
    var canvas = new Canvas() {
        width = width,
        height = height };
    canvas.draw.connect((cr, width, height) => {
        cr.set_line_width(10);
        cr.set_source_rgba(0, 0, 0, 1);
        
        cr.move_to(310, 355);
        cr.line_to(260, 340);
        cr.stroke();
        
        cr.move_to(310, 355);
        cr.line_to(350, 345);
        cr.stroke();
        
        return true;
    });
    
    var canvasActor = new Actor() {
        x = 0,
        y = 0,
        width = width,
        height = height,
        content = canvas };
    
    stage.add_child(canvasActor);
    
    canvas.invalidate();
    
    stage.show();
    stage.destroy.connect(Clutter.main_quit);
    Clutter.main();
}

Attic/Clutter/Tutorial/ImagesAndCanvases (last edited 2024-04-11 15:39:35 by EmmanueleBassi)