koishi-plugin-drf 0.0.1 → 0.0.3
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.js +139 -0
- package/package.json +7 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
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
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
var import_koishi = require("koishi");
|
|
29
|
+
var import_canvas = require("canvas");
|
|
30
|
+
var import_mathjs = require("mathjs");
|
|
31
|
+
var name = "function-plotter";
|
|
32
|
+
var Config = import_koishi.Schema.object({
|
|
33
|
+
width: import_koishi.Schema.number().default(600).description("宽度"),
|
|
34
|
+
height: import_koishi.Schema.number().default(600).description("高度"),
|
|
35
|
+
xMin: import_koishi.Schema.number().default(-10).description("X轴始值"),
|
|
36
|
+
xMax: import_koishi.Schema.number().default(10).description("X轴终值"),
|
|
37
|
+
step: import_koishi.Schema.number().default(0.1).description("步长"),
|
|
38
|
+
backgroundColor: import_koishi.Schema.string().default("white").description("背景色"),
|
|
39
|
+
lineColor: import_koishi.Schema.string().default("navy").description("线条颜色,例如red,green,blue,cyan,teal,maroon"),
|
|
40
|
+
gridColor: import_koishi.Schema.string().default("#e0e0e0").description("网格色"),
|
|
41
|
+
showGrid: import_koishi.Schema.boolean().default(true).description("显示网格")
|
|
42
|
+
});
|
|
43
|
+
function apply(ctx, config) {
|
|
44
|
+
ctx.command("drf <expression>", "绘制函数图像").alias("函数绘图").example("drf f(x)=x^2").action(async ({ session }, expression) => {
|
|
45
|
+
if (!expression) {
|
|
46
|
+
return "[color提醒您]不给我表达式怎麽画(*´・д・)?";
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const compiled = (0, import_mathjs.compile)(expression);
|
|
50
|
+
const scope = { x: 0 };
|
|
51
|
+
const canvas = (0, import_canvas.createCanvas)(config.width, config.height);
|
|
52
|
+
const ctxCanvas = canvas.getContext("2d");
|
|
53
|
+
ctxCanvas.fillStyle = config.backgroundColor;
|
|
54
|
+
ctxCanvas.fillRect(0, 0, config.width, config.height);
|
|
55
|
+
const xAxis = config.height / 2;
|
|
56
|
+
const yAxis = config.width / 2;
|
|
57
|
+
const xScale = config.width / (config.xMax - config.xMin);
|
|
58
|
+
const yScale = config.height / 20;
|
|
59
|
+
if (config.showGrid) {
|
|
60
|
+
ctxCanvas.strokeStyle = config.gridColor;
|
|
61
|
+
ctxCanvas.lineWidth = 1;
|
|
62
|
+
for (let x = Math.ceil(config.xMin); x <= config.xMax; x++) {
|
|
63
|
+
if (x === 0) continue;
|
|
64
|
+
const xPos = yAxis + x * xScale;
|
|
65
|
+
ctxCanvas.beginPath();
|
|
66
|
+
ctxCanvas.moveTo(xPos, 0);
|
|
67
|
+
ctxCanvas.lineTo(xPos, config.height);
|
|
68
|
+
ctxCanvas.stroke();
|
|
69
|
+
}
|
|
70
|
+
for (let y = -10; y <= 10; y++) {
|
|
71
|
+
if (y === 0) continue;
|
|
72
|
+
const yPos = xAxis - y * yScale;
|
|
73
|
+
ctxCanvas.beginPath();
|
|
74
|
+
ctxCanvas.moveTo(0, yPos);
|
|
75
|
+
ctxCanvas.lineTo(config.width, yPos);
|
|
76
|
+
ctxCanvas.stroke();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
ctxCanvas.strokeStyle = "black";
|
|
80
|
+
ctxCanvas.lineWidth = 2;
|
|
81
|
+
ctxCanvas.beginPath();
|
|
82
|
+
ctxCanvas.moveTo(0, xAxis);
|
|
83
|
+
ctxCanvas.lineTo(config.width, xAxis);
|
|
84
|
+
ctxCanvas.stroke();
|
|
85
|
+
ctxCanvas.beginPath();
|
|
86
|
+
ctxCanvas.moveTo(yAxis, 0);
|
|
87
|
+
ctxCanvas.lineTo(yAxis, config.height);
|
|
88
|
+
ctxCanvas.stroke();
|
|
89
|
+
ctxCanvas.fillStyle = "black";
|
|
90
|
+
ctxCanvas.font = "12px Arial";
|
|
91
|
+
ctxCanvas.fillText("X", config.width - 15, xAxis - 5);
|
|
92
|
+
ctxCanvas.fillText("Y", yAxis + 5, 15);
|
|
93
|
+
ctxCanvas.strokeStyle = config.lineColor;
|
|
94
|
+
ctxCanvas.lineWidth = 3;
|
|
95
|
+
ctxCanvas.beginPath();
|
|
96
|
+
let isFirstPoint = true;
|
|
97
|
+
for (let x = config.xMin; x <= config.xMax; x += config.step) {
|
|
98
|
+
scope.x = x;
|
|
99
|
+
let y;
|
|
100
|
+
try {
|
|
101
|
+
y = compiled.evaluate(scope);
|
|
102
|
+
if (typeof y !== "number" || !isFinite(y)) {
|
|
103
|
+
isFirstPoint = true;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (y < -100) y = -100;
|
|
107
|
+
if (y > 100) y = 100;
|
|
108
|
+
const xPixel = yAxis + x * xScale;
|
|
109
|
+
const yPixel = xAxis - y * yScale;
|
|
110
|
+
if (isFirstPoint) {
|
|
111
|
+
ctxCanvas.moveTo(xPixel, yPixel);
|
|
112
|
+
isFirstPoint = false;
|
|
113
|
+
} else {
|
|
114
|
+
ctxCanvas.lineTo(xPixel, yPixel);
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
isFirstPoint = true;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
ctxCanvas.stroke();
|
|
122
|
+
ctxCanvas.fillStyle = "#333";
|
|
123
|
+
ctxCanvas.font = "bold 16px Arial";
|
|
124
|
+
ctxCanvas.textAlign = "center";
|
|
125
|
+
ctxCanvas.fillText("draw by mf[color]: " + expression, config.width / 2, 25);
|
|
126
|
+
const buffer = canvas.toBuffer("image/png");
|
|
127
|
+
return import_koishi.h.image(buffer, "image/png");
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return `绘制函数图像时出错: ${error.message}`;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
__name(apply, "apply");
|
|
134
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
135
|
+
0 && (module.exports = {
|
|
136
|
+
Config,
|
|
137
|
+
apply,
|
|
138
|
+
name
|
|
139
|
+
});
|
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.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"函数",
|
|
20
20
|
"绘图"
|
|
21
21
|
],
|
|
22
|
-
"contributors": [
|
|
22
|
+
"contributors": [
|
|
23
23
|
"Duo xngc2021@gmail.com"
|
|
24
24
|
],
|
|
25
25
|
"devDependencies": {
|
|
@@ -30,9 +30,13 @@
|
|
|
30
30
|
"koishi": "^4.18.7"
|
|
31
31
|
},
|
|
32
32
|
"koishi": {
|
|
33
|
-
"description": {
|
|
33
|
+
"description": {
|
|
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
|
}
|