首页 > 解决方案 > 使用 cairo 制作动画时修复图案

问题描述

我正在用 Cairo 为填充有图案的文本制作动画。问题是当文本被移动时,模式也被“移动”了。如何将模式固定在文本字符内?请参阅随附的两个屏幕截图。

if (pattern_filename)
    {
        cairo_surface_t  *tmp_surf;
        cairo_pattern_t  *font_pattern;

        tmp_surf = cairo_image_surface_create_from_png(pattern_filename);
        font_pattern = cairo_pattern_create_for_surface(tmp_surf);
        cairo_pattern_set_extend (font_pattern, CAIRO_EXTEND_REPEAT);

        cairo_set_source(cr, font_pattern);
    }
    else
    {
        /* Draw the subtitle */
        /* Set source color */
        cairo_set_source_rgba( cr, font_color[0],
                                font_color[1],
                                font_color[2],
                                font_color[3] );
    }
    /* Move to proper place and paint text */
    cairo_move_to( cr, posx, posy );
    pango_cairo_show_layout( cr, layout );

谢谢

动画结束 动画开始

标签: imageanimationcairogtk2

解决方案


默认情况下,表面图案将源表面的 (0,0) 放置在目标表面的 (0,0) 处。要更改这一点,您可以使用cairo_set_source_surface直接将曲面设置为具有给定偏移的图案。或者,您可以像在代码中那样显式使用模式,并使用 and 设置一些转换cairo_matrix_init_translate矩阵cairo_pattern_set_matrix


推荐阅读