@codehz/draw-call 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,284 @@
1
+ /**
2
+ * 示例:使用 draw-call 的 RichText 组件
3
+ * 运行: bun examples/richtext.ts
4
+ */
5
+ import { Box, RichText, Text } from "@codehz/draw-call";
6
+ import { createNodeCanvas } from "@codehz/draw-call/node";
7
+ import { GlobalFonts } from "@napi-rs/canvas";
8
+ import { fileURLToPath } from "bun";
9
+
10
+ GlobalFonts.registerFromPath(fileURLToPath(import.meta.resolve("@fontpkg/unifont/unifont-15.0.01.ttf")), "unifont");
11
+
12
+ const canvas = createNodeCanvas({
13
+ width: 400,
14
+ height: 820,
15
+ pixelRatio: 2,
16
+ });
17
+
18
+ // 绘制背景
19
+ canvas.render(
20
+ Box({
21
+ width: "fill",
22
+ height: "fill",
23
+ background: "#f8f9fa",
24
+ padding: 20,
25
+ direction: "column",
26
+ gap: 20,
27
+ children: [
28
+ // 标题
29
+ Text({
30
+ content: "RichText 富文本演示",
31
+ font: { size: 20, weight: "bold", family: "unifont" },
32
+ color: "#333",
33
+ }),
34
+
35
+ // 基本用法
36
+ Box({
37
+ background: "#ffffff",
38
+ border: { radius: 8 },
39
+ padding: 12,
40
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
41
+ direction: "column",
42
+ gap: 8,
43
+ children: [
44
+ Text({
45
+ content: "基本用法 - 多样式文本",
46
+ font: { size: 12, family: "unifont" },
47
+ color: "#666",
48
+ }),
49
+ RichText({
50
+ spans: [
51
+ { text: "Hello, ", color: "#333", font: { size: 14, family: "unifont" } },
52
+ { text: "World", color: "#ff6b6b", font: { size: 18, weight: "bold", family: "unifont" } },
53
+ { text: "!", color: "#333", font: { size: 14, family: "unifont" } },
54
+ ],
55
+ }),
56
+ ],
57
+ }),
58
+
59
+ // 渐变色文本
60
+ Box({
61
+ background: "#ffffff",
62
+ border: { radius: 8 },
63
+ padding: 12,
64
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
65
+ direction: "column",
66
+ gap: 8,
67
+ children: [
68
+ Text({
69
+ content: "带背景色的文本",
70
+ font: { size: 12, family: "unifont" },
71
+ color: "#666",
72
+ }),
73
+ RichText({
74
+ spans: [
75
+ { text: "红色背景", background: "#ffe8e8", color: "#f5222d" },
76
+ { text: " " },
77
+ { text: "绿色背景", background: "#f6ffed", color: "#52c41a" },
78
+ { text: " " },
79
+ { text: "蓝色背景", background: "#e8f4ff", color: "#1890ff" },
80
+ ],
81
+ }),
82
+ ],
83
+ }),
84
+
85
+ // 下划线和删除线
86
+ Box({
87
+ background: "#ffffff",
88
+ border: { radius: 8 },
89
+ padding: 12,
90
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
91
+ direction: "column",
92
+ gap: 8,
93
+ children: [
94
+ Text({
95
+ content: "文本装饰 - 下划线和删除线",
96
+ font: { size: 12, family: "unifont" },
97
+ color: "#666",
98
+ }),
99
+ RichText({
100
+ spans: [
101
+ { text: "下划线文本", underline: true, color: "#333", font: { size: 14, family: "unifont" } },
102
+ { text: " " },
103
+ { text: "删除线文本", strikethrough: true, color: "#333", font: { size: 14, family: "unifont" } },
104
+ { text: " " },
105
+ {
106
+ text: "组合效果",
107
+ underline: true,
108
+ strikethrough: true,
109
+ color: "#f5222d",
110
+ font: { size: 14, family: "unifont" },
111
+ },
112
+ ],
113
+ }),
114
+ ],
115
+ }),
116
+
117
+ // 自动换行
118
+ Box({
119
+ background: "#ffffff",
120
+ border: { radius: 8 },
121
+ padding: 12,
122
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
123
+ direction: "column",
124
+ gap: 8,
125
+ children: [
126
+ Text({
127
+ content: "自动换行 - 长文本",
128
+ font: { size: 12, family: "unifont" },
129
+ color: "#666",
130
+ }),
131
+ RichText({
132
+ spans: [
133
+ {
134
+ text: "这是一个很长很长的富文本段落,它会自动换行以适应容器的宽度。",
135
+ color: "#333",
136
+ font: { size: 13, family: "unifont" },
137
+ },
138
+ ],
139
+ }),
140
+ ],
141
+ }),
142
+
143
+ // 混合样式换行
144
+ Box({
145
+ background: "#ffffff",
146
+ border: { radius: 8 },
147
+ padding: 12,
148
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
149
+ direction: "column",
150
+ gap: 8,
151
+ children: [
152
+ Text({
153
+ content: "混合样式自动换行",
154
+ font: { size: 12, family: "unifont" },
155
+ color: "#666",
156
+ }),
157
+ RichText({
158
+ spans: [
159
+ { text: "draw-call ", color: "#333", font: { size: 13, family: "unifont" } },
160
+ { text: "是一个", color: "#666", font: { size: 13, family: "unifont" } },
161
+ { text: "声明式", color: "#1890ff", font: { size: 13, weight: "bold", family: "unifont" } },
162
+ { text: " Canvas 绘图库,支持", color: "#333", font: { size: 13, family: "unifont" } },
163
+ { text: "Flexbox 布局", color: "#52c41a", font: { size: 13, family: "unifont" }, underline: true },
164
+ { text: "和", color: "#333", font: { size: 13, family: "unifont" } },
165
+ { text: "富文本渲染", color: "#f5222d", font: { size: 13, weight: "bold", family: "unifont" } },
166
+ { text: "。", color: "#333", font: { size: 13, family: "unifont" } },
167
+ ],
168
+ }),
169
+ ],
170
+ }),
171
+
172
+ // 对齐方式
173
+ Box({
174
+ background: "#ffffff",
175
+ border: { radius: 8 },
176
+ padding: 12,
177
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
178
+ direction: "column",
179
+ gap: 8,
180
+ children: [
181
+ Text({
182
+ content: "文本对齐方式",
183
+ font: { size: 12, family: "unifont" },
184
+ color: "#666",
185
+ }),
186
+ Box({
187
+ width: "fill",
188
+ background: "#f5f5f5",
189
+ border: { radius: 4 },
190
+ padding: 8,
191
+ direction: "column",
192
+ gap: 4,
193
+ children: [
194
+ Text({
195
+ content: "左对齐:",
196
+ font: { size: 11, family: "unifont" },
197
+ color: "#999",
198
+ }),
199
+ RichText({
200
+ align: "left",
201
+ spans: [{ text: "这是一段左对齐的富文本", color: "#333", font: { size: 12, family: "unifont" } }],
202
+ }),
203
+ ],
204
+ }),
205
+ Box({
206
+ width: "fill",
207
+ background: "#f5f5f5",
208
+ border: { radius: 4 },
209
+ padding: 8,
210
+ direction: "column",
211
+ gap: 4,
212
+ children: [
213
+ Text({
214
+ content: "居中对齐:",
215
+ font: { size: 11, family: "unifont" },
216
+ color: "#999",
217
+ }),
218
+ RichText({
219
+ align: "center",
220
+ width: "fill",
221
+ spans: [{ text: "这是一段居中对齐的富文本", color: "#333", font: { size: 12, family: "unifont" } }],
222
+ }),
223
+ ],
224
+ }),
225
+ Box({
226
+ width: "fill",
227
+ background: "#f5f5f5",
228
+ border: { radius: 4 },
229
+ padding: 8,
230
+ direction: "column",
231
+ gap: 4,
232
+ children: [
233
+ Text({
234
+ content: "右对齐:",
235
+ font: { size: 11, family: "unifont" },
236
+ color: "#999",
237
+ }),
238
+ RichText({
239
+ align: "right",
240
+ width: "fill",
241
+ spans: [{ text: "这是一段右对齐的富文本", color: "#333", font: { size: 12, family: "unifont" } }],
242
+ }),
243
+ ],
244
+ }),
245
+ ],
246
+ }),
247
+
248
+ // 彩色标签云
249
+ Box({
250
+ background: "#ffffff",
251
+ border: { radius: 8 },
252
+ padding: 12,
253
+ shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.06)" },
254
+ direction: "column",
255
+ gap: 8,
256
+ children: [
257
+ Text({
258
+ content: "彩色标签展示",
259
+ font: { size: 12, family: "unifont" },
260
+ color: "#666",
261
+ }),
262
+ RichText({
263
+ spans: [
264
+ { text: "TypeScript", background: "#e8f4ff", color: "#1890ff", font: { size: 12, family: "unifont" } },
265
+ { text: " " },
266
+ { text: "Canvas", background: "#f6ffed", color: "#52c41a", font: { size: 12, family: "unifont" } },
267
+ { text: " " },
268
+ { text: "Flexbox", background: "#fff7e6", color: "#fa8c16", font: { size: 12, family: "unifont" } },
269
+ { text: " " },
270
+ { text: "声明式", background: "#ffe8e8", color: "#f5222d", font: { size: 12, family: "unifont" } },
271
+ { text: " " },
272
+ { text: "组件化", background: "#f0f0ff", color: "#2f54eb", font: { size: 12, family: "unifont" } },
273
+ ],
274
+ }),
275
+ ],
276
+ }),
277
+ ],
278
+ })
279
+ );
280
+
281
+ // 保存到文件
282
+ const buffer = await canvas.toBuffer("image/png");
283
+ await Bun.write("examples/richtext.png", buffer);
284
+ console.log("RichText demo saved to examples/richtext.png");