apexify.js 3.3.6 → 3.3.7
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 +3 -6
- 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 +2 -2
- 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
package/SECURITY.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
Use this section to tell people about which versions of your project are
|
|
6
|
+
currently being supported with security updates.
|
|
7
|
+
|
|
8
|
+
| Version | Supported |
|
|
9
|
+
| ------- | ------------------ |
|
|
10
|
+
| >v1.x.x | :white_check_mark: |
|
|
11
|
+
| >v2.x.x | :white_check_mark: |
|
|
12
|
+
| >v3.3.x | :white_check_mark: |
|
|
13
|
+
|
|
14
|
+
## Reporting a Vulnerability
|
|
15
|
+
|
|
16
|
+
Use this section to tell people how to report a vulnerability.
|
|
17
|
+
|
|
18
|
+
Tell them where to go, how often they can expect to get an update on a
|
|
19
|
+
reported vulnerability, what to expect if the vulnerability is accepted or
|
|
20
|
+
declined, etc.
|
package/change logs.md
CHANGED
package/examples/barchart.txt
CHANGED
|
@@ -14,7 +14,6 @@ const data = {
|
|
|
14
14
|
},
|
|
15
15
|
bg: {
|
|
16
16
|
image: 'https://png.pngtree.com/thumb_back/fh260/background/20211118/pngtree-financial-technology-data-graph-image_908921.jpg',
|
|
17
|
-
baseDir: __dirname,
|
|
18
17
|
bgColor: 'darkgray'
|
|
19
18
|
},
|
|
20
19
|
grid: {
|
|
@@ -58,7 +57,6 @@ const data = {
|
|
|
58
57
|
'blue': 'Label 3',
|
|
59
58
|
'yellow': 'Lable 4'
|
|
60
59
|
},
|
|
61
|
-
keyColor: '',
|
|
62
60
|
xTitle: 'X Axis Title',
|
|
63
61
|
yTitle: 'Y Axis Title',
|
|
64
62
|
labelStyle: {
|
package/examples/linechart.txt
CHANGED
|
@@ -79,13 +79,11 @@ const data = {
|
|
|
79
79
|
lineWidth: 3,
|
|
80
80
|
},
|
|
81
81
|
canvas: {
|
|
82
|
-
bgColor: 'black',
|
|
83
82
|
fontColor: 'white',
|
|
84
83
|
fontSize: 16,
|
|
85
84
|
width: 800,
|
|
86
85
|
height: 600,
|
|
87
86
|
image: 'image url or path if u dont want a bg color',
|
|
88
|
-
baseDir: __dirname // need to be added if u using a local path image
|
|
89
87
|
}
|
|
90
88
|
}
|
|
91
89
|
};
|
package/lib/ai/ApexAI.ts
CHANGED
|
@@ -10,12 +10,11 @@ import {
|
|
|
10
10
|
typeWriter,
|
|
11
11
|
readImage,
|
|
12
12
|
} from "./utils";
|
|
13
|
-
import axios from "axios";
|
|
14
13
|
import {
|
|
15
14
|
ModalBuilder,
|
|
16
15
|
TextInputBuilder,
|
|
17
16
|
TextInputStyle,
|
|
18
|
-
ActionRowBuilder
|
|
17
|
+
ActionRowBuilder
|
|
19
18
|
} from "discord.js";
|
|
20
19
|
import { filters } from "./buttons/tools";
|
|
21
20
|
import { imageTools } from "./buttons/drawMenu";
|
|
@@ -406,7 +405,6 @@ export async function ApexAI (message: any, aiOptions: Options) {
|
|
|
406
405
|
await typeWriter(message.channel, part, speed, delay);
|
|
407
406
|
}
|
|
408
407
|
}
|
|
409
|
-
|
|
410
408
|
} else {
|
|
411
409
|
if (response.length <= 2000) {
|
|
412
410
|
await message.reply({
|
|
@@ -479,106 +477,139 @@ export async function ApexAI (message: any, aiOptions: Options) {
|
|
|
479
477
|
}
|
|
480
478
|
|
|
481
479
|
export async function gemmaAi_4(prompt: string) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
480
|
+
try {
|
|
481
|
+
const response = await fetch('https://api-inference.huggingface.co/models/google/gemma-7b-it', {
|
|
482
|
+
method: 'POST',
|
|
483
|
+
headers: {
|
|
484
|
+
'Content-Type': 'application/json',
|
|
485
|
+
'Authorization': 'Bearer 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'
|
|
486
|
+
},
|
|
487
|
+
body: JSON.stringify({ inputs: prompt })
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
if (!response.ok) {
|
|
491
|
+
throw new Error('Network response was not ok');
|
|
490
492
|
}
|
|
493
|
+
|
|
494
|
+
const responseData = await response.json();
|
|
495
|
+
return responseData[0].generated_text;
|
|
496
|
+
} catch (error: any) {
|
|
497
|
+
console.error('Error fetching response:', error.message);
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
491
500
|
}
|
|
492
501
|
|
|
493
502
|
export async function gemmaAi_3(prompt: string) {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
503
|
+
try {
|
|
504
|
+
const response = await fetch('https://api-inference.huggingface.co/models/google/gemma-2b-it', {
|
|
505
|
+
method: 'POST',
|
|
506
|
+
headers: {
|
|
507
|
+
'Content-Type': 'application/json',
|
|
508
|
+
'Authorization': 'Bearer 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'
|
|
509
|
+
},
|
|
510
|
+
body: JSON.stringify({ inputs: prompt })
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
if (!response.ok) {
|
|
514
|
+
throw new Error('Network response was not ok');
|
|
515
|
+
}
|
|
504
516
|
|
|
517
|
+
const responseData = await response.json();
|
|
518
|
+
return responseData[0].generated_text;
|
|
519
|
+
} catch (error: any) {
|
|
520
|
+
console.error('Error fetching response:', error.message);
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
505
524
|
export async function apexai(prompt: string) {
|
|
506
|
-
|
|
525
|
+
try {
|
|
507
526
|
const messages = [
|
|
508
|
-
|
|
527
|
+
{"role": "user", "content": `${prompt}`}
|
|
509
528
|
];
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
const generatedText = response.data[0].generated_text;
|
|
529
|
+
const formattedMessages = messages.map(message => `[${message.role}] ${message.content}`).join('\n');
|
|
530
|
+
|
|
531
|
+
const response = await fetch('https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1', {
|
|
532
|
+
method: 'POST',
|
|
533
|
+
headers: {
|
|
534
|
+
'Authorization': 'Bearer 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',
|
|
535
|
+
'Content-Type': 'application/json'
|
|
536
|
+
},
|
|
537
|
+
body: JSON.stringify({ inputs: formattedMessages })
|
|
538
|
+
});
|
|
522
539
|
|
|
523
|
-
|
|
540
|
+
if (!response.ok) {
|
|
541
|
+
throw new Error('Network response was not ok');
|
|
542
|
+
}
|
|
524
543
|
|
|
525
|
-
|
|
544
|
+
const responseData = await response.json();
|
|
545
|
+
const generatedText = responseData[0].generated_text;
|
|
546
|
+
const lines = generatedText.split('\n').slice(1);
|
|
547
|
+
const output = lines.join('\n');
|
|
526
548
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
console.error('Error:', error.
|
|
530
|
-
return 'Please wait i am on cooldown.'
|
|
531
|
-
|
|
549
|
+
return output;
|
|
550
|
+
} catch (error: any) {
|
|
551
|
+
console.error('Error:', error.message);
|
|
552
|
+
return 'Please wait i am on cooldown.';
|
|
553
|
+
}
|
|
532
554
|
}
|
|
533
555
|
|
|
534
556
|
export async function starChat(prompt: string) {
|
|
535
|
-
const messages = [{"role":"user","content": `${prompt}`}]
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
557
|
+
const messages = [{"role":"user","content": `${prompt}`}];
|
|
558
|
+
|
|
559
|
+
try {
|
|
560
|
+
const response = await fetch('https://api-inference.huggingface.co/models/HuggingFaceH4/starchat2-15b-v0.1', {
|
|
561
|
+
method: 'POST',
|
|
562
|
+
headers: {
|
|
563
|
+
'Authorization': 'Bearer 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',
|
|
564
|
+
'Content-Type': 'application/json'
|
|
565
|
+
},
|
|
566
|
+
body: JSON.stringify({ inputs: messages })
|
|
567
|
+
});
|
|
545
568
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
const chatbotResponseString = chatbotResponseArray.join(' ');
|
|
569
|
+
if (!response.ok) {
|
|
570
|
+
throw new Error('Network response was not ok');
|
|
571
|
+
}
|
|
550
572
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
573
|
+
const responseData = await response.json();
|
|
574
|
+
const chatbotReply = responseData[0];
|
|
575
|
+
const chatbotResponseText = chatbotReply.generated_text.replace(/^.*?\n.*?\n/, '');
|
|
576
|
+
const chatbotResponseArray = JSON.parse(chatbotResponseText);
|
|
577
|
+
const chatbotResponseString = chatbotResponseArray.join(' ');
|
|
578
|
+
|
|
579
|
+
return chatbotResponseString;
|
|
580
|
+
} catch (error: any) {
|
|
581
|
+
console.error('Error:', error.message);
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
556
584
|
}
|
|
557
585
|
|
|
558
586
|
export async function zephyr_beta(prompt: string) {
|
|
587
|
+
const messages = [{"role":"user","content": `${prompt}` }];
|
|
588
|
+
|
|
589
|
+
try {
|
|
590
|
+
const response = await fetch('https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta', {
|
|
591
|
+
method: 'POST',
|
|
592
|
+
headers: {
|
|
593
|
+
'Authorization': 'Bearer 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',
|
|
594
|
+
'Content-Type': 'application/json'
|
|
595
|
+
},
|
|
596
|
+
body: JSON.stringify({ inputs: messages })
|
|
597
|
+
});
|
|
559
598
|
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
inputs: JSON.stringify(messages),
|
|
564
|
-
}, {
|
|
565
|
-
headers: {
|
|
566
|
-
'Authorization': `Bearer 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`,
|
|
567
|
-
},
|
|
568
|
-
});
|
|
569
|
-
|
|
570
|
-
const chatbotReply = response.data[0];
|
|
571
|
-
|
|
572
|
-
const textParts = chatbotReply.generated_text.split('\n');
|
|
573
|
-
|
|
574
|
-
const secondArrayString = textParts[2];
|
|
575
|
-
const chatbotResponseArray = JSON.parse(secondArrayString);
|
|
576
|
-
|
|
577
|
-
const chatbotResponseString = chatbotResponseArray.map((obj: any) => obj.content).join(' ');
|
|
599
|
+
if (!response.ok) {
|
|
600
|
+
throw new Error('Network response was not ok');
|
|
601
|
+
}
|
|
578
602
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
603
|
+
const responseData = await response.json();
|
|
604
|
+
const chatbotReply = responseData[0];
|
|
605
|
+
const textParts = chatbotReply.generated_text.split('\n');
|
|
606
|
+
const secondArrayString = textParts[2];
|
|
607
|
+
const chatbotResponseArray = JSON.parse(secondArrayString);
|
|
608
|
+
const chatbotResponseString = chatbotResponseArray.map((obj: any) => obj.content).join(' ');
|
|
609
|
+
|
|
610
|
+
return chatbotResponseString;
|
|
611
|
+
} catch (error: any) {
|
|
612
|
+
console.error('Error:', error.message);
|
|
613
|
+
return null;
|
|
614
|
+
}
|
|
615
|
+
}
|
package/lib/ai/functions/draw.ts
CHANGED
|
@@ -88,8 +88,8 @@ async function aiImagine(
|
|
|
88
88
|
nsfw: boolean,
|
|
89
89
|
nsfwKeyWords: string[],
|
|
90
90
|
) {
|
|
91
|
-
const maxRetryAttempts =
|
|
92
|
-
const retryInterval =
|
|
91
|
+
const maxRetryAttempts = 3;
|
|
92
|
+
const retryInterval = 10000;
|
|
93
93
|
let response: any;
|
|
94
94
|
async function retry(fn: any, retriesLeft = maxRetryAttempts) {
|
|
95
95
|
try {
|
|
@@ -98,7 +98,6 @@ async function aiImagine(
|
|
|
98
98
|
if (retriesLeft === 0) {
|
|
99
99
|
throw error;
|
|
100
100
|
}
|
|
101
|
-
|
|
102
101
|
await new Promise((resolve) => setTimeout(resolve, retryInterval));
|
|
103
102
|
return retry(fn, retriesLeft - 1);
|
|
104
103
|
}
|
|
@@ -421,9 +420,7 @@ async function attemptImageCaptioning(imageUrl: string) {
|
|
|
421
420
|
try {
|
|
422
421
|
return await fetchData();
|
|
423
422
|
} catch (error: any) {
|
|
424
|
-
|
|
425
|
-
`Error fetching data (Retry ${retryCount + 1}): ${error.message}`,
|
|
426
|
-
);
|
|
423
|
+
|
|
427
424
|
retryCount++;
|
|
428
425
|
}
|
|
429
426
|
}
|
|
@@ -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
|
}
|