Skip to content

Commit 723b124

Browse files
committed
roundCorners
(cherry picked from commit 9d85c16)
1 parent 7934642 commit 723b124

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎ImageScript.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,48 @@ export class Image {
10001000
};
10011001
}
10021002

1003+
/**
1004+
* Rounds the images corners
1005+
* @param {number} [radius=min(width,height)/4] The radius of the corners
1006+
* @return {Image}
1007+
*/
1008+
roundCorners(radius = Math.min(this.width, this.height) / 4) {
1009+
const radSquared = radius ** 2;
1010+
for (let x = 1; x <= radius; x++) {
1011+
const xRad = (x - radius) ** 2;
1012+
for (let y = 1; y <= radius; y++) {
1013+
if (xRad + (y - radius) ** 2 > radSquared)
1014+
this.bitmap[((y - 1) * this.width + x - 1) * 4 + 3] = 0;
1015+
}
1016+
}
1017+
1018+
for (let x = 1; x <= radius; x++) {
1019+
const xRad = (x - radius) ** 2;
1020+
for (let y = this.height - radius; y <= this.height; y++) {
1021+
if (xRad + ((this.height - y) - radius) ** 2 > radSquared)
1022+
this.bitmap[((y - 1) * this.width + x - 1) * 4 + 3] = 0;
1023+
}
1024+
}
1025+
1026+
for (let x = this.width - radius; x <= this.width; x++) {
1027+
const xRad = ((this.width - x) - radius) ** 2;
1028+
for (let y = 1; y <= radius; y++) {
1029+
if (xRad + (y - radius) ** 2 > radSquared)
1030+
this.bitmap[((y - 1) * this.width + x - 1) * 4 + 3] = 0;
1031+
}
1032+
}
1033+
1034+
for (let x = this.width - radius; x <= this.width; x++) {
1035+
const xRad = ((this.width - x) - radius) ** 2;
1036+
for (let y = this.height - radius; y <= this.height; y++) {
1037+
if (xRad + ((this.height - y) - radius) ** 2 > radSquared)
1038+
this.bitmap[((y - 1) * this.width + x - 1) * 4 + 3] = 0;
1039+
}
1040+
}
1041+
1042+
return this;
1043+
}
1044+
10031045
/**
10041046
* @private
10051047
*/

0 commit comments

Comments
 (0)