簡體   English   中英

如何在上面的另一個繪制形狀的幫助下切割形狀的一部分?

[英]How to cut a part of a shape with the help of another painted shape above?

在下面的屏幕上,我有繪制半透明矩形和繪制不透明矩形的圖像。 我的目的是切割不透明矩形的區域 - 刪除半透明矩形中的像素以查看初始圖像。

在此處輸入圖像描述

cairo_surface_t *surface = cairo_xlib_surface_create(xdisplay, xroot,  DefaultVisual(xdisplay, scr), DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
cairo_t *cr = cairo_create(surface);

cairo_surface_write_to_png(
  surface,
  "test.png");

cairo_surface_t *surfaceTmp = cairo_image_surface_create_from_png("./test.png");




if (xevent.type == MotionNotify && isPressed == true)
{
  int tmp_x = xevent.xmotion.x_root;
  int tmp_y = xevent.xmotion.y_root;

  cairo_save(cr);

  cairo_set_source_surface(cr, surfaceTmp, 1, 1);
  cairo_paint(cr);

  cairo_set_source_rgba(cr, 0, 0, 0, 0.2);
  cairo_rectangle(cr, 0, 0, DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
  cairo_fill(cr);

  cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
  cairo_rectangle(cr, init_x, init_y, tmp_x - init_x, tmp_y - init_y); // set rectangle
  cairo_fill(cr);

  cairo_restore(cr);
}

為什么我有這個黑色的矩形? 我認為 CAIRO_OPERATOR_CLEAR 應該刪除下面的形狀部分。

期望的結果:

在此處輸入圖像描述

也許,由於 cairo 的繪圖直接更改像素(=不緩沖),一旦你繪制了一些東西,就沒有可以在之后恢復underlying original pixels 如果您想在矩形上打洞,請嘗試填充規則: CAIRO_FILL_RULE_EVEN_ODD

cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);  // The default is CAIRO_FILL_RULE_WINDING.

cairo_set_source_rgba(cr, 0, 0, 0, 0.2);
cairo_rectangle(cr, 0, 0, DisplayWidth(xdisplay, scr), DisplayHeight(xdisplay, scr));
//cairo_fill(cr);

//cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_rectangle(cr, init_x, init_y, tmp_x - init_x, tmp_y - init_y); // set rectangle

cairo_fill(cr);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM