Cairo 图形指南8裁剪与遮蔽.docx
- 文档编号:3246664
- 上传时间:2022-11-21
- 格式:DOCX
- 页数:14
- 大小:54.29KB
Cairo 图形指南8裁剪与遮蔽.docx
《Cairo 图形指南8裁剪与遮蔽.docx》由会员分享,可在线阅读,更多相关《Cairo 图形指南8裁剪与遮蔽.docx(14页珍藏版)》请在冰豆网上搜索。
Cairo图形指南8裁剪与遮蔽
Cairo图形指南(8)——裁剪与遮蔽
在这一篇中讲述裁剪(Clipping)与遮蔽(Masking)。
裁剪
在下面的示例中,对一幅图像进行裁剪。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include
#include
#include
cairo_surface_t*image;
staticgboolean
on_expose_event(GtkWidget*widget,
GdkEventExpose*event,
gpointerdata)
{
cairo_t*cr;
staticgintpos_x=128;
staticgintpos_y=128;
gintradius=40;
staticgintdelta[]={3,3};
cr=gdk_cairo_create(widget->window);
gintwidth,height;
gtk_window_get_size(GTK_WINDOW(widget),&width,&height);
if(pos_x<0+radius){
delta[0]=rand()%4+5;
}elseif(pos_x>width-radius){
delta[0]=-(rand()%4+5);
}
if(pos_y<0+radius){
delta[1]=rand()%4+5;
}elseif(pos_y>height-radius){
delta[1]=-(rand()%4+5);
}
pos_x+=delta[0];
pos_y+=delta[1];
cairo_set_source_surface(cr,image,1,1);
cairo_arc(cr,pos_x,pos_y,radius,0,2*M_PI);
cairo_clip(cr);
cairo_paint(cr);
cairo_destroy(cr);
returnFALSE;
}
staticgboolean
time_handler(GtkWidget*widget)
{
if(widget->window==NULL)returnFALSE;
gtk_widget_queue_draw(widget);
returnTRUE;
}
intmain(intargc,char*argv[])
{
GtkWidget*window;
gintwidth,height;
image=cairo_image_surface_create_from_png("turnacastle.png");
width=cairo_image_surface_get_width(image);
height=cairo_image_surface_get_height(image);
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window),"expose-event",
G_CALLBACK(on_expose_event),NULL);
g_signal_connect(G_OBJECT(window),"destroy",
G_CALLBACK(gtk_main_quit),NULL);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window),width+2,height+2);
gtk_widget_set_app_paintable(window,TRUE);
gtk_widget_show_all(window);
g_timeout_add(100,(GSourceFunc)time_handler,(gpointer)window);
gtk_main();
cairo_surface_destroy(image);
return0;
}
在这一示例中,在窗口中会有一个圆形区域不断移动,并且在该区域显示位于其下的图像,仿佛是通过一个孔洞观看图像。
?
1
2
3
4
5
if(pos_x<0+radius){
delta[0]=rand()%4+5;
}elseif(pos_x>width-radius){
delta[0]=-(rand()%4+5);
}
当这个圆形区域碰到窗口边界,它的移动方向就会随机改变。
?
1
2
cairo_set_source_surface(cr,image,1,1);
cairo_arc(cr,pos_x,pos_y,radius,0,2*M_PI);
这里是绘制一幅图像和一个圆。
注意:
这时,图形尚未绘制到窗口中,它们还在内存里。
?
1
cairo_clip(cr);
cairo_clip()函数设定裁剪域——当前所用的路径,即cairo_arc()函数所创建的路径。
?
1
cairo_paint(cr);
cairo_paint()函数绘制当前落入裁剪域中的源。
裁剪矩形
下面这个示例是对一个Java2D示例的模拟。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include
#include
#include
staticgboolean
on_expose_event(GtkWidget*widget,
GdkEventExpose*event,
gpointerdata)
{
cairo_t*cr;
cr=gdk_cairo_create(widget->window);
staticgbooleanxdirection=TRUE;
staticgintcounter=0;
intwidth,height;
gtk_window_get_size(GTK_WINDOW(widget),&width,&height);
staticgdoublerotate=0;
staticgintbigx=20;
staticgintbigy=200;
staticgintdelta=1;
counter+=1;
if(bigx>width){
xdirection=FALSE;
delta=-delta;
bigx=width;
}
if(bigx<1){
bigx=1;
delta=-delta;
}
if(bigy>height){
xdirection=TRUE;
delta=-delta;
bigy=height;
}
if(bigy<1){
delta=-delta;
bigy=1;
}
if(xdirection){
bigx+=delta;
}else{
bigy+=delta;
}
cairo_translate(cr,width/2,height/2);
cairo_rectangle(cr,-bigx/2,-bigy/2,bigx-2,bigy-2);
cairo_set_source_rgb(cr,0,0,0);
cairo_set_line_width(cr,1);
cairo_stroke(cr);
cairo_rotate(cr,rotate);
rotate+=0.01;
cairo_rectangle(cr,-50,-25,100,50);
cairo_stroke(cr);
GdkRectanglebigrect;
GdkRectanglerect;
GdkRectangleintersect;
bigrect.x=-bigx/2;
bigrect.y=-bigy/2;
bi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Cairo 图形指南8 裁剪与遮蔽 图形 指南 裁剪 遮蔽