- 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
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
import { earcut } from '@pixi/utils';
// for type only
import type { IShapeBuildCommand } from './IShapeBuildCommand';
import type { RoundedRectangle } from '@pixi/math';
import { Graphics } from '../Graphics';
import { buildCircle } from './buildCircle';
/**
* Calculate a single point for a quadratic bezier curve.
* Utility function used by quadraticBezierCurve.
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {number} n1 - first number
* @param {number} n2 - second number
* @param {number} perc - percentage
* @returns {number} the result
*/
function getPt(n1: number, n2: number, perc: number): number
{
const diff = n2 - n1;
return n1 + (diff * perc);
}
/**
* Calculate the points for a quadratic bezier curve. (helper function..)
* Based on: https://stackoverflow.com/questions/785097/how-do-i-implement-a-bezier-curve-in-c
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {number} fromX - Origin point x
* @param {number} fromY - Origin point x
* @param {number} cpX - Control point x
* @param {number} cpY - Control point y
* @param {number} toX - Destination point x
* @param {number} toY - Destination point y
* @param {number[]} [out=[]] - The output array to add points into. If not passed, a new array is created.
* @returns {number[]} an array of points
*/
function quadraticBezierCurve(
fromX: number, fromY: number,
cpX: number, cpY: number,
toX: number, toY: number,
out: Array<number> = []): Array<number>
{
const n = 20;
const points = out;
let xa = 0;
let ya = 0;
let xb = 0;
let yb = 0;
let x = 0;
let y = 0;
for (let i = 0, j = 0; i <= n; ++i)
{
j = i / n;
// The Green Line
xa = getPt(fromX, cpX, j);
ya = getPt(fromY, cpY, j);
xb = getPt(cpX, toX, j);
yb = getPt(cpY, toY, j);
// The Black Dot
x = getPt(xa, xb, j);
y = getPt(ya, yb, j);
// Handle case when first curve points overlaps and earcut fails to triangulate
if (i === 0 && points[points.length - 2] === x && points[points.length - 1] === y)
{
continue;
}
points.push(x, y);
}
return points;
}
/**
* Builds a rounded rectangle to draw
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {PIXI.WebGLGraphicsData} graphicsData - The graphics object containing all the necessary properties
* @param {object} webGLData - an object containing all the WebGL-specific information to create this shape
* @param {object} webGLDataNativeLines - an object containing all the WebGL-specific information to create nativeLines
*/
export const buildRoundedRectangle: IShapeBuildCommand = {
build(graphicsData)
{
if (Graphics.nextRoundedRectBehavior)
{
buildCircle.build(graphicsData);
return;
}
const rrectData = graphicsData.shape as RoundedRectangle;
const points = graphicsData.points;
const x = rrectData.x;
const y = rrectData.y;
const width = rrectData.width;
const height = rrectData.height;
// Don't allow negative radius or greater than half the smallest width
const radius = Math.max(0, Math.min(rrectData.radius, Math.min(width, height) / 2));
points.length = 0;
// No radius, do a simple rectangle
if (!radius)
{
points.push(x, y,
x + width, y,
x + width, y + height,
x, y + height);
}
else
{
quadraticBezierCurve(x, y + radius,
x, y,
x + radius, y,
points);
quadraticBezierCurve(x + width - radius,
y, x + width, y,
x + width, y + radius,
points);
quadraticBezierCurve(x + width, y + height - radius,
x + width, y + height,
x + width - radius, y + height,
points);
quadraticBezierCurve(x + radius, y + height,
x, y + height,
x, y + height - radius,
points);
}
},
triangulate(graphicsData, graphicsGeometry)
{
if (Graphics.nextRoundedRectBehavior)
{
buildCircle.triangulate(graphicsData, graphicsGeometry);
return;
}
const points = graphicsData.points;
const verts = graphicsGeometry.points;
const indices = graphicsGeometry.indices;
const vecPos = verts.length / 2;
const triangles = earcut(points, null, 2);
for (let i = 0, j = triangles.length; i < j; i += 3)
{
indices.push(triangles[i] + vecPos);
// indices.push(triangles[i] + vecPos);
indices.push(triangles[i + 1] + vecPos);
// indices.push(triangles[i + 2] + vecPos);
indices.push(triangles[i + 2] + vecPos);
}
for (let i = 0, j = points.length; i < j; i++)
{
verts.push(points[i], points[++i]);
}
},
};