@codehz/draw-call 0.2.0 → 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 +45 -2
- package/canvas.d.cts +60 -6
- package/canvas.d.mts +60 -6
- package/examples/customdraw-basic.ts +269 -0
- package/examples/customdraw.ts +339 -0
- package/examples/transform.ts +437 -0
- package/index.cjs +20 -0
- package/index.d.cts +7 -14
- package/index.d.mts +7 -14
- package/index.mjs +19 -1
- package/package.json +1 -1
- package/render.cjs +325 -13
- package/render.mjs +325 -13
package/README.md
CHANGED
|
@@ -5,10 +5,12 @@
|
|
|
5
5
|
## ✨ 特性
|
|
6
6
|
|
|
7
7
|
- **Flexbox 布局引擎** - 支持完整的 Flexbox 布局,包括方向、对齐、间距等
|
|
8
|
-
- **组件化渲染** - 提供 Box、Text、Image、Svg、Stack 等组件
|
|
8
|
+
- **组件化渲染** - 提供 Box、Text、Image、Svg、Stack、RichText、CustomDraw、Transform 等组件
|
|
9
9
|
- **丰富的样式支持** - 渐变、阴影、边框、圆角等
|
|
10
|
-
- **文本排版** -
|
|
10
|
+
- **文本排版** - 自动换行、省略号、行高控制、富文本支持等
|
|
11
11
|
- **SVG 图形** - 支持矩形、圆形、椭圆、路径等 SVG 图形
|
|
12
|
+
- **2D 变换** - 支持旋转、倾斜等 2D 变换操作
|
|
13
|
+
- **自定义绘制** - 支持自定义 Canvas 绘制逻辑
|
|
12
14
|
- **跨平台** - 支持浏览器和 Node.js 环境
|
|
13
15
|
- **TypeScript** - 完整的类型支持
|
|
14
16
|
|
|
@@ -222,6 +224,44 @@ Stack({
|
|
|
222
224
|
});
|
|
223
225
|
```
|
|
224
226
|
|
|
227
|
+
### Transform
|
|
228
|
+
|
|
229
|
+
2D 变换组件,支持旋转、倾斜等变换操作。
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
Transform({
|
|
233
|
+
children: Box({
|
|
234
|
+
width: 100,
|
|
235
|
+
height: 100,
|
|
236
|
+
background: "#667eea",
|
|
237
|
+
}),
|
|
238
|
+
transform: {
|
|
239
|
+
rotate: Math.PI / 4, // 旋转 45 度
|
|
240
|
+
},
|
|
241
|
+
transformOrigin: ["50%", "50%"], // 变换原点
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### CustomDraw
|
|
246
|
+
|
|
247
|
+
自定义绘制组件,提供原生 Canvas 上下文进行自定义绘制。
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
CustomDraw({
|
|
251
|
+
width: 200,
|
|
252
|
+
height: 100,
|
|
253
|
+
draw: (ctx, options) => {
|
|
254
|
+
// 使用原生 Canvas API 进行绘制
|
|
255
|
+
ctx.fillStyle = "#667eea";
|
|
256
|
+
ctx.fillRect(10, 10, 180, 80);
|
|
257
|
+
ctx.fillStyle = "#ffffff";
|
|
258
|
+
ctx.font = "bold 16px sans-serif";
|
|
259
|
+
ctx.textAlign = "center";
|
|
260
|
+
ctx.fillText("Custom Draw", 100, 60);
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
225
265
|
## 🎨 样式
|
|
226
266
|
|
|
227
267
|
### 尺寸
|
|
@@ -373,6 +413,9 @@ console.log(layoutNode.layout.y); // 0
|
|
|
373
413
|
|
|
374
414
|
- [demo.ts](examples/demo.ts) - 浏览器环境演示
|
|
375
415
|
- [card.ts](examples/card.ts) - Node.js 环境卡片示例
|
|
416
|
+
- [richtext.ts](examples/richtext.ts) - 富文本示例
|
|
417
|
+
- [transform.ts](examples/transform.ts) - 2D 变换示例
|
|
418
|
+
- [customdraw.ts](examples/customdraw.ts) - 自定义绘制示例
|
|
376
419
|
|
|
377
420
|
## 🤝 贡献
|
|
378
421
|
|
package/canvas.d.cts
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";
|
|
@@ -136,7 +149,12 @@ interface LayoutConstraints {
|
|
|
136
149
|
}
|
|
137
150
|
//#endregion
|
|
138
151
|
//#region src/types/components.d.ts
|
|
139
|
-
type ElementType = "box" | "text" | "richtext" | "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
|
+
}
|
|
140
158
|
interface ElementBase {
|
|
141
159
|
type: ElementType;
|
|
142
160
|
}
|
|
@@ -209,10 +227,10 @@ interface SvgStyleProps {
|
|
|
209
227
|
}
|
|
210
228
|
interface SvgTransformProps {
|
|
211
229
|
transform?: {
|
|
212
|
-
translate?: [number, number];
|
|
230
|
+
translate?: [number, number]; /** 旋转角度(弧度)或 [angle(弧度), cx, cy](绕点旋转) */
|
|
213
231
|
rotate?: number | [number, number, number];
|
|
214
|
-
scale?: number | [number, number];
|
|
215
|
-
skewX?: number;
|
|
232
|
+
scale?: number | [number, number]; /** 水平偏移角度(弧度) */
|
|
233
|
+
skewX?: number; /** 垂直偏移角度(弧度) */
|
|
216
234
|
skewY?: number;
|
|
217
235
|
matrix?: [number, number, number, number, number, number];
|
|
218
236
|
};
|
|
@@ -307,7 +325,43 @@ interface StackProps extends LayoutProps {
|
|
|
307
325
|
interface StackElement extends ElementBase, StackProps {
|
|
308
326
|
type: "stack";
|
|
309
327
|
}
|
|
310
|
-
|
|
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;
|
|
311
365
|
//#endregion
|
|
312
366
|
//#region src/canvas.d.ts
|
|
313
367
|
interface CanvasOptions {
|
|
@@ -342,4 +396,4 @@ interface DrawCallCanvas {
|
|
|
342
396
|
*/
|
|
343
397
|
declare function createCanvas(options: CanvasOptions): DrawCallCanvas;
|
|
344
398
|
//#endregion
|
|
345
|
-
export {
|
|
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 };
|
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";
|
|
@@ -136,7 +149,12 @@ interface LayoutConstraints {
|
|
|
136
149
|
}
|
|
137
150
|
//#endregion
|
|
138
151
|
//#region src/types/components.d.ts
|
|
139
|
-
type ElementType = "box" | "text" | "richtext" | "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
|
+
}
|
|
140
158
|
interface ElementBase {
|
|
141
159
|
type: ElementType;
|
|
142
160
|
}
|
|
@@ -209,10 +227,10 @@ interface SvgStyleProps {
|
|
|
209
227
|
}
|
|
210
228
|
interface SvgTransformProps {
|
|
211
229
|
transform?: {
|
|
212
|
-
translate?: [number, number];
|
|
230
|
+
translate?: [number, number]; /** 旋转角度(弧度)或 [angle(弧度), cx, cy](绕点旋转) */
|
|
213
231
|
rotate?: number | [number, number, number];
|
|
214
|
-
scale?: number | [number, number];
|
|
215
|
-
skewX?: number;
|
|
232
|
+
scale?: number | [number, number]; /** 水平偏移角度(弧度) */
|
|
233
|
+
skewX?: number; /** 垂直偏移角度(弧度) */
|
|
216
234
|
skewY?: number;
|
|
217
235
|
matrix?: [number, number, number, number, number, number];
|
|
218
236
|
};
|
|
@@ -307,7 +325,43 @@ interface StackProps extends LayoutProps {
|
|
|
307
325
|
interface StackElement extends ElementBase, StackProps {
|
|
308
326
|
type: "stack";
|
|
309
327
|
}
|
|
310
|
-
|
|
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;
|
|
311
365
|
//#endregion
|
|
312
366
|
//#region src/canvas.d.ts
|
|
313
367
|
interface CanvasOptions {
|
|
@@ -342,4 +396,4 @@ interface DrawCallCanvas {
|
|
|
342
396
|
*/
|
|
343
397
|
declare function createCanvas(options: CanvasOptions): DrawCallCanvas;
|
|
344
398
|
//#endregion
|
|
345
|
-
export {
|
|
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,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);
|