1+
12---
23title: Canvas 模块
34sidebar_position: 400
@@ -6,16 +7,16 @@ sidebar_position: 400
67import Image00 from ' ./00.png' ;
78import Image01 from ' ./01.png' ;
89
9- Canvas 模块提供了一组简洁的 API 用于在 DQL 脚本中实现绘图功能。
10+ Canvas 模块提供了一组简洁的 API, 用于在 DQL 脚本中实现绘图功能。
1011
1112下面是一个简单的例子,绘制一个圆形图案。
1213
1314``` python
14- dc = canvas.Context(1000 , 1000 )
15- dc.draw_circle(500 , 500 , 400 )
16- dc.set_hex_color(" #FF0000" )
17- dc.fill()
18- return dc.get_data_uri()
15+ dc = canvas.Context(1000 , 1000 ) # 创建画布对象,指定大小为1000x1000
16+ dc.draw_circle(500 , 500 , 400 ) # 在画布上绘制一个圆形,圆心为(500, 500),半径为400
17+ dc.set_hex_color(" #FF0000" ) # 设置颜色为红色
18+ dc.fill() # 填充圆形
19+ return dc.get_data_uri() # 返回图像的 Data URI
1920```
2021
2122图片效果为:
@@ -26,7 +27,6 @@ return dc.get_data_uri()
2627Canvas API 基于 [ https://github.com/fogleman/gg ] ( https://github.com/fogleman/gg ) 实现,更多详细信息请参考 [ https://github.com/fogleman/gg ] ( https://github.com/fogleman/gg ) 。
2728:::
2829
29-
3030## API
3131
3232``` python
@@ -42,138 +42,138 @@ canvas.degrees(radians float64) float64
4242
4343## Context
4444
45- context 类似 HTML Canvas API 的 Context2D,用于提供绘制相关的函数。
45+ ` context ` 类似 HTML Canvas API 的 ` Context2D ` ,用于提供绘制相关的函数。
4646
4747### 画图
4848
4949``` python
50- draw_point(x, y, r float64)
51- draw_line(x1, y1, x2, y2 float64)
52- draw_rectangle(x, y, w, h float64)
53- draw_rounded_rectangle(x, y, w, h, r float64)
54- draw_circle(x, y, r float64)
55- draw_arc(x, y, r, angle1, angle2 float64)
56- draw_ellipse(x, y, rx, ry float64)
57- draw_elliptical_arc(x, y, rx, ry, angle1, angle2 float64)
58- draw_regular_polygon(n int , x, y, r, rotation float64)
59- set_pixel(x, y int )
60-
61- move_to(x, y float64)
62- line_to(x, y float64)
63- quadratic_to(x1, y1, x2, y2 float64)
64- cubic_to(x1, y1, x2, y2, x3, y3 float64)
65- close_path()
66- clear_path()
67- new_sub_path()
68-
69- clear()
70- stroke()
71- fill()
72- stroke_preserve()
73- fill_preserve()
50+ draw_point(x, y, r float64) # 绘制一个点
51+ draw_line(x1, y1, x2, y2 float64) # 绘制一条线段
52+ draw_rectangle(x, y, w, h float64) # 绘制矩形
53+ draw_rounded_rectangle(x, y, w, h, r float64) # 绘制圆角矩形
54+ draw_circle(x, y, r float64) # 绘制圆形
55+ draw_arc(x, y, r, angle1, angle2 float64) # 绘制圆弧
56+ draw_ellipse(x, y, rx, ry float64) # 绘制椭圆
57+ draw_elliptical_arc(x, y, rx, ry, angle1, angle2 float64) # 绘制椭圆弧
58+ draw_regular_polygon(n int , x, y, r, rotation float64) # 绘制正多边形
59+ set_pixel(x, y int ) # 设置像素点
60+
61+ move_to(x, y float64) # 移动到指定坐标
62+ line_to(x, y float64) # 绘制一条线到指定坐标
63+ quadratic_to(x1, y1, x2, y2 float64) # 绘制二次贝塞尔曲线
64+ cubic_to(x1, y1, x2, y2, x3, y3 float64) # 绘制三次贝塞尔曲线
65+ close_path() # 关闭路径
66+ clear_path() # 清除路径
67+ new_sub_path() # 新建子路径
68+
69+ clear() # 清除画布
70+ stroke() # 描边
71+ fill() # 填充
72+ stroke_preserve() # 描边并保持路径
73+ fill_preserve() # 填充并保持路径
7474```
7575
76- 通常希望将图像居中于一点。
77- 使用 ` draw_image_anchored ` 并将 ` ax ` 和 ` ay ` 设置为 ` 0.5 ` 来执行此操作。
78- 使用 ` 0 ` 左对齐或上对齐。
79- 使用 ` 1 ` 右对齐或下对齐。
76+ 通常希望将图像居中于一点。
77+ 使用 ` draw_image_anchored ` 并将 ` ax ` 和 ` ay ` 设置为 ` 0.5 ` 来执行此操作。
78+ 使用 ` 0 ` 左对齐或上对齐。
79+ 使用 ` 1 ` 右对齐或下对齐。
8080` draw_string_anchored ` 对文本执行相同的操作,因此您无需自己调用 ` measure_string ` 。
8181
8282### 文本
8383
8484``` python
85- draw_string(s string, x, y float64)
86- draw_string_anchored(s string, x, y, ax, ay float64)
87- draw_string_wrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align) # Align: left=0, center = 1, right = 2
88- measure_string(s string) (w, h float64)
89- measure_multiline_string(s string, lineSpacing float64) (w, h float64)
90- word_wrap(s string, w float64) []string
85+ draw_string(s string, x, y float64) # 绘制文本
86+ draw_string_anchored(s string, x, y, ax, ay float64) # 绘制锚定位置的文本
87+ draw_string_wrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align) # 换行文本
88+ measure_string(s string) (w, h float64) # 测量字符串的宽度和高度
89+ measure_multiline_string(s string, lineSpacing float64) (w, h float64) # 测量多行字符串的宽度和高度
90+ word_wrap(s string, w float64) []string # 将文本按宽度换行
9191```
9292
9393### 颜色
9494
9595``` python
96- set_rgb(r, g, b float64)
97- set_rgba(r, g, b, a float64)
98- set_rgb255(r, g, b int )
99- set_rgba255(r, g, b, a int )
100- set_hex_color(x string)
96+ set_rgb(r, g, b float64) # 设置RGB颜色
97+ set_rgba(r, g, b, a float64) # 设置RGBA颜色
98+ set_rgb255(r, g, b int ) # 设置RGB颜色(255范围)
99+ set_rgba255(r, g, b, a int ) # 设置RGBA颜色(255范围)
100+ set_hex_color(x string) # 设置十六进制颜色
101101```
102102
103103### 描边和填充
104104
105105``` python
106- set_line_width(lineWidth float64)
107- set_line_cap(lineCap LineCap) # LineCap: Round = 0, Butt = 1, Square = 2
108- set_line_join(lineJoin LineJoin) # LineJoin: Round = 0, Bevel = 1
109- set_dash(dashes ... float64)
110- set_dash_offset(offset float64)
111- set_fill_rule(fillRule FillRule) # Winding = 0, EvenOdd = 1
106+ set_line_width(lineWidth float64) # 设置线条宽度
107+ set_line_cap(lineCap LineCap) # 设置线条端点样式
108+ set_line_join(lineJoin LineJoin) # 设置线条连接样式
109+ set_dash(dashes ... float64) # 设置虚线样式
110+ set_dash_offset(offset float64) # 设置虚线的偏移
111+ set_fill_rule(fillRule FillRule) # 设置填充规则
112112```
113113
114114### 渐变和图案
115115
116116``` python
117- new_linear_gradient(x0, y0, x1, y1 float64)
118- new_radial_gradient(x0, y0, r0, x1, y1, r1 float64)
119- new_conic_gradient(cx, cy, deg float64)
117+ new_linear_gradient(x0, y0, x1, y1 float64) # 创建线性渐变
118+ new_radial_gradient(x0, y0, r0, x1, y1, r1 float64) # 创建径向渐变
119+ new_conic_gradient(cx, cy, deg float64) # 创建圆锥渐变
120120```
121121
122122### 变形
123123
124124``` python
125- identity()
126- translate(x, y float64)
127- scale(x, y float64)
128- rotate(angle float64)
129- shear(x, y float64)
130- scale_about(sx, sy, x, y float64)
131- rotate_about(angle, x, y float64)
132- shear_about(sx, sy, x, y float64)
133- transform_point(x, y float64) (tx, ty float64)
134- invert_y()
125+ identity() # 恢复默认变换
126+ translate(x, y float64) # 平移变换
127+ scale(x, y float64) # 缩放变换
128+ rotate(angle float64) # 旋转变换
129+ shear(x, y float64) # 剪切变换
130+ scale_about(sx, sy, x, y float64) # 绕点缩放
131+ rotate_about(angle, x, y float64) # 绕点旋转
132+ shear_about(sx, sy, x, y float64) # 绕点剪切
133+ transform_point(x, y float64) (tx, ty float64) # 变换点坐标
134+ invert_y() # 反转Y轴方向
135135```
136136
137- 经常需要围绕一个非原点的点进行旋转或缩放。为了方便起见,我们提供了旋转(RotateAbout)、缩放(ScaleAbout)和剪切(ShearAbout)函数。
137+ 经常需要围绕一个非原点的点进行旋转或缩放。为了方便起见,我们提供了旋转(` RotateAbout ` )、缩放(` ScaleAbout ` )和剪切(` ShearAbout ` )函数。
138138
139- InvertY 是为了防止 Y 应从下到上增加而不是默认的从上到下增加。
139+ ` InvertY ` 是为了防止 Y 应从下到上增加而不是默认的从上到下增加。
140140
141- ### 堆栈
141+ ### 状态保存/恢复
142142
143143保存和恢复上下文的状态。这些可以嵌套。
144144
145145``` python
146- push()
147- pop()
146+ push() # 保存当前状态
147+ pop() # 恢复上一个状态
148148```
149149
150150### 裁剪
151151
152152使用裁剪区域可将绘图操作限制在使用路径定义的区域内。
153153
154154``` python
155- clip()
156- clip_preserve()
157- reset_clip()
158- invert_mask()
155+ clip() # 启用裁剪
156+ clip_preserve() # 启用裁剪并保持当前路径
157+ reset_clip() # 重置裁剪区域
158+ invert_mask() # 反转掩码
159159```
160160
161161## 例子
162162
163- 这是另外一个例子
163+ 这是另外一个例子,绘制旋转的椭圆:
164164
165165``` python
166- S = 1024
167- dc = canvas.Context(S, S)
168- dc.set_rgba(0 , 0 , 0 , 0.1 )
169- for i in range (0 , 360 , 15 ):
170- dc.push()
171- dc.rotate_about(canvas.radians(i), S/ 2 , S/ 2 )
172- dc.draw_ellipse(S/ 2 , S/ 2 , S* 7 / 16 , S/ 8 )
173- dc.fill()
174- dc.pop()
175-
176- return dc.get_data_uri()
166+ S = 1024 # 设置画布大小
167+ dc = canvas.Context(S, S) # 创建画布对象
168+ dc.set_rgba(0 , 0 , 0 , 0.1 ) # 设置颜色为半透明黑色
169+ for i in range (0 , 360 , 15 ): # 循环绘制多个旋转的椭圆
170+ dc.push() # 保存当前状态
171+ dc.rotate_about(canvas.radians(i), S/ 2 , S/ 2 ) # 绕画布中心旋转
172+ dc.draw_ellipse(S/ 2 , S/ 2 , S* 7 / 16 , S/ 8 ) # 绘制椭圆
173+ dc.fill() # 填充颜色
174+ dc.pop() # 恢复到之前的状态
175+
176+ return dc.get_data_uri() # 返回图像的 Data URI
177177```
178178
179179<img src = { Image01 .src } height = { 320 } />
0 commit comments