我正在尝试使用 Cairo 在透明 X 窗口上绘图。据开罗官方FAQ ,清除透明表面的一种方法是将 Cairo 操作符设置为 CLEAR。这似乎不适用于我的情况。我正在画一个不断增大和缩小的实心圆。最初,圆圈会变大,但当它缩小时,大圆圈仍保留在窗口上。这让我觉得也许我应该在每次重绘时清除 X 窗口本身。
这是我想出的代码
XClearWindow(self->display, self->win_id);
cairo_push_group(self->context);
// Re-draw the whole canvas: Doesn't work as expected
// cairo_save(self->context);
// cairo_set_operator(self->context, CAIRO_OPERATOR_CLEAR);
// cairo_paint(self->context);
// cairo_restore(self->context);
// Invoke the draw callback <- Cairo drawing done here from Python
Canvas_on_draw(self, args_tuple, NULL);
cairo_pop_group_to_source(self->context);
cairo_paint(self->context);
cairo_surface_flush(self->surface);
XFlush(self->display);
如您所见,我的解决方案是在使用 Cairo 绘图之前调用 XClearWindow
,然后使用 XFlush
刷新所有内容。但是,我不确定这是最干净的解决方案,而且感觉像是一种黑客攻击,而不是正确的方法。例如,如果没有 XFlush
,我会出现相当大的闪烁,但 Xlib 文档似乎暗示大多数应用程序不需要直接调用此函数。
编辑:在下面的答案之后,这就是我的代码:
cairo_push_group(self->context);
// Draw stuff
cairo_pop_group_to_source(self->context);
// The following cairo paradigm seems to have the same
// effect as the following commented out lines:
// XClearWindow(self->display, self->win_id);
// cairo_paint(self->context);
cairo_save(self->context);
cairo_set_operator(self->context, CAIRO_OPERATOR_SOURCE);
cairo_paint(self->context);
cairo_restore(self->context);
这完成了预期的事情。
最佳答案
添加cairo_set_operator(self->context, CAIRO_OPERATOR_SOURCE);
在你的期末考试之前cairo_paint()
.
您正在清除您创建的中间组。这意味着它在圆之外是完全透明的。然后,使用默认运算符 OVER
将此组绘制到目标窗口。 。在任何东西上绘制完全透明只会留下旧内容。使用SOURCE
绘图实际上将源复制到目标表面。
旁注:您的XClearWindow
除非您调用cairo_surface_flush
,否则不能保证该方法有效。和cairo_surface_mark_dirty
根据需要围绕它。
关于c - 通过开罗表面绘画时清除 X 窗口的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50380046/