@vpmedia/phaser 1.119.0 → 1.121.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +499 -607
  4. package/dist/index.js.map +1 -1
  5. package/dist/phaser/core/animation.d.ts.map +1 -1
  6. package/dist/phaser/core/animation_manager.d.ts.map +1 -1
  7. package/dist/phaser/core/animation_parser.d.ts +1 -1
  8. package/dist/phaser/core/array_set.d.ts.map +1 -1
  9. package/dist/phaser/core/callback.d.ts +12 -0
  10. package/dist/phaser/core/callback.d.ts.map +1 -0
  11. package/dist/phaser/core/device.d.ts +1 -5
  12. package/dist/phaser/core/device.d.ts.map +1 -1
  13. package/dist/phaser/core/dom.d.ts.map +1 -1
  14. package/dist/phaser/core/factory.d.ts +2 -1
  15. package/dist/phaser/core/factory.d.ts.map +1 -1
  16. package/dist/phaser/core/game.d.ts +3 -0
  17. package/dist/phaser/core/game.d.ts.map +1 -1
  18. package/dist/phaser/core/input.d.ts +11 -9
  19. package/dist/phaser/core/input.d.ts.map +1 -1
  20. package/dist/phaser/core/input_event.d.ts +1 -1
  21. package/dist/phaser/core/input_event.d.ts.map +1 -1
  22. package/dist/phaser/core/input_handler.d.ts.map +1 -1
  23. package/dist/phaser/core/input_mouse.d.ts.map +1 -1
  24. package/dist/phaser/core/input_mspointer.d.ts.map +1 -1
  25. package/dist/phaser/core/input_pointer.d.ts +3 -2
  26. package/dist/phaser/core/input_pointer.d.ts.map +1 -1
  27. package/dist/phaser/core/input_touch.d.ts.map +1 -1
  28. package/dist/phaser/core/loader.d.ts +5 -4
  29. package/dist/phaser/core/loader.d.ts.map +1 -1
  30. package/dist/phaser/core/scale_manager.d.ts +5 -4
  31. package/dist/phaser/core/scale_manager.d.ts.map +1 -1
  32. package/dist/phaser/core/scene_manager.d.ts.map +1 -1
  33. package/dist/phaser/core/signal.d.ts +14 -7
  34. package/dist/phaser/core/signal.d.ts.map +1 -1
  35. package/dist/phaser/core/signal_binding.d.ts +10 -10
  36. package/dist/phaser/core/signal_binding.d.ts.map +1 -1
  37. package/dist/phaser/core/sound.d.ts +3 -3
  38. package/dist/phaser/core/sound.d.ts.map +1 -1
  39. package/dist/phaser/core/sound_manager.d.ts +9 -3
  40. package/dist/phaser/core/sound_manager.d.ts.map +1 -1
  41. package/dist/phaser/core/sound_sprite.d.ts +19 -7
  42. package/dist/phaser/core/sound_sprite.d.ts.map +1 -1
  43. package/dist/phaser/core/timer.d.ts +5 -4
  44. package/dist/phaser/core/timer.d.ts.map +1 -1
  45. package/dist/phaser/core/timer_event.d.ts +3 -2
  46. package/dist/phaser/core/timer_event.d.ts.map +1 -1
  47. package/dist/phaser/core/tween.d.ts +7 -6
  48. package/dist/phaser/core/tween.d.ts.map +1 -1
  49. package/dist/phaser/core/tween_data.d.ts +2 -1
  50. package/dist/phaser/core/tween_data.d.ts.map +1 -1
  51. package/dist/phaser/core/tween_easing.d.ts +31 -31
  52. package/dist/phaser/core/tween_manager.d.ts.map +1 -1
  53. package/dist/phaser/display/bitmap_text.d.ts.map +1 -1
  54. package/dist/phaser/display/button.d.ts +2 -1
  55. package/dist/phaser/display/button.d.ts.map +1 -1
  56. package/dist/phaser/display/canvas/pool.d.ts.map +1 -1
  57. package/dist/phaser/display/canvas/renderer.d.ts +1 -1
  58. package/dist/phaser/display/canvas/renderer.d.ts.map +1 -1
  59. package/dist/phaser/display/canvas/tinter.d.ts.map +1 -1
  60. package/dist/phaser/display/canvas/util.d.ts +2 -2
  61. package/dist/phaser/display/canvas/util.d.ts.map +1 -1
  62. package/dist/phaser/display/display_object.d.ts +8 -6
  63. package/dist/phaser/display/display_object.d.ts.map +1 -1
  64. package/dist/phaser/display/graphics.d.ts +7 -3
  65. package/dist/phaser/display/graphics.d.ts.map +1 -1
  66. package/dist/phaser/display/group.d.ts +1 -1
  67. package/dist/phaser/display/group.d.ts.map +1 -1
  68. package/dist/phaser/display/image.d.ts.map +1 -1
  69. package/dist/phaser/display/sprite_util.d.ts.map +1 -1
  70. package/dist/phaser/display/text.d.ts +5 -0
  71. package/dist/phaser/display/text.d.ts.map +1 -1
  72. package/dist/phaser/display/webgl/base_texture.d.ts.map +1 -1
  73. package/dist/phaser/display/webgl/earcut.d.ts.map +1 -1
  74. package/dist/phaser/display/webgl/fast_sprite_batch.d.ts.map +1 -1
  75. package/dist/phaser/display/webgl/filter_manager.d.ts.map +1 -1
  76. package/dist/phaser/display/webgl/graphics.d.ts.map +1 -1
  77. package/dist/phaser/display/webgl/graphics_data.d.ts.map +1 -1
  78. package/dist/phaser/display/webgl/render_texture.d.ts.map +1 -1
  79. package/dist/phaser/display/webgl/renderer.d.ts.map +1 -1
  80. package/dist/phaser/display/webgl/sprite_batch.d.ts.map +1 -1
  81. package/dist/phaser/display/webgl/texture.d.ts.map +1 -1
  82. package/dist/phaser/geom/polygon.d.ts +11 -5
  83. package/dist/phaser/geom/polygon.d.ts.map +1 -1
  84. package/dist/phaser/geom/rectangle.d.ts.map +1 -1
  85. package/dist/phaser/geom/rounded_rectangle.d.ts.map +1 -1
  86. package/dist/phaser/util/math.d.ts.map +1 -1
  87. package/package.json +1 -1
  88. package/src/index.ts +2 -0
  89. package/src/phaser/core/animation.ts +1 -2
  90. package/src/phaser/core/animation_manager.ts +3 -11
  91. package/src/phaser/core/animation_parser.ts +2 -2
  92. package/src/phaser/core/array_set.ts +8 -10
  93. package/src/phaser/core/cache.ts +2 -2
  94. package/src/phaser/core/callback.ts +14 -0
  95. package/src/phaser/core/device.ts +24 -53
  96. package/src/phaser/core/device_util.ts +6 -6
  97. package/src/phaser/core/dom.ts +10 -7
  98. package/src/phaser/core/factory.ts +3 -2
  99. package/src/phaser/core/game.ts +4 -5
  100. package/src/phaser/core/input.ts +16 -12
  101. package/src/phaser/core/input_event.ts +1 -1
  102. package/src/phaser/core/input_handler.ts +21 -22
  103. package/src/phaser/core/input_mouse.ts +12 -17
  104. package/src/phaser/core/input_mspointer.ts +8 -11
  105. package/src/phaser/core/input_pointer.ts +22 -21
  106. package/src/phaser/core/input_touch.ts +16 -19
  107. package/src/phaser/core/loader.ts +75 -86
  108. package/src/phaser/core/loader_parser.ts +6 -6
  109. package/src/phaser/core/scale_manager.ts +34 -42
  110. package/src/phaser/core/scene_manager.ts +2 -1
  111. package/src/phaser/core/signal.ts +21 -13
  112. package/src/phaser/core/signal_binding.ts +20 -21
  113. package/src/phaser/core/sound.ts +9 -11
  114. package/src/phaser/core/sound_manager.ts +53 -47
  115. package/src/phaser/core/sound_sprite.ts +34 -21
  116. package/src/phaser/core/stage.ts +4 -4
  117. package/src/phaser/core/timer.ts +13 -30
  118. package/src/phaser/core/timer_event.ts +3 -2
  119. package/src/phaser/core/tween.ts +9 -8
  120. package/src/phaser/core/tween_data.ts +15 -41
  121. package/src/phaser/core/tween_easing.test.ts +35 -35
  122. package/src/phaser/core/tween_easing.ts +33 -33
  123. package/src/phaser/core/tween_manager.ts +77 -75
  124. package/src/phaser/display/bitmap_text.ts +7 -11
  125. package/src/phaser/display/button.ts +3 -2
  126. package/src/phaser/display/canvas/graphics.ts +7 -7
  127. package/src/phaser/display/canvas/pool.ts +5 -6
  128. package/src/phaser/display/canvas/renderer.ts +7 -4
  129. package/src/phaser/display/canvas/tinter.ts +9 -11
  130. package/src/phaser/display/canvas/util.test.ts +122 -0
  131. package/src/phaser/display/canvas/util.ts +34 -35
  132. package/src/phaser/display/display_object.ts +30 -43
  133. package/src/phaser/display/graphics.ts +30 -45
  134. package/src/phaser/display/group.ts +1 -1
  135. package/src/phaser/display/image.ts +9 -16
  136. package/src/phaser/display/sprite_util.ts +0 -1
  137. package/src/phaser/display/text.ts +27 -43
  138. package/src/phaser/display/webgl/base_texture.ts +3 -3
  139. package/src/phaser/display/webgl/earcut.ts +2 -3
  140. package/src/phaser/display/webgl/fast_sprite_batch.ts +1 -2
  141. package/src/phaser/display/webgl/filter_manager.ts +6 -18
  142. package/src/phaser/display/webgl/filter_texture.ts +3 -3
  143. package/src/phaser/display/webgl/graphics.ts +28 -38
  144. package/src/phaser/display/webgl/graphics_data.ts +0 -1
  145. package/src/phaser/display/webgl/render_texture.ts +4 -13
  146. package/src/phaser/display/webgl/renderer.ts +0 -1
  147. package/src/phaser/display/webgl/sprite_batch.ts +2 -9
  148. package/src/phaser/display/webgl/texture.ts +12 -15
  149. package/src/phaser/geom/point.ts +1 -1
  150. package/src/phaser/geom/polygon.test.ts +59 -0
  151. package/src/phaser/geom/polygon.ts +43 -45
  152. package/src/phaser/geom/rectangle.ts +3 -15
  153. package/src/phaser/geom/rounded_rectangle.ts +2 -4
  154. package/src/phaser/geom/util/point.ts +1 -1
  155. package/src/phaser/geom/util/rectangle.ts +2 -2
  156. package/src/phaser/util/math.ts +1 -5
@@ -253,16 +253,7 @@ export class Text extends Image {
253
253
  if (tabs === 0) {
254
254
  // Simple layout (no tabs)
255
255
  lineWidth = this.style.strokeThickness + this.padding.x;
256
- if (
257
- this.colors.length > 0 ||
258
- this.strokeColors.length > 0 ||
259
- this.fontWeights.length > 0 ||
260
- this.fontStyles.length > 0
261
- ) {
262
- lineWidth += this.measureLine(lines[i]);
263
- } else {
264
- lineWidth += this.context.measureText(lines[i]).width;
265
- }
256
+ lineWidth += this.hasCharacterStyles() ? this.measureLine(lines[i]) : this.context.measureText(lines[i]).width;
266
257
  // Adjust for wrapped text
267
258
  if (this.style.wordWrap) {
268
259
  lineWidth -= this.context.measureText(' ').width;
@@ -274,35 +265,20 @@ export class Text extends Image {
274
265
  if (Array.isArray(tabs)) {
275
266
  let tab = 0;
276
267
  for (let c = 0; c < line.length; c += 1) {
277
- let section = 0;
278
- if (
279
- this.colors.length > 0 ||
280
- this.strokeColors.length > 0 ||
281
- this.fontWeights.length > 0 ||
282
- this.fontStyles.length > 0
283
- ) {
284
- section = this.measureLine(line[c]);
285
- } else {
286
- section = Math.ceil(this.context.measureText(line[c]).width);
287
- }
268
+ const section = this.hasCharacterStyles()
269
+ ? this.measureLine(line[c])
270
+ : Math.ceil(this.context.measureText(line[c]).width);
288
271
  if (c > 0) {
289
272
  tab += tabs[c - 1]!;
290
273
  }
291
274
  lineWidth = tab + section;
292
275
  }
293
276
  } else {
294
- for (let c = 0; c < line.length; c += 1) {
277
+ for (const section of line) {
295
278
  // How far to the next tab?
296
- if (
297
- this.colors.length > 0 ||
298
- this.strokeColors.length > 0 ||
299
- this.fontWeights.length > 0 ||
300
- this.fontStyles.length > 0
301
- ) {
302
- lineWidth += this.measureLine(line[c]);
303
- } else {
304
- lineWidth += Math.ceil(this.context.measureText(line[c]).width);
305
- }
279
+ lineWidth += this.hasCharacterStyles()
280
+ ? this.measureLine(section)
281
+ : Math.ceil(this.context.measureText(section).width);
306
282
  const diff = snapToCeil(lineWidth, tabs) - lineWidth;
307
283
  lineWidth += diff;
308
284
  }
@@ -356,12 +332,7 @@ export class Text extends Image {
356
332
  linePositionX = Math.round(linePositionX);
357
333
  linePositionY = Math.round(linePositionY);
358
334
  }
359
- if (
360
- this.colors.length > 0 ||
361
- this.strokeColors.length > 0 ||
362
- this.fontWeights.length > 0 ||
363
- this.fontStyles.length > 0
364
- ) {
335
+ if (this.hasCharacterStyles()) {
365
336
  this.updateLine(lines[i], linePositionX, linePositionY);
366
337
  } else {
367
338
  if (this.style.stroke && this.style.strokeThickness) {
@@ -411,14 +382,14 @@ export class Text extends Image {
411
382
  }
412
383
  }
413
384
  } else {
414
- for (let c = 0; c < text.length; c += 1) {
415
- const section = Math.ceil(this.context.measureText(text[c]!).width);
385
+ for (const part of text) {
386
+ const section = Math.ceil(this.context.measureText(part).width);
416
387
  // How far to the next tab?
417
388
  snap = snapToCeil(x, tabs);
418
389
  if (fill) {
419
- this.context.fillText(text[c]!, snap, y);
390
+ this.context.fillText(part, snap, y);
420
391
  } else {
421
- this.context.strokeText(text[c]!, snap, y);
392
+ this.context.strokeText(part, snap, y);
422
393
  }
423
394
  x = snap + section;
424
395
  }
@@ -443,6 +414,19 @@ export class Text extends Image {
443
414
  }
444
415
  }
445
416
 
417
+ /**
418
+ * Reports whether any per-character style has been set, which forces the slower per-glyph path.
419
+ * @returns {boolean} True when a colour, weight or style has been assigned to a character.
420
+ */
421
+ public hasCharacterStyles(): boolean {
422
+ return (
423
+ this.colors.length > 0 ||
424
+ this.strokeColors.length > 0 ||
425
+ this.fontWeights.length > 0 ||
426
+ this.fontStyles.length > 0
427
+ );
428
+ }
429
+
446
430
  /**
447
431
  * Measures the width of a line of text.
448
432
  * @param {string} line - The line of text to measure.
@@ -1088,7 +1072,7 @@ export class Text extends Image {
1088
1072
  break;
1089
1073
  }
1090
1074
  }
1091
- // TODO might need a tweak. kind of a temp fix!
1075
+ // the six-pixel pad keeps descenders inside the measured box on fonts that under-report them
1092
1076
  const descent = i - baseline + 6;
1093
1077
  const properties: FontProperties = { ascent, descent, fontSize: ascent + descent };
1094
1078
  fontPropertiesCache[fontStyle] = properties;
@@ -4,9 +4,9 @@ export type TextureSource = HTMLImageElement | HTMLCanvasElement;
4
4
  import { removeByCanvas } from '../canvas/pool.js';
5
5
 
6
6
  export class BaseTexture {
7
- public resolution: number = 1;
8
- public width: number = 100;
9
- public height: number = 100;
7
+ public resolution = 1;
8
+ public width = 100;
9
+ public height = 100;
10
10
  public scaleMode!: number;
11
11
  public hasLoaded!: boolean;
12
12
  public source!: TextureSource | null;
@@ -668,8 +668,7 @@ export function earcutLinked(
668
668
  let next;
669
669
  // iterate through ears, slicing them one by one
670
670
  while (ear.prev !== ear.next) {
671
- prev = ear.prev;
672
- next = ear.next;
671
+ ({ prev, next } = ear);
673
672
  if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) {
674
673
  // cut off the triangle
675
674
  triangles.push(prev.i / dim);
@@ -708,7 +707,7 @@ export function earcutLinked(
708
707
  * @returns {object} The processed earcut data structure with triangulation information.
709
708
  */
710
709
  export function triangulate(data: number[], holeIndices: number[] | null, dim = 2): number[] {
711
- const hasHoles = holeIndices && holeIndices.length;
710
+ const hasHoles = holeIndices !== null && holeIndices.length > 0;
712
711
  const outerLen = hasHoles ? holeIndices[0]! * dim : data.length;
713
712
  let outerNode = linkedList(data, 0, outerLen, dim, true);
714
713
  const triangles: number[] = [];
@@ -95,7 +95,7 @@ export class FastSpriteBatch {
95
95
  */
96
96
  public render(spriteBatch: any): void {
97
97
  const { children } = spriteBatch;
98
- const sprite = children[0];
98
+ const [sprite] = children;
99
99
  // if the uvs have not updated then no point rendering just yet!
100
100
  // check texture.
101
101
  if (!sprite.texture._uvs) {
@@ -121,7 +121,6 @@ export class FastSpriteBatch {
121
121
  if (!sprite.visible) {
122
122
  return;
123
123
  }
124
- // TODO trim??
125
124
  if (sprite.texture.baseTexture !== this.currentBaseTexture && !sprite.texture.baseTexture.skipRender) {
126
125
  this.flush();
127
126
  this.currentBaseTexture = sprite.texture.baseTexture;
@@ -26,42 +26,30 @@ export class WebGLFilterManager {
26
26
  /**
27
27
  * Applies a filter to the specified texture.
28
28
  */
29
- public begin(): void {
30
- // TODO
31
- }
29
+ public begin(): void {}
32
30
 
33
31
  /**
34
32
  * Updates the filter texture for the specified size.
35
33
  */
36
- public pushFilter(_filterBlock: object | null): void {
37
- // TODO
38
- }
34
+ public pushFilter(_filterBlock: object | null): void {}
39
35
 
40
36
  /**
41
37
  * Resizes the filter manager to the specified dimensions.
42
38
  */
43
- public popFilter(): void {
44
- // TODO
45
- }
39
+ public popFilter(): void {}
46
40
 
47
41
  /**
48
42
  * Destroys this filter manager and cleans up resources.
49
43
  */
50
- public applyFilterPass(): void {
51
- // TODO
52
- }
44
+ public applyFilterPass(): void {}
53
45
 
54
46
  /**
55
47
  * Initializes the filter manager.
56
48
  */
57
- public initShaderBuffers(): void {
58
- // TODO
59
- }
49
+ public initShaderBuffers(): void {}
60
50
 
61
51
  /**
62
52
  * Sets up the filter manager for WebGL rendering.
63
53
  */
64
- public destroy(): void {
65
- // TODO
66
- }
54
+ public destroy(): void {}
67
55
  }
@@ -18,10 +18,10 @@ export class FilterTexture {
18
18
  this.gl = gl;
19
19
  this.frameBuffer = gl.createFramebuffer();
20
20
  this.texture = gl.createTexture();
21
- scaleMode = scaleMode || globalThis.PhaserRegistry.TEXTURE_SCALE_MODE;
21
+ const filterMode = scaleMode || globalThis.PhaserRegistry.TEXTURE_SCALE_MODE;
22
22
  gl.bindTexture(gl.TEXTURE_2D, this.texture);
23
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === SCALE_LINEAR ? gl.LINEAR : gl.NEAREST);
24
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === SCALE_LINEAR ? gl.LINEAR : gl.NEAREST);
23
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filterMode === SCALE_LINEAR ? gl.LINEAR : gl.NEAREST);
24
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filterMode === SCALE_LINEAR ? gl.LINEAR : gl.NEAREST);
25
25
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
26
26
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
27
27
  gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
@@ -61,7 +61,6 @@ export const switchMode = (webGL: GraphicsWebGLBucket, type: number) => {
61
61
  * @param {GraphicsData} webGLData - The WebGL graphics data object.
62
62
  */
63
63
  export const buildLine = (graphicsData: DisplayGraphicsData, webGLData: GraphicsData): void => {
64
- // TODO OPTIMISE!
65
64
  let i = 0;
66
65
  let { points } = graphicsData;
67
66
  if (points.length === 0) {
@@ -130,7 +129,7 @@ export const buildLine = (graphicsData: DisplayGraphicsData, webGLData: Graphics
130
129
  p2y = points[3]!;
131
130
  perpx = -(p1y - p2y);
132
131
  perpy = p1x - p2x;
133
- dist = Math.sqrt(perpx * perpx + perpy * perpy);
132
+ dist = Math.hypot(perpx, perpy);
134
133
  perpx /= dist;
135
134
  perpy /= dist;
136
135
  perpx *= width;
@@ -147,14 +146,14 @@ export const buildLine = (graphicsData: DisplayGraphicsData, webGLData: Graphics
147
146
  p3y = points[(i + 1) * 2 + 1]!;
148
147
  perpx = -(p1y - p2y);
149
148
  perpy = p1x - p2x;
150
- dist = Math.sqrt(perpx * perpx + perpy * perpy);
149
+ dist = Math.hypot(perpx, perpy);
151
150
  perpx /= dist;
152
151
  perpy /= dist;
153
152
  perpx *= width;
154
153
  perpy *= width;
155
154
  perp2x = -(p2y - p3y);
156
155
  perp2y = p2x - p3x;
157
- dist = Math.sqrt(perp2x * perp2x + perp2y * perp2y);
156
+ dist = Math.hypot(perp2x, perp2y);
158
157
  perp2x /= dist;
159
158
  perp2y /= dist;
160
159
  perp2x *= width;
@@ -178,7 +177,7 @@ export const buildLine = (graphicsData: DisplayGraphicsData, webGLData: Graphics
178
177
  if (pdist > 140 * 140) {
179
178
  perp3x = perpx - perp2x;
180
179
  perp3y = perpy - perp2y;
181
- dist = Math.sqrt(perp3x * perp3x + perp3y * perp3y);
180
+ dist = Math.hypot(perp3x, perp3y);
182
181
  perp3x /= dist;
183
182
  perp3y /= dist;
184
183
  perp3x *= width;
@@ -203,7 +202,7 @@ export const buildLine = (graphicsData: DisplayGraphicsData, webGLData: Graphics
203
202
  p2y = points[(length - 1) * 2 + 1]!;
204
203
  perpx = -(p1y - p2y);
205
204
  perpy = p1x - p2x;
206
- dist = Math.sqrt(perpx * perpx + perpy * perpy);
205
+ dist = Math.hypot(perpx, perpy);
207
206
  perpx /= dist;
208
207
  perpy /= dist;
209
208
  perpx *= width;
@@ -322,14 +321,14 @@ export const buildRoundedRectangle = (graphicsData: DisplayGraphicsData, webGLDa
322
321
  const { width } = rrectData;
323
322
  const { height } = rrectData;
324
323
  const { radius } = rrectData;
325
- let recPoints = [];
326
- recPoints.push(x, y + radius);
327
- recPoints = recPoints.concat(quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height));
328
- recPoints = recPoints.concat(
329
- quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)
330
- );
331
- recPoints = recPoints.concat(quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y));
332
- recPoints = recPoints.concat(quadraticBezierCurve(x + radius, y, x, y, x, y + radius));
324
+ const recPoints = [
325
+ x,
326
+ y + radius,
327
+ ...quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height),
328
+ ...quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius),
329
+ ...quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y),
330
+ ...quadraticBezierCurve(x + radius, y, x, y, x, y + radius),
331
+ ];
333
332
  if (graphicsData.fill) {
334
333
  const color = hex2rgb(graphicsData.fillColor ?? 0);
335
334
  const alpha = graphicsData.fillAlpha;
@@ -348,7 +347,6 @@ export const buildRoundedRectangle = (graphicsData: DisplayGraphicsData, webGLDa
348
347
  indices.push(triangles[i + 2]! + vecPos);
349
348
  }
350
349
  for (let i = 0; i < recPoints.length; i += 2) {
351
- // TODO verify
352
350
  verts.push(recPoints[i]!, recPoints[i + 1]!, r, g, b, alpha);
353
351
  }
354
352
  }
@@ -372,13 +370,11 @@ export const buildCircle = (graphicsData: DisplayGraphicsData, webGLData: Graphi
372
370
  const { y } = circleData;
373
371
  let width;
374
372
  let height;
375
- // TODO - bit hacky??
376
373
  if (circleData instanceof Circle) {
377
374
  width = circleData.radius;
378
375
  height = circleData.radius;
379
376
  } else {
380
- width = circleData.width;
381
- height = circleData.height;
377
+ ({ width, height } = circleData);
382
378
  }
383
379
  const totalSegs = 40;
384
380
  const seg = (Math.PI * 2) / totalSegs;
@@ -417,7 +413,6 @@ export const buildCircle = (graphicsData: DisplayGraphicsData, webGLData: Graphi
417
413
  * @param {GraphicsData} webGLData - The WebGL graphics data object.
418
414
  */
419
415
  export const buildComplexPoly = (graphicsData: DisplayGraphicsData, webGLData: GraphicsData): void => {
420
- // TODO - no need to copy this as it gets turned into a Float32Array anyways..
421
416
  const points = [...graphicsData.points];
422
417
  if (points.length < 6) {
423
418
  return;
@@ -448,7 +443,6 @@ export const buildComplexPoly = (graphicsData: DisplayGraphicsData, webGLData: G
448
443
  // add a quad to the end cos there is no point making another buffer!
449
444
  points.push(minX, minY, maxX, minY, maxX, maxY, minX, maxY);
450
445
  // push a quad onto the end..
451
- // TODO - this aint needed!
452
446
  const length = points.length / 2;
453
447
  for (let i = 0; i < length; i += 1) {
454
448
  indices.push(i);
@@ -532,27 +526,23 @@ export const updateGraphics = (graphics: Graphics, gl: IdentifiedWebGLRenderingC
532
526
  const data = graphics.graphicsData[i]!;
533
527
  if (data.shape instanceof Polygon) {
534
528
  // need to add the points the the graphics object..
535
- data.points = [...data.shape.points];
536
- if (data.shape.closed) {
537
- // close the poly if the value is true!
538
- if (data.points[0] !== data.points.at(-2) || data.points[1] !== data.points.at(-1)) {
539
- data.points.push(data.points[0]!, data.points[1]!);
540
- }
529
+ data.points = [...(data.shape.points as number[])];
530
+ // close the poly if the value is true!
531
+ if (data.shape.closed && (data.points[0] !== data.points.at(-2) || data.points[1] !== data.points.at(-1))) {
532
+ data.points.push(data.points[0]!, data.points[1]!);
541
533
  }
542
534
  // MAKE SURE WE HAVE THE CORRECT TYPE..
543
- if (data.fill) {
544
- if (data.points.length >= stencilBufferLimit) {
545
- if (data.points.length < stencilBufferLimit * 2) {
546
- webGLData = switchMode(webGL, 0);
547
- const canDrawUsingSimple = buildPoly(data, webGLData);
548
- if (!canDrawUsingSimple) {
549
- webGLData = switchMode(webGL, 1);
550
- buildComplexPoly(data, webGLData);
551
- }
552
- } else {
535
+ if (data.fill && data.points.length >= stencilBufferLimit) {
536
+ if (data.points.length < stencilBufferLimit * 2) {
537
+ webGLData = switchMode(webGL, 0);
538
+ const canDrawUsingSimple = buildPoly(data, webGLData);
539
+ if (!canDrawUsingSimple) {
553
540
  webGLData = switchMode(webGL, 1);
554
541
  buildComplexPoly(data, webGLData);
555
542
  }
543
+ } else {
544
+ webGLData = switchMode(webGL, 1);
545
+ buildComplexPoly(data, webGLData);
556
546
  }
557
547
  }
558
548
  if (data.lineWidth > 0) {
@@ -602,8 +592,8 @@ export const renderGraphics = (graphics: Graphics, renderSession: RenderSession)
602
592
  return;
603
593
  }
604
594
  // This could be speeded up for sure!
605
- for (let i = 0; i < webGL.data.length; i += 1) {
606
- webGLData = webGL.data[i]!;
595
+ for (const data of webGL.data) {
596
+ webGLData = data;
607
597
  if (webGLData.mode === 1) {
608
598
  renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession);
609
599
  // render quad..
@@ -16,7 +16,6 @@ export class GraphicsData {
16
16
  */
17
17
  public constructor(gl: WebGLRenderingContext) {
18
18
  this.gl = gl;
19
- // TODO does this need to be split before uploading??
20
19
  this.color = [0, 0, 0]; // color split!
21
20
  this.points = [];
22
21
  this.indices = [];
@@ -71,30 +71,22 @@ export class RenderTexture extends Texture {
71
71
  /**
72
72
  * Destroys this render texture and cleans up resources.
73
73
  */
74
- public resize(): void {
75
- // TODO
76
- }
74
+ public resize(): void {}
77
75
 
78
76
  /**
79
77
  * Updates the size of this render texture.
80
78
  */
81
- public clear(): void {
82
- // TODO
83
- }
79
+ public clear(): void {}
84
80
 
85
81
  /**
86
82
  * Updates the resolution of this render texture.
87
83
  */
88
- public renderWebGL(): void {
89
- // TODO
90
- }
84
+ public renderWebGL(): void {}
91
85
 
92
86
  /**
93
87
  * Updates the scale mode of this render texture.
94
88
  */
95
- public renderCanvas(): void {
96
- // TODO
97
- }
89
+ public renderCanvas(): void {}
98
90
 
99
91
  /**
100
92
  * Gets the HTML image element for this render texture.
@@ -119,7 +111,6 @@ export class RenderTexture extends Texture {
119
111
  * @returns {HTMLCanvasElement} The canvas element.
120
112
  */
121
113
  public getCanvas(): HTMLCanvasElement | null {
122
- // TODO
123
114
  return null;
124
115
  }
125
116
  }
@@ -139,7 +139,6 @@ export class WebGLRenderer {
139
139
  */
140
140
  public initContext(game: Game): void {
141
141
  game.logger.info('initContext');
142
- // TODO: view.addEventListener('webglcontextcreationerror', this.onWebGLContextCreationError, false);
143
142
  /** @type {WebGLRenderingContext & { id: number }} */
144
143
  const gl = this.view.getContext('webgl', this._contextOptions) as IdentifiedWebGLRenderingContext | null;
145
144
  if (!gl) {
@@ -215,9 +215,7 @@ export class WebGLSpriteBatch {
215
215
  /**
216
216
  * Binds the sprite batch to the WebGL context.
217
217
  */
218
- public renderTilingSprite(): void {
219
- // TODO
220
- }
218
+ public renderTilingSprite(): void {}
221
219
 
222
220
  /**
223
221
  * Updates the sprite batch.
@@ -264,11 +262,7 @@ export class WebGLSpriteBatch {
264
262
  let sprite;
265
263
  for (let i = 0, j = this.currentBatchSize; i < j; i += 1) {
266
264
  sprite = this.sprites[i];
267
- if (sprite.tilingTexture) {
268
- nextTexture = sprite.tilingTexture.baseTexture;
269
- } else {
270
- nextTexture = sprite.texture.baseTexture;
271
- }
265
+ nextTexture = sprite.tilingTexture ? sprite.tilingTexture.baseTexture : sprite.texture.baseTexture;
272
266
  nextBlendMode = sprite.blendMode;
273
267
  nextShader = sprite.shader ?? this.defaultShader;
274
268
  blendSwap = currentBlendMode !== nextBlendMode;
@@ -305,7 +299,6 @@ export class WebGLSpriteBatch {
305
299
  // set the projection
306
300
  const { projection } = this.renderSession;
307
301
  gl.uniform2f(shader.projectionVector, projection.x, projection.y);
308
- // TODO - this is temporary!
309
302
  const offsetVector = this.renderSession.offset;
310
303
  gl.uniform2f(shader.offsetVector, offsetVector.x, offsetVector.y);
311
304
  // set the pointers
@@ -39,16 +39,12 @@ export class Texture {
39
39
  crop: Rectangle | null | undefined = null,
40
40
  trim: Rectangle | null | undefined = null
41
41
  ) {
42
- if (!frame) {
43
- this.noFrame = true;
44
- frame = new Rectangle(0, 0, 1, 1);
45
- }
46
- if (baseTexture instanceof Texture) {
47
- baseTexture = baseTexture.baseTexture;
48
- }
49
- this.baseTexture = baseTexture;
42
+ this.noFrame = !frame;
43
+ let textureFrame = frame ?? new Rectangle(0, 0, 1, 1);
44
+ const source = baseTexture instanceof Texture ? baseTexture.baseTexture : baseTexture;
45
+ this.baseTexture = source;
50
46
  /** @type {Rectangle} */
51
- this.frame = frame;
47
+ this.frame = textureFrame;
52
48
  /** @type {Rectangle | null | undefined} */
53
49
  this.trim = trim;
54
50
  /** @type {boolean} */
@@ -67,11 +63,11 @@ export class Texture {
67
63
  this.height = 0;
68
64
  /** @type {Rectangle} */
69
65
  this.crop = crop ?? new Rectangle(0, 0, 1, 1);
70
- if (baseTexture.hasLoaded) {
66
+ if (source.hasLoaded) {
71
67
  if (this.noFrame) {
72
- frame = new Rectangle(0, 0, baseTexture.width, baseTexture.height);
68
+ textureFrame = new Rectangle(0, 0, source.width, source.height);
73
69
  }
74
- this.setFrame(frame);
70
+ this.setFrame(textureFrame);
75
71
  }
76
72
  }
77
73
 
@@ -89,7 +85,7 @@ export class Texture {
89
85
  * Destroys this texture and cleans up resources.
90
86
  * @param {boolean} destroyBase - Whether to destroy the base texture as well.
91
87
  */
92
- public destroy(destroyBase: boolean = false): void {
88
+ public destroy(destroyBase = false): void {
93
89
  if (destroyBase) {
94
90
  this.baseTexture.destroy();
95
91
  }
@@ -116,10 +112,11 @@ export class Texture {
116
112
  ) {
117
113
  // If `true` then `PIXI.Texture.setFrame` will no longer throw an error if the texture dimensions are incorrect.
118
114
  // Instead `Texture.valid` will be set to `false` (#1556)
119
- // TODO: make this configurable
120
115
  const isTextureSilentFail = true;
121
116
  if (!isTextureSilentFail) {
122
- throw new Error(`Texture Error: frame does not fit inside the base Texture dimensions ${this}`);
117
+ throw new Error(
118
+ `Texture Error: frame ${frame.width}x${frame.height} does not fit inside the base texture ${this.baseTexture.width}x${this.baseTexture.height}`
119
+ );
123
120
  }
124
121
  this.valid = false;
125
122
  return;
@@ -218,7 +218,7 @@ export class Point {
218
218
  * @returns {number} The magnitude of the point.
219
219
  */
220
220
  public getMagnitude(): number {
221
- return Math.sqrt(this.x * this.x + this.y * this.y);
221
+ return Math.hypot(this.x, this.y);
222
222
  }
223
223
 
224
224
  /**
@@ -79,4 +79,63 @@ describe('Polygon', () => {
79
79
  expect(polygon._points.every((value) => typeof value === 'number')).toBe(true);
80
80
  });
81
81
  });
82
+
83
+ describe('setTo', () => {
84
+ it('reads a flat list of loose numbers as x/y pairs', () => {
85
+ expect(new Polygon([0, 0, 10, 0, 10, 10]).toNumberArray()).toStrictEqual([0, 0, 10, 0, 10, 10]);
86
+ });
87
+
88
+ it('reads a list of x/y tuples', () => {
89
+ expect(
90
+ new Polygon([
91
+ [0, 0],
92
+ [10, 0],
93
+ [10, 10],
94
+ ]).toNumberArray()
95
+ ).toStrictEqual([0, 0, 10, 0, 10, 10]);
96
+ });
97
+
98
+ it('reads a list of plain x/y objects', () => {
99
+ expect(new Polygon([{ x: 0, y: 0 }, { x: 10, y: 0 }, new Point(10, 10)]).toNumberArray()).toStrictEqual([
100
+ 0, 0, 10, 0, 10, 10,
101
+ ]);
102
+ });
103
+
104
+ it('stores every vertex as a point whatever form it came in as', () => {
105
+ expect(new Polygon([0, 0, 10, 0]).points.every((value) => value instanceof Point)).toBe(true);
106
+ });
107
+
108
+ it('replaces the previous points', () => {
109
+ const polygon = square();
110
+ polygon.points = [new Point(1, 1), new Point(2, 2)];
111
+ expect(polygon.toNumberArray()).toStrictEqual([1, 1, 2, 2]);
112
+ });
113
+
114
+ it('empties the polygon when the points are cleared', () => {
115
+ const polygon = square();
116
+ polygon.points = null;
117
+ expect(polygon.points).toStrictEqual([]);
118
+ expect(polygon.area).toBe(0);
119
+ });
120
+ });
121
+
122
+ describe('calculateArea', () => {
123
+ it('measures a square', () => {
124
+ expect(Math.abs(square().area)).toBe(100);
125
+ });
126
+
127
+ it('measures a triangle as half its bounding square', () => {
128
+ expect(Math.abs(triangle().area)).toBe(50);
129
+ });
130
+ });
131
+
132
+ describe('clone', () => {
133
+ it('copies the vertices into a detached polygon', () => {
134
+ const original = square();
135
+ const copy = original.clone();
136
+ expect(copy.toNumberArray()).toStrictEqual(original.toNumberArray());
137
+ original.points = [new Point(1, 1)];
138
+ expect(copy.toNumberArray()).toStrictEqual([0, 0, 10, 0, 10, 10, 0, 10]);
139
+ });
140
+ });
82
141
  });