@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
package/index.mjs
CHANGED
|
@@ -61,6 +61,15 @@ function Box(props) {
|
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/components/CustomDraw.ts
|
|
66
|
+
function CustomDraw(props) {
|
|
67
|
+
return {
|
|
68
|
+
type: "customdraw",
|
|
69
|
+
...props
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
64
73
|
//#endregion
|
|
65
74
|
//#region src/components/Image.ts
|
|
66
75
|
function Image(props) {
|
|
@@ -70,6 +79,15 @@ function Image(props) {
|
|
|
70
79
|
};
|
|
71
80
|
}
|
|
72
81
|
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/components/RichText.ts
|
|
84
|
+
function RichText(props) {
|
|
85
|
+
return {
|
|
86
|
+
type: "richtext",
|
|
87
|
+
...props
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
//#endregion
|
|
74
92
|
//#region src/components/Stack.ts
|
|
75
93
|
function Stack(props) {
|
|
@@ -136,4 +154,71 @@ function Text(props) {
|
|
|
136
154
|
}
|
|
137
155
|
|
|
138
156
|
//#endregion
|
|
139
|
-
|
|
157
|
+
//#region src/components/Transform.ts
|
|
158
|
+
function Transform(props) {
|
|
159
|
+
return {
|
|
160
|
+
type: "transform",
|
|
161
|
+
...props
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/layout/utils/print.ts
|
|
167
|
+
/**
|
|
168
|
+
* 获取元素类型的显示名称
|
|
169
|
+
*/
|
|
170
|
+
function getElementType(element) {
|
|
171
|
+
switch (element.type) {
|
|
172
|
+
case "box": return "Box";
|
|
173
|
+
case "text": return `Text "${element.content.slice(0, 20)}${element.content.length > 20 ? "..." : ""}"`;
|
|
174
|
+
case "stack": return "Stack";
|
|
175
|
+
case "image": return "Image";
|
|
176
|
+
case "svg": return "Svg";
|
|
177
|
+
default: return element.type;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* 递归打印布局树
|
|
182
|
+
*/
|
|
183
|
+
function printLayoutToString(node, prefix = "", isLast = true, depth = 0) {
|
|
184
|
+
const lines = [];
|
|
185
|
+
const connector = isLast ? "└─ " : "├─ ";
|
|
186
|
+
const type = getElementType(node.element);
|
|
187
|
+
const { x, y, width, height } = node.layout;
|
|
188
|
+
const childCount = node.children.length;
|
|
189
|
+
lines.push(`${prefix}${connector}${type} @(${Math.round(x)},${Math.round(y)}) size:${Math.round(width)}x${Math.round(height)}`);
|
|
190
|
+
if (node.element.type === "text" && node.lines) {
|
|
191
|
+
const contentPrefix = prefix + (isLast ? " " : "│ ");
|
|
192
|
+
for (let i = 0; i < node.lines.length; i++) {
|
|
193
|
+
const lineText = node.lines[i];
|
|
194
|
+
const isLastLine = i === node.lines.length - 1 && childCount === 0;
|
|
195
|
+
lines.push(`${contentPrefix}${isLastLine ? "└─ " : "├─ "}${JSON.stringify(lineText)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
199
|
+
const child = node.children[i];
|
|
200
|
+
const isChildLast = i === node.children.length - 1;
|
|
201
|
+
const childPrefix = prefix + (isLast ? " " : "│ ");
|
|
202
|
+
lines.push(...printLayoutToString(child, childPrefix, isChildLast, depth + 1));
|
|
203
|
+
}
|
|
204
|
+
return lines;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 打印 LayoutNode 树结构到控制台
|
|
208
|
+
*/
|
|
209
|
+
function printLayout(node) {
|
|
210
|
+
const lines = printLayoutToString(node, "", true);
|
|
211
|
+
console.log(lines.join("\n"));
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 将 LayoutNode 转换为美观的字符串
|
|
215
|
+
* @param node LayoutNode 根节点
|
|
216
|
+
* @param indent 缩进字符串,默认为两个空格
|
|
217
|
+
* @returns 格式化的字符串
|
|
218
|
+
*/
|
|
219
|
+
function layoutToString(node, _indent = " ") {
|
|
220
|
+
return printLayoutToString(node, "", true).join("\n");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
//#endregion
|
|
224
|
+
export { Box, CustomDraw, Image, RichText, Stack, Svg, Text, Transform, computeLayout, createCanvas, createCanvasMeasureContext, layoutToString, linearGradient, printLayout, radialGradient, svg };
|
package/node.cjs
CHANGED
|
@@ -8,7 +8,7 @@ let _napi_rs_canvas = require("@napi-rs/canvas");
|
|
|
8
8
|
* 此函数需要 @napi-rs/canvas 作为依赖
|
|
9
9
|
* 安装: bun add @napi-rs/canvas
|
|
10
10
|
*/
|
|
11
|
-
function
|
|
11
|
+
function createNodeCanvas(options) {
|
|
12
12
|
const { width, height, pixelRatio = 1 } = options;
|
|
13
13
|
const canvas = (0, _napi_rs_canvas.createCanvas)(width * pixelRatio, height * pixelRatio);
|
|
14
14
|
const ctx = canvas.getContext("2d");
|
|
@@ -45,4 +45,4 @@ function createCanvas(options) {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
//#endregion
|
|
48
|
-
exports.
|
|
48
|
+
exports.createNodeCanvas = createNodeCanvas;
|
package/node.d.cts
CHANGED
|
@@ -7,6 +7,6 @@ import { n as DrawCallCanvas, t as CanvasOptions } from "./canvas.cjs";
|
|
|
7
7
|
* 此函数需要 @napi-rs/canvas 作为依赖
|
|
8
8
|
* 安装: bun add @napi-rs/canvas
|
|
9
9
|
*/
|
|
10
|
-
declare function
|
|
10
|
+
declare function createNodeCanvas(options: Omit<CanvasOptions, "canvas">): DrawCallCanvas;
|
|
11
11
|
//#endregion
|
|
12
|
-
export {
|
|
12
|
+
export { createNodeCanvas };
|
package/node.d.mts
CHANGED
|
@@ -7,6 +7,6 @@ import { n as DrawCallCanvas, t as CanvasOptions } from "./canvas.mjs";
|
|
|
7
7
|
* 此函数需要 @napi-rs/canvas 作为依赖
|
|
8
8
|
* 安装: bun add @napi-rs/canvas
|
|
9
9
|
*/
|
|
10
|
-
declare function
|
|
10
|
+
declare function createNodeCanvas(options: Omit<CanvasOptions, "canvas">): DrawCallCanvas;
|
|
11
11
|
//#endregion
|
|
12
|
-
export {
|
|
12
|
+
export { createNodeCanvas };
|
package/node.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as computeLayout, r as createCanvasMeasureContext, t as renderNode } from "./render.mjs";
|
|
2
|
-
import { createCanvas
|
|
2
|
+
import { createCanvas } from "@napi-rs/canvas";
|
|
3
3
|
|
|
4
4
|
//#region src/node.ts
|
|
5
5
|
/**
|
|
@@ -8,9 +8,9 @@ import { createCanvas as createCanvas$1 } from "@napi-rs/canvas";
|
|
|
8
8
|
* 此函数需要 @napi-rs/canvas 作为依赖
|
|
9
9
|
* 安装: bun add @napi-rs/canvas
|
|
10
10
|
*/
|
|
11
|
-
function
|
|
11
|
+
function createNodeCanvas(options) {
|
|
12
12
|
const { width, height, pixelRatio = 1 } = options;
|
|
13
|
-
const canvas = createCanvas
|
|
13
|
+
const canvas = createCanvas(width * pixelRatio, height * pixelRatio);
|
|
14
14
|
const ctx = canvas.getContext("2d");
|
|
15
15
|
if (pixelRatio !== 1) ctx.scale(pixelRatio, pixelRatio);
|
|
16
16
|
const measureCtx = createCanvasMeasureContext(ctx);
|
|
@@ -45,4 +45,4 @@ function createCanvas(options) {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
//#endregion
|
|
48
|
-
export {
|
|
48
|
+
export { createNodeCanvas };
|