- 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
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645
- 646
- 647
- 648
- 649
- 650
- 651
- 652
- 653
- 654
- 655
- 656
- 657
- 658
- 659
- 660
- 661
- 662
- 663
- 664
- 665
- 666
- 667
- 668
- 669
- 670
- 671
- 672
- 673
- 674
- 675
- 676
- 677
- 678
- 679
- 680
- 681
- 682
- 683
- 684
- 685
- 686
- 687
- 688
- 689
- 690
- 691
- 692
- 693
- 694
- 695
- 696
- 697
- 698
- 699
- 700
- 701
- 702
- 703
- 704
- 705
- 706
- 707
- 708
- 709
- 710
- 711
- 712
- 713
- 714
- 715
- 716
- 717
- 718
- 719
- 720
- 721
- 722
- 723
- 724
- 725
- 726
- 727
- 728
- 729
- 730
- 731
- 732
- 733
- 734
- 735
- 736
- 737
- 738
- 739
- 740
- 741
- 742
- 743
- 744
- 745
- 746
- 747
- 748
- 749
- 750
- 751
- 752
- 753
- 754
- 755
- 756
- 757
- 758
- 759
- 760
- 761
- 762
- 763
- 764
- 765
- 766
- 767
- 768
- 769
- 770
- 771
- 772
- 773
- 774
- 775
- 776
- 777
- 778
- 779
- 780
- 781
- 782
- 783
- 784
- 785
- 786
- 787
- 788
- 789
- 790
- 791
- 792
- 793
- 794
- 795
- 796
- 797
- 798
- 799
- 800
- 801
- 802
- 803
- 804
- 805
- 806
- 807
- 808
- 809
- 810
- 811
- 812
- 813
- 814
- 815
- 816
- 817
- 818
- 819
- 820
- 821
- 822
- 823
- 824
- 825
- 826
- 827
- 828
- 829
- 830
- 831
- 832
- 833
- 834
- 835
- 836
- 837
- 838
- 839
- 840
- 841
- 842
- 843
- 844
- 845
- 846
- 847
- 848
- 849
- 850
- 851
- 852
- 853
- 854
- 855
- 856
- 857
- 858
- 859
- 860
- 861
- 862
- 863
- 864
- 865
- 866
- 867
- 868
- 869
- 870
- 871
- 872
- 873
- 874
- 875
- 876
- 877
- 878
- 879
- 880
- 881
- 882
- 883
- 884
- 885
- 886
- 887
- 888
- 889
- 890
- 891
- 892
- 893
- 894
- 895
- 896
- 897
- 898
- 899
- 900
- 901
- 902
- 903
- 904
- 905
- 906
- 907
- 908
- 909
- 910
- 911
- 912
- 913
- 914
- 915
- 916
- 917
- 918
- 919
- 920
- 921
- 922
- 923
- 924
- 925
- 926
- 927
- 928
- 929
- 930
- 931
- 932
- 933
- 934
- 935
- 936
- 937
- 938
- 939
- 940
- 941
- 942
- 943
- 944
- 945
- 946
- 947
- 948
- 949
- 950
- 951
- 952
- 953
- 954
- 955
- 956
- 957
- 958
- 959
- 960
- 961
- 962
- 963
- 964
- 965
- 966
- 967
- 968
- 969
- 970
- 971
- 972
- 973
- 974
- 975
- 976
- 977
- 978
- 979
- 980
- 981
- 982
- 983
- 984
- 985
- 986
- 987
- 988
- 989
- 990
- 991
- 992
- 993
- 994
- 995
- 996
- 997
- 998
- 999
- 1000
- 1001
- 1002
- 1003
- 1004
- 1005
- 1006
- 1007
- 1008
- 1009
- 1010
- 1011
- 1012
- 1013
- 1014
- 1015
- 1016
- 1017
- 1018
- 1019
- 1020
- 1021
- 1022
- 1023
- 1024
- 1025
- 1026
- 1027
- 1028
- 1029
- 1030
- 1031
- 1032
- 1033
- 1034
- 1035
- 1036
- 1037
- 1038
- 1039
- 1040
- 1041
- 1042
- 1043
- 1044
- 1045
- 1046
- 1047
- 1048
- 1049
- 1050
- 1051
- 1052
- 1053
- 1054
- 1055
- 1056
- 1057
- 1058
- 1059
- 1060
- 1061
- 1062
- 1063
- 1064
- 1065
- 1066
- 1067
- 1068
- 1069
- 1070
- 1071
- 1072
- 1073
- 1074
- 1075
- 1076
- 1077
- 1078
- 1079
- 1080
- 1081
- 1082
- 1083
- 1084
- 1085
- 1086
- 1087
- 1088
- 1089
- 1090
- 1091
- 1092
- 1093
- 1094
- 1095
- 1096
- 1097
- 1098
- 1099
- 1100
- 1101
- 1102
- 1103
- 1104
- 1105
- 1106
- 1107
- 1108
- 1109
- 1110
- 1111
- 1112
- 1113
- 1114
- 1115
- 1116
- 1117
- 1118
- 1119
- 1120
- 1121
- 1122
- 1123
- 1124
- 1125
- 1126
- 1127
- 1128
- 1129
- 1130
- 1131
- 1132
- 1133
- 1134
- 1135
- 1136
- 1137
- 1138
- 1139
- 1140
- 1141
- 1142
- 1143
- 1144
- 1145
- 1146
- 1147
- 1148
- 1149
- 1150
- 1151
- 1152
- 1153
- 1154
- 1155
- 1156
- 1157
- 1158
- 1159
- 1160
- 1161
- 1162
- 1163
- 1164
- 1165
- 1166
- 1167
- 1168
- 1169
- 1170
- 1171
- 1172
- 1173
- 1174
- 1175
- 1176
- 1177
- 1178
- 1179
- 1180
- 1181
- 1182
- 1183
- 1184
- 1185
- 1186
- 1187
- 1188
- 1189
- 1190
- 1191
- 1192
- 1193
- 1194
- 1195
- 1196
- 1197
- 1198
- 1199
- 1200
- 1201
- 1202
- 1203
- 1204
- 1205
- 1206
- 1207
- 1208
- 1209
- 1210
- 1211
- 1212
- 1213
import {
Circle,
Ellipse,
PI_2,
Point,
Polygon,
Rectangle,
RoundedRectangle,
Matrix,
SHAPES,
} from '@pixi/math';
import type { Renderer, BatchDrawCall } from '@pixi/core';
import { Texture, UniformGroup, State, Shader } from '@pixi/core';
import { BezierUtils, QuadraticUtils, ArcUtils } from './utils';
import { hex2rgb } from '@pixi/utils';
import { GraphicsGeometry } from './GraphicsGeometry';
import { FillStyle } from './styles/FillStyle';
import { LineStyle } from './styles/LineStyle';
import { BLEND_MODES } from '@pixi/constants';
import { Container } from '@pixi/display';
import type { IShape, IPointData } from '@pixi/math';
import type { IDestroyOptions } from '@pixi/display';
import { LINE_JOIN, LINE_CAP } from './const';
/** Batch element computed from Graphics geometry */
export interface IGraphicsBatchElement
{
vertexData: Float32Array;
blendMode: BLEND_MODES;
indices: Uint16Array | Uint32Array;
uvs: Float32Array;
alpha: number;
worldAlpha: number;
_batchRGB: number[];
_tintRGB: number;
_texture: Texture;
}
export interface IFillStyleOptions
{
color?: number;
alpha?: number;
texture?: Texture;
matrix?: Matrix;
}
export interface ILineStyleOptions extends IFillStyleOptions
{
width?: number;
alignment?: number;
native?: boolean;
cap?: LINE_CAP;
join?: LINE_JOIN;
miterLimit?: number;
}
const temp = new Float32Array(3);
// a default shaders map used by graphics..
const DEFAULT_SHADERS: {[key: string]: Shader} = {};
export interface Graphics extends GlobalMixins.Graphics, Container {}
/**
* The Graphics class is primarily used to render primitive shapes such as lines, circles and
* rectangles to the display, and to color and fill them. However, you can also use a Graphics
* object to build a list of primitives to use as a mask, or as a complex hitArea.
*
* Please note that due to legacy naming conventions, the behavior of some functions in this class
* can be confusing. Each call to `drawRect()`, `drawPolygon()`, etc. actually stores that primitive
* in the Geometry class's GraphicsGeometry object for later use in rendering or hit testing - the
* functions do not directly draw anything to the screen. Similarly, the `clear()` function doesn't
* change the screen, it simply resets the list of primitives, which can be useful if you want to
* rebuild the contents of an existing Graphics object.
*
* Once a GraphicsGeometry list is built, you can re-use it in other Geometry objects as
* an optimization, by passing it into a new Geometry object's constructor. Because of this
* ability, it's important to call `destroy()` on Geometry objects once you are done with them, to
* properly dereference each GraphicsGeometry and prevent memory leaks.
* @memberof PIXI
*/
export class Graphics extends Container
{
/**
* New rendering behavior for rounded rectangles: circular arcs instead of quadratic bezier curves.
* In the next major release, we'll enable this by default.
*/
public static nextRoundedRectBehavior = false;
/**
* Temporary point to use for containsPoint.
* @private
*/
static _TEMP_POINT = new Point();
/**
* Represents the vertex and fragment shaders that processes the geometry and runs on the GPU.
* Can be shared between multiple Graphics objects.
*/
public shader: Shader = null;
/** Renderer plugin for batching */
public pluginName = 'batch';
/**
* Current path
* @readonly
*/
public currentPath: Polygon = null;
/** A collections of batches! These can be drawn by the renderer batch system. */
protected batches: Array<IGraphicsBatchElement> = [];
/** Update dirty for limiting calculating tints for batches. */
protected batchTint = -1;
/** Update dirty for limiting calculating batches.*/
protected batchDirty = -1;
/** Copy of the object vertex data. */
protected vertexData: Float32Array = null;
/** Current fill style. */
protected _fillStyle: FillStyle = new FillStyle();
/** Current line style. */
protected _lineStyle: LineStyle = new LineStyle();
/** Current shape transform matrix. */
protected _matrix: Matrix = null;
/** Current hole mode is enabled. */
protected _holeMode = false;
protected _transformID: number;
protected _tint: number;
/**
* Represents the WebGL state the Graphics required to render, excludes shader and geometry. E.g.,
* blend mode, culling, depth testing, direction of rendering triangles, backface, etc.
*/
private state: State = State.for2d();
private _geometry: GraphicsGeometry;
/**
* Includes vertex positions, face indices, normals, colors, UVs, and
* custom attributes within buffers, reducing the cost of passing all
* this data to the GPU. Can be shared between multiple Mesh or Graphics objects.
* @readonly
*/
public get geometry(): GraphicsGeometry
{
return this._geometry;
}
/**
* @param geometry - Geometry to use, if omitted will create a new GraphicsGeometry instance.
*/
constructor(geometry: GraphicsGeometry = null)
{
super();
this._geometry = geometry || new GraphicsGeometry();
this._geometry.refCount++;
/**
* When cacheAsBitmap is set to true the graphics object will be rendered as if it was a sprite.
* This is useful if your graphics element does not change often, as it will speed up the rendering
* of the object in exchange for taking up texture memory. It is also useful if you need the graphics
* object to be anti-aliased, because it will be rendered using canvas. This is not recommended if
* you are constantly redrawing the graphics element.
* @name cacheAsBitmap
* @member {boolean}
* @memberof PIXI.Graphics#
* @default false
*/
this._transformID = -1;
// Set default
this.tint = 0xFFFFFF;
this.blendMode = BLEND_MODES.NORMAL;
}
/**
* Creates a new Graphics object with the same values as this one.
* Note that only the geometry of the object is cloned, not its transform (position,scale,etc)
* @returns - A clone of the graphics object
*/
public clone(): Graphics
{
this.finishPoly();
return new Graphics(this._geometry);
}
/**
* The blend mode to be applied to the graphic shape. Apply a value of
* `PIXI.BLEND_MODES.NORMAL` to reset the blend mode. Note that, since each
* primitive in the GraphicsGeometry list is rendered sequentially, modes
* such as `PIXI.BLEND_MODES.ADD` and `PIXI.BLEND_MODES.MULTIPLY` will
* be applied per-primitive.
* @default PIXI.BLEND_MODES.NORMAL
*/
public set blendMode(value: BLEND_MODES)
{
this.state.blendMode = value;
}
public get blendMode(): BLEND_MODES
{
return this.state.blendMode;
}
/**
* The tint applied to each graphic shape. This is a hex value. A value of
* 0xFFFFFF will remove any tint effect.
* @default 0xFFFFFF
*/
public get tint(): number
{
return this._tint;
}
public set tint(value: number)
{
this._tint = value;
}
/**
* The current fill style.
* @readonly
*/
public get fill(): FillStyle
{
return this._fillStyle;
}
/**
* The current line style.
* @readonly
*/
public get line(): LineStyle
{
return this._lineStyle;
}
/**
* Specifies the line style used for subsequent calls to Graphics methods such as the lineTo()
* method or the drawCircle() method.
* @param [width=0] - width of the line to draw, will update the objects stored style
* @param [color=0x0] - color of the line to draw, will update the objects stored style
* @param [alpha=1] - alpha of the line to draw, will update the objects stored style
* @param [alignment=0.5] - alignment of the line to draw, (0 = inner, 0.5 = middle, 1 = outer).
* WebGL only.
* @param [native=false] - If true the lines will be draw using LINES instead of TRIANGLE_STRIP
* @returns - This Graphics object. Good for chaining method calls
*/
public lineStyle(width: number, color?: number, alpha?: number, alignment?: number, native?: boolean): this;
/**
* Specifies the line style used for subsequent calls to Graphics methods such as the lineTo()
* method or the drawCircle() method.
* @param options - Line style options
* @param {number} [options.width=0] - width of the line to draw, will update the objects stored style
* @param {number} [options.color=0x0] - color of the line to draw, will update the objects stored style
* @param {number} [options.alpha=1] - alpha of the line to draw, will update the objects stored style
* @param {number} [options.alignment=0.5] - alignment of the line to draw, (0 = inner, 0.5 = middle, 1 = outer).
* WebGL only.
* @param {boolean} [options.native=false] - If true the lines will be draw using LINES instead of TRIANGLE_STRIP
* @param {PIXI.LINE_CAP}[options.cap=PIXI.LINE_CAP.BUTT] - line cap style
* @param {PIXI.LINE_JOIN}[options.join=PIXI.LINE_JOIN.MITER] - line join style
* @param {number}[options.miterLimit=10] - miter limit ratio
* @returns {PIXI.Graphics} This Graphics object. Good for chaining method calls
*/
public lineStyle(options?: ILineStyleOptions): this;
public lineStyle(options: ILineStyleOptions | number = null,
color = 0x0, alpha = 1, alignment = 0.5, native = false): this
{
// Support non-object params: (width, color, alpha, alignment, native)
if (typeof options === 'number')
{
options = { width: options, color, alpha, alignment, native } as ILineStyleOptions;
}
return this.lineTextureStyle(options);
}
/**
* Like line style but support texture for line fill.
* @param [options] - Collection of options for setting line style.
* @param {number} [options.width=0] - width of the line to draw, will update the objects stored style
* @param {PIXI.Texture} [options.texture=PIXI.Texture.WHITE] - Texture to use
* @param {number} [options.color=0x0] - color of the line to draw, will update the objects stored style.
* Default 0xFFFFFF if texture present.
* @param {number} [options.alpha=1] - alpha of the line to draw, will update the objects stored style
* @param {PIXI.Matrix} [options.matrix=null] - Texture matrix to transform texture
* @param {number} [options.alignment=0.5] - alignment of the line to draw, (0 = inner, 0.5 = middle, 1 = outer).
* WebGL only.
* @param {boolean} [options.native=false] - If true the lines will be draw using LINES instead of TRIANGLE_STRIP
* @param {PIXI.LINE_CAP}[options.cap=PIXI.LINE_CAP.BUTT] - line cap style
* @param {PIXI.LINE_JOIN}[options.join=PIXI.LINE_JOIN.MITER] - line join style
* @param {number}[options.miterLimit=10] - miter limit ratio
* @returns {PIXI.Graphics} This Graphics object. Good for chaining method calls
*/
public lineTextureStyle(options?: ILineStyleOptions): this
{
// Apply defaults
options = Object.assign({
width: 0,
texture: Texture.WHITE,
color: (options && options.texture) ? 0xFFFFFF : 0x0,
alpha: 1,
matrix: null,
alignment: 0.5,
native: false,
cap: LINE_CAP.BUTT,
join: LINE_JOIN.MITER,
miterLimit: 10,
}, options);
if (this.currentPath)
{
this.startPoly();
}
const visible = options.width > 0 && options.alpha > 0;
if (!visible)
{
this._lineStyle.reset();
}
else
{
if (options.matrix)
{
options.matrix = options.matrix.clone();
options.matrix.invert();
}
Object.assign(this._lineStyle, { visible }, options);
}
return this;
}
/**
* Start a polygon object internally.
* @protected
*/
protected startPoly(): void
{
if (this.currentPath)
{
const points = this.currentPath.points;
const len = this.currentPath.points.length;
if (len > 2)
{
this.drawShape(this.currentPath);
this.currentPath = new Polygon();
this.currentPath.closeStroke = false;
this.currentPath.points.push(points[len - 2], points[len - 1]);
}
}
else
{
this.currentPath = new Polygon();
this.currentPath.closeStroke = false;
}
}
/**
* Finish the polygon object.
* @protected
*/
finishPoly(): void
{
if (this.currentPath)
{
if (this.currentPath.points.length > 2)
{
this.drawShape(this.currentPath);
this.currentPath = null;
}
else
{
this.currentPath.points.length = 0;
}
}
}
/**
* Moves the current drawing position to x, y.
* @param x - the X coordinate to move to
* @param y - the Y coordinate to move to
* @returns - This Graphics object. Good for chaining method calls
*/
public moveTo(x: number, y: number): this
{
this.startPoly();
this.currentPath.points[0] = x;
this.currentPath.points[1] = y;
return this;
}
/**
* Draws a line using the current line style from the current drawing position to (x, y);
* The current drawing position is then set to (x, y).
* @param x - the X coordinate to draw to
* @param y - the Y coordinate to draw to
* @returns - This Graphics object. Good for chaining method calls
*/
public lineTo(x: number, y: number): this
{
if (!this.currentPath)
{
this.moveTo(0, 0);
}
// remove duplicates..
const points = this.currentPath.points;
const fromX = points[points.length - 2];
const fromY = points[points.length - 1];
if (fromX !== x || fromY !== y)
{
points.push(x, y);
}
return this;
}
/**
* Initialize the curve
* @param x
* @param y
*/
protected _initCurve(x = 0, y = 0): void
{
if (this.currentPath)
{
if (this.currentPath.points.length === 0)
{
this.currentPath.points = [x, y];
}
}
else
{
this.moveTo(x, y);
}
}
/**
* Calculate the points for a quadratic bezier curve and then draws it.
* Based on: https://stackoverflow.com/questions/785097/how-do-i-implement-a-bezier-curve-in-c
* @param cpX - Control point x
* @param cpY - Control point y
* @param toX - Destination point x
* @param toY - Destination point y
* @returns - This Graphics object. Good for chaining method calls
*/
public quadraticCurveTo(cpX: number, cpY: number, toX: number, toY: number): this
{
this._initCurve();
const points = this.currentPath.points;
if (points.length === 0)
{
this.moveTo(0, 0);
}
QuadraticUtils.curveTo(cpX, cpY, toX, toY, points);
return this;
}
/**
* Calculate the points for a bezier curve and then draws it.
* @param cpX - Control point x
* @param cpY - Control point y
* @param cpX2 - Second Control point x
* @param cpY2 - Second Control point y
* @param toX - Destination point x
* @param toY - Destination point y
* @returns This Graphics object. Good for chaining method calls
*/
public bezierCurveTo(cpX: number, cpY: number, cpX2: number, cpY2: number, toX: number, toY: number): this
{
this._initCurve();
BezierUtils.curveTo(cpX, cpY, cpX2, cpY2, toX, toY, this.currentPath.points);
return this;
}
/**
* The arcTo() method creates an arc/curve between two tangents on the canvas.
*
* "borrowed" from https://code.google.com/p/fxcanvas/ - thanks google!
* @param x1 - The x-coordinate of the first tangent point of the arc
* @param y1 - The y-coordinate of the first tangent point of the arc
* @param x2 - The x-coordinate of the end of the arc
* @param y2 - The y-coordinate of the end of the arc
* @param radius - The radius of the arc
* @returns - This Graphics object. Good for chaining method calls
*/
public arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this
{
this._initCurve(x1, y1);
const points = this.currentPath.points;
const result = ArcUtils.curveTo(x1, y1, x2, y2, radius, points);
if (result)
{
const { cx, cy, radius, startAngle, endAngle, anticlockwise } = result;
this.arc(cx, cy, radius, startAngle, endAngle, anticlockwise);
}
return this;
}
/**
* The arc method creates an arc/curve (used to create circles, or parts of circles).
* @param cx - The x-coordinate of the center of the circle
* @param cy - The y-coordinate of the center of the circle
* @param radius - The radius of the circle
* @param startAngle - The starting angle, in radians (0 is at the 3 o'clock position
* of the arc's circle)
* @param endAngle - The ending angle, in radians
* @param anticlockwise - Specifies whether the drawing should be
* counter-clockwise or clockwise. False is default, and indicates clockwise, while true
* indicates counter-clockwise.
* @returns - This Graphics object. Good for chaining method calls
*/
public arc(cx: number, cy: number, radius: number, startAngle: number, endAngle: number, anticlockwise = false): this
{
if (startAngle === endAngle)
{
return this;
}
if (!anticlockwise && endAngle <= startAngle)
{
endAngle += PI_2;
}
else if (anticlockwise && startAngle <= endAngle)
{
startAngle += PI_2;
}
const sweep = endAngle - startAngle;
if (sweep === 0)
{
return this;
}
const startX = cx + (Math.cos(startAngle) * radius);
const startY = cy + (Math.sin(startAngle) * radius);
const eps = this._geometry.closePointEps;
// If the currentPath exists, take its points. Otherwise call `moveTo` to start a path.
let points = this.currentPath ? this.currentPath.points : null;
if (points)
{
// TODO: make a better fix.
// We check how far our start is from the last existing point
const xDiff = Math.abs(points[points.length - 2] - startX);
const yDiff = Math.abs(points[points.length - 1] - startY);
if (xDiff < eps && yDiff < eps)
{
// If the point is very close, we don't add it, since this would lead to artifacts
// during tessellation due to floating point imprecision.
}
else
{
points.push(startX, startY);
}
}
else
{
this.moveTo(startX, startY);
points = this.currentPath.points;
}
ArcUtils.arc(startX, startY, cx, cy, radius, startAngle, endAngle, anticlockwise, points);
return this;
}
/**
* Specifies a simple one-color fill that subsequent calls to other Graphics methods
* (such as lineTo() or drawCircle()) use when drawing.
* @param color - the color of the fill
* @param alpha - the alpha of the fill
* @returns - This Graphics object. Good for chaining method calls
*/
public beginFill(color = 0, alpha = 1): this
{
return this.beginTextureFill({ texture: Texture.WHITE, color, alpha });
}
/**
* Begin the texture fill
* @param options - Object object.
* @param {PIXI.Texture} [options.texture=PIXI.Texture.WHITE] - Texture to fill
* @param {number} [options.color=0xffffff] - Background to fill behind texture
* @param {number} [options.alpha=1] - Alpha of fill
* @param {PIXI.Matrix} [options.matrix=null] - Transform matrix
* @returns {PIXI.Graphics} This Graphics object. Good for chaining method calls
*/
beginTextureFill(options?: IFillStyleOptions): this
{
// Apply defaults
options = Object.assign({
texture: Texture.WHITE,
color: 0xFFFFFF,
alpha: 1,
matrix: null,
}, options) as IFillStyleOptions;
if (this.currentPath)
{
this.startPoly();
}
const visible = options.alpha > 0;
if (!visible)
{
this._fillStyle.reset();
}
else
{
if (options.matrix)
{
options.matrix = options.matrix.clone();
options.matrix.invert();
}
Object.assign(this._fillStyle, { visible }, options);
}
return this;
}
/**
* Applies a fill to the lines and shapes that were added since the last call to the beginFill() method.
* @returns - This Graphics object. Good for chaining method calls
*/
public endFill(): this
{
this.finishPoly();
this._fillStyle.reset();
return this;
}
/**
* Draws a rectangle shape.
* @param x - The X coord of the top-left of the rectangle
* @param y - The Y coord of the top-left of the rectangle
* @param width - The width of the rectangle
* @param height - The height of the rectangle
* @returns - This Graphics object. Good for chaining method calls
*/
public drawRect(x: number, y: number, width: number, height: number): this
{
return this.drawShape(new Rectangle(x, y, width, height));
}
/**
* Draw a rectangle shape with rounded/beveled corners.
* @param x - The X coord of the top-left of the rectangle
* @param y - The Y coord of the top-left of the rectangle
* @param width - The width of the rectangle
* @param height - The height of the rectangle
* @param radius - Radius of the rectangle corners
* @returns - This Graphics object. Good for chaining method calls
*/
public drawRoundedRect(x: number, y: number, width: number, height: number, radius: number): this
{
return this.drawShape(new RoundedRectangle(x, y, width, height, radius));
}
/**
* Draws a circle.
* @param x - The X coordinate of the center of the circle
* @param y - The Y coordinate of the center of the circle
* @param radius - The radius of the circle
* @returns - This Graphics object. Good for chaining method calls
*/
public drawCircle(x: number, y: number, radius: number): this
{
return this.drawShape(new Circle(x, y, radius));
}
/**
* Draws an ellipse.
* @param x - The X coordinate of the center of the ellipse
* @param y - The Y coordinate of the center of the ellipse
* @param width - The half width of the ellipse
* @param height - The half height of the ellipse
* @returns - This Graphics object. Good for chaining method calls
*/
public drawEllipse(x: number, y: number, width: number, height: number): this
{
return this.drawShape(new Ellipse(x, y, width, height));
}
public drawPolygon(...path: Array<number> | Array<IPointData>): this;
public drawPolygon(path: Array<number> | Array<IPointData> | Polygon): this;
/**
* Draws a polygon using the given path.
* @param {number[]|PIXI.IPointData[]|PIXI.Polygon} path - The path data used to construct the polygon.
* @returns - This Graphics object. Good for chaining method calls
*/
public drawPolygon(...path: any[]): this
{
let points: Array<number> | Array<IPointData>;
let closeStroke = true;// !!this._fillStyle;
const poly = path[0] as Polygon;
// check if data has points..
if (poly.points)
{
closeStroke = poly.closeStroke;
points = poly.points;
}
else
if (Array.isArray(path[0]))
{
points = path[0];
}
else
{
points = path;
}
const shape = new Polygon(points);
shape.closeStroke = closeStroke;
this.drawShape(shape);
return this;
}
/**
* Draw any shape.
* @param {PIXI.Circle|PIXI.Ellipse|PIXI.Polygon|PIXI.Rectangle|PIXI.RoundedRectangle} shape - Shape to draw
* @returns - This Graphics object. Good for chaining method calls
*/
public drawShape(shape: IShape): this
{
if (!this._holeMode)
{
this._geometry.drawShape(
shape,
this._fillStyle.clone(),
this._lineStyle.clone(),
this._matrix
);
}
else
{
this._geometry.drawHole(shape, this._matrix);
}
return this;
}
/**
* Clears the graphics that were drawn to this Graphics object, and resets fill and line style settings.
* @returns - This Graphics object. Good for chaining method calls
*/
public clear(): this
{
this._geometry.clear();
this._lineStyle.reset();
this._fillStyle.reset();
this._boundsID++;
this._matrix = null;
this._holeMode = false;
this.currentPath = null;
return this;
}
/**
* True if graphics consists of one rectangle, and thus, can be drawn like a Sprite and
* masked with gl.scissor.
* @returns - True if only 1 rect.
*/
public isFastRect(): boolean
{
const data = this._geometry.graphicsData;
return data.length === 1
&& data[0].shape.type === SHAPES.RECT
&& !data[0].matrix
&& !data[0].holes.length
&& !(data[0].lineStyle.visible && data[0].lineStyle.width);
}
/**
* Renders the object using the WebGL renderer
* @param renderer - The renderer
*/
protected _render(renderer: Renderer): void
{
this.finishPoly();
const geometry = this._geometry;
// batch part..
// batch it!
geometry.updateBatches();
if (geometry.batchable)
{
if (this.batchDirty !== geometry.batchDirty)
{
this._populateBatches();
}
this._renderBatched(renderer);
}
else
{
// no batching...
renderer.batch.flush();
this._renderDirect(renderer);
}
}
/** Populating batches for rendering. */
protected _populateBatches(): void
{
const geometry = this._geometry;
const blendMode = this.blendMode;
const len = geometry.batches.length;
this.batchTint = -1;
this._transformID = -1;
this.batchDirty = geometry.batchDirty;
this.batches.length = len;
this.vertexData = new Float32Array(geometry.points);
for (let i = 0; i < len; i++)
{
const gI = geometry.batches[i];
const color = gI.style.color;
const vertexData = new Float32Array(this.vertexData.buffer,
gI.attribStart * 4 * 2,
gI.attribSize * 2);
const uvs = new Float32Array(geometry.uvsFloat32.buffer,
gI.attribStart * 4 * 2,
gI.attribSize * 2);
const indices = new Uint16Array(geometry.indicesUint16.buffer,
gI.start * 2,
gI.size);
const batch = {
vertexData,
blendMode,
indices,
uvs,
_batchRGB: hex2rgb(color) as Array<number>,
_tintRGB: color,
_texture: gI.style.texture,
alpha: gI.style.alpha,
worldAlpha: 1 };
this.batches[i] = batch;
}
}
/**
* Renders the batches using the BathedRenderer plugin
* @param renderer - The renderer
*/
protected _renderBatched(renderer: Renderer): void
{
if (!this.batches.length)
{
return;
}
renderer.batch.setObjectRenderer(renderer.plugins[this.pluginName]);
this.calculateVertices();
this.calculateTints();
for (let i = 0, l = this.batches.length; i < l; i++)
{
const batch = this.batches[i];
batch.worldAlpha = this.worldAlpha * batch.alpha;
renderer.plugins[this.pluginName].render(batch);
}
}
/**
* Renders the graphics direct
* @param renderer - The renderer
*/
protected _renderDirect(renderer: Renderer): void
{
const shader = this._resolveDirectShader(renderer);
const geometry = this._geometry;
const tint = this.tint;
const worldAlpha = this.worldAlpha;
const uniforms = shader.uniforms;
const drawCalls = geometry.drawCalls;
// lets set the transfomr
uniforms.translationMatrix = this.transform.worldTransform;
// and then lets set the tint..
uniforms.tint[0] = (((tint >> 16) & 0xFF) / 255) * worldAlpha;
uniforms.tint[1] = (((tint >> 8) & 0xFF) / 255) * worldAlpha;
uniforms.tint[2] = ((tint & 0xFF) / 255) * worldAlpha;
uniforms.tint[3] = worldAlpha;
// the first draw call, we can set the uniforms of the shader directly here.
// this means that we can tack advantage of the sync function of pixi!
// bind and sync uniforms..
// there is a way to optimise this..
renderer.shader.bind(shader);
renderer.geometry.bind(geometry, shader);
// set state..
renderer.state.set(this.state);
// then render the rest of them...
for (let i = 0, l = drawCalls.length; i < l; i++)
{
this._renderDrawCallDirect(renderer, geometry.drawCalls[i]);
}
}
/**
* Renders specific DrawCall
* @param renderer
* @param drawCall
*/
protected _renderDrawCallDirect(renderer: Renderer, drawCall: BatchDrawCall): void
{
const { texArray, type, size, start } = drawCall;
const groupTextureCount = texArray.count;
for (let j = 0; j < groupTextureCount; j++)
{
renderer.texture.bind(texArray.elements[j], j);
}
renderer.geometry.draw(type, size, start);
}
/**
* Resolves shader for direct rendering
* @param renderer - The renderer
*/
protected _resolveDirectShader(renderer: Renderer): Shader
{
let shader = this.shader;
const pluginName = this.pluginName;
if (!shader)
{
// if there is no shader here, we can use the default shader.
// and that only gets created if we actually need it..
// but may be more than one plugins for graphics
if (!DEFAULT_SHADERS[pluginName])
{
const { MAX_TEXTURES } = renderer.plugins[pluginName];
const sampleValues = new Int32Array(MAX_TEXTURES);
for (let i = 0; i < MAX_TEXTURES; i++)
{
sampleValues[i] = i;
}
const uniforms = {
tint: new Float32Array([1, 1, 1, 1]),
translationMatrix: new Matrix(),
default: UniformGroup.from({ uSamplers: sampleValues }, true),
};
const program = renderer.plugins[pluginName]._shader.program;
DEFAULT_SHADERS[pluginName] = new Shader(program, uniforms);
}
shader = DEFAULT_SHADERS[pluginName];
}
return shader;
}
/** Retrieves the bounds of the graphic shape as a rectangle object. */
protected _calculateBounds(): void
{
this.finishPoly();
const geometry = this._geometry;
// skipping when graphics is empty, like a container
if (!geometry.graphicsData.length)
{
return;
}
const { minX, minY, maxX, maxY } = geometry.bounds;
this._bounds.addFrame(this.transform, minX, minY, maxX, maxY);
}
/**
* Tests if a point is inside this graphics object
* @param point - the point to test
* @returns - the result of the test
*/
public containsPoint(point: IPointData): boolean
{
this.worldTransform.applyInverse(point, Graphics._TEMP_POINT);
return this._geometry.containsPoint(Graphics._TEMP_POINT);
}
/** Recalculate the tint by applying tint to batches using Graphics tint. */
protected calculateTints(): void
{
if (this.batchTint !== this.tint)
{
this.batchTint = this.tint;
const tintRGB = hex2rgb(this.tint, temp);
for (let i = 0; i < this.batches.length; i++)
{
const batch = this.batches[i];
const batchTint = batch._batchRGB;
const r = (tintRGB[0] * batchTint[0]) * 255;
const g = (tintRGB[1] * batchTint[1]) * 255;
const b = (tintRGB[2] * batchTint[2]) * 255;
// TODO Ivan, can this be done in one go?
const color = (r << 16) + (g << 8) + (b | 0);
batch._tintRGB = (color >> 16)
+ (color & 0xff00)
+ ((color & 0xff) << 16);
}
}
}
/** If there's a transform update or a change to the shape of the geometry, recalculate the vertices. */
protected calculateVertices(): void
{
const wtID = this.transform._worldID;
if (this._transformID === wtID)
{
return;
}
this._transformID = wtID;
const wt = this.transform.worldTransform;
const a = wt.a;
const b = wt.b;
const c = wt.c;
const d = wt.d;
const tx = wt.tx;
const ty = wt.ty;
const data = this._geometry.points;// batch.vertexDataOriginal;
const vertexData = this.vertexData;
let count = 0;
for (let i = 0; i < data.length; i += 2)
{
const x = data[i];
const y = data[i + 1];
vertexData[count++] = (a * x) + (c * y) + tx;
vertexData[count++] = (d * y) + (b * x) + ty;
}
}
/**
* Closes the current path.
* @returns - Returns itself.
*/
public closePath(): this
{
const currentPath = this.currentPath;
if (currentPath)
{
// we don't need to add extra point in the end because buildLine will take care of that
currentPath.closeStroke = true;
// ensure that the polygon is completed, and is available for hit detection
// (even if the graphics is not rendered yet)
this.finishPoly();
}
return this;
}
/**
* Apply a matrix to the positional data.
* @param matrix - Matrix to use for transform current shape.
* @returns - Returns itself.
*/
public setMatrix(matrix: Matrix): this
{
this._matrix = matrix;
return this;
}
/**
* Begin adding holes to the last draw shape
* IMPORTANT: holes must be fully inside a shape to work
* Also weirdness ensues if holes overlap!
* Ellipses, Circles, Rectangles and Rounded Rectangles cannot be holes or host for holes in CanvasRenderer,
* please use `moveTo` `lineTo`, `quadraticCurveTo` if you rely on pixi-legacy bundle.
* @returns - Returns itself.
*/
public beginHole(): this
{
this.finishPoly();
this._holeMode = true;
return this;
}
/**
* End adding holes to the last draw shape.
* @returns - Returns itself.
*/
public endHole(): this
{
this.finishPoly();
this._holeMode = false;
return this;
}
/**
* Destroys the Graphics object.
* @param options - Options parameter. A boolean will act as if all
* options have been set to that value
* @param {boolean} [options.children=false] - if set to true, all the children will have
* their destroy method called as well. 'options' will be passed on to those calls.
* @param {boolean} [options.texture=false] - Only used for child Sprites if options.children is set to true
* Should it destroy the texture of the child sprite
* @param {boolean} [options.baseTexture=false] - Only used for child Sprites if options.children is set to true
* Should it destroy the base texture of the child sprite
*/
public destroy(options?: IDestroyOptions | boolean): void
{
this._geometry.refCount--;
if (this._geometry.refCount === 0)
{
this._geometry.dispose();
}
this._matrix = null;
this.currentPath = null;
this._lineStyle.destroy();
this._lineStyle = null;
this._fillStyle.destroy();
this._fillStyle = null;
this._geometry = null;
this.shader = null;
this.vertexData = null;
this.batches.length = 0;
this.batches = null;
super.destroy(options);
}
}