koishi-plugin-drf 0.0.2 → 0.0.4

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/lib/index.d.ts CHANGED
@@ -1,5 +1,35 @@
1
- import { Context, Schema } from 'koishi';
1
+ import { Context, Schema, h } from 'koishi';
2
+ declare module 'koishi' {
3
+ interface Context {
4
+ canvas: CanvasService;
5
+ }
6
+ }
7
+ interface CanvasRenderingContext2D<C extends Canvas = Canvas, I extends Image = Image> extends Omit<globalThis.CanvasRenderingContext2D, 'drawImage' | 'canvas'> {
8
+ canvas: C;
9
+ drawImage(image: C | I, dx: number, dy: number): void;
10
+ drawImage(image: C | I, dx: number, dy: number, dw: number, dh: number): void;
11
+ drawImage(image: C | I, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
12
+ }
13
+ interface Canvas {
14
+ width: number;
15
+ height: number;
16
+ getContext(type: '2d'): CanvasRenderingContext2D;
17
+ toBuffer(type: 'image/png'): Promise<Buffer>;
18
+ toDataURL(type: 'image/png'): Promise<string>;
19
+ dispose(): Promise<void>;
20
+ }
21
+ interface Image {
22
+ readonly naturalWidth: number;
23
+ readonly naturalHeight: number;
24
+ dispose(): Promise<void>;
25
+ }
26
+ declare abstract class CanvasService {
27
+ abstract createCanvas(width: number, height: number): Promise<Canvas>;
28
+ abstract loadImage(source: string | URL | Buffer | ArrayBufferLike): Promise<Image>;
29
+ render(width: number, height: number, callback: (ctx: CanvasRenderingContext2D) => Promise<void>): Promise<h>;
30
+ }
2
31
  export declare const name = "function-plotter";
32
+ export declare const using: readonly ["canvas"];
3
33
  export interface Config {
4
34
  width: number;
5
35
  height: number;
@@ -13,3 +43,4 @@ export interface Config {
13
43
  }
14
44
  export declare const Config: Schema<Config>;
15
45
  export declare function apply(ctx: Context, config: Config): void;
46
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,144 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ name: () => name,
26
+ using: () => using
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+ var import_koishi = require("koishi");
30
+ var import_mathjs = require("mathjs");
31
+ var name = "function-plotter";
32
+ var using = ["canvas"];
33
+ var Config = import_koishi.Schema.object({
34
+ width: import_koishi.Schema.number().default(800).description("图像宽度"),
35
+ height: import_koishi.Schema.number().default(600).description("图像高度"),
36
+ xMin: import_koishi.Schema.number().default(-10).description("X轴最小值"),
37
+ xMax: import_koishi.Schema.number().default(10).description("X轴最大值"),
38
+ step: import_koishi.Schema.number().default(0.1).description("绘图步长"),
39
+ backgroundColor: import_koishi.Schema.string().default("white").description("背景颜色"),
40
+ lineColor: import_koishi.Schema.string().default("blue").description("线条颜色"),
41
+ gridColor: import_koishi.Schema.string().default("#e0e0e0").description("网格颜色"),
42
+ showGrid: import_koishi.Schema.boolean().default(true).description("显示网格")
43
+ });
44
+ function apply(ctx, config) {
45
+ if (!ctx.canvas) {
46
+ ctx.logger.warn("canvas 插件未安装,函数绘图功能将不可用");
47
+ return;
48
+ }
49
+ ctx.command("drf <expression>", "绘制函数图像").alias("函数绘图").example("drf f(x)=x^2").action(async ({ session }, expression) => {
50
+ if (!expression) {
51
+ return "请输入函数表达式,例如:drf f(x)=x^2";
52
+ }
53
+ try {
54
+ const compiled = (0, import_mathjs.compile)(expression);
55
+ const scope = { x: 0 };
56
+ return await ctx.canvas.render(config.width, config.height, async (ctxCanvas) => {
57
+ ctxCanvas.fillStyle = config.backgroundColor;
58
+ ctxCanvas.fillRect(0, 0, config.width, config.height);
59
+ const xAxis = config.height / 2;
60
+ const yAxis = config.width / 2;
61
+ const xScale = config.width / (config.xMax - config.xMin);
62
+ const yScale = config.height / 20;
63
+ if (config.showGrid) {
64
+ ctxCanvas.strokeStyle = config.gridColor;
65
+ ctxCanvas.lineWidth = 1;
66
+ for (let x = Math.ceil(config.xMin); x <= config.xMax; x++) {
67
+ if (x === 0) continue;
68
+ const xPos = yAxis + x * xScale;
69
+ ctxCanvas.beginPath();
70
+ ctxCanvas.moveTo(xPos, 0);
71
+ ctxCanvas.lineTo(xPos, config.height);
72
+ ctxCanvas.stroke();
73
+ }
74
+ for (let y = -10; y <= 10; y++) {
75
+ if (y === 0) continue;
76
+ const yPos = xAxis - y * yScale;
77
+ ctxCanvas.beginPath();
78
+ ctxCanvas.moveTo(0, yPos);
79
+ ctxCanvas.lineTo(config.width, yPos);
80
+ ctxCanvas.stroke();
81
+ }
82
+ }
83
+ ctxCanvas.strokeStyle = "black";
84
+ ctxCanvas.lineWidth = 2;
85
+ ctxCanvas.beginPath();
86
+ ctxCanvas.moveTo(0, xAxis);
87
+ ctxCanvas.lineTo(config.width, xAxis);
88
+ ctxCanvas.stroke();
89
+ ctxCanvas.beginPath();
90
+ ctxCanvas.moveTo(yAxis, 0);
91
+ ctxCanvas.lineTo(yAxis, config.height);
92
+ ctxCanvas.stroke();
93
+ ctxCanvas.fillStyle = "black";
94
+ ctxCanvas.font = "12px Arial";
95
+ ctxCanvas.fillText("X", config.width - 15, xAxis - 5);
96
+ ctxCanvas.fillText("Y", yAxis + 5, 15);
97
+ ctxCanvas.strokeStyle = config.lineColor;
98
+ ctxCanvas.lineWidth = 3;
99
+ ctxCanvas.beginPath();
100
+ let isFirstPoint = true;
101
+ for (let x = config.xMin; x <= config.xMax; x += config.step) {
102
+ scope.x = x;
103
+ let y;
104
+ try {
105
+ y = compiled.evaluate(scope);
106
+ if (typeof y !== "number" || !isFinite(y)) {
107
+ isFirstPoint = true;
108
+ continue;
109
+ }
110
+ if (y < -100) y = -100;
111
+ if (y > 100) y = 100;
112
+ const xPixel = yAxis + x * xScale;
113
+ const yPixel = xAxis - y * yScale;
114
+ if (isFirstPoint) {
115
+ ctxCanvas.moveTo(xPixel, yPixel);
116
+ isFirstPoint = false;
117
+ } else {
118
+ ctxCanvas.lineTo(xPixel, yPixel);
119
+ }
120
+ } catch (error) {
121
+ isFirstPoint = true;
122
+ continue;
123
+ }
124
+ }
125
+ ctxCanvas.stroke();
126
+ ctxCanvas.fillStyle = "#333";
127
+ ctxCanvas.font = "bold 16px Arial";
128
+ ctxCanvas.textAlign = "center";
129
+ ctxCanvas.fillText(expression, config.width / 2, 25);
130
+ });
131
+ } catch (error) {
132
+ ctx.logger.error("绘制函数图像时出错:", error);
133
+ return `绘制函数图像时出错: ${error.message}`;
134
+ }
135
+ });
136
+ }
137
+ __name(apply, "apply");
138
+ // Annotate the CommonJS export names for ESM import in node:
139
+ 0 && (module.exports = {
140
+ Config,
141
+ apply,
142
+ name,
143
+ using
144
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-drf",
3
3
  "description": "draw math function in your group",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -34,5 +34,9 @@
34
34
  "en": "draw math function in your group",
35
35
  "zh": "在你的群组中绘制函数图像"
36
36
  }
37
+ },
38
+ "dependencies": {
39
+ "canvas": "^3.2.0",
40
+ "mathjs": "^14.7.0"
37
41
  }
38
42
  }