Skip to content

Commit 715f866

Browse files
committed
Fix #180 : Implement rounded corners on rect element
1 parent f00db8f commit 715f866

5 files changed

Lines changed: 76 additions & 12 deletions

File tree

dist/vivus.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* vivus - JavaScript library to make drawing animation on SVG
3-
* @version v0.4.1
3+
* @version v0.4.2
44
* @link https://github.com/maxwellito/vivus
55
* @license MIT
66
*/
@@ -123,10 +123,28 @@ Pathformer.prototype.rectToPath = function (element) {
123123
width = parseFloat(element.width) || 0,
124124
height = parseFloat(element.height) || 0;
125125

126-
newElement.d = 'M' + x + ' ' + y + ' ';
127-
newElement.d += 'L' + (x + width) + ' ' + y + ' ';
128-
newElement.d += 'L' + (x + width) + ' ' + (y + height) + ' ';
129-
newElement.d += 'L' + x + ' ' + (y + height) + ' Z';
126+
if (element.rx || element.ry) {
127+
var rx = parseInt(element.rx, 10) || -1,
128+
ry = parseInt(element.ry, 10) || -1;
129+
rx = Math.min(Math.max(rx < 0 ? ry : rx, 0), width/2);
130+
ry = Math.min(Math.max(ry < 0 ? rx : ry, 0), height/2);
131+
132+
newElement.d = 'M ' + (x + rx) + ',' + y + ' ' +
133+
'L ' + (x + width - rx) + ',' + y + ' ' +
134+
'A ' + rx + ',' + ry + ',0,0,1,' + (x + width) + ',' + (y + ry) + ' ' +
135+
'L ' + (x + width) + ',' + (y + height - ry) + ' ' +
136+
'A ' + rx + ',' + ry + ',0,0,1,' + (x + width - rx) + ',' + (y + height) + ' ' +
137+
'L ' + (x + rx) + ',' + (y + height) + ' ' +
138+
'A ' + rx + ',' + ry + ',0,0,1,' + x + ',' + (y + height - ry) + ' ' +
139+
'L ' + x + ',' + (y + ry) + ' ' +
140+
'A ' + rx + ',' + ry + ',0,0,1,' + (x + rx) + ',' + y;
141+
}
142+
else {
143+
newElement.d = 'M' + x + ' ' + y + ' ' +
144+
'L' + (x + width) + ' ' + y + ' ' +
145+
'L' + (x + width) + ' ' + (y + height) + ' ' +
146+
'L' + x + ' ' + (y + height) + ' Z';
147+
}
130148
return newElement;
131149
};
132150

0 commit comments

Comments
 (0)