forked from RatWasHere/bmods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvasEditImageBorder_MOD.js
115 lines (104 loc) · 2.99 KB
/
CanvasEditImageBorder_MOD.js
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
module.exports = {
data: {
name: "Canvas Edit Image Border",
},
modules: ["canvas"],
category: "Canvas Images",
info: {
source: "https://github.com/RatWasHere/bmods/tree/master/Actions",
creator: "TheMonDon",
},
UI: [
{
element: "variableInsertion",
storeAs: "image",
name: "Source Image",
},
"-",
{
element: "typedDropdown",
storeAs: "circleinfo",
name: "Circle?",
choices: {
yes: { name: "Yes" },
no: { name: "No" },
},
},
{
element: "input",
storeAs: "radius",
name: "Round Corner Radius",
},
"-",
{
element: "storageInput",
storeAs: "store",
},
],
async run(values, interaction, client, bridge) {
const Canvas = await client.getMods().require("canvas");
const imageData = await bridge.getImage(values.image);
if (!imageData) {
console.error("Image data is undefined or null.");
return;
}
// Convert imageData to a valid format if needed
const validImageData = Buffer.isBuffer(imageData)
? `data:image/png;base64,${imageData.toString("base64")}`
: imageData;
try {
// Load the image
const [imagedata] = await Promise.all([Canvas.loadImage(validImageData)]);
const canvas = Canvas.createCanvas(imagedata.width, imagedata.height);
const ctx = canvas.getContext("2d");
const radius = parseInt(bridge.transf(values.radius), 10) || 0;
const circleinfo = values.circleinfo === "yes"; // Convert dropdown choice to boolean
// Function to clip as a circle
function circle() {
ctx.beginPath();
ctx.arc(
imagedata.width / 2,
imagedata.height / 2,
Math.min(imagedata.width, imagedata.height) / 2,
0,
Math.PI * 2
);
ctx.closePath();
ctx.clip();
}
// Function to clip with rounded corners
function corner(r) {
ctx.beginPath();
ctx.moveTo(r, 0);
ctx.lineTo(imagedata.width - r, 0);
ctx.quadraticCurveTo(imagedata.width, 0, imagedata.width, r);
ctx.lineTo(imagedata.width, imagedata.height - r);
ctx.quadraticCurveTo(
imagedata.width,
imagedata.height,
imagedata.width - r,
imagedata.height
);
ctx.lineTo(r, imagedata.height);
ctx.quadraticCurveTo(0, imagedata.height, 0, imagedata.height - r);
ctx.lineTo(0, r);
ctx.quadraticCurveTo(0, 0, r, 0);
ctx.closePath();
ctx.clip();
}
// Apply circle or corner clipping
if (circleinfo && imagedata.width === imagedata.height) {
circle();
} else if (radius > 0) {
corner(radius);
}
// Draw the image
ctx.drawImage(imagedata, 0, 0);
// Export the canvas as a buffer
const buffer = canvas.toBuffer();
bridge.store(values.store, buffer);
} catch (error) {
console.error("Error during image processing:", error);
}
},
};