- 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
import type { Graphics } from '@pixi/graphics';
/**
* Draw a regular polygon with rounded corners.
*
* _Note: Only available with **@pixi/graphics-extras**._
* @method PIXI.Graphics#drawRoundedPolygon
* @param this
* @param {number} x - X position
* @param {number} y - Y position
* @param {number} radius - Polygon radius
* @param {number} sides - Minimum value is 3
* @param {number} corner - Corner size in pixels.
* @param {number} rotation - Starting rotation values in radians..
* @returns {PIXI.Graphics} This Graphics object. Good for chaining method calls
*/
export function drawRoundedPolygon(this: Graphics,
x: number,
y: number,
radius: number,
sides: number,
corner: number,
rotation = 0): Graphics
{
sides = Math.max((sides | 0), 3);
if (corner <= 0)
{
return this.drawRegularPolygon(x, y, radius, sides, rotation);
}
const sideLength = (radius * Math.sin(Math.PI / sides)) - 0.001;
corner = Math.min(corner, sideLength);
const startAngle = (-1 * Math.PI / 2) + rotation;
const delta = (Math.PI * 2) / sides;
const internalAngle = ((sides - 2) * Math.PI) / sides / 2;
for (let i = 0; i < sides; i++)
{
const angle = (i * delta) + startAngle;
const x0 = x + (radius * Math.cos(angle));
const y0 = y + (radius * Math.sin(angle));
const a1 = angle + (Math.PI) + internalAngle;
const a2 = angle - (Math.PI) - internalAngle;
const x1 = x0 + (corner * Math.cos(a1));
const y1 = y0 + (corner * Math.sin(a1));
const x3 = x0 + (corner * Math.cos(a2));
const y3 = y0 + (corner * Math.sin(a2));
if (i === 0)
{
this.moveTo(x1, y1);
}
else
{
this.lineTo(x1, y1);
}
this.quadraticCurveTo(x0, y0, x3, y3);
}
return this.closePath();
}