@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/canvas.d.mts CHANGED
@@ -67,6 +67,19 @@ interface Bounds {
67
67
  height: number;
68
68
  }
69
69
  //#endregion
70
+ //#region src/layout/utils/measure.d.ts
71
+ interface MeasureTextResult {
72
+ width: number;
73
+ height: number;
74
+ offset: number;
75
+ ascent: number;
76
+ descent: number;
77
+ }
78
+ interface MeasureContext {
79
+ measureText(text: string, font: FontProps): MeasureTextResult;
80
+ }
81
+ declare function createCanvasMeasureContext(ctx: CanvasRenderingContext2D): MeasureContext;
82
+ //#endregion
70
83
  //#region src/types/layout.d.ts
71
84
  type FlexDirection = "row" | "column" | "row-reverse" | "column-reverse";
72
85
  type JustifyContent = "start" | "end" | "center" | "space-between" | "space-around" | "space-evenly";
@@ -101,12 +114,32 @@ interface ComputedLayout {
101
114
  contentWidth: number;
102
115
  contentHeight: number;
103
116
  }
117
+ interface RichTextSpanSegment {
118
+ text: string;
119
+ font: FontProps;
120
+ color: Color;
121
+ background: Color;
122
+ underline: boolean;
123
+ strikethrough: boolean;
124
+ width: number;
125
+ height: number;
126
+ ascent: number;
127
+ descent: number;
128
+ offset: number;
129
+ }
130
+ interface RichTextLine {
131
+ segments: RichTextSpanSegment[];
132
+ width: number;
133
+ height: number;
134
+ baseline: number;
135
+ }
104
136
  interface LayoutNode {
105
137
  element: Element;
106
138
  layout: ComputedLayout;
107
139
  children: LayoutNode[];
108
140
  lines?: string[];
109
141
  lineOffsets?: number[];
142
+ richLines?: RichTextLine[];
110
143
  }
111
144
  interface LayoutConstraints {
112
145
  minWidth: number;
@@ -116,7 +149,12 @@ interface LayoutConstraints {
116
149
  }
117
150
  //#endregion
118
151
  //#region src/types/components.d.ts
119
- type ElementType = "box" | "text" | "image" | "svg" | "stack";
152
+ type ElementType = "box" | "text" | "richtext" | "image" | "svg" | "stack" | "transform" | "customdraw";
153
+ interface ProxiedCanvasContextOptions {
154
+ inner?: () => void;
155
+ width: number;
156
+ height: number;
157
+ }
120
158
  interface ElementBase {
121
159
  type: ElementType;
122
160
  }
@@ -147,6 +185,27 @@ interface TextProps extends LayoutProps {
147
185
  interface TextElement extends ElementBase, TextProps {
148
186
  type: "text";
149
187
  }
188
+ interface RichTextStyleProps {
189
+ font?: FontProps;
190
+ color?: Color;
191
+ background?: Color;
192
+ underline?: boolean;
193
+ strikethrough?: boolean;
194
+ }
195
+ interface RichTextSpan extends RichTextStyleProps {
196
+ text: string;
197
+ }
198
+ interface RichTextProps extends LayoutProps, RichTextStyleProps {
199
+ spans: RichTextSpan[];
200
+ lineHeight?: number;
201
+ align?: "left" | "center" | "right";
202
+ verticalAlign?: "top" | "middle" | "bottom";
203
+ maxLines?: number;
204
+ ellipsis?: boolean;
205
+ }
206
+ interface RichTextElement extends ElementBase, RichTextProps {
207
+ type: "richtext";
208
+ }
150
209
  interface ImageProps extends LayoutProps {
151
210
  src: ImageBitmap | CanvasImageSource;
152
211
  fit?: "contain" | "cover" | "fill" | "none" | "scale-down";
@@ -168,10 +227,10 @@ interface SvgStyleProps {
168
227
  }
169
228
  interface SvgTransformProps {
170
229
  transform?: {
171
- translate?: [number, number];
230
+ translate?: [number, number]; /** 旋转角度(弧度)或 [angle(弧度), cx, cy](绕点旋转) */
172
231
  rotate?: number | [number, number, number];
173
- scale?: number | [number, number];
174
- skewX?: number;
232
+ scale?: number | [number, number]; /** 水平偏移角度(弧度) */
233
+ skewX?: number; /** 垂直偏移角度(弧度) */
175
234
  skewY?: number;
176
235
  matrix?: [number, number, number, number, number, number];
177
236
  };
@@ -266,7 +325,43 @@ interface StackProps extends LayoutProps {
266
325
  interface StackElement extends ElementBase, StackProps {
267
326
  type: "stack";
268
327
  }
269
- type Element = BoxElement | TextElement | ImageElement | SvgElement | StackElement;
328
+ interface TransformSimpleObject {
329
+ translate?: [number, number];
330
+ /** 旋转角度(弧度)或 [angle(弧度), cx, cy](绕点旋转) */
331
+ rotate?: number | [number, number, number];
332
+ scale?: number | [number, number];
333
+ /** 水平偏移角度(弧度) */
334
+ skewX?: number;
335
+ /** 垂直偏移角度(弧度) */
336
+ skewY?: number;
337
+ }
338
+ interface DOMMatrix2DInit {
339
+ a?: number;
340
+ b?: number;
341
+ c?: number;
342
+ d?: number;
343
+ e?: number;
344
+ f?: number;
345
+ }
346
+ type TransformValue = TransformSimpleObject | DOMMatrix2DInit | [number, number, number, number, number, number];
347
+ interface TransformProps {
348
+ children: Element;
349
+ /** 变换值(rotate/skew 使用弧度) */
350
+ transform?: TransformValue;
351
+ /** 变换原点,默认为 [0, 0](左上角) */
352
+ transformOrigin?: [number | string, number | string];
353
+ }
354
+ interface TransformElement extends ElementBase, TransformProps {
355
+ type: "transform";
356
+ }
357
+ interface CustomDrawProps extends LayoutProps {
358
+ draw: (ctx: CanvasRenderingContext2D, options: ProxiedCanvasContextOptions) => void;
359
+ children?: Element;
360
+ }
361
+ interface CustomDrawElement extends ElementBase, CustomDrawProps {
362
+ type: "customdraw";
363
+ }
364
+ type Element = BoxElement | TextElement | RichTextElement | ImageElement | SvgElement | StackElement | TransformElement | CustomDrawElement;
270
365
  //#endregion
271
366
  //#region src/canvas.d.ts
272
367
  interface CanvasOptions {
@@ -301,4 +396,4 @@ interface DrawCallCanvas {
301
396
  */
302
397
  declare function createCanvas(options: CanvasOptions): DrawCallCanvas;
303
398
  //#endregion
304
- export { AlignItems as A, Color as B, SvgProps as C, SvgTransformProps as D, SvgTextChild as E, LayoutConstraints as F, RadialGradientDescriptor as G, FontProps as H, LayoutNode as I, Spacing as J, Shadow as K, LayoutProps as L, ContainerLayoutProps as M, FlexDirection as N, TextElement as O, JustifyContent as P, Border as R, SvgPolylineChild as S, SvgStyleProps as T, GradientDescriptor as U, ColorStop as V, LinearGradientDescriptor as W, linearGradient as X, StrokeProps as Y, radialGradient as Z, SvgEllipseChild as _, BoxElement as a, SvgPathChild as b, ImageElement as c, StackElement as d, StackProps as f, SvgElement as g, SvgCircleChild as h, createCanvas as i, AlignSelf as j, TextProps as k, ImageProps as l, SvgChild as m, DrawCallCanvas as n, BoxProps as o, SvgAlign as p, Size as q, LayoutSize as r, Element as s, CanvasOptions as t, StackAlign as u, SvgGroupChild as v, SvgRectChild as w, SvgPolygonChild as x, SvgLineChild as y, Bounds as z };
399
+ export { GradientDescriptor as $, SvgRectChild as A, ContainerLayoutProps as B, SvgEllipseChild as C, SvgPolygonChild as D, SvgPathChild as E, TextProps as F, LayoutProps as G, JustifyContent as H, TransformElement as I, Border as J, MeasureContext as K, TransformProps as L, SvgTextChild as M, SvgTransformProps as N, SvgPolylineChild as O, TextElement as P, FontProps as Q, AlignItems as R, SvgElement as S, SvgLineChild as T, LayoutConstraints as U, FlexDirection as V, LayoutNode as W, Color as X, Bounds as Y, ColorStop as Z, StackElement as _, BoxElement as a, StrokeProps as at, SvgChild as b, CustomDrawProps as c, ImageProps as d, LinearGradientDescriptor as et, ProxiedCanvasContextOptions as f, StackAlign as g, RichTextSpan as h, createCanvas as i, Spacing as it, SvgStyleProps as j, SvgProps as k, Element as l, RichTextProps as m, DrawCallCanvas as n, Shadow as nt, BoxProps as o, linearGradient as ot, RichTextElement as p, createCanvasMeasureContext as q, LayoutSize as r, Size as rt, CustomDrawElement as s, radialGradient as st, CanvasOptions as t, RadialGradientDescriptor as tt, ImageElement as u, StackProps as v, SvgGroupChild as w, SvgCircleChild as x, SvgAlign as y, AlignSelf as z };
@@ -0,0 +1,155 @@
1
+ /**
2
+ * 示例:使用 draw-call 绘制一个卡片
3
+ * 运行: bun examples/card.ts
4
+ */
5
+ import { Box, linearGradient, printLayout, Svg, svg, 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: 320,
15
+ pixelRatio: 2,
16
+ });
17
+
18
+ // 绘制背景
19
+ const layout = canvas.render(
20
+ Box({
21
+ width: "fill",
22
+ height: "fill",
23
+ background: "#f0f2f5",
24
+ padding: 20,
25
+ justify: "center",
26
+ align: "center",
27
+ children: [
28
+ // 卡片
29
+ Box({
30
+ width: 360,
31
+ background: "#ffffff",
32
+ border: { radius: 12 },
33
+ shadow: { offsetY: 4, blur: 16, color: "rgba(0,0,0,0.12)" },
34
+ direction: "column",
35
+ clip: true,
36
+ children: [
37
+ // 卡片头部
38
+ Box({
39
+ height: 100,
40
+ background: linearGradient(135, "#667eea", "#764ba2"),
41
+ padding: 20,
42
+ justify: "space-between",
43
+ align: "end",
44
+ children: [
45
+ // SVG 图标演示
46
+ Svg({
47
+ width: 48,
48
+ height: 48,
49
+ viewBox: { width: 24, height: 24 },
50
+ children: [
51
+ // 绘制一个简单的画笔图标
52
+ svg.circle({ cx: 12, cy: 12, r: 10, fill: "rgba(255,255,255,0.2)" }),
53
+ svg.path({
54
+ d: "M4 20h4l10.5-10.5a1.5 1.5 0 0 0-4-4L4 16v4z",
55
+ fill: "#ffffff",
56
+ }),
57
+ svg.line({
58
+ x1: 13.5,
59
+ y1: 6.5,
60
+ x2: 17.5,
61
+ y2: 10.5,
62
+ stroke: { color: "#ffffff", width: 1.5 },
63
+ }),
64
+ ],
65
+ }),
66
+ Text({
67
+ content: "draw-call",
68
+ font: { size: 28, weight: "bold", family: "unifont" },
69
+ color: "#ffffff",
70
+ shadow: {
71
+ offsetX: 1,
72
+ offsetY: 1,
73
+ blur: 2,
74
+ color: "rgba(0,0,0,0.3)",
75
+ },
76
+ }),
77
+ ],
78
+ }),
79
+ // 卡片内容
80
+ Box({
81
+ padding: 20,
82
+ direction: "column",
83
+ gap: 12,
84
+ children: [
85
+ Text({
86
+ content: "声明式 Canvas 绘图",
87
+ font: { size: 18, weight: "bold", family: "unifont" },
88
+ color: "#333333",
89
+ }),
90
+ Text({
91
+ content: "使用类似 UI 框架的方式来绘制 Canvas 内容,支持 Flexbox 布局、文本自动换行等特性。",
92
+ font: { size: 14, family: "unifont" },
93
+ color: "#666666",
94
+ lineHeight: 1.6,
95
+ wrap: true,
96
+ }),
97
+ // 标签
98
+ Box({
99
+ direction: "row",
100
+ gap: 8,
101
+ children: [
102
+ Box({
103
+ padding: { left: 10, right: 10, top: 4, bottom: 4 },
104
+ background: "#e8f4ff",
105
+ border: { radius: 4 },
106
+ children: [
107
+ Text({
108
+ content: "Canvas",
109
+ font: { size: 12, family: "unifont" },
110
+ color: "#1890ff",
111
+ }),
112
+ ],
113
+ }),
114
+ Box({
115
+ padding: { left: 10, right: 10, top: 4, bottom: 4 },
116
+ background: "#f6ffed",
117
+ border: { radius: 4 },
118
+ children: [
119
+ Text({
120
+ content: "TypeScript",
121
+ font: { size: 12, family: "unifont" },
122
+ color: "#52c41a",
123
+ }),
124
+ ],
125
+ }),
126
+ Box({
127
+ padding: { left: 10, right: 10, top: 4, bottom: 4 },
128
+ background: "#fff7e6",
129
+ border: { radius: 4 },
130
+ children: [
131
+ Text({
132
+ content: "声明式",
133
+ font: { size: 12, family: "unifont" },
134
+ color: "#fa8c16",
135
+ }),
136
+ ],
137
+ }),
138
+ ],
139
+ }),
140
+ ],
141
+ }),
142
+ ],
143
+ }),
144
+ ],
145
+ })
146
+ );
147
+
148
+ // 保存到文件
149
+ const buffer = await canvas.toBuffer("image/png");
150
+ await Bun.write("examples/card.png", buffer);
151
+ console.log("Card saved to examples/card.png");
152
+
153
+ // 美观打印布局树
154
+ console.log("\n=== Layout Tree ===");
155
+ printLayout(layout);
@@ -0,0 +1,269 @@
1
+ /**
2
+ * 示例:CustomDraw 基础用法
3
+ * 展示如何使用 CustomDraw 进行简单的自定义绘制
4
+ * 运行: bun examples/customdraw-basic.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: 600,
15
+ height: 1000,
16
+ pixelRatio: 2,
17
+ });
18
+
19
+ // 1. 最基础的例子:简单矩形
20
+ const SimpleRect = CustomDraw({
21
+ width: 150,
22
+ height: 80,
23
+ draw(ctx, { width, height }) {
24
+ ctx.fillStyle = "#667eea";
25
+ ctx.fillRect(0, 0, width, height);
26
+ ctx.fillStyle = "#000000";
27
+ ctx.font = "bold 16px sans-serif";
28
+ ctx.textAlign = "center";
29
+ ctx.textBaseline = "middle";
30
+ ctx.fillText("Simple Rect", width / 2, height / 2);
31
+ },
32
+ });
33
+
34
+ // 2. 使用 save/restore 的例子:旋转的矩形
35
+ const RotatedRect = CustomDraw({
36
+ width: 150,
37
+ height: 80,
38
+ draw(ctx, { width, height }) {
39
+ // save/restore 会自动平衡,即使不显式调用也能正确恢复
40
+ ctx.save();
41
+ ctx.translate(width / 2, height / 2);
42
+ ctx.rotate((Math.PI / 4) * 0.3); // 15 度
43
+ ctx.fillStyle = "#764ba2";
44
+ ctx.fillRect(-width / 2, -height / 2, width, height);
45
+ ctx.restore();
46
+
47
+ // 恢复后可以正常绘制
48
+ ctx.fillStyle = "#000000";
49
+ ctx.font = "16px sans-serif";
50
+ ctx.textAlign = "center";
51
+ ctx.textBaseline = "middle";
52
+ ctx.fillText("Rotated", width / 2, height / 2);
53
+ },
54
+ });
55
+
56
+ // 3. 圆形进度条(使用子元素)
57
+ const CircleProgress = CustomDraw({
58
+ width: 140,
59
+ height: 140,
60
+ draw(ctx, { width, height, inner }) {
61
+ const centerX = width / 2;
62
+ const centerY = height / 2;
63
+ const radius = Math.min(width, height) / 2 - 8;
64
+
65
+ // 绘制背景圆
66
+ ctx.strokeStyle = "rgba(0, 0, 0, 0.1)";
67
+ ctx.lineWidth = 8;
68
+ ctx.beginPath();
69
+ ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
70
+ ctx.stroke();
71
+
72
+ // 绘制进度圆(65% 完成)
73
+ const percentage = 65;
74
+ ctx.strokeStyle = "#ff6b6b";
75
+ ctx.lineCap = "round";
76
+ ctx.beginPath();
77
+ ctx.arc(centerX, centerY, radius, -Math.PI / 2, -Math.PI / 2 + (percentage / 100) * Math.PI * 2);
78
+ ctx.stroke();
79
+
80
+ // 调用 inner() 在中央渲染子元素(百分比文本)
81
+ inner?.();
82
+ },
83
+ children: Box({
84
+ width: "fill",
85
+ height: "fill",
86
+ justify: "center",
87
+ align: "center",
88
+ children: [
89
+ Text({
90
+ content: "65%",
91
+ font: { size: 24, weight: "bold", family: "unifont" },
92
+ color: "#ff6b6b",
93
+ }),
94
+ ],
95
+ }),
96
+ });
97
+
98
+ // 4. 星形图案
99
+ const StarShape = CustomDraw({
100
+ width: 150,
101
+ height: 150,
102
+ draw(ctx, { width, height }) {
103
+ const centerX = width / 2;
104
+ const centerY = height / 2;
105
+ const points = 5;
106
+ const outerRadius = Math.min(width, height) / 2 - 5;
107
+ const innerRadius = outerRadius * 0.4;
108
+
109
+ ctx.fillStyle = "#ffd93d";
110
+ ctx.beginPath();
111
+
112
+ for (let i = 0; i < points * 2; i++) {
113
+ const radius = i % 2 === 0 ? outerRadius : innerRadius;
114
+ const angle = (i * Math.PI) / points - Math.PI / 2;
115
+ const x = centerX + radius * Math.cos(angle);
116
+ const y = centerY + radius * Math.sin(angle);
117
+
118
+ if (i === 0) {
119
+ ctx.moveTo(x, y);
120
+ } else {
121
+ ctx.lineTo(x, y);
122
+ }
123
+ }
124
+
125
+ ctx.closePath();
126
+ ctx.fill();
127
+
128
+ // 描边
129
+ ctx.strokeStyle = "#fa8c16";
130
+ ctx.lineWidth = 2;
131
+ ctx.stroke();
132
+ },
133
+ });
134
+
135
+ const layout = canvas.render(
136
+ Box({
137
+ width: "fill",
138
+ height: "fill",
139
+ background: "#f5f7fa",
140
+ padding: 20,
141
+ direction: "column",
142
+ gap: 20,
143
+ children: [
144
+ // 标题
145
+ Text({
146
+ content: "CustomDraw 基础用法",
147
+ font: { size: 24, weight: "bold", family: "unifont" },
148
+ color: "#333333",
149
+ }),
150
+
151
+ // 示例 1:简单矩形
152
+ Box({
153
+ direction: "column",
154
+ gap: 10,
155
+ children: [
156
+ Text({
157
+ content: "1. 简单的自定义绘制",
158
+ font: { size: 14, weight: "bold", family: "unifont" },
159
+ color: "#666666",
160
+ }),
161
+ Box({
162
+ background: "#ffffff",
163
+ border: { radius: 8 },
164
+ padding: 20,
165
+ justify: "center",
166
+ align: "center",
167
+ children: [SimpleRect],
168
+ }),
169
+ ],
170
+ }),
171
+
172
+ // 示例 2:旋转矩形(save/restore 管理)
173
+ Box({
174
+ direction: "column",
175
+ gap: 10,
176
+ children: [
177
+ Text({
178
+ content: "2. Transform 和 Save/Restore 管理",
179
+ font: { size: 14, weight: "bold", family: "unifont" },
180
+ color: "#666666",
181
+ }),
182
+ Box({
183
+ background: "#ffffff",
184
+ border: { radius: 8 },
185
+ padding: 20,
186
+ justify: "center",
187
+ align: "center",
188
+ children: [RotatedRect],
189
+ }),
190
+ ],
191
+ }),
192
+
193
+ // 示例 3:进度条(子元素)
194
+ Box({
195
+ direction: "column",
196
+ gap: 10,
197
+ children: [
198
+ Text({
199
+ content: "3. CustomDraw 包含子元素(inner 函数)",
200
+ font: { size: 14, weight: "bold", family: "unifont" },
201
+ color: "#666666",
202
+ }),
203
+ Box({
204
+ background: "#ffffff",
205
+ border: { radius: 8 },
206
+ padding: 20,
207
+ justify: "center",
208
+ align: "center",
209
+ children: [CircleProgress],
210
+ }),
211
+ ],
212
+ }),
213
+
214
+ // 示例 4:星形(复杂路径)
215
+ Box({
216
+ direction: "column",
217
+ gap: 10,
218
+ children: [
219
+ Text({
220
+ content: "4. 复杂形状(五角星)",
221
+ font: { size: 14, weight: "bold", family: "unifont" },
222
+ color: "#666666",
223
+ }),
224
+ Box({
225
+ background: "#ffffff",
226
+ border: { radius: 8 },
227
+ padding: 20,
228
+ justify: "center",
229
+ align: "center",
230
+ children: [StarShape],
231
+ }),
232
+ ],
233
+ }),
234
+
235
+ // 说明
236
+ Box({
237
+ background: "#e8f4ff",
238
+ border: { radius: 8, color: "#1890ff" },
239
+ padding: 15,
240
+ direction: "column",
241
+ gap: 8,
242
+ children: [
243
+ Text({
244
+ content: "提示:",
245
+ font: { size: 12, weight: "bold", family: "unifont" },
246
+ color: "#1890ff",
247
+ }),
248
+ Text({
249
+ content:
250
+ "CustomDraw 支持直接调用 Canvas API。内部的 save/restore 会自动平衡,transform 也会自动相对处理。使用 inner() 方法可以在自定义绘制中渲染子元素。",
251
+ font: { size: 12, family: "unifont" },
252
+ color: "#1890ff",
253
+ wrap: true,
254
+ lineHeight: 1.5,
255
+ }),
256
+ ],
257
+ }),
258
+ ],
259
+ })
260
+ );
261
+
262
+ // 保存文件
263
+ const buffer = await canvas.toBuffer("image/png");
264
+ await Bun.write("examples/customdraw-basic.png", buffer);
265
+ console.log("✅ CustomDraw basic demo saved to examples/customdraw-basic.png");
266
+
267
+ // 打印布局树
268
+ console.log("\n=== Layout Tree ===");
269
+ printLayout(layout);