Skip to content

Commit cc5f1bb

Browse files
authored
Merge pull request phaserjs#3755 from TadejZupancic/patch-8
Add rounded rectangle to Graphics
2 parents ebdd7c3 + c1d0c11 commit cc5f1bb

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

src/gameobjects/graphics/Graphics.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,108 @@ var Graphics = new Class({
548548
return this;
549549
},
550550

551+
/**
552+
* Fill a rounded rectangle with the given position, size and radius.
553+
*
554+
* @method Phaser.GameObjects.Graphics#fillRoundedRect
555+
* @since 3.11.0
556+
*
557+
* @param {number} x - The x coordinate of the top-left of the rectangle.
558+
* @param {number} y - The y coordinate of the top-left of the rectangle.
559+
* @param {number} width - The width of the rectangle.
560+
* @param {number} height - The height of the rectangle.
561+
* @param {number} [radius = 20] - The corner radius; It can also be an object to specify different radii for corners
562+
* @param {number} [radius.tl = 0] Top left
563+
* @param {number} [radius.tr = 0] Top right
564+
* @param {number} [radius.br = 0] Bottom right
565+
* @param {number} [radius.bl = 0] Bottom left
566+
*
567+
* @return {Phaser.GameObjects.Graphics} This Game Object.
568+
*/
569+
fillRoundedRect: function (x, y, width, height, radius)
570+
{
571+
if (typeof radius === 'number')
572+
{
573+
radius = {tl: radius, tr: radius, br: radius, bl: radius};
574+
}
575+
else if (typeof radius === 'object')
576+
{
577+
radius.tl = radius.tl || 0;
578+
radius.tr = radius.tr || 0;
579+
radius.br = radius.br || 0;
580+
radius.bl = radius.bl || 0;
581+
}
582+
else
583+
{
584+
radius = {tl: 20, tr: 20, br: 20, bl: 20};
585+
}
586+
587+
this.beginPath();
588+
this.moveTo(x + radius.tl, y);
589+
this.lineTo(x + width - radius.tr, y);
590+
this.arc(x + width - radius.tr, y + radius.tr, radius.tr, -MATH_CONST.TAU, 0);
591+
this.lineTo(x + width, y + height - radius.br);
592+
this.arc(x + width - radius.br, y + height - radius.br, radius.br, 0, MATH_CONST.TAU);
593+
this.lineTo(x + radius.bl, y + height);
594+
this.arc(x + radius.bl, y + height - radius.bl, radius.bl, MATH_CONST.TAU, Math.PI);
595+
this.lineTo(x, y + radius.tl);
596+
this.arc(x + radius.tl, y + radius.tl, radius.tl, -Math.PI, -MATH_CONST.TAU);
597+
this.fillPath();
598+
599+
return this;
600+
},
601+
602+
/**
603+
* Stroke a rounded rectangle with the given position, size and radius.
604+
*
605+
* @method Phaser.GameObjects.Graphics#strokeRoundedRect
606+
* @since 3.11.0
607+
*
608+
* @param {number} x - The x coordinate of the top-left of the rectangle.
609+
* @param {number} y - The y coordinate of the top-left of the rectangle.
610+
* @param {number} width - The width of the rectangle.
611+
* @param {number} height - The height of the rectangle.
612+
* @param {number} [radius = 20] - The corner radius; It can also be an object to specify different radii for corners
613+
* @param {number} [radius.tl = 0] Top left
614+
* @param {number} [radius.tr = 0] Top right
615+
* @param {number} [radius.br = 0] Bottom right
616+
* @param {number} [radius.bl = 0] Bottom left
617+
*
618+
* @return {Phaser.GameObjects.Graphics} This Game Object.
619+
*/
620+
strokeRoundedRect: function (x, y, width, height, radius)
621+
{
622+
if (typeof radius === 'number')
623+
{
624+
radius = {tl: radius, tr: radius, br: radius, bl: radius};
625+
}
626+
else if (typeof radius === 'object')
627+
{
628+
radius.tl = radius.tl || 0;
629+
radius.tr = radius.tr || 0;
630+
radius.br = radius.br || 0;
631+
radius.bl = radius.bl || 0;
632+
}
633+
else
634+
{
635+
radius = {tl: 20, tr: 20, br: 20, bl: 20};
636+
}
637+
638+
this.beginPath();
639+
this.moveTo(x + radius.tl, y);
640+
this.lineTo(x + width - radius.tr, y);
641+
this.arc(x + width - radius.tr, y + radius.tr, radius.tr, -MATH_CONST.TAU, 0);
642+
this.lineTo(x + width, y + height - radius.br);
643+
this.arc(x + width - radius.br, y + height - radius.br, radius.br, 0, MATH_CONST.TAU);
644+
this.lineTo(x + radius.bl, y + height);
645+
this.arc(x + radius.bl, y + height - radius.bl, radius.bl, MATH_CONST.TAU, Math.PI);
646+
this.lineTo(x, y + radius.tl);
647+
this.arc(x + radius.tl, y + radius.tl, radius.tl, -Math.PI, -MATH_CONST.TAU);
648+
this.strokePath();
649+
650+
return this;
651+
},
652+
551653
/**
552654
* Fill the given point.
553655
*

0 commit comments

Comments
 (0)