- 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
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
import { Point, SHAPES } from '@pixi/math';
import type { Polygon } from '@pixi/math';
import type { GraphicsData } from '../GraphicsData';
import type { GraphicsGeometry } from '../GraphicsGeometry';
import { LINE_JOIN, LINE_CAP, GRAPHICS_CURVES } from '../const';
/**
* Buffers vertices to draw a square cap.
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {number} x - X-coord of end point
* @param {number} y - Y-coord of end point
* @param {number} nx - X-coord of line normal pointing inside
* @param {number} ny - Y-coord of line normal pointing inside
* @param {number} innerWeight - Weight of inner points
* @param {number} outerWeight - Weight of outer points
* @param {boolean} clockwise - Whether the cap is drawn clockwise
* @param {Array<number>} verts - vertex buffer
* @returns {number} - no. of vertices pushed
*/
function square(
x: number,
y: number,
nx: number,
ny: number,
innerWeight: number,
outerWeight: number,
clockwise: boolean, /* rotation for square (true at left end, false at right end) */
verts: Array<number>
): number
{
const ix = x - (nx * innerWeight);
const iy = y - (ny * innerWeight);
const ox = x + (nx * outerWeight);
const oy = y + (ny * outerWeight);
/* Rotate nx,ny for extension vector */
let exx; let
eyy;
if (clockwise)
{
exx = ny;
eyy = -nx;
}
else
{
exx = -ny;
eyy = nx;
}
/* [i|0]x,y extended at cap */
const eix = ix + exx;
const eiy = iy + eyy;
const eox = ox + exx;
const eoy = oy + eyy;
/* Square itself must be inserted clockwise*/
verts.push(eix, eiy);
verts.push(eox, eoy);
return 2;
}
/**
* Buffers vertices to draw an arc at the line joint or cap.
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {number} cx - X-coord of center
* @param {number} cy - Y-coord of center
* @param {number} sx - X-coord of arc start
* @param {number} sy - Y-coord of arc start
* @param {number} ex - X-coord of arc end
* @param {number} ey - Y-coord of arc end
* @param {Array<number>} verts - buffer of vertices
* @param {boolean} clockwise - orientation of vertices
* @returns {number} - no. of vertices pushed
*/
function round(
cx: number,
cy: number,
sx: number,
sy: number,
ex: number,
ey: number,
verts: Array<number>,
clockwise: boolean, /* if not cap, then clockwise is turn of joint, otherwise rotation from angle0 to angle1 */
): number
{
const cx2p0x = sx - cx;
const cy2p0y = sy - cy;
let angle0 = Math.atan2(cx2p0x, cy2p0y);
let angle1 = Math.atan2(ex - cx, ey - cy);
if (clockwise && angle0 < angle1)
{
angle0 += Math.PI * 2;
}
else if (!clockwise && angle0 > angle1)
{
angle1 += Math.PI * 2;
}
let startAngle = angle0;
const angleDiff = angle1 - angle0;
const absAngleDiff = Math.abs(angleDiff);
/* if (absAngleDiff >= PI_LBOUND && absAngleDiff <= PI_UBOUND)
{
const r1x = cx - nxtPx;
const r1y = cy - nxtPy;
if (r1x === 0)
{
if (r1y > 0)
{
angleDiff = -angleDiff;
}
}
else if (r1x >= -GRAPHICS_CURVES.epsilon)
{
angleDiff = -angleDiff;
}
}*/
const radius = Math.sqrt((cx2p0x * cx2p0x) + (cy2p0y * cy2p0y));
const segCount = ((15 * absAngleDiff * Math.sqrt(radius) / Math.PI) >> 0) + 1;
const angleInc = angleDiff / segCount;
startAngle += angleInc;
if (clockwise)
{
verts.push(cx, cy);
verts.push(sx, sy);
for (let i = 1, angle = startAngle; i < segCount; i++, angle += angleInc)
{
verts.push(cx, cy);
verts.push(cx + ((Math.sin(angle) * radius)),
cy + ((Math.cos(angle) * radius)));
}
verts.push(cx, cy);
verts.push(ex, ey);
}
else
{
verts.push(sx, sy);
verts.push(cx, cy);
for (let i = 1, angle = startAngle; i < segCount; i++, angle += angleInc)
{
verts.push(cx + ((Math.sin(angle) * radius)),
cy + ((Math.cos(angle) * radius)));
verts.push(cx, cy);
}
verts.push(ex, ey);
verts.push(cx, cy);
}
return segCount * 2;
}
/**
* Builds a line to draw using the polygon method.
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {PIXI.GraphicsData} graphicsData - The graphics object containing all the necessary properties
* @param {PIXI.GraphicsGeometry} graphicsGeometry - Geometry where to append output
*/
function buildNonNativeLine(graphicsData: GraphicsData, graphicsGeometry: GraphicsGeometry): void
{
const shape = graphicsData.shape as Polygon;
let points = graphicsData.points || shape.points.slice();
const eps = graphicsGeometry.closePointEps;
if (points.length === 0)
{
return;
}
// if the line width is an odd number add 0.5 to align to a whole pixel
// commenting this out fixes #711 and #1620
// if (graphicsData.lineWidth%2)
// {
// for (i = 0; i < points.length; i++)
// {
// points[i] += 0.5;
// }
// }
const style = graphicsData.lineStyle;
// get first and last point.. figure out the middle!
const firstPoint = new Point(points[0], points[1]);
const lastPoint = new Point(points[points.length - 2], points[points.length - 1]);
const closedShape = shape.type !== SHAPES.POLY || shape.closeStroke;
const closedPath = Math.abs(firstPoint.x - lastPoint.x) < eps
&& Math.abs(firstPoint.y - lastPoint.y) < eps;
// if the first point is the last point - gonna have issues :)
if (closedShape)
{
// need to clone as we are going to slightly modify the shape..
points = points.slice();
if (closedPath)
{
points.pop();
points.pop();
lastPoint.set(points[points.length - 2], points[points.length - 1]);
}
const midPointX = (firstPoint.x + lastPoint.x) * 0.5;
const midPointY = (lastPoint.y + firstPoint.y) * 0.5;
points.unshift(midPointX, midPointY);
points.push(midPointX, midPointY);
}
const verts = graphicsGeometry.points;
const length = points.length / 2;
let indexCount = points.length;
const indexStart = verts.length / 2;
// Max. inner and outer width
const width = style.width / 2;
const widthSquared = width * width;
const miterLimitSquared = style.miterLimit * style.miterLimit;
/* Line segments of interest where (x1,y1) forms the corner. */
let x0 = points[0];
let y0 = points[1];
let x1 = points[2];
let y1 = points[3];
let x2 = 0;
let y2 = 0;
/* perp[?](x|y) = the line normal with magnitude lineWidth. */
let perpx = -(y0 - y1);
let perpy = x0 - x1;
let perp1x = 0;
let perp1y = 0;
let dist = Math.sqrt((perpx * perpx) + (perpy * perpy));
perpx /= dist;
perpy /= dist;
perpx *= width;
perpy *= width;
const ratio = style.alignment;// 0.5;
const innerWeight = (1 - ratio) * 2;
const outerWeight = ratio * 2;
if (!closedShape)
{
if (style.cap === LINE_CAP.ROUND)
{
indexCount += round(
x0 - (perpx * (innerWeight - outerWeight) * 0.5),
y0 - (perpy * (innerWeight - outerWeight) * 0.5),
x0 - (perpx * innerWeight),
y0 - (perpy * innerWeight),
x0 + (perpx * outerWeight),
y0 + (perpy * outerWeight),
verts,
true,
) + 2;
}
else if (style.cap === LINE_CAP.SQUARE)
{
indexCount += square(x0, y0, perpx, perpy, innerWeight, outerWeight, true, verts);
}
}
// Push first point (below & above vertices)
verts.push(
x0 - (perpx * innerWeight),
y0 - (perpy * innerWeight));
verts.push(
x0 + (perpx * outerWeight),
y0 + (perpy * outerWeight));
for (let i = 1; i < length - 1; ++i)
{
x0 = points[(i - 1) * 2];
y0 = points[((i - 1) * 2) + 1];
x1 = points[i * 2];
y1 = points[(i * 2) + 1];
x2 = points[(i + 1) * 2];
y2 = points[((i + 1) * 2) + 1];
perpx = -(y0 - y1);
perpy = x0 - x1;
dist = Math.sqrt((perpx * perpx) + (perpy * perpy));
perpx /= dist;
perpy /= dist;
perpx *= width;
perpy *= width;
perp1x = -(y1 - y2);
perp1y = x1 - x2;
dist = Math.sqrt((perp1x * perp1x) + (perp1y * perp1y));
perp1x /= dist;
perp1y /= dist;
perp1x *= width;
perp1y *= width;
/* d[x|y](0|1) = the component displacement between points p(0,1|1,2) */
const dx0 = x1 - x0;
const dy0 = y0 - y1;
const dx1 = x1 - x2;
const dy1 = y2 - y1;
/* +ve if internal angle counterclockwise, -ve if internal angle clockwise. */
const cross = (dy0 * dx1) - (dy1 * dx0);
const clockwise = (cross < 0);
/* Going nearly straight? */
if (Math.abs(cross) < 0.1)
{
verts.push(
x1 - (perpx * innerWeight),
y1 - (perpy * innerWeight));
verts.push(
x1 + (perpx * outerWeight),
y1 + (perpy * outerWeight));
continue;
}
/* p[x|y] is the miter point. pdist is the distance between miter point and p1. */
const c1 = ((-perpx + x0) * (-perpy + y1)) - ((-perpx + x1) * (-perpy + y0));
const c2 = ((-perp1x + x2) * (-perp1y + y1)) - ((-perp1x + x1) * (-perp1y + y2));
const px = ((dx0 * c2) - (dx1 * c1)) / cross;
const py = ((dy1 * c1) - (dy0 * c2)) / cross;
const pdist = ((px - x1) * (px - x1)) + ((py - y1) * (py - y1));
/* Inner miter point */
const imx = x1 + ((px - x1) * innerWeight);
const imy = y1 + ((py - y1) * innerWeight);
/* Outer miter point */
const omx = x1 - ((px - x1) * outerWeight);
const omy = y1 - ((py - y1) * outerWeight);
/* Is the inside miter point too far away, creating a spike? */
const smallerInsideSegmentSq = Math.min((dx0 * dx0) + (dy0 * dy0), (dx1 * dx1) + (dy1 * dy1));
const insideWeight = clockwise ? innerWeight : outerWeight;
const smallerInsideDiagonalSq = smallerInsideSegmentSq + (insideWeight * insideWeight * widthSquared);
const insideMiterOk = pdist <= smallerInsideDiagonalSq;
if (insideMiterOk)
{
if (style.join === LINE_JOIN.BEVEL || pdist / widthSquared > miterLimitSquared)
{
if (clockwise) /* rotating at inner angle */
{
verts.push(imx, imy);// inner miter point
verts.push(x1 + (perpx * outerWeight), y1 + (perpy * outerWeight));// first segment's outer vertex
verts.push(imx, imy);// inner miter point
verts.push(x1 + (perp1x * outerWeight), y1 + (perp1y * outerWeight));// second segment's outer vertex
}
else /* rotating at outer angle */
{
verts.push(x1 - (perpx * innerWeight), y1 - (perpy * innerWeight));// first segment's inner vertex
verts.push(omx, omy);// outer miter point
verts.push(x1 - (perp1x * innerWeight), y1 - (perp1y * innerWeight));// second segment's outer vertex
verts.push(omx, omy);// outer miter point
}
indexCount += 2;
}
else if (style.join === LINE_JOIN.ROUND)
{
if (clockwise) /* arc is outside */
{
verts.push(imx, imy);
verts.push(x1 + (perpx * outerWeight), y1 + (perpy * outerWeight));
indexCount += round(
x1, y1,
x1 + (perpx * outerWeight), y1 + (perpy * outerWeight),
x1 + (perp1x * outerWeight), y1 + (perp1y * outerWeight),
verts, true
) + 4;
verts.push(imx, imy);
verts.push(x1 + (perp1x * outerWeight), y1 + (perp1y * outerWeight));
}
else /* arc is inside */
{
verts.push(x1 - (perpx * innerWeight), y1 - (perpy * innerWeight));
verts.push(omx, omy);
indexCount += round(
x1, y1,
x1 - (perpx * innerWeight), y1 - (perpy * innerWeight),
x1 - (perp1x * innerWeight), y1 - (perp1y * innerWeight),
verts, false
) + 4;
verts.push(x1 - (perp1x * innerWeight), y1 - (perp1y * innerWeight));
verts.push(omx, omy);
}
}
else
{
verts.push(imx, imy);
verts.push(omx, omy);
}
}
else // inside miter is NOT ok
{
verts.push(x1 - (perpx * innerWeight), y1 - (perpy * innerWeight)); // first segment's inner vertex
verts.push(x1 + (perpx * outerWeight), y1 + (perpy * outerWeight)); // first segment's outer vertex
if (style.join === LINE_JOIN.ROUND)
{
if (clockwise) /* arc is outside */
{
indexCount += round(
x1, y1,
x1 + (perpx * outerWeight), y1 + (perpy * outerWeight),
x1 + (perp1x * outerWeight), y1 + (perp1y * outerWeight),
verts, true
) + 2;
}
else /* arc is inside */
{
indexCount += round(
x1, y1,
x1 - (perpx * innerWeight), y1 - (perpy * innerWeight),
x1 - (perp1x * innerWeight), y1 - (perp1y * innerWeight),
verts, false
) + 2;
}
}
else if (style.join === LINE_JOIN.MITER && pdist / widthSquared <= miterLimitSquared)
{
if (clockwise)
{
verts.push(omx, omy); // inner miter point
verts.push(omx, omy); // inner miter point
}
else
{
verts.push(imx, imy); // outer miter point
verts.push(imx, imy); // outer miter point
}
indexCount += 2;
}
verts.push(x1 - (perp1x * innerWeight), y1 - (perp1y * innerWeight)); // second segment's inner vertex
verts.push(x1 + (perp1x * outerWeight), y1 + (perp1y * outerWeight)); // second segment's outer vertex
indexCount += 2;
}
}
x0 = points[(length - 2) * 2];
y0 = points[((length - 2) * 2) + 1];
x1 = points[(length - 1) * 2];
y1 = points[((length - 1) * 2) + 1];
perpx = -(y0 - y1);
perpy = x0 - x1;
dist = Math.sqrt((perpx * perpx) + (perpy * perpy));
perpx /= dist;
perpy /= dist;
perpx *= width;
perpy *= width;
verts.push(x1 - (perpx * innerWeight), y1 - (perpy * innerWeight));
verts.push(x1 + (perpx * outerWeight), y1 + (perpy * outerWeight));
if (!closedShape)
{
if (style.cap === LINE_CAP.ROUND)
{
indexCount += round(
x1 - (perpx * (innerWeight - outerWeight) * 0.5),
y1 - (perpy * (innerWeight - outerWeight) * 0.5),
x1 - (perpx * innerWeight),
y1 - (perpy * innerWeight),
x1 + (perpx * outerWeight),
y1 + (perpy * outerWeight),
verts,
false
) + 2;
}
else if (style.cap === LINE_CAP.SQUARE)
{
indexCount += square(x1, y1, perpx, perpy, innerWeight, outerWeight, false, verts);
}
}
const indices = graphicsGeometry.indices;
const eps2 = GRAPHICS_CURVES.epsilon * GRAPHICS_CURVES.epsilon;
// indices.push(indexStart);
for (let i = indexStart; i < indexCount + indexStart - 2; ++i)
{
x0 = verts[(i * 2)];
y0 = verts[(i * 2) + 1];
x1 = verts[(i + 1) * 2];
y1 = verts[((i + 1) * 2) + 1];
x2 = verts[(i + 2) * 2];
y2 = verts[((i + 2) * 2) + 1];
/* Skip zero area triangles */
if (Math.abs((x0 * (y1 - y2)) + (x1 * (y2 - y0)) + (x2 * (y0 - y1))) < eps2)
{
continue;
}
indices.push(i, i + 1, i + 2);
}
}
/**
* Builds a line to draw using the gl.drawArrays(gl.LINES) method
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {PIXI.GraphicsData} graphicsData - The graphics object containing all the necessary properties
* @param {PIXI.GraphicsGeometry} graphicsGeometry - Geometry where to append output
*/
function buildNativeLine(graphicsData: GraphicsData, graphicsGeometry: GraphicsGeometry): void
{
let i = 0;
const shape = graphicsData.shape as Polygon;
const points = graphicsData.points || shape.points;
const closedShape = shape.type !== SHAPES.POLY || shape.closeStroke;
if (points.length === 0) return;
const verts = graphicsGeometry.points;
const indices = graphicsGeometry.indices;
const length = points.length / 2;
const startIndex = verts.length / 2;
let currentIndex = startIndex;
verts.push(points[0], points[1]);
for (i = 1; i < length; i++)
{
verts.push(points[i * 2], points[(i * 2) + 1]);
indices.push(currentIndex, currentIndex + 1);
currentIndex++;
}
if (closedShape)
{
indices.push(currentIndex, startIndex);
}
}
/**
* Builds a line to draw
*
* Ignored from docs since it is not directly exposed.
* @ignore
* @private
* @param {PIXI.GraphicsData} graphicsData - The graphics object containing all the necessary properties
* @param {PIXI.GraphicsGeometry} graphicsGeometry - Geometry where to append output
*/
export function buildLine(graphicsData: GraphicsData, graphicsGeometry: GraphicsGeometry): void
{
if (graphicsData.lineStyle.native)
{
buildNativeLine(graphicsData, graphicsGeometry);
}
else
{
buildNonNativeLine(graphicsData, graphicsGeometry);
}
}