apexify.js 3.3.6 → 3.3.8
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/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/dependabot.yml +7 -0
- package/.tsbuildinfo +1 -1
- package/CODE_OF_CONDUCT.md +128 -0
- package/LICENSE +21 -0
- package/README.md +27 -21
- package/SECURITY.md +20 -0
- package/change logs.md +1 -1
- package/examples/barchart.txt +0 -2
- package/examples/linechart.txt +0 -2
- package/lib/ai/ApexAI.ts +116 -85
- package/lib/ai/functions/draw.ts +67 -69
- package/lib/ai/functions/generateVoiceResponse.ts +15 -22
- package/lib/ai/functions/readImagess.ts +6 -6
- package/lib/ai/functions/typeWriter.ts +0 -2
- package/lib/canvas/ApexPainter.ts +75 -80
- package/lib/canvas/utils/bg.ts +7 -4
- package/lib/canvas/utils/charts.ts +37 -22
- package/lib/canvas/utils/general functions.ts +71 -53
- package/lib/canvas/utils/types.ts +14 -7
- package/lib/index.ts +6 -6
- package/package.json +3 -3
- package/dist/ai/ApexAI.d.ts +0 -62
- package/dist/ai/ApexAI.d.ts.map +0 -1
- package/dist/ai/ApexAI.js +0 -421
- package/dist/ai/ApexAI.js.map +0 -1
- package/dist/ai/buttons/drawMenu.d.ts +0 -3
- package/dist/ai/buttons/drawMenu.d.ts.map +0 -1
- package/dist/ai/buttons/drawMenu.js +0 -274
- package/dist/ai/buttons/drawMenu.js.map +0 -1
- package/dist/ai/buttons/tools.d.ts +0 -3
- package/dist/ai/buttons/tools.d.ts.map +0 -1
- package/dist/ai/buttons/tools.js +0 -445
- package/dist/ai/buttons/tools.js.map +0 -1
- package/dist/ai/functions/aivoice.d.ts +0 -1
- package/dist/ai/functions/aivoice.d.ts.map +0 -1
- package/dist/ai/functions/aivoice.js +0 -2
- package/dist/ai/functions/aivoice.js.map +0 -1
- package/dist/ai/functions/chunkString.d.ts +0 -2
- package/dist/ai/functions/chunkString.d.ts.map +0 -1
- package/dist/ai/functions/chunkString.js +0 -8
- package/dist/ai/functions/chunkString.js.map +0 -1
- package/dist/ai/functions/draw.d.ts +0 -3
- package/dist/ai/functions/draw.d.ts.map +0 -1
- package/dist/ai/functions/draw.js +0 -387
- package/dist/ai/functions/draw.js.map +0 -1
- package/dist/ai/functions/generateVoiceResponse.d.ts +0 -3
- package/dist/ai/functions/generateVoiceResponse.d.ts.map +0 -1
- package/dist/ai/functions/generateVoiceResponse.js +0 -158
- package/dist/ai/functions/generateVoiceResponse.js.map +0 -1
- package/dist/ai/functions/imageReader.d.ts +0 -3
- package/dist/ai/functions/imageReader.d.ts.map +0 -1
- package/dist/ai/functions/imageReader.js +0 -23
- package/dist/ai/functions/imageReader.js.map +0 -1
- package/dist/ai/functions/readFiles.d.ts +0 -3
- package/dist/ai/functions/readFiles.d.ts.map +0 -1
- package/dist/ai/functions/readFiles.js +0 -62
- package/dist/ai/functions/readFiles.js.map +0 -1
- package/dist/ai/functions/readImagess.d.ts +0 -2
- package/dist/ai/functions/readImagess.d.ts.map +0 -1
- package/dist/ai/functions/readImagess.js +0 -48
- package/dist/ai/functions/readImagess.js.map +0 -1
- package/dist/ai/functions/shouldDrawImage.d.ts +0 -2
- package/dist/ai/functions/shouldDrawImage.d.ts.map +0 -1
- package/dist/ai/functions/shouldDrawImage.js +0 -12
- package/dist/ai/functions/shouldDrawImage.js.map +0 -1
- package/dist/ai/functions/typeWriter.d.ts +0 -2
- package/dist/ai/functions/typeWriter.d.ts.map +0 -1
- package/dist/ai/functions/typeWriter.js +0 -24
- package/dist/ai/functions/typeWriter.js.map +0 -1
- package/dist/ai/models.d.ts +0 -9
- package/dist/ai/models.d.ts.map +0 -1
- package/dist/ai/models.js +0 -575
- package/dist/ai/models.js.map +0 -1
- package/dist/ai/utils.d.ts +0 -11
- package/dist/ai/utils.d.ts.map +0 -1
- package/dist/ai/utils.js +0 -24
- package/dist/ai/utils.js.map +0 -1
- package/dist/canvas/ApexPainter.d.ts +0 -38
- package/dist/canvas/ApexPainter.d.ts.map +0 -1
- package/dist/canvas/ApexPainter.js +0 -439
- package/dist/canvas/ApexPainter.js.map +0 -1
- package/dist/canvas/utils/bg.d.ts +0 -23
- package/dist/canvas/utils/bg.d.ts.map +0 -1
- package/dist/canvas/utils/bg.js +0 -74
- package/dist/canvas/utils/bg.js.map +0 -1
- package/dist/canvas/utils/charts.d.ts +0 -9
- package/dist/canvas/utils/charts.d.ts.map +0 -1
- package/dist/canvas/utils/charts.js +0 -454
- package/dist/canvas/utils/charts.js.map +0 -1
- package/dist/canvas/utils/circular.d.ts +0 -10
- package/dist/canvas/utils/circular.d.ts.map +0 -1
- package/dist/canvas/utils/circular.js +0 -22
- package/dist/canvas/utils/circular.js.map +0 -1
- package/dist/canvas/utils/customLines.d.ts +0 -2
- package/dist/canvas/utils/customLines.d.ts.map +0 -1
- package/dist/canvas/utils/customLines.js +0 -50
- package/dist/canvas/utils/customLines.js.map +0 -1
- package/dist/canvas/utils/general functions.d.ts +0 -21
- package/dist/canvas/utils/general functions.d.ts.map +0 -1
- package/dist/canvas/utils/general functions.js +0 -391
- package/dist/canvas/utils/general functions.js.map +0 -1
- package/dist/canvas/utils/imageProperties.d.ts +0 -60
- package/dist/canvas/utils/imageProperties.d.ts.map +0 -1
- package/dist/canvas/utils/imageProperties.js +0 -331
- package/dist/canvas/utils/imageProperties.js.map +0 -1
- package/dist/canvas/utils/radius.d.ts +0 -10
- package/dist/canvas/utils/radius.d.ts.map +0 -1
- package/dist/canvas/utils/radius.js +0 -31
- package/dist/canvas/utils/radius.js.map +0 -1
- package/dist/canvas/utils/textProperties.d.ts +0 -18
- package/dist/canvas/utils/textProperties.d.ts.map +0 -1
- package/dist/canvas/utils/textProperties.js +0 -67
- package/dist/canvas/utils/textProperties.js.map +0 -1
- package/dist/canvas/utils/types.d.ts +0 -414
- package/dist/canvas/utils/types.d.ts.map +0 -1
- package/dist/canvas/utils/types.js +0 -5
- package/dist/canvas/utils/types.js.map +0 -1
- package/dist/canvas/utils/utils.d.ts +0 -20
- package/dist/canvas/utils/utils.d.ts.map +0 -1
- package/dist/canvas/utils/utils.js +0 -48
- package/dist/canvas/utils/utils.js.map +0 -1
- package/dist/index.d.ts +0 -11
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -65
- package/dist/index.js.map +0 -1
- package/dist/utils.d.ts +0 -12
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -12
- package/dist/utils.js.map +0 -1
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createCanvas, loadImage, GlobalFonts } from "@napi-rs/canvas";
|
|
2
|
-
import axios, { responseEncoding } from "axios";
|
|
3
2
|
import path from 'path';
|
|
4
3
|
import GIFEncoder from 'gifencoder';
|
|
5
4
|
import { PassThrough, Writable} from 'stream'
|
|
@@ -11,55 +10,57 @@ import { CanvasConfig, TextObject, ImageProperties, ImageObject, GIFOptions, GIF
|
|
|
11
10
|
import { } from "./utils/general functions";
|
|
12
11
|
|
|
13
12
|
export class ApexPainter {
|
|
14
|
-
async createCanvas(canvas: CanvasConfig): Promise<Buffer> {
|
|
15
|
-
let canvasWidth: number = canvas.width || 500;
|
|
16
|
-
let canvasHeight: number = canvas.height || 500;
|
|
17
|
-
let borderRadius: number | string = canvas.borderRadius || 0;
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
14
|
+
async createCanvas(canvas: CanvasConfig): Promise<Buffer> {
|
|
15
|
+
let canvasWidth: number = canvas.width || 500;
|
|
16
|
+
let canvasHeight: number = canvas.height || 500;
|
|
17
|
+
let borderRadius: number | string = canvas.borderRadius || 0;
|
|
18
|
+
|
|
19
|
+
if (canvas.customBg) {
|
|
20
|
+
try {
|
|
21
|
+
const response = await fetch(canvas.customBg);
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error("Failed to fetch background image.");
|
|
29
24
|
}
|
|
25
|
+
const buffer = await response.arrayBuffer();
|
|
26
|
+
const image = await loadImage(Buffer.from(buffer));
|
|
27
|
+
canvasWidth = image.width;
|
|
28
|
+
canvasHeight = image.height;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('Error loading custom background image:', error);
|
|
30
31
|
}
|
|
32
|
+
}
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const canvasInstance = createCanvas(canvasWidth, canvasHeight);
|
|
35
|
-
const ctx: any = canvasInstance.getContext('2d');
|
|
34
|
+
const canvasInstance = createCanvas(canvasWidth, canvasHeight);
|
|
35
|
+
const ctx: any = canvasInstance.getContext('2d');
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
applyRotation(ctx, canvas.rotation || 0, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
|
|
37
|
+
if (!ctx) {
|
|
38
|
+
throw new Error('Unable to get 2D rendering context from canvas');
|
|
39
|
+
}
|
|
42
40
|
|
|
43
|
-
|
|
41
|
+
applyRotation(ctx, canvas.rotation || 0, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
|
|
44
42
|
|
|
45
|
-
|
|
46
|
-
circularBorder(ctx, canvasWidth, canvasHeight);
|
|
47
|
-
} else if (typeof borderRadius === 'number') {
|
|
48
|
-
radiusBorder(ctx, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight, borderRadius);
|
|
49
|
-
}
|
|
43
|
+
applyShadow(ctx, canvas.shadow, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
|
|
50
44
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
await drawBackgroundColor(ctx, canvas);
|
|
57
|
-
}
|
|
58
|
-
applyStroke(ctx, canvas.stroke, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
|
|
45
|
+
if (typeof borderRadius === 'string') {
|
|
46
|
+
circularBorder(ctx, canvasWidth, canvasHeight);
|
|
47
|
+
} else if (typeof borderRadius === 'number') {
|
|
48
|
+
radiusBorder(ctx, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight, borderRadius);
|
|
49
|
+
}
|
|
59
50
|
|
|
60
|
-
|
|
51
|
+
if (canvas.customBg) {
|
|
52
|
+
await customBackground(ctx, canvas);
|
|
53
|
+
} else if (canvas.gradientBg) {
|
|
54
|
+
await drawBackgroundGradient(ctx, canvas);
|
|
55
|
+
} else {
|
|
56
|
+
await drawBackgroundColor(ctx, canvas);
|
|
61
57
|
}
|
|
62
58
|
|
|
59
|
+
applyStroke(ctx, canvas.stroke, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
|
|
60
|
+
|
|
61
|
+
return canvasInstance.toBuffer('image/png');
|
|
62
|
+
}
|
|
63
|
+
|
|
63
64
|
async createImage(images: ImageProperties[], canvasBuffer: Buffer): Promise<Buffer> {
|
|
64
65
|
|
|
65
66
|
if (!canvasBuffer) {
|
|
@@ -517,51 +518,45 @@ export class ApexPainter {
|
|
|
517
518
|
}
|
|
518
519
|
|
|
519
520
|
try {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
521
|
+
let img: any;
|
|
522
|
+
const shapeNames = ['circle', 'square', 'triangle', 'pentagon', 'hexagon', 'heptagon', 'octagon', 'star', 'kite'];
|
|
523
|
+
const isShape = shapeNames.includes(source.toLowerCase());
|
|
524
|
+
|
|
525
|
+
if (source.startsWith('http')) {
|
|
526
|
+
const response = await fetch(source);
|
|
527
|
+
if (!response.ok) {
|
|
528
|
+
throw new Error("Failed to fetch image.");
|
|
529
|
+
}
|
|
530
|
+
const buffer = await response.arrayBuffer();
|
|
531
|
+
img = await loadImage(Buffer.from(buffer));
|
|
532
|
+
} else if (isShape) {
|
|
533
|
+
drawShape(ctx, { source, x, y, width, height, rotation, borderRadius, stroke, shadow, isFilled, color, gradient });
|
|
534
|
+
} else {
|
|
535
|
+
const imagePath = path.join(process.cwd(), source);
|
|
536
|
+
try {
|
|
537
|
+
img = await loadImage(imagePath);
|
|
538
|
+
} catch (e: any) {
|
|
539
|
+
throw new Error(`Error loading image: ${e.message}`);
|
|
540
|
+
}
|
|
538
541
|
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
if (img !== undefined) {
|
|
542
|
-
|
|
543
|
-
ctx.save();
|
|
544
|
-
|
|
545
|
-
applyRotation(ctx, rotation || 0, x, y, width, height);
|
|
546
|
-
|
|
547
|
-
applyShadow(ctx, shadow, x, y, width, height);
|
|
548
542
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
543
|
+
if (img !== undefined) {
|
|
544
|
+
ctx.save();
|
|
545
|
+
applyRotation(ctx, rotation || 0, x, y, width, height);
|
|
546
|
+
applyShadow(ctx, shadow, x, y, width, height);
|
|
547
|
+
imageRadius(ctx, img, x, y, width, height, borderRadius || 0);
|
|
548
|
+
applyStroke(ctx, stroke, x, y, width, height);
|
|
549
|
+
ctx.restore();
|
|
550
|
+
} else {
|
|
551
|
+
if (!isShape) {
|
|
552
|
+
throw new Error('Invalid image source or shape name');
|
|
553
|
+
}
|
|
557
554
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
}
|
|
555
|
+
} catch (error: any) {
|
|
556
|
+
throw new Error(error.message);
|
|
557
|
+
}
|
|
558
|
+
|
|
563
559
|
}
|
|
564
|
-
|
|
565
560
|
public validHex(hexColor: string): any {
|
|
566
561
|
const hexPattern = /^#[0-9a-fA-F]{6}$/;
|
|
567
562
|
if (!hexPattern.test(hexColor)) {
|
package/lib/canvas/utils/bg.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { loadImage } from "@napi-rs/canvas";
|
|
2
2
|
import { CanvasConfig } from './types';
|
|
3
|
-
import axios from 'axios';
|
|
4
3
|
import fs from 'fs';
|
|
5
4
|
import path from 'path';
|
|
6
5
|
|
|
@@ -58,8 +57,12 @@ export async function customBackground(ctx: any, canvas: CanvasConfig): Promise<
|
|
|
58
57
|
let imagePath: string;
|
|
59
58
|
|
|
60
59
|
if (canvas.customBg.startsWith('http')) {
|
|
61
|
-
const response = await
|
|
62
|
-
|
|
60
|
+
const response = await fetch(canvas.customBg);
|
|
61
|
+
if (!response.ok) {
|
|
62
|
+
throw new Error("Failed to fetch custom background image.");
|
|
63
|
+
}
|
|
64
|
+
const buffer = await response.arrayBuffer();
|
|
65
|
+
imageBuffer = Buffer.from(buffer);
|
|
63
66
|
} else {
|
|
64
67
|
imagePath = path.join(process.cwd(), canvas.customBg);
|
|
65
68
|
imageBuffer = fs.readFileSync(imagePath);
|
|
@@ -72,7 +75,7 @@ export async function customBackground(ctx: any, canvas: CanvasConfig): Promise<
|
|
|
72
75
|
|
|
73
76
|
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
74
77
|
|
|
75
|
-
} catch (error) {
|
|
78
|
+
} catch (error: any) {
|
|
76
79
|
console.error('Error loading custom background image:', error);
|
|
77
80
|
}
|
|
78
81
|
}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { createCanvas, loadImage } from '@napi-rs/canvas';
|
|
2
2
|
import { barChart_1, bgConfig, DataItem, KeyBoxConfig, PieDataConfig, PieChartData, LineChartConfig, DataPoint } from "./types";
|
|
3
3
|
import path from 'path';
|
|
4
|
-
import axios from 'axios';
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
////////////////////////////////////////BAR CHARTS////////////////////////////////////////
|
|
8
7
|
|
|
9
8
|
export async function verticalBarChart(data: barChart_1) {
|
|
10
9
|
try {
|
|
11
|
-
const { chartData, xLabels, yLabels, data: { xAxis, yAxis, keys,
|
|
10
|
+
const { chartData, xLabels, yLabels, data: { xAxis, yAxis, keys, xTitle, yTitle, labelStyle } } = data;
|
|
12
11
|
|
|
13
12
|
if (!xLabels || !yLabels || !xAxis || !yAxis) {
|
|
14
13
|
throw new Error('Required data is missing.');
|
|
@@ -33,15 +32,24 @@ export async function verticalBarChart(data: barChart_1) {
|
|
|
33
32
|
const chartHeight = canvasHeight * (chartData?.heightPerc || 0.8);
|
|
34
33
|
|
|
35
34
|
if (chartData?.bg?.image) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
try {
|
|
36
|
+
let img;
|
|
37
|
+
if (chartData.bg.image.startsWith('http')) {
|
|
38
|
+
const response = await fetch(chartData.bg.image);
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
throw new Error("Failed to fetch image.");
|
|
41
|
+
}
|
|
42
|
+
const buffer = await response.arrayBuffer();
|
|
43
|
+
img = await loadImage(Buffer.from(buffer));
|
|
44
|
+
} else {
|
|
45
|
+
const imagePath = path.join(process.cwd(), chartData.bg.image);
|
|
46
|
+
img = await loadImage(imagePath);
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('Error loading image:', error);
|
|
43
50
|
}
|
|
44
51
|
}
|
|
52
|
+
|
|
45
53
|
const canvas = createCanvas(800, 600);
|
|
46
54
|
const ctx = canvas.getContext('2d');
|
|
47
55
|
|
|
@@ -347,26 +355,32 @@ export async function lineChart(data: { data: DataPoint[][], lineConfig: LineCha
|
|
|
347
355
|
|
|
348
356
|
let img: any;
|
|
349
357
|
if (data.lineConfig?.canvas?.image) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
358
|
+
try {
|
|
359
|
+
if (data.lineConfig.canvas.image.startsWith('http')) {
|
|
360
|
+
const response = await fetch(data.lineConfig.canvas.image);
|
|
361
|
+
if (!response.ok) {
|
|
362
|
+
throw new Error("Failed to fetch image.");
|
|
363
|
+
}
|
|
364
|
+
const buffer = await response.arrayBuffer();
|
|
365
|
+
img = await loadImage(Buffer.from(buffer));
|
|
366
|
+
} else {
|
|
367
|
+
const imagePath = path.join(process.cwd(), data.lineConfig.canvas.image);
|
|
368
|
+
img = await loadImage(imagePath);
|
|
369
|
+
}
|
|
370
|
+
} catch (error) {
|
|
371
|
+
console.error('Error loading image:', error);
|
|
357
372
|
}
|
|
358
373
|
}
|
|
359
374
|
|
|
360
|
-
|
|
361
375
|
const canvas = createCanvas(canvasWidth, canvasHeight);
|
|
362
376
|
const ctx = canvas.getContext('2d');
|
|
363
377
|
|
|
364
378
|
const xAxisLabels = data.data[0].map(point => point.label);
|
|
365
|
-
const yAxisLabel = 'Y Axis';
|
|
366
|
-
ctx.font =
|
|
379
|
+
const yAxisLabel = data.lineConfig?.yaxisLabel?.label || 'Y Axis';
|
|
380
|
+
ctx.font = `${data.lineConfig?.yaxisLabel?.fontSize || 16} Arial`;
|
|
367
381
|
|
|
368
382
|
if (data.lineConfig?.canvas?.image) {
|
|
369
|
-
ctx.drawImage(img, 0, 0,
|
|
383
|
+
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
|
|
370
384
|
} else {
|
|
371
385
|
ctx.fillStyle = data.lineConfig?.canvas?.bgColor || '#f0f0f0';
|
|
372
386
|
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
|
|
@@ -401,7 +415,8 @@ export async function lineChart(data: { data: DataPoint[][], lineConfig: LineCha
|
|
|
401
415
|
ctx.save();
|
|
402
416
|
ctx.translate(20, canvasHeight / 2);
|
|
403
417
|
ctx.rotate(-Math.PI / 2);
|
|
404
|
-
ctx.
|
|
418
|
+
ctx.fillStyle = data.lineConfig?.yaxisLabel?.color || 'black';
|
|
419
|
+
ctx.fillText(yAxisLabel, data.lineConfig?.yaxisLabel?.y || 0, data.lineConfig?.yaxisLabel?.x || 0);
|
|
405
420
|
ctx.restore();
|
|
406
421
|
|
|
407
422
|
let maxY = 0;
|
|
@@ -423,7 +438,7 @@ export async function lineChart(data: { data: DataPoint[][], lineConfig: LineCha
|
|
|
423
438
|
ctx.strokeStyle = lineConfig?.lineColor[index] || 'blue';
|
|
424
439
|
ctx.lineWidth = 2;
|
|
425
440
|
|
|
426
|
-
const tension = lineConfig.lineTension[index] || 0.1;
|
|
441
|
+
const tension = (lineConfig && lineConfig.lineTension && lineConfig.lineTension[index]) || 0.1;
|
|
427
442
|
|
|
428
443
|
line.forEach((point, index) => {
|
|
429
444
|
const x = index * segmentWidth + 50;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
1
|
import path from 'path'
|
|
3
2
|
import sharp from 'sharp';
|
|
4
3
|
import { cropOptions } from './types';
|
|
@@ -6,24 +5,26 @@ import { createCanvas, loadImage } from '@napi-rs/canvas';
|
|
|
6
5
|
import Jimp from 'jimp';
|
|
7
6
|
|
|
8
7
|
export async function loadImages(imagePath: string) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
try {
|
|
9
|
+
if (!imagePath) {
|
|
10
|
+
throw new Error("Image path is required.");
|
|
11
|
+
}
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
13
|
+
if (imagePath.startsWith("http")) {
|
|
14
|
+
const response = await fetch(imagePath);
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error("Failed to fetch image.");
|
|
17
|
+
}
|
|
18
|
+
const buffer = await response.arrayBuffer();
|
|
19
|
+
return sharp(buffer);
|
|
20
|
+
} else {
|
|
21
|
+
const absolutePath = path.join(process.cwd(), imagePath);
|
|
22
|
+
return sharp(absolutePath);
|
|
23
|
+
}
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error("Error loading image:", error);
|
|
26
|
+
throw new Error("Failed to load image");
|
|
27
|
+
}
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export async function resizingImg(resizeOptions: any): Promise<any> {
|
|
@@ -52,13 +53,13 @@ export async function resizingImg(resizeOptions: any): Promise<any> {
|
|
|
52
53
|
|
|
53
54
|
ctx.drawImage(image, 0, 0, resizeOptions.size?.width || 500, resizeOptions.size?.height || 500);
|
|
54
55
|
|
|
55
|
-
const resizedBuffer =
|
|
56
|
+
const resizedBuffer = canvas.toBuffer('image/png');
|
|
56
57
|
|
|
57
58
|
return resizedBuffer;
|
|
58
59
|
} catch (error) {
|
|
59
60
|
console.error("Error resizing image:", error);
|
|
60
61
|
throw new Error("Failed to resize image");
|
|
61
|
-
}
|
|
62
|
+
}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
export async function converter(imagePath: string, newExtension: string) {
|
|
@@ -73,10 +74,12 @@ export async function converter(imagePath: string, newExtension: string) {
|
|
|
73
74
|
let image: sharp.Sharp;
|
|
74
75
|
|
|
75
76
|
if (imagePath.startsWith("http")) {
|
|
76
|
-
const response = await
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
const response = await fetch(imagePath);
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
throw new Error("Failed to fetch image.");
|
|
80
|
+
}
|
|
81
|
+
const buffer = await response.arrayBuffer();
|
|
82
|
+
image = sharp(Buffer.from(buffer));
|
|
80
83
|
} else {
|
|
81
84
|
if (!imagePath) {
|
|
82
85
|
throw new Error("Image path is required.");
|
|
@@ -329,12 +332,16 @@ export async function detectColors(imagePath: string): Promise<{ color: string;
|
|
|
329
332
|
try {
|
|
330
333
|
let image: any;
|
|
331
334
|
if (imagePath.startsWith('http')) {
|
|
332
|
-
const response = await
|
|
333
|
-
|
|
334
|
-
|
|
335
|
+
const response = await fetch(imagePath);
|
|
336
|
+
if (!response.ok) {
|
|
337
|
+
throw new Error("Failed to fetch image.");
|
|
338
|
+
}
|
|
339
|
+
const buffer = await response.arrayBuffer();
|
|
340
|
+
image = await loadImage(Buffer.from(buffer));
|
|
335
341
|
} else {
|
|
336
|
-
|
|
337
|
-
|
|
342
|
+
const localImagePath = path.join(process.cwd(), imagePath);
|
|
343
|
+
image = await loadImage(localImagePath);
|
|
344
|
+
}
|
|
338
345
|
|
|
339
346
|
const canvas = createCanvas(image.width, image.height);
|
|
340
347
|
const ctx = canvas.getContext('2d') as any;
|
|
@@ -376,12 +383,16 @@ export async function removeColor(inputImagePath: string, colorToRemove: { red:
|
|
|
376
383
|
try {
|
|
377
384
|
let image: any;
|
|
378
385
|
if (inputImagePath.startsWith('http')) {
|
|
379
|
-
const response = await
|
|
380
|
-
|
|
381
|
-
|
|
386
|
+
const response = await fetch(inputImagePath);
|
|
387
|
+
if (!response.ok) {
|
|
388
|
+
throw new Error("Failed to fetch image.");
|
|
389
|
+
}
|
|
390
|
+
const buffer = await response.arrayBuffer();
|
|
391
|
+
image = await loadImage(Buffer.from(buffer));
|
|
382
392
|
} else {
|
|
383
|
-
|
|
384
|
-
|
|
393
|
+
const localImagePath = path.join(process.cwd(), inputImagePath);
|
|
394
|
+
image = await loadImage(localImagePath);
|
|
395
|
+
}
|
|
385
396
|
|
|
386
397
|
const canvas = createCanvas(image.width, image.height);
|
|
387
398
|
const ctx = canvas.getContext('2d') as any;
|
|
@@ -411,24 +422,31 @@ export async function removeColor(inputImagePath: string, colorToRemove: { red:
|
|
|
411
422
|
}
|
|
412
423
|
|
|
413
424
|
export async function bgRemoval(imgURL: string, API_KEY: string): Promise<Buffer | undefined> {
|
|
414
|
-
|
|
415
|
-
|
|
425
|
+
try {
|
|
416
426
|
if (!API_KEY) {
|
|
417
|
-
|
|
427
|
+
throw new Error("API_KEY is required. Please visit remove.bg, create an account, and obtain your API key at: https://accounts.kaleido.ai/users/sign_in#api-key");
|
|
418
428
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
429
|
+
|
|
430
|
+
const response = await fetch('https://api.remove.bg/v1.0/removebg', {
|
|
431
|
+
method: 'POST',
|
|
432
|
+
headers: {
|
|
433
|
+
'X-Api-Key': API_KEY,
|
|
434
|
+
'Content-Type': 'application/json'
|
|
435
|
+
},
|
|
436
|
+
body: JSON.stringify({
|
|
437
|
+
image_url: imgURL,
|
|
438
|
+
size: 'auto'
|
|
439
|
+
}),
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
if (!response.ok) {
|
|
443
|
+
throw new Error("Failed to remove background.");
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const buffer = await response.arrayBuffer();
|
|
447
|
+
return Buffer.from(buffer);
|
|
448
|
+
} catch (error) {
|
|
449
|
+
console.error('Error:', error);
|
|
450
|
+
return undefined;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
@@ -113,8 +113,8 @@ export interface TextObject {
|
|
|
113
113
|
color?: string;
|
|
114
114
|
maxWidth?: number;
|
|
115
115
|
lineHeight?: number;
|
|
116
|
-
textAlign?:
|
|
117
|
-
textBaseline?:
|
|
116
|
+
textAlign?: string | "end" | "center" | "left" | "right" | "start";
|
|
117
|
+
textBaseline?: string | "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top";
|
|
118
118
|
shadow?: {
|
|
119
119
|
color?: string;
|
|
120
120
|
offsetX?: number;
|
|
@@ -372,19 +372,26 @@ export interface LineChartConfig {
|
|
|
372
372
|
yLabels: string[];
|
|
373
373
|
fillArea: { color: string }[];
|
|
374
374
|
lineColor: string[];
|
|
375
|
-
plot
|
|
375
|
+
plot?: {
|
|
376
376
|
enable: boolean;
|
|
377
377
|
color: string[];
|
|
378
378
|
size: number;
|
|
379
379
|
};
|
|
380
|
-
|
|
381
|
-
|
|
380
|
+
yaxisLabel?: {
|
|
381
|
+
label?: string;
|
|
382
|
+
x?: number;
|
|
383
|
+
y?: number;
|
|
384
|
+
color?: string;
|
|
385
|
+
fontSize?: string;
|
|
386
|
+
};
|
|
387
|
+
lineTension?: number[];
|
|
388
|
+
grid?: {
|
|
382
389
|
type: 'vertical' | 'horizontal' | 'both' | string;
|
|
383
390
|
color: string;
|
|
384
391
|
width: number;
|
|
385
392
|
};
|
|
386
|
-
keys
|
|
387
|
-
keysConfig
|
|
393
|
+
keys?: { [color: string]: string };
|
|
394
|
+
keysConfig?: {
|
|
388
395
|
radius?: number;
|
|
389
396
|
keyPadding?: number;
|
|
390
397
|
textPadding?: number;
|
package/lib/index.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
import * as path from "path";
|
|
3
2
|
import * as fs from "fs";
|
|
4
3
|
|
|
@@ -15,10 +14,12 @@ const getLibraryVersion = function(library: string): string {
|
|
|
15
14
|
return version;
|
|
16
15
|
};
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
.
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
fetch("https://registry.npmjs.com/-/v1/search?text=apexify.js")
|
|
18
|
+
.then(function(response: Response) {
|
|
19
|
+
return response.json();
|
|
20
|
+
})
|
|
21
|
+
.then(function(data: any) {
|
|
22
|
+
const version: string = data.objects[0]?.package?.version;
|
|
22
23
|
if (version && getLibraryVersion("apexify.js") !== version) {
|
|
23
24
|
console.error(CYAN +
|
|
24
25
|
"Error: Please update apexify.js to the latest version (" + version + ")." +
|
|
@@ -27,7 +28,6 @@ axios
|
|
|
27
28
|
})
|
|
28
29
|
.catch(function(error: any) {});
|
|
29
30
|
|
|
30
|
-
|
|
31
31
|
import { ApexAI, ApexChat, ApexImagine, ApexPainter } from "./utils";
|
|
32
32
|
|
|
33
33
|
export { ApexPainter, ApexAI, ApexImagine, ApexChat };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apexify.js",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.8",
|
|
4
4
|
"description": "Ai and Canvas library. Supports typescript and javascript",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -140,8 +140,8 @@
|
|
|
140
140
|
"dependencies": {
|
|
141
141
|
"@iamtraction/google-translate": "^2.0.1",
|
|
142
142
|
"@napi-rs/canvas": "^0.1.51",
|
|
143
|
+
"apexify.js": "^3.3.6",
|
|
143
144
|
"api": "^6.1.1",
|
|
144
|
-
"axios": "^1.6.7",
|
|
145
145
|
"gifencoder": "^2.0.1",
|
|
146
146
|
"hercai": "^12.2.0",
|
|
147
147
|
"jimp": "^0.22.12",
|
|
@@ -153,7 +153,7 @@
|
|
|
153
153
|
"@types/gifencoder": "^2.0.3",
|
|
154
154
|
"@types/node": "^20.11.25",
|
|
155
155
|
"@types/pdf-parse": "^1.1.4",
|
|
156
|
-
"discord.js": "^14.
|
|
156
|
+
"discord.js": "^14.15.2",
|
|
157
157
|
"typescript": "^5.4.2"
|
|
158
158
|
}
|
|
159
159
|
}
|