Languages

Menu
Sites
Language
I can not show a canvas

Hi to all,

I have two questions, first and more basic, I have this code, supose to show some shapes in the screen, but is not showing nothing:

Evas_Object *img;
    img = evas_object_image_filled_add(evas_object_evas_get(ad->win));
    evas_object_show(img);
    cairo_surface_t *surface;
    cairo_t *cairo;
    evas_object_geometry_get(ad->win, NULL, NULL, &screen_size_w, &screen_size_h);
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, screen_size_w, screen_size_h);
    cairo = cairo_create (surface);
    cairo_set_source_rgba(cairo, 1, 1, 1, 1);
        cairo_paint(cairo);

        cairo_translate(cairo,100, 100);
        cairo_set_line_width(cairo,2);
        cairo_set_source_rgba(cairo, 0.0, 0.0, 1.0, 1.0);
        cairo_move_to (cairo, 200 , 200);
            cairo_line_to (cairo, 400, 300);
            cairo_rel_line_to (cairo, 200 , 100);
        cairo_surface_flush(surface);
        unsigned char * imageData = cairo_image_surface_get_data(cairo_get_target(cairo));
            evas_object_image_data_set(img, imageData);
            evas_object_image_data_update_add(img, 0, 0, screen_size_w, screen_size_h);

 

Second question, Is possible to use the canvas object to draw, not on the window, to draw on a, for example, label object?

Thanks to all!!

View Selected Answer

Responses

2 Replies
Mark as answer
Shaswati Saha

Add the line below in the create_base_gui() function.
 

evas_object_event_callback_add(ad->win, EVAS_CALLBACK_RESIZE, win_resize_cb, ad);

Write the win_resize_cb() as below:

static void
win_resize_cb(void *data, Evas *e , Evas_Object *obj , void *event_info)
{
    appdata_s *ad = data;

	if(ad->surface)
	{
		cairo_surface_destroy(ad->surface);
		cairo_destroy(ad->cairo);
		ad->surface = NULL;
		ad->cairo = NULL;
	}

	evas_object_geometry_get(obj, NULL, NULL, &ad->width, &ad->height);
	evas_object_image_size_set(ad->img, ad->width, ad->height);
	evas_object_resize(ad->img, ad->width, ad->height);
	evas_object_show(ad->img);

	if(!ad->surface)
	{
		ad->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, ad->width, ad->height);
		ad->cairo = cairo_create (ad->surface);
		cairo_drawing(ad);
	}
}

Write cairo_drawing() function as below:

void cairo_drawing(void *data)
{
    appdata_s *ad = data;


	/* clear background as white */
	cairo_set_source_rgba(ad->cairo, 1, 1, 1, 1);
	cairo_paint(ad->cairo);

	cairo_translate(ad->cairo, 100,100);
	cairo_set_line_width(ad->cairo,2);
	cairo_set_source_rgba(ad->cairo, 0.0, 0.0, 1.0, 1.0);

	cairo_move_to (ad->cairo, 200 , 200);
	cairo_line_to (ad->cairo,400, 300);
	cairo_rel_line_to (ad->cairo, 200, 100);
	//cairo_arc (ad->cairo, 0.4 * d, 0.4 * d, 0.2* d * sqrt(2), -0.25 * M_PI, 0.25 * M_PI);

	cairo_stroke(ad->cairo);
	cairo_surface_flush(ad->surface);

	/* display cairo drawin on screen */
	unsigned char * imageData = cairo_image_surface_get_data(cairo_get_target(ad->cairo));
	evas_object_image_data_set(ad->img, imageData);
	evas_object_image_data_update_add(ad->img, 0, 0, ad->width, ad->height);
}

Note: I didn't find the below line in your code. 

cairo_stroke(ad->cairo);
Carlos Dominguez

Thanks!