@codehz/draw-call 0.1.0 → 0.1.2

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/node.cjs ADDED
@@ -0,0 +1,48 @@
1
+ const require_render = require('./render.cjs');
2
+ let _napi_rs_canvas = require("@napi-rs/canvas");
3
+
4
+ //#region src/node.ts
5
+ /**
6
+ * 创建适用于 Node.js/Bun 环境的 Canvas
7
+ *
8
+ * 此函数需要 @napi-rs/canvas 作为依赖
9
+ * 安装: bun add @napi-rs/canvas
10
+ */
11
+ function createCanvas(options) {
12
+ const { width, height, pixelRatio = 1 } = options;
13
+ const canvas = (0, _napi_rs_canvas.createCanvas)(width * pixelRatio, height * pixelRatio);
14
+ const ctx = canvas.getContext("2d");
15
+ if (pixelRatio !== 1) ctx.scale(pixelRatio, pixelRatio);
16
+ const measureCtx = require_render.createCanvasMeasureContext(ctx);
17
+ return {
18
+ width,
19
+ height,
20
+ pixelRatio,
21
+ canvas,
22
+ render(element) {
23
+ const layoutTree = require_render.computeLayout(element, measureCtx, {
24
+ minWidth: 0,
25
+ maxWidth: width,
26
+ minHeight: 0,
27
+ maxHeight: height
28
+ });
29
+ require_render.renderNode(ctx, layoutTree);
30
+ return layoutTree;
31
+ },
32
+ clear() {
33
+ ctx.clearRect(0, 0, width, height);
34
+ },
35
+ getContext() {
36
+ return ctx;
37
+ },
38
+ toDataURL(type, quality) {
39
+ return canvas.toDataURL(type, quality);
40
+ },
41
+ toBuffer(type = "image/png") {
42
+ return canvas.toBuffer(type);
43
+ }
44
+ };
45
+ }
46
+
47
+ //#endregion
48
+ exports.createCanvas = createCanvas;
package/node.d.cts ADDED
@@ -0,0 +1,12 @@
1
+ import { n as DrawCallCanvas, t as CanvasOptions } from "./canvas.cjs";
2
+
3
+ //#region src/node.d.ts
4
+ /**
5
+ * 创建适用于 Node.js/Bun 环境的 Canvas
6
+ *
7
+ * 此函数需要 @napi-rs/canvas 作为依赖
8
+ * 安装: bun add @napi-rs/canvas
9
+ */
10
+ declare function createCanvas(options: Omit<CanvasOptions, "canvas">): DrawCallCanvas;
11
+ //#endregion
12
+ export { createCanvas };
package/node.d.mts ADDED
@@ -0,0 +1,12 @@
1
+ import { n as DrawCallCanvas, t as CanvasOptions } from "./canvas.mjs";
2
+
3
+ //#region src/node.d.ts
4
+ /**
5
+ * 创建适用于 Node.js/Bun 环境的 Canvas
6
+ *
7
+ * 此函数需要 @napi-rs/canvas 作为依赖
8
+ * 安装: bun add @napi-rs/canvas
9
+ */
10
+ declare function createCanvas(options: Omit<CanvasOptions, "canvas">): DrawCallCanvas;
11
+ //#endregion
12
+ export { createCanvas };
package/node.mjs ADDED
@@ -0,0 +1,48 @@
1
+ import { n as computeLayout, r as createCanvasMeasureContext, t as renderNode } from "./render.mjs";
2
+ import { createCanvas as createCanvas$1 } from "@napi-rs/canvas";
3
+
4
+ //#region src/node.ts
5
+ /**
6
+ * 创建适用于 Node.js/Bun 环境的 Canvas
7
+ *
8
+ * 此函数需要 @napi-rs/canvas 作为依赖
9
+ * 安装: bun add @napi-rs/canvas
10
+ */
11
+ function createCanvas(options) {
12
+ const { width, height, pixelRatio = 1 } = options;
13
+ const canvas = createCanvas$1(width * pixelRatio, height * pixelRatio);
14
+ const ctx = canvas.getContext("2d");
15
+ if (pixelRatio !== 1) ctx.scale(pixelRatio, pixelRatio);
16
+ const measureCtx = createCanvasMeasureContext(ctx);
17
+ return {
18
+ width,
19
+ height,
20
+ pixelRatio,
21
+ canvas,
22
+ render(element) {
23
+ const layoutTree = computeLayout(element, measureCtx, {
24
+ minWidth: 0,
25
+ maxWidth: width,
26
+ minHeight: 0,
27
+ maxHeight: height
28
+ });
29
+ renderNode(ctx, layoutTree);
30
+ return layoutTree;
31
+ },
32
+ clear() {
33
+ ctx.clearRect(0, 0, width, height);
34
+ },
35
+ getContext() {
36
+ return ctx;
37
+ },
38
+ toDataURL(type, quality) {
39
+ return canvas.toDataURL(type, quality);
40
+ },
41
+ toBuffer(type = "image/png") {
42
+ return canvas.toBuffer(type);
43
+ }
44
+ };
45
+ }
46
+
47
+ //#endregion
48
+ export { createCanvas };
package/package.json CHANGED
@@ -25,19 +25,33 @@
25
25
  "optionalDependencies": {
26
26
  "@napi-rs/canvas": "^0.1.88"
27
27
  },
28
- "version": "0.1.0",
28
+ "version": "0.1.2",
29
29
  "main": "./index.cjs",
30
30
  "types": "./index.d.cts",
31
31
  "exports": {
32
- "require": {
33
- "types": "./index.d.cts",
34
- "import": "./index.cjs",
35
- "default": "./index.cjs"
32
+ ".": {
33
+ "require": {
34
+ "types": "./index.d.cts",
35
+ "import": "./index.cjs",
36
+ "default": "./index.cjs"
37
+ },
38
+ "import": {
39
+ "types": "./index.d.mts",
40
+ "import": "./index.mjs",
41
+ "default": "./index.mjs"
42
+ }
36
43
  },
37
- "import": {
38
- "types": "./index.d.mts",
39
- "import": "./index.mjs",
40
- "default": "./index.mjs"
44
+ "./node": {
45
+ "require": {
46
+ "types": "./node.d.cts",
47
+ "import": "./node.cjs",
48
+ "default": "./node.cjs"
49
+ },
50
+ "import": {
51
+ "types": "./node.d.mts",
52
+ "import": "./node.mjs",
53
+ "default": "./node.mjs"
54
+ }
41
55
  }
42
56
  }
43
57
  }