File size: 30,861 Bytes
80ce16c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | const Canvas = require("canvas");
const { uploadZippyshare } = global.utils;
const defaultFontName = "BeVietnamPro-SemiBold";
const defaultPathFontName = `${__dirname}/assets/font/BeVietnamPro-SemiBold.ttf`;
const { randomString } = global.utils;
const percentage = total => total / 100;
Canvas.registerFont(`${__dirname}/assets/font/BeVietnamPro-Bold.ttf`, {
family: "BeVietnamPro-Bold"
});
Canvas.registerFont(defaultPathFontName, {
family: defaultFontName
});
let deltaNext;
const expToLevel = (exp, deltaNextLevel = deltaNext) => Math.floor((1 + Math.sqrt(1 + 8 * exp / deltaNextLevel)) / 2);
const levelToExp = (level, deltaNextLevel = deltaNext) => Math.floor(((Math.pow(level, 2) - level) * deltaNextLevel) / 2);
global.client.makeRankCard = makeRankCard;
module.exports = {
config: {
name: "rank",
version: "1.6",
author: "NTKhang",
countDown: 5,
role: 0,
shortDescription: {
vi: "Xem level của người dùng",
en: "View level of user"
},
longDescription: {
vi: "Xem level của bạn hoặc người được tag. Có thể tag nhiều người",
en: "View your level or the level of the tagged person. You can tag many people"
},
category: "rank",
guide: {
vi: " {pn} [để trống | @tags]",
en: " {pn} [empty | @tags]"
},
envConfig: {
deltaNext: 5
}
},
onStart: async function ({ message, event, usersData, threadsData, commandName, envCommands, api }) {
deltaNext = envCommands[commandName].deltaNext;
let targetUsers;
const arrayMentions = Object.keys(event.mentions);
if (arrayMentions.length == 0)
targetUsers = [event.senderID];
else
targetUsers = arrayMentions;
const rankCards = await Promise.all(targetUsers.map(async userID => {
const rankCard = await makeRankCard(userID, usersData, threadsData, event.threadID, deltaNext, api);
rankCard.path = `${randomString(10)}.png`;
return rankCard;
}));
return message.reply({
attachment: rankCards
});
},
onChat: async function ({ usersData, event }) {
let { exp } = await usersData.get(event.senderID);
if (isNaN(exp) || typeof exp != "number")
exp = 0;
try {
await usersData.set(event.senderID, {
exp: exp + 1
});
}
catch (e) { }
}
};
const defaultDesignCard = {
widthCard: 2000,
heightCard: 500,
main_color: "#474747",
sub_color: "rgba(255, 255, 255, 0.5)",
alpha_subcard: 0.9,
exp_color: "#e1e1e1",
expNextLevel_color: "#3f3f3f",
text_color: "#000000"
};
async function makeRankCard(userID, usersData, threadsData, threadID, deltaNext, api = global.GoatBot.fcaApi) {
const { exp } = await usersData.get(userID);
const levelUser = expToLevel(exp, deltaNext);
const expNextLevel = levelToExp(levelUser + 1, deltaNext) - levelToExp(levelUser, deltaNext);
const currentExp = expNextLevel - (levelToExp(levelUser + 1, deltaNext) - exp);
const allUser = await usersData.getAll();
allUser.sort((a, b) => b.exp - a.exp);
const rank = allUser.findIndex(user => user.userID == userID) + 1;
const customRankCard = await threadsData.get(threadID, "data.customRankCard") || {};
const dataLevel = {
exp: currentExp,
expNextLevel,
name: allUser[rank - 1].name,
rank: `#${rank}/${allUser.length}`,
level: levelUser,
avatar: await usersData.getAvatarUrl(userID)
};
const configRankCard = {
...defaultDesignCard,
...customRankCard
};
const checkImagKey = [
"main_color",
"sub_color",
"line_color",
"exp_color",
"expNextLevel_color"
];
for (const key of checkImagKey) {
if (!isNaN(configRankCard[key]))
configRankCard[key] = await api.resolvePhotoUrl(configRankCard[key]);
}
const image = new RankCard({
...configRankCard,
...dataLevel
});
return await image.buildCard();
}
class RankCard {
/**
* Create a new RankCard
* @param {Object} options - Options for the RankCard:
* @param {String} options.main_color - The main color of the card
* @param {String} options.sub_color - The sub color of the card
* @param {Number} options.alpha_subcard - The alpha of the sub card
* @param {String} options.exp_color - The color of the exp bar
* @param {String} options.expNextLevel_color - The color of the expNextLevel bar
* @param {String} options.text_color - The color of the text
* @param {String} options.name_color - The color of the name
* @param {String} options.level_color - The color of the level
* @param {String} options.rank_color - The color of the rank
* @param {String} options.line_color - The color of the line
* @param {String} options.exp_text_color - The color of the exp text
* @param {Number} options.exp - The exp of the user
* @param {Number} options.expNextLevel - The expNextLevel of the user
* @param {String} options.name - The name of the user
* @param {Number} options.level - The level of the user
* @param {Number} options.rank - The rank of the user
* @param {String} options.avatar - The avatar of the user
* @param {Number} options.widthCard - The width of the card
* @param {Number} options.heightCard - The height of the card
* @param {String} options.fontName - The font name of the card
* @param {String} options.textSize - The value will be added to the font size of all text, default is 0
*
* @example
* const fs = require("fs-extra");
* const card = new RankCard()
* .setWidthCard(2000)
* .setHeightCard(500)
* .setMainColor("#474747")
* .setSubColor("rgba(255, 255, 255, 0.5)")
* .setAlphaSubCard(0.9)
* .setExpColor("#e1e1e1")
* .setExpBarColor("#3f3f3f")
* .setTextColor("#000000");
*
* rank.buildCard()
* .then(buffer => {
* fs.writeFileSync("rank.png", buffer);
* })
* .catch(err => {
* console.log(err);
* });
*
* // or
* const card = new RankCard({
* widthCard: 2000,
* heightCard: 500,
* main_color: "#474747",
* sub_color: "rgba(255, 255, 255, 0.5)",
* alpha_subcard: 0.9,
* exp_color: "#e1e1e1",
* expNextLevel_color: "#3f3f3f",
* text_color: "#000000"
* });
*
* rank.buildCard()
* .then(buffer => {
* fs.writeFileSync("rank.png", buffer);
* })
* .catch(err => {
* console.log(err);
* });
*/
constructor(options) {
this.widthCard = 2000;
this.heightCard = 500;
this.main_color = "#474747";
this.sub_color = "rgba(255, 255, 255, 0.5)";
this.alpha_subcard = 0.9;
this.exp_color = "#e1e1e1";
this.expNextLevel_color = "#3f3f3f";
this.text_color = "#000000";
this.fontName = "BeVietnamPro-Bold";
this.textSize = 0;
for (const key in options)
this[key] = options[key];
}
/**
* @param {string} path
* @param {string} name
* @description Register a new font
* @returns {RankCard}
* @example
* .registerFont("path/to/font.ttf", "FontName");
*/
registerFont(path, name) {
Canvas.registerFont(path, {
family: name
});
return this;
}
/**
* @param {string} fontName
* @description Set the font name
* @returns {RankCard}
* @example
* .setFontName("BeVietnamPro-SemiBold");
* .setFontName("BeVietnamPro-Bold");
* .setFontName("Arial");
* .setFontName("Arial Italic");
*/
setFontName(fontName) {
this.fontName = fontName;
return this;
}
/**
* @param {size} size
* @description increase the size of all the text by {size} units
* @returns {RankCard}
* @example
* .increaseTextSize(10);
* .increaseTextSize(20);
*/
increaseTextSize(size) {
if (isNaN(size))
throw new Error("Size must be a number");
if (size < 0)
throw new Error("Size must be greater than 0");
this.textSize = size;
return this;
}
/**
* @param {number} size
* @description decrease the size of all the text by {size} units
* @returns {RankCard}
* @example
* .decreaseTextSize(10);
* .decreaseTextSize(20);
*/
decreaseTextSize(size) {
if (isNaN(size))
throw new Error("Size must be a number");
if (size < 0)
throw new Error("Size must be greater than 0");
this.textSize = -size;
return this;
}
/**
* @param {number} widthCard
* @description Width of the card
* @returns {RankCard}
* @example
* .setWidthCard(2000);
*/
setWidthCard(widthCard) {
if (isNaN(widthCard))
throw new Error("Width card must be a number");
if (widthCard < 0)
throw new Error("Width card must be greater than 0");
this.widthCard = Number(widthCard);
return this;
}
/**
* @param {number} heightCard
* @description Height of the card
* @returns {RankCard}
* @example
* .setHeightCard(500);
*/
setHeightCard(heightCard) {
if (isNaN(heightCard))
throw new Error("Height card must be a number");
if (heightCard < 0)
throw new Error("Height card must be greater than 0");
this.heightCard = Number(heightCard);
return this;
}
/**
* @param {number} alpha_subcard
* @description Alpha of the sub card is a number between 0 and 1
* @returns {RankCard}
* @example
* .setAlphaSubCard(0.5)
* 0.5 = 50% opacity
* 0.9 = 90% opacity
* 1 = 100% opacity
* 0 = 0% opacity
*/
setAlphaSubCard(alpha_subcard) {
if (isNaN(alpha_subcard))
throw new Error("Alpha subcard must be a number");
if (alpha_subcard < 0 || alpha_subcard > 1)
throw new Error("Alpha subcard must be between 0 and 1");
this.alpha_subcard = Number(alpha_subcard);
return this;
}
/**
* @param {string|string[]} main_color
* @description Color of the main card (background) is a string or array that can be a `hex color`, `rgb`, `rgba`, `image url`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setMainColor("#474747");
* .setMainColor("rgb(255, 255, 255)");
* .setMainColor("rgba(255, 255, 255, 0.5)");
* .setMainColor("https://example.com/image.png");
*/
setMainColor(main_color) {
if (typeof main_color !== "string" && !Array.isArray(main_color))
throw new Error("Main color must be a string or array");
checkFormatColor(main_color);
this.main_color = main_color;
return this;
}
/**
* @param {string|string[]} sub_color
* @description Color of the sub card is a string or array that can be a `hex color`, `rgb`, `rgba`, `image url`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setSubColor("rgba(255, 255, 255, 0.5)")
* .setSubColor("#474747")
* .setSubColor("rgb(255, 255, 255)")
* .setSubColor("https://example.com/image.png")
*/
setSubColor(sub_color) {
if (typeof sub_color !== "string" && !Array.isArray(sub_color))
throw new Error("Sub color must be a string or array");
checkFormatColor(sub_color);
this.sub_color = sub_color;
return this;
}
/**
* @param {string|string[]} exp_color
* @description Color of the exp bar is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setExpColor("#474747")
* .setExpColor("rgb(255, 255, 255)")
* .setExpColor("rgba(255, 255, 255, 0.5)")
*/
setExpColor(exp_color) {
if (typeof exp_color !== "string" && !Array.isArray(exp_color))
throw new Error("Exp color must be a string or array");
checkFormatColor(exp_color);
this.exp_color = exp_color;
return this;
}
/**
* @param {string|string[]} expNextLevel_color
* @description Color of the exp bar next level is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setExpBarColor("#474747")
* .setExpBarColor("rgb(255, 255, 255)")
* .setExpBarColor("rgba(255, 255, 255, 0.5)")
*/
setExpBarColor(expNextLevel_color) {
if (typeof expNextLevel_color !== "string" && !Array.isArray(expNextLevel_color))
throw new Error("Exp next level color must be a string");
checkFormatColor(expNextLevel_color);
this.expNextLevel_color = expNextLevel_color;
return this;
}
/**
* @param {string|string[]} text_color
* @description Color of the all text is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setTextColor("#474747")
* .setTextColor("rgb(255, 255, 255)")
* .setTextColor("rgba(255, 255, 255, 0.5)")
*/
setTextColor(text_color) {
if (typeof text_color !== "string" && !Array.isArray(text_color))
throw new Error("Text color must be a string or an array of string");
checkFormatColor(text_color, false);
this.text_color = text_color;
return this;
}
/**
* @param {string|string[]} name_color
* @description Color of the name is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setNameColor("#474747")
* .setNameColor("rgb(255, 255, 255)")
* .setNameColor("rgba(255, 255, 255, 0.5)")
* .setNameColor(["#474747", "#474747"])
* .setNameColor(['rgb(133, 255, 189)', 'rgb(255, 251, 125)'])
*/
setNameColor(name_color) {
if (typeof name_color !== "string" && !Array.isArray(name_color))
throw new Error("Name color must be a string or an array of string");
checkFormatColor(name_color, false);
this.name_color = name_color;
return this;
}
/**
* @param {string|string[]} level_color
* @description Color of the level text is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setLevelColor("#474747")
* .setLevelColor("rgb(255, 255, 255)")
* .setLevelColor("rgba(255, 255, 255, 0.5)")
* .setLevelColor(["#474747", "#474747"])
* .setLevelColor(['rgb(133, 255, 189)', 'rgb(255, 251, 125)'])
*/
setLevelColor(level_color) {
if (typeof level_color !== "string" && !Array.isArray(level_color))
throw new Error("Level color must be a string or an array of string");
checkFormatColor(level_color, false);
this.level_color = level_color;
return this;
}
/**
* @param {string|string[]} exp_text_color
* @description Color of the exp text is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setExpTextColor("#474747")
* .setExpTextColor("rgb(255, 255, 255)")
* .setExpTextColor("rgba(255, 255, 255, 0.5)")
* .setExpTextColor(["#474747", "#474747"])
* .setExpTextColor(['rgb(133, 255, 189)', 'rgb(255, 251, 125)'])
*
*/
setExpTextColor(exp_text_color) {
if (typeof exp_text_color !== "string" && !Array.isArray(exp_text_color))
throw new Error("Exp text color must be a string or an array of string");
checkFormatColor(exp_text_color, false);
this.exp_text_color = exp_text_color;
return this;
}
/**
* @param {string|string[]} rank_color
* @description Color of the rank is a string or array that can be a `hex color`, `rgb` or `rgba`. If it's an array it will be a `gradient` color
* @returns {RankCard}
*/
setRankColor(rank_color) {
if (typeof rank_color !== "string" && !Array.isArray(rank_color))
throw new Error("Rank color must be a string or an array of string");
checkFormatColor(rank_color, false);
this.rank_color = rank_color;
return this;
}
/**
* @param {string|string[]} line_color
* @description Color of the line is a string or array that can be a `hex color`, `rgb`, `rgba` or url of image. If it's an array it will be a `gradient` color
* @returns {RankCard}
* @example
* .setLineColor("#474747")
* .setLineColor("rgb(255, 255, 255)")
* .setLineColor("rgba(255, 255, 255, 0.5)")
* .setLineColor(['#00DBDE', '#FC00FF'])
* .setLineColor(['rgb(133, 255, 189)', 'rgb(255, 251, 125)'])
* .setLineColor(['rgba(133, 255, 189, 0.5)', 'rgba(255, 251, 125, 0.5)'])
*/
setLineColor(line_color) {
if (typeof line_color !== "string" && !Array.isArray(line_color))
throw new Error("Line color must be a string or an array of string");
this.line_color = line_color;
return this;
}
/**
* @param {number} exp
* @description Exp of the user
* @returns {RankCard}
*/
setExp(exp) {
this.exp = exp;
return this;
}
/**
* @param {number} expNextLevel
* @description Exp next level of the user
* @returns {RankCard}
*/
setExpNextLevel(expNextLevel) {
this.expNextLevel = expNextLevel;
return this;
}
/**
* @param {number} level
* @description Level of the user
* @returns {RankCard}
*/
setLevel(level) {
this.level = level;
return this;
}
/**
* @param {string} rank
* @description Rank of the user
* @returns {RankCard}
* @example
* .setRank("#1/100")
*/
setRank(rank) {
this.rank = rank;
return this;
}
/**
* @param {string} name
* @description Name of the user
* @returns {RankCard}
*/
setName(name) {
this.name = name;
return this;
}
/**
* @param {string} avatar
* @description url or path of the avatar
* @returns {RankCard}
*/
setAvatar(avatar) {
this.avatar = avatar;
return this;
}
async buildCard() {
let {
widthCard,
heightCard
} = this;
const {
main_color,
sub_color,
alpha_subcard,
exp_color,
expNextLevel_color,
text_color,
name_color,
level_color,
rank_color,
line_color,
exp_text_color,
exp,
expNextLevel,
name,
level,
rank,
avatar
} = this;
widthCard = Number(widthCard);
heightCard = Number(heightCard);
const canvas = Canvas.createCanvas(widthCard, heightCard);
const ctx = canvas.getContext("2d");
/*
+-----------------------+
| DRAW SUBCARD |
+-----------------------+
*/
const alignRim = 3 * percentage(widthCard);
const Alpha = parseFloat(alpha_subcard || 0);
ctx.globalAlpha = Alpha;
await checkColorOrImageAndDraw(alignRim, alignRim, widthCard - alignRim * 2, heightCard - alignRim * 2, ctx, sub_color, 20, alpha_subcard);
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "destination-out";
const xyAvatar = heightCard / 2;
const resizeAvatar = 60 * percentage(heightCard);
// Kẽ đường ngang ở giữa
// Draw a horizontal line in the middle
const widthLineBetween = 58 * percentage(widthCard);
const heightLineBetween = 2 * percentage(heightCard);
const angleLineCenter = 40;
const edge = heightCard / 2 * Math.tan(angleLineCenter * Math.PI / 180);
if (line_color) {
if (!isUrl(line_color)) {
ctx.fillStyle = ctx.strokeStyle = checkGradientColor(ctx,
Array.isArray(line_color) ? line_color : [line_color],
xyAvatar - resizeAvatar / 2 - heightLineBetween,
0,
xyAvatar + resizeAvatar / 2 + widthLineBetween + edge,
0
);
ctx.globalCompositeOperation = "source-over";
}
else {
ctx.save();
const img = Canvas.loadImage(line_color);
ctx.globalCompositeOperation = "source-over";
ctx.beginPath();
ctx.arc(xyAvatar, xyAvatar, resizeAvatar / 2 + heightLineBetween, 0, 2 * Math.PI);
ctx.fill();
ctx.rect(xyAvatar + resizeAvatar / 2, heightCard / 2 - heightLineBetween / 2, widthLineBetween, heightLineBetween);
ctx.fill();
ctx.translate(xyAvatar + resizeAvatar / 2 + widthLineBetween + edge, 0);
ctx.rotate(angleLineCenter * Math.PI / 180);
ctx.rect(0, 0, heightLineBetween, 1000);
ctx.fill();
ctx.rotate(-angleLineCenter * Math.PI / 180);
ctx.translate(-xyAvatar - resizeAvatar / 2 - widthLineBetween - edge, 0);
ctx.clip();
ctx.drawImage(await img, 0, 0, widthCard, heightCard);
ctx.restore();
}
}
ctx.beginPath();
if (!isUrl(line_color))
ctx.rect(xyAvatar + resizeAvatar / 2, heightCard / 2 - heightLineBetween / 2, widthLineBetween, heightLineBetween);
ctx.fill();
// Kẽ đường chéo ở cuối
// Draw a slant at the end
ctx.beginPath();
if (!isUrl(line_color)) {
ctx.moveTo(xyAvatar + resizeAvatar / 2 + widthLineBetween + edge, 0);
ctx.lineTo(xyAvatar + resizeAvatar / 2 + widthLineBetween - edge, heightCard);
ctx.lineWidth = heightLineBetween;
ctx.stroke();
}
// Xóa nền vị trí đặt avatar
// Remove background of avatar placement
ctx.beginPath();
if (!isUrl(line_color))
ctx.arc(xyAvatar, xyAvatar, resizeAvatar / 2 + heightLineBetween, 0, 2 * Math.PI);
ctx.fill();
ctx.globalCompositeOperation = "destination-out";
// Xóa xung quanh sub card
// Remove around sub card
ctx.fillRect(0, 0, widthCard, alignRim);
ctx.fillRect(0, heightCard - alignRim, widthCard, alignRim);
// Xóa nền tại vị trí đặt thanh Exp
// Remove the background at the location where the Exp bar is located
const radius = 6 * percentage(heightCard);
const xStartExp = (25 + 1.5) * percentage(widthCard),
yStartExp = 67 * percentage(heightCard),
widthExp = 40.5 * percentage(widthCard),
heightExp = radius * 2;
ctx.globalCompositeOperation = "source-over";
centerImage(ctx, await Canvas.loadImage(avatar), xyAvatar, xyAvatar, resizeAvatar, resizeAvatar);
// Vẽ thanh Exp
// Draw Exp bar
if (!isUrl(expNextLevel_color)) {
ctx.beginPath();
ctx.fillStyle = checkGradientColor(ctx, expNextLevel_color, xStartExp, yStartExp, xStartExp + widthExp, yStartExp);
ctx.arc(xStartExp, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI, true);
ctx.fill();
ctx.fillRect(xStartExp, yStartExp, widthExp, heightExp);
ctx.arc(xStartExp + widthExp, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI, false);
ctx.fill();
}
else {
ctx.save();
ctx.beginPath();
ctx.moveTo(xStartExp, yStartExp);
ctx.lineTo(xStartExp + widthExp, yStartExp);
ctx.arcTo(xStartExp + widthExp + radius, yStartExp, xStartExp + widthExp + radius, yStartExp + radius, radius);
ctx.lineTo(xStartExp + widthExp + radius, yStartExp + heightExp - radius);
ctx.arcTo(xStartExp + widthExp + radius, yStartExp + heightExp, xStartExp + widthExp, yStartExp + heightExp, radius);
ctx.lineTo(xStartExp, yStartExp + heightExp);
ctx.arcTo(xStartExp, yStartExp + heightExp, xStartExp - radius, yStartExp + heightExp - radius, radius);
ctx.lineTo(xStartExp - radius, yStartExp + radius);
ctx.arcTo(xStartExp, yStartExp, xStartExp, yStartExp, radius);
ctx.closePath();
ctx.clip();
ctx.drawImage(await Canvas.loadImage(expNextLevel_color), xStartExp, yStartExp, widthExp + radius, heightExp);
ctx.restore();
}
// Exp hiện tại
// Current Exp
const widthExpCurrent = (100 / expNextLevel * exp) * percentage(widthExp);
if (!isUrl(exp_color)) {
ctx.fillStyle = checkGradientColor(ctx, exp_color, xStartExp, yStartExp, xStartExp + widthExp, yStartExp);
ctx.beginPath();
ctx.arc(xStartExp, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI, true);
ctx.fill();
ctx.fillRect(xStartExp, yStartExp, widthExpCurrent, heightExp);
ctx.beginPath();
ctx.arc(xStartExp + widthExpCurrent - 1, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI);
ctx.fill();
}
else {
const imgExp = await Canvas.loadImage(exp_color);
ctx.save();
ctx.beginPath();
ctx.moveTo(xStartExp, yStartExp);
ctx.lineTo(xStartExp + widthExpCurrent, yStartExp);
ctx.arc(xStartExp + widthExpCurrent, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI, false);
ctx.lineTo(xStartExp + widthExpCurrent + radius, yStartExp + heightExp - radius);
ctx.arcTo(xStartExp + widthExpCurrent + radius, yStartExp + heightExp, xStartExp + widthExpCurrent, yStartExp + heightExp, radius);
ctx.lineTo(xStartExp, yStartExp + heightExp);
ctx.arc(xStartExp, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI, true);
ctx.lineTo(xStartExp - radius, yStartExp + radius);
ctx.arc(xStartExp, yStartExp + radius, radius, 1.5 * Math.PI, 0.5 * Math.PI, true);
ctx.closePath();
ctx.clip();
ctx.drawImage(imgExp, xStartExp - radius, yStartExp, widthExp + radius * 2, heightExp);
ctx.restore();
}
const maxSizeFont_Name = 4 * percentage(widthCard) + this.textSize;
const maxSizeFont_Exp = 2 * percentage(widthCard) + this.textSize;
const maxSizeFont_Level = 3.25 * percentage(widthCard) + this.textSize;
const maxSizeFont_Rank = 4 * percentage(widthCard) + this.textSize;
ctx.textAlign = "end";
// Vẽ chữ Rank
// Draw rank text
ctx.font = autoSizeFont(18.4 * percentage(widthCard), maxSizeFont_Rank, rank, ctx, this.fontName);
const metricsRank = ctx.measureText(rank);
ctx.fillStyle = checkGradientColor(ctx, rank_color || text_color,
94 * percentage(widthCard) - metricsRank.width,
76 * percentage(heightCard) + metricsRank.emHeightDescent,
94 * percentage(widthCard),
76 * percentage(heightCard) - metricsRank.actualBoundingBoxAscent
);
ctx.fillText(rank, 94 * percentage(widthCard), 76 * percentage(heightCard));
// Draw Level text
const textLevel = `Lv ${level}`;
ctx.font = autoSizeFont(9.8 * percentage(widthCard), maxSizeFont_Level, textLevel, ctx, this.fontName);
const metricsLevel = ctx.measureText(textLevel);
const xStartLevel = 94 * percentage(widthCard);
const yStartLevel = 32 * percentage(heightCard);
ctx.fillStyle = checkGradientColor(ctx, level_color || text_color,
xStartLevel - ctx.measureText(textLevel).width,
yStartLevel + metricsLevel.emHeightDescent,
xStartLevel,
yStartLevel - metricsLevel.actualBoundingBoxAscent
);
ctx.fillText(textLevel, xStartLevel, yStartLevel);
ctx.font = autoSizeFont(52.1 * percentage(widthCard), maxSizeFont_Name, name, ctx, this.fontName);
ctx.textAlign = "center";
// Draw Name
const metricsName = ctx.measureText(name);
ctx.fillStyle = checkGradientColor(ctx, name_color || text_color,
47.5 * percentage(widthCard) - metricsName.width / 2,
40 * percentage(heightCard) + metricsName.emHeightDescent,
47.5 * percentage(widthCard) + metricsName.width / 2,
40 * percentage(heightCard) - metricsName.actualBoundingBoxAscent
);
ctx.fillText(name, 47.5 * percentage(widthCard), 40 * percentage(heightCard));
// Draw Exp text
const textExp = `Exp ${exp}/${expNextLevel}`;
ctx.font = autoSizeFont(49 * percentage(widthCard), maxSizeFont_Exp, textExp, ctx, this.fontName);
const metricsExp = ctx.measureText(textExp);
ctx.fillStyle = checkGradientColor(ctx, exp_text_color || text_color,
47.5 * percentage(widthCard) - metricsExp.width / 2,
61.4 * percentage(heightCard) + metricsExp.emHeightDescent,
47.5 * percentage(widthCard) + metricsExp.width / 2,
61.4 * percentage(heightCard) - metricsExp.actualBoundingBoxAscent
);
ctx.fillText(textExp, 47.5 * percentage(widthCard), 61.4 * percentage(heightCard));
/*
+------------------------------------+
| DRAW MAINCARD (BACKGROUND) |
+------------------------------------+
*/
ctx.globalCompositeOperation = "destination-over";
if (main_color.match?.(/^https?:\/\//) || Buffer.isBuffer(main_color)) {
ctx.beginPath();
ctx.moveTo(radius, 0);
ctx.lineTo(widthCard - radius, 0);
ctx.quadraticCurveTo(widthCard, 0, widthCard, radius);
ctx.lineTo(widthCard, heightCard - radius);
ctx.quadraticCurveTo(widthCard, heightCard, widthCard - radius, heightCard);
ctx.lineTo(radius, heightCard);
ctx.quadraticCurveTo(0, heightCard, 0, heightCard - radius);
ctx.lineTo(0, radius);
ctx.quadraticCurveTo(0, 0, radius, 0);
ctx.closePath();
ctx.clip();
ctx.drawImage(await Canvas.loadImage(main_color), 0, 0, widthCard, heightCard);
}
else {
ctx.fillStyle = checkGradientColor(ctx, main_color, 0, 0, widthCard, heightCard);
drawSquareRounded(ctx, 0, 0, widthCard, heightCard, radius, main_color);
}
// return canvas.toBuffer();
// return stream
return canvas.createPNGStream();
}
}
async function checkColorOrImageAndDraw(xStart, yStart, width, height, ctx, colorOrImage, r) {
if (!colorOrImage.match?.(/^https?:\/\//)) {
if (Array.isArray(colorOrImage)) {
const gradient = ctx.createLinearGradient(xStart, yStart, xStart + width, yStart + height);
colorOrImage.forEach((color, index) => {
gradient.addColorStop(index / (colorOrImage.length - 1), color);
});
ctx.fillStyle = gradient;
}
drawSquareRounded(ctx, xStart, yStart, width, height, r, colorOrImage);
}
else {
const imageLoad = await Canvas.loadImage(colorOrImage);
ctx.save();
roundedImage(xStart, yStart, width, height, r, ctx);
ctx.clip();
ctx.drawImage(imageLoad, xStart, yStart, width, height);
ctx.restore();
}
}
function drawSquareRounded(ctx, x, y, w, h, r, color, defaultGlobalCompositeOperation, notChangeColor) {
ctx.save();
if (defaultGlobalCompositeOperation)
ctx.globalCompositeOperation = "source-over";
if (w < 2 * r)
r = w / 2;
if (h < 2 * r)
r = h / 2;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
if (!notChangeColor)
ctx.fillStyle = color;
ctx.fill();
ctx.restore();
}
function roundedImage(x, y, width, height, radius, ctx) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function centerImage(ctx, img, xCenter, yCenter, w, h) {
const x = xCenter - w / 2;
const y = yCenter - h / 2;
ctx.save();
ctx.beginPath();
ctx.arc(xCenter, yCenter, w / 2, 0, 2 * Math.PI);
ctx.clip();
ctx.closePath();
ctx.drawImage(img, x, y, w, h);
ctx.restore();
}
function autoSizeFont(maxWidthText, maxSizeFont, text, ctx, fontName) {
let sizeFont = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
sizeFont += 1;
ctx.font = sizeFont + "px " + fontName;
const widthText = ctx.measureText(text).width;
if (widthText > maxWidthText || sizeFont > maxSizeFont) break;
}
return sizeFont + "px " + fontName;
}
function checkGradientColor(ctx, color, x1, y1, x2, y2) {
if (Array.isArray(color)) {
const gradient = ctx.createLinearGradient(x1, y1, x2, y2);
color.forEach((c, index) => {
gradient.addColorStop(index / (color.length - 1), c);
});
return gradient;
}
else {
return color;
}
}
function isUrl(string) {
try {
new URL(string);
return true;
}
catch (err) {
return false;
}
}
function checkFormatColor(color, enableUrl = true) {
if (
!/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(color) &&
!/^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/.test(color) &&
!/^rgba\((\d{1,3}), (\d{1,3}), (\d{1,3}), (\d{1,3})\)$/.test(color) &&
(enableUrl ? !isUrl(color) : true) &&
!Array.isArray(color)
)
throw new Error(`The color format must be a hex, rgb, rgba ${enableUrl ? ", url image" : ""} or an array of colors`);
}
|