@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.
- package/README.md +417 -6
- package/canvas.d.cts +101 -6
- package/canvas.d.mts +101 -6
- package/examples/card.ts +155 -0
- package/examples/customdraw-basic.ts +269 -0
- package/examples/customdraw.ts +339 -0
- package/examples/demo.ts +478 -0
- package/examples/richtext.ts +284 -0
- package/examples/transform.ts +437 -0
- package/index.cjs +90 -0
- package/index.d.cts +23 -11
- package/index.d.mts +23 -11
- package/index.mjs +86 -1
- package/node.cjs +2 -2
- package/node.d.cts +2 -2
- package/node.d.mts +2 -2
- package/node.mjs +4 -4
- package/package.json +1 -1
- package/render.cjs +567 -12
- package/render.mjs +567 -12
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 示例:使用 CustomDraw 组件进行自定义绘制
|
|
3
|
+
* 展示直接调用 Canvas API、Transform 管理和子元素渲染功能
|
|
4
|
+
* 运行: bun examples/customdraw.ts
|
|
5
|
+
*/
|
|
6
|
+
import { Box, CustomDraw, printLayout, Text } from "@codehz/draw-call";
|
|
7
|
+
import { createNodeCanvas } from "@codehz/draw-call/node";
|
|
8
|
+
import { GlobalFonts } from "@napi-rs/canvas";
|
|
9
|
+
import { fileURLToPath } from "bun";
|
|
10
|
+
|
|
11
|
+
GlobalFonts.registerFromPath(fileURLToPath(import.meta.resolve("@fontpkg/unifont/unifont-15.0.01.ttf")), "unifont");
|
|
12
|
+
|
|
13
|
+
const canvas = createNodeCanvas({
|
|
14
|
+
width: 800,
|
|
15
|
+
height: 1000,
|
|
16
|
+
pixelRatio: 2,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 绘制饼图
|
|
20
|
+
function PieChart(data: Array<{ label: string; value: number; color: string }>) {
|
|
21
|
+
const total = data.reduce((sum, item) => sum + item.value, 0);
|
|
22
|
+
|
|
23
|
+
return CustomDraw({
|
|
24
|
+
width: 200,
|
|
25
|
+
height: 200,
|
|
26
|
+
draw(ctx, { width, height }) {
|
|
27
|
+
const centerX = width / 2;
|
|
28
|
+
const centerY = height / 2;
|
|
29
|
+
const radius = Math.min(width, height) / 2 - 5;
|
|
30
|
+
|
|
31
|
+
let currentAngle = -Math.PI / 2;
|
|
32
|
+
|
|
33
|
+
for (const item of data) {
|
|
34
|
+
const sliceAngle = (item.value / total) * Math.PI * 2;
|
|
35
|
+
|
|
36
|
+
// 绘制扇形
|
|
37
|
+
ctx.beginPath();
|
|
38
|
+
ctx.moveTo(centerX, centerY);
|
|
39
|
+
ctx.arc(centerX, centerY, radius, currentAngle, currentAngle + sliceAngle);
|
|
40
|
+
ctx.closePath();
|
|
41
|
+
ctx.fillStyle = item.color;
|
|
42
|
+
ctx.fill();
|
|
43
|
+
|
|
44
|
+
// 绘制边框
|
|
45
|
+
ctx.strokeStyle = "#ffffff";
|
|
46
|
+
ctx.lineWidth = 2;
|
|
47
|
+
ctx.stroke();
|
|
48
|
+
|
|
49
|
+
currentAngle += sliceAngle;
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 绘制网格背景
|
|
56
|
+
function GridBackground() {
|
|
57
|
+
return CustomDraw({
|
|
58
|
+
width: 100,
|
|
59
|
+
height: 100,
|
|
60
|
+
draw(ctx, { width, height }) {
|
|
61
|
+
const gridSize = 20;
|
|
62
|
+
|
|
63
|
+
ctx.strokeStyle = "rgba(200, 200, 200, 0.3)";
|
|
64
|
+
ctx.lineWidth = 1;
|
|
65
|
+
|
|
66
|
+
// 绘制垂直线
|
|
67
|
+
for (let x = 0; x <= width; x += gridSize) {
|
|
68
|
+
ctx.beginPath();
|
|
69
|
+
ctx.moveTo(x, 0);
|
|
70
|
+
ctx.lineTo(x, height);
|
|
71
|
+
ctx.stroke();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 绘制水平线
|
|
75
|
+
for (let y = 0; y <= height; y += gridSize) {
|
|
76
|
+
ctx.beginPath();
|
|
77
|
+
ctx.moveTo(0, y);
|
|
78
|
+
ctx.lineTo(width, y);
|
|
79
|
+
ctx.stroke();
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 绘制圆形进度条
|
|
86
|
+
function ProgressRing(percentage: number, color: string) {
|
|
87
|
+
return CustomDraw({
|
|
88
|
+
width: 120,
|
|
89
|
+
height: 120,
|
|
90
|
+
draw(ctx, { inner, width, height }) {
|
|
91
|
+
const centerX = width / 2;
|
|
92
|
+
const centerY = height / 2;
|
|
93
|
+
const radius = Math.min(width, height) / 2 - 8;
|
|
94
|
+
|
|
95
|
+
// 绘制背景圆
|
|
96
|
+
ctx.strokeStyle = "rgba(0, 0, 0, 0.1)";
|
|
97
|
+
ctx.lineWidth = 8;
|
|
98
|
+
ctx.beginPath();
|
|
99
|
+
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
100
|
+
ctx.stroke();
|
|
101
|
+
|
|
102
|
+
// 绘制进度圆
|
|
103
|
+
const endAngle = (percentage / 100) * Math.PI * 2 - Math.PI / 2;
|
|
104
|
+
ctx.strokeStyle = color;
|
|
105
|
+
ctx.lineCap = "round";
|
|
106
|
+
ctx.beginPath();
|
|
107
|
+
ctx.arc(centerX, centerY, radius, -Math.PI / 2, endAngle);
|
|
108
|
+
ctx.stroke();
|
|
109
|
+
|
|
110
|
+
// 绘制百分比文本的背景
|
|
111
|
+
ctx.save();
|
|
112
|
+
ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
|
|
113
|
+
ctx.globalAlpha = 0.1;
|
|
114
|
+
ctx.fillRect(centerX - 30, centerY - 20, 60, 40);
|
|
115
|
+
ctx.restore();
|
|
116
|
+
|
|
117
|
+
inner?.();
|
|
118
|
+
},
|
|
119
|
+
children: Box({
|
|
120
|
+
width: "fill",
|
|
121
|
+
height: "fill",
|
|
122
|
+
justify: "center",
|
|
123
|
+
align: "center",
|
|
124
|
+
children: [
|
|
125
|
+
Text({
|
|
126
|
+
content: `${percentage}%`,
|
|
127
|
+
font: { size: 20, weight: "bold", family: "unifont" },
|
|
128
|
+
color: color,
|
|
129
|
+
}),
|
|
130
|
+
],
|
|
131
|
+
}),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 绘制折线图
|
|
136
|
+
function LineChart() {
|
|
137
|
+
const data = [10, 25, 15, 30, 20, 35, 25, 40];
|
|
138
|
+
|
|
139
|
+
return CustomDraw({
|
|
140
|
+
width: 300,
|
|
141
|
+
height: 150,
|
|
142
|
+
draw(ctx, { width, height }) {
|
|
143
|
+
const padding = 10;
|
|
144
|
+
const graphWidth = width - padding * 2;
|
|
145
|
+
const graphHeight = height - padding * 2;
|
|
146
|
+
|
|
147
|
+
const maxValue = Math.max(...data);
|
|
148
|
+
const pointSpacing = graphWidth / (data.length - 1);
|
|
149
|
+
|
|
150
|
+
// 绘制网格线
|
|
151
|
+
ctx.strokeStyle = "rgba(200, 200, 200, 0.2)";
|
|
152
|
+
ctx.lineWidth = 1;
|
|
153
|
+
for (let i = 0; i <= 4; i++) {
|
|
154
|
+
const y = padding + (graphHeight / 4) * i;
|
|
155
|
+
ctx.beginPath();
|
|
156
|
+
ctx.moveTo(padding, y);
|
|
157
|
+
ctx.lineTo(width - padding, y);
|
|
158
|
+
ctx.stroke();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 绘制折线
|
|
162
|
+
ctx.strokeStyle = "#667eea";
|
|
163
|
+
ctx.lineWidth = 2;
|
|
164
|
+
ctx.beginPath();
|
|
165
|
+
|
|
166
|
+
for (let i = 0; i < data.length; i++) {
|
|
167
|
+
const x = padding + i * pointSpacing;
|
|
168
|
+
const y = height - padding - (data[i] / maxValue) * graphHeight;
|
|
169
|
+
|
|
170
|
+
if (i === 0) {
|
|
171
|
+
ctx.moveTo(x, y);
|
|
172
|
+
} else {
|
|
173
|
+
ctx.lineTo(x, y);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
ctx.stroke();
|
|
178
|
+
|
|
179
|
+
// 绘制数据点
|
|
180
|
+
ctx.fillStyle = "#667eea";
|
|
181
|
+
for (let i = 0; i < data.length; i++) {
|
|
182
|
+
const x = padding + i * pointSpacing;
|
|
183
|
+
const y = height - padding - (data[i] / maxValue) * graphHeight;
|
|
184
|
+
|
|
185
|
+
ctx.beginPath();
|
|
186
|
+
ctx.arc(x, y, 3, 0, Math.PI * 2);
|
|
187
|
+
ctx.fill();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 绘制阴影
|
|
191
|
+
ctx.save();
|
|
192
|
+
ctx.globalAlpha = 0.2;
|
|
193
|
+
ctx.fillStyle = "#667eea";
|
|
194
|
+
ctx.beginPath();
|
|
195
|
+
ctx.moveTo(padding, height - padding);
|
|
196
|
+
|
|
197
|
+
for (let i = 0; i < data.length; i++) {
|
|
198
|
+
const x = padding + i * pointSpacing;
|
|
199
|
+
const y = height - padding - (data[i] / maxValue) * graphHeight;
|
|
200
|
+
ctx.lineTo(x, y);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
ctx.lineTo(width - padding, height - padding);
|
|
204
|
+
ctx.closePath();
|
|
205
|
+
ctx.fill();
|
|
206
|
+
ctx.restore();
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const layout = canvas.render(
|
|
212
|
+
Box({
|
|
213
|
+
width: "fill",
|
|
214
|
+
height: "fill",
|
|
215
|
+
background: "#f5f7fa",
|
|
216
|
+
padding: 20,
|
|
217
|
+
direction: "column",
|
|
218
|
+
gap: 20,
|
|
219
|
+
children: [
|
|
220
|
+
// 标题
|
|
221
|
+
Text({
|
|
222
|
+
content: "CustomDraw 组件演示",
|
|
223
|
+
font: { size: 28, weight: "bold", family: "unifont" },
|
|
224
|
+
color: "#333333",
|
|
225
|
+
}),
|
|
226
|
+
|
|
227
|
+
Text({
|
|
228
|
+
content: "直接调用 Canvas API 进行自定义绘制,同时支持可选的子元素",
|
|
229
|
+
font: { size: 14, family: "unifont" },
|
|
230
|
+
color: "#666666",
|
|
231
|
+
}),
|
|
232
|
+
|
|
233
|
+
// 第一部分:简单绘制
|
|
234
|
+
Box({
|
|
235
|
+
direction: "column",
|
|
236
|
+
gap: 10,
|
|
237
|
+
children: [
|
|
238
|
+
Text({
|
|
239
|
+
content: "1. 网格背景 + 直接 Canvas 绘制",
|
|
240
|
+
font: { size: 16, weight: "bold", family: "unifont" },
|
|
241
|
+
color: "#666666",
|
|
242
|
+
}),
|
|
243
|
+
Box({
|
|
244
|
+
background: "#ffffff",
|
|
245
|
+
border: { radius: 8 },
|
|
246
|
+
clip: true,
|
|
247
|
+
children: [GridBackground()],
|
|
248
|
+
}),
|
|
249
|
+
],
|
|
250
|
+
}),
|
|
251
|
+
|
|
252
|
+
// 第二部分:饼图
|
|
253
|
+
Box({
|
|
254
|
+
direction: "column",
|
|
255
|
+
gap: 10,
|
|
256
|
+
children: [
|
|
257
|
+
Text({
|
|
258
|
+
content: "2. 饼图示例",
|
|
259
|
+
font: { size: 16, weight: "bold", family: "unifont" },
|
|
260
|
+
color: "#666666",
|
|
261
|
+
}),
|
|
262
|
+
Box({
|
|
263
|
+
background: "#ffffff",
|
|
264
|
+
border: { radius: 8 },
|
|
265
|
+
padding: 20,
|
|
266
|
+
justify: "center",
|
|
267
|
+
align: "center",
|
|
268
|
+
direction: "row",
|
|
269
|
+
gap: 40,
|
|
270
|
+
children: [
|
|
271
|
+
PieChart([
|
|
272
|
+
{ label: "类别 A", value: 30, color: "#667eea" },
|
|
273
|
+
{ label: "类别 B", value: 25, color: "#764ba2" },
|
|
274
|
+
{ label: "类别 C", value: 25, color: "#f093fb" },
|
|
275
|
+
{ label: "类别 D", value: 20, color: "#4facfe" },
|
|
276
|
+
]),
|
|
277
|
+
PieChart([
|
|
278
|
+
{ label: "类别 1", value: 40, color: "#ff6b6b" },
|
|
279
|
+
{ label: "类别 2", value: 35, color: "#ff922b" },
|
|
280
|
+
{ label: "类别 3", value: 25, color: "#ffd93d" },
|
|
281
|
+
]),
|
|
282
|
+
],
|
|
283
|
+
}),
|
|
284
|
+
],
|
|
285
|
+
}),
|
|
286
|
+
|
|
287
|
+
// 第三部分:进度圈(子元素示例)
|
|
288
|
+
Box({
|
|
289
|
+
direction: "column",
|
|
290
|
+
gap: 10,
|
|
291
|
+
children: [
|
|
292
|
+
Text({
|
|
293
|
+
content: "3. 进度圆环(包含子元素内容)",
|
|
294
|
+
font: { size: 16, weight: "bold", family: "unifont" },
|
|
295
|
+
color: "#666666",
|
|
296
|
+
}),
|
|
297
|
+
Box({
|
|
298
|
+
background: "#ffffff",
|
|
299
|
+
border: { radius: 8 },
|
|
300
|
+
padding: 20,
|
|
301
|
+
direction: "row",
|
|
302
|
+
gap: 20,
|
|
303
|
+
justify: "center",
|
|
304
|
+
align: "center",
|
|
305
|
+
children: [ProgressRing(75, "#667eea"), ProgressRing(45, "#764ba2"), ProgressRing(90, "#ff6b6b")],
|
|
306
|
+
}),
|
|
307
|
+
],
|
|
308
|
+
}),
|
|
309
|
+
|
|
310
|
+
// 第四部分:折线图
|
|
311
|
+
Box({
|
|
312
|
+
direction: "column",
|
|
313
|
+
gap: 10,
|
|
314
|
+
children: [
|
|
315
|
+
Text({
|
|
316
|
+
content: "4. 折线图与网格",
|
|
317
|
+
font: { size: 16, weight: "bold", family: "unifont" },
|
|
318
|
+
color: "#666666",
|
|
319
|
+
}),
|
|
320
|
+
Box({
|
|
321
|
+
background: "#ffffff",
|
|
322
|
+
border: { radius: 8 },
|
|
323
|
+
padding: 10,
|
|
324
|
+
children: [LineChart()],
|
|
325
|
+
}),
|
|
326
|
+
],
|
|
327
|
+
}),
|
|
328
|
+
],
|
|
329
|
+
})
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
// 保存文件
|
|
333
|
+
const buffer = await canvas.toBuffer("image/png");
|
|
334
|
+
await Bun.write("examples/customdraw.png", buffer);
|
|
335
|
+
console.log("✅ CustomDraw demo saved to examples/customdraw.png");
|
|
336
|
+
|
|
337
|
+
// 打印布局树
|
|
338
|
+
console.log("\n=== Layout Tree ===");
|
|
339
|
+
printLayout(layout);
|