Skip to content

Commit 553245c

Browse files
authored
Merge pull request #1950 from Caden-Hornyak/fix/corner-radius-negative-dimensions
Fixed corner radius for Konva.Rect negative width/height
2 parents e33341d + 33e5ddf commit 553245c

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/Util.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,11 @@ export const Util = {
10171017
height: number,
10181018
cornerRadius: number | number[]
10191019
) {
1020+
// if negative dimensions, abs width/height and move rectangle
1021+
let xOrigin = width < 0 ? width : 0;
1022+
let yOrigin = height < 0 ? height : 0;
1023+
width = Math.abs(width);
1024+
height = Math.abs(height);
10201025
let topLeft = 0;
10211026
let topRight = 0;
10221027
let bottomLeft = 0;
@@ -1033,35 +1038,42 @@ export const Util = {
10331038
bottomRight = Math.min(cornerRadius[2] || 0, width / 2, height / 2);
10341039
bottomLeft = Math.min(cornerRadius[3] || 0, width / 2, height / 2);
10351040
}
1036-
context.moveTo(topLeft, 0);
1037-
context.lineTo(width - topRight, 0);
1041+
context.moveTo(xOrigin + topLeft, yOrigin);
1042+
context.lineTo(xOrigin + width - topRight, yOrigin);
10381043
context.arc(
1039-
width - topRight,
1040-
topRight,
1044+
xOrigin + width - topRight,
1045+
yOrigin + topRight,
10411046
topRight,
10421047
(Math.PI * 3) / 2,
10431048
0,
10441049
false
10451050
);
1046-
context.lineTo(width, height - bottomRight);
1051+
context.lineTo(xOrigin + width, yOrigin + height - bottomRight);
10471052
context.arc(
1048-
width - bottomRight,
1049-
height - bottomRight,
1053+
xOrigin + width - bottomRight,
1054+
yOrigin + height - bottomRight,
10501055
bottomRight,
10511056
0,
10521057
Math.PI / 2,
10531058
false
10541059
);
1055-
context.lineTo(bottomLeft, height);
1060+
context.lineTo(xOrigin + bottomLeft, yOrigin + height);
10561061
context.arc(
1057-
bottomLeft,
1058-
height - bottomLeft,
1062+
xOrigin + bottomLeft,
1063+
yOrigin + height - bottomLeft,
10591064
bottomLeft,
10601065
Math.PI / 2,
10611066
Math.PI,
10621067
false
10631068
);
1064-
context.lineTo(0, topLeft);
1065-
context.arc(topLeft, topLeft, topLeft, Math.PI, (Math.PI * 3) / 2, false);
1069+
context.lineTo(xOrigin, yOrigin + topLeft);
1070+
context.arc(
1071+
xOrigin + topLeft,
1072+
yOrigin + topLeft,
1073+
topLeft,
1074+
Math.PI,
1075+
(Math.PI * 3) / 2,
1076+
false
1077+
);
10661078
},
10671079
};

test/unit/Rect-test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,28 @@ describe('Rect', function () {
234234
'clearRect(0,0,578,200);save();transform(1,0,0,1,50,50);beginPath();moveTo(0,0);lineTo(90,0);arc(90,10,10,4.712,0,false);lineTo(100,80);arc(80,80,20,0,1.571,false);lineTo(30,100);arc(30,70,30,1.571,3.142,false);lineTo(0,0);arc(0,0,0,3.142,4.712,false);closePath();fillStyle=black;fill();restore();clearRect(0,0,578,200);save();transform(1,0,0,1,50,50);beginPath();moveTo(0,0);lineTo(90,0);arc(90,10,10,4.712,0,false);lineTo(100,80);arc(80,80,20,0,1.571,false);lineTo(30,100);arc(30,70,30,1.571,3.142,false);lineTo(0,0);arc(0,0,0,3.142,4.712,false);closePath();fillStyle=black;fill();restore();'
235235
);
236236
});
237+
238+
// ======================================================
239+
it('corner radius with negative width and height', function () {
240+
var stage = addStage();
241+
var layer = new Konva.Layer();
242+
var rect = new Konva.Rect({
243+
x: 100,
244+
y: 100,
245+
width: -100,
246+
height: -100,
247+
fill: 'black',
248+
cornerRadius: [0, 10, 20, 30],
249+
});
250+
251+
layer.add(rect);
252+
stage.add(layer);
253+
layer.draw();
254+
255+
var trace = layer.getContext().getTrace();
256+
assert.equal(
257+
trace,
258+
'clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);beginPath();moveTo(-100,-100);lineTo(-10,-100);arc(-10,-90,10,4.712,0,false);lineTo(0,-20);arc(-20,-20,20,0,1.571,false);lineTo(-70,0);arc(-70,-30,30,1.571,3.142,false);lineTo(-100,-100);arc(-100,-100,0,3.142,4.712,false);closePath();fillStyle=black;fill();restore();clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);beginPath();moveTo(-100,-100);lineTo(-10,-100);arc(-10,-90,10,4.712,0,false);lineTo(0,-20);arc(-20,-20,20,0,1.571,false);lineTo(-70,0);arc(-70,-30,30,1.571,3.142,false);lineTo(-100,-100);arc(-100,-100,0,3.142,4.712,false);closePath();fillStyle=black;fill();restore();'
259+
);
260+
});
237261
});

0 commit comments

Comments
 (0)