examples/component/canvas_animated.cpp

    //Copyright2021ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEDfile.
    #include<cmath>//forsin,cos
    #include<ftxui/dom/elements.hpp>//forcanvas,Element,separator,hbox,operator|,border
    #include<ftxui/screen/screen.hpp>//forPixel
    #include<memory>//forallocator,shared_ptr,__shared_ptr_access
    #include<string>//forstring,basic_string
    #include<utility>//formove
    #include<vector>//forvector,__alloc_traits<>::value_type

    #include"ftxui/component/component.hpp"//forRenderer,CatchEvent,Horizontal,Menu,Tab
    #include"ftxui/component/component_base.hpp"//forComponentBase
    #include"ftxui/component/event.hpp"//forEvent
    #include"ftxui/component/mouse.hpp"//forMouse
    #include"ftxui/component/screen_interactive.hpp"//forScreenInteractive
    #include"ftxui/dom/canvas.hpp"//forCanvas
    #include"ftxui/screen/color.hpp"//forColor,Color::Red,Color::Blue,Color::Green,ftxui

    intmain(){
    usingnamespaceftxui;

    intmouse_x=0;
    intmouse_y=0;

    //Atrianglefollowingthemouse,usingbraillecharacters.
    autorenderer_line_braille=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Severallines(braille)");
    c.DrawPointLine(mouse_x,mouse_y,80,10,Color::Red);
    c.DrawPointLine(80,10,80,40,Color::Blue);
    c.DrawPointLine(80,40,mouse_x,mouse_y,Color::Green);
    returncanvas(std::move(c));
    });

    //Atrianglefollowingthemouse,usingblockcharacters.
    autorenderer_line_block=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Severallines(block)");
    c.DrawBlockLine(mouse_x,mouse_y,80,10,Color::Red);
    c.DrawBlockLine(80,10,80,40,Color::Blue);
    c.DrawBlockLine(80,40,mouse_x,mouse_y,Color::Green);
    returncanvas(std::move(c));
    });

    //Acirclefollowingthemouse,usingbraillecharacters.
    autorenderer_circle_braille=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Acircle(braille)");
    c.DrawPointCircle(mouse_x,mouse_y,30);
    returncanvas(std::move(c));
    });

    //Acirclefollowingthemouse,usingblockcharacters.
    autorenderer_circle_block=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Acircle(block)");
    c.DrawBlockCircle(mouse_x,mouse_y,30);
    returncanvas(std::move(c));
    });

    //Afilledcirclefollowingthemouse,usingbraillecharacters.
    autorenderer_circle_filled_braille=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Acirclefilled(braille)");
    c.DrawPointCircleFilled(mouse_x,mouse_y,30);
    returncanvas(std::move(c));
    });

    //Afilledcirclefollowingthemouse,usingblockcharacters.
    autorenderer_circle_filled_block=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Acirclefilled(block)");
    c.DrawBlockCircleFilled(mouse_x,mouse_y,30);
    returncanvas(std::move(c));
    });

    //Anellipsefollowingthemouse,usingbraillecharacters.
    autorenderer_ellipse_braille=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Anellipse(braille)");
    c.DrawPointEllipse(mouse_x/2,mouse_y/2,mouse_x/2,mouse_y/2);
    returncanvas(std::move(c));
    });

    //Anellipsefollowingthemouse,usingblockcharacters.
    autorenderer_ellipse_block=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Anellipse(block)");
    c.DrawBlockEllipse(mouse_x/2,mouse_y/2,mouse_x/2,mouse_y/2);
    returncanvas(std::move(c));
    });

    //Anellipsefollowingthemousefilled,usingbraillecharacters.
    autorenderer_ellipse_filled_braille=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Afilledellipse(braille)");
    c.DrawPointEllipseFilled(mouse_x/2,mouse_y/2,mouse_x/2,
    mouse_y/2);
    returncanvas(std::move(c));
    });

    //Anellipsefollowingthemousefilled,usingblockcharacters.
    autorenderer_ellipse_filled_block=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Afilledellipse(block)");
    c.DrawBlockEllipseFilled(mouse_x/2,mouse_y/2,mouse_x/2,
    mouse_y/2);
    c.DrawBlockEllipse(mouse_x/2,mouse_y/2,mouse_x/2,mouse_y/2);
    returncanvas(std::move(c));
    });

    //Atextfollowingthemouse
    autorenderer_text=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Apieceoftext");
    c.DrawText(mouse_x,mouse_y,"Thisisapieceoftextwitheffects",
    [](Pixel&p){
    p.foreground_color=Color::Red;
    p.underlined=true;
    p.bold=true;
    });
    returncanvas(std::move(c));
    });

    autorenderer_plot_1=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Agraph");

    std::vector<int>ys(100);
    for(intx=0;x<100;x++){
    floatdx=float(x-mouse_x);
    floatdy=50.f;
    ys[x]=int(dy+20*cos(dx*0.14)+10*sin(dx*0.42));
    }
    for(intx=1;x<99;x++)
    c.DrawPointLine(x,ys[x],x+1,ys[x+1]);

    returncanvas(std::move(c));
    });

    autorenderer_plot_2=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"Asymmetricalgraphfilled");
    std::vector<int>ys(100);
    for(intx=0;x<100;x++){
    ys[x]=int(30+//
    10*cos(x*0.2-mouse_x*0.05)+//
    5*sin(x*0.4)+//
    5*sin(x*0.3-mouse_y*0.05));//
    }
    for(intx=0;x<100;x++){
    c.DrawPointLine(x,50+ys[x],x,50-ys[x],Color::Red);
    }

    returncanvas(std::move(c));
    });

    autorenderer_plot_3=Renderer([&]{
    autoc=Canvas(100,100);
    c.DrawText(0,0,"A2Dgaussianplot");
    intsize=15;

    //mouse_x=5mx+3*my
    //mouse_y=0mx+-5my+90
    floatmy=(mouse_y-90)/-5.f;
    floatmx=(mouse_x-3*my)/5.f;
    std::vector<std::vector<float>>ys(size,std::vector<float>(size));
    for(inty=0;y<size;y++){
    for(intx=0;x<size;x++){
    floatdx=x-mx;
    floatdy=y-my;
    ys[y][x]=-1.5+3.0*std::exp(-0.2f*(dx*dx+dy*dy));
    }
    }
    for(inty=0;y<size;y++){
    for(intx=0;x<size;x++){
    if(x!=0){
    c.DrawPointLine(
    5*(x-1)+3*(y-0),90-5*(y-0)-5*ys[y][x-1],
    5*(x-0)+3*(y-0),90-5*(y-0)-5*ys[y][x]);
    }
    if(y!=0){
    c.DrawPointLine(
    5*(x-0)+3*(y-1),90-5*(y-1)-5*ys[y-1][x],
    5*(x-0)+3*(y-0),90-5*(y-0)-5*ys[y][x]);
    }
    }
    }

    returncanvas(std::move(c));
    });

    intselected_tab=12;
    autotab=Container::Tab(
    {
    renderer_line_braille,
    renderer_line_block,
    renderer_circle_braille,
    renderer_circle_block,
    renderer_circle_filled_braille,
    renderer_circle_filled_block,
    renderer_ellipse_braille,
    renderer_ellipse_block,
    renderer_ellipse_filled_braille,
    renderer_ellipse_filled_block,

    renderer_plot_1,
    renderer_plot_2,
    renderer_plot_3,

    renderer_text,
    },
    &selected_tab);

    //Thiscapturethelastmouseposition.
    autotab_with_mouse=CatchEvent(tab,[&](Evente){
    if(e.is_mouse()){
    mouse_x=(e.mouse().x-1)*2;
    mouse_y=(e.mouse().y-1)*4;
    }
    returnfalse;
    });

    std::vector<std::string>tab_titles={
    "line(braille)",
    "line(block)",
    "circle(braille)",
    "circle(block)",
    "circlefilled(braille)",
    "circlefilled(block)",
    "ellipse(braille)",
    "ellipse(block)",
    "ellipsefilled(braille)",
    "ellipsefilled(block)",
    "plot_1simple",
    "plot_2filled",
    "plot_33D",
    "text",
    };
    autotab_toggle=Menu(&tab_titles,&selected_tab);

    autocomponent=Container::Horizontal({
    tab_with_mouse,
    tab_toggle,
    });

    //Addsomeseparatortodecoratethewholecomponent:
    autocomponent_renderer=Renderer(component,[&]{
    returnhbox({
    tab_with_mouse->Render(),
    separator(),
    tab_toggle->Render(),
    })|
    border;
    });

    autoscreen=ScreenInteractive::FitComponent();
    screen.Loop(component_renderer);

    return0;
    }