melonjs 10.6.1 → 10.8.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 (79) hide show
  1. package/dist/melonjs.js +37947 -36530
  2. package/dist/melonjs.min.js +22 -22
  3. package/dist/melonjs.module.d.ts +1352 -307
  4. package/dist/melonjs.module.js +2929 -1501
  5. package/package.json +14 -12
  6. package/src/camera/camera2d.js +1 -1
  7. package/src/entity/entity.js +6 -7
  8. package/src/game.js +5 -5
  9. package/src/geometries/ellipse.js +10 -11
  10. package/src/geometries/line.js +3 -3
  11. package/src/geometries/path2d.js +319 -0
  12. package/src/geometries/poly.js +11 -11
  13. package/src/geometries/rectangle.js +30 -15
  14. package/src/geometries/roundrect.js +67 -0
  15. package/src/index.js +9 -5
  16. package/src/input/gamepad.js +12 -10
  17. package/src/input/keyboard.js +5 -3
  18. package/src/input/pointer.js +1 -1
  19. package/src/input/pointerevent.js +3 -4
  20. package/src/lang/deprecated.js +1 -1
  21. package/src/level/tiled/TMXLayer.js +1 -1
  22. package/src/level/tiled/TMXObject.js +9 -12
  23. package/src/level/tiled/TMXTileMap.js +23 -4
  24. package/src/level/tiled/TMXUtils.js +1 -1
  25. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  26. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  27. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  28. package/src/level/tiled/renderer/TMXRenderer.js +1 -1
  29. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  30. package/src/loader/loader.js +5 -5
  31. package/src/loader/loadingscreen.js +1 -1
  32. package/src/math/color.js +1 -1
  33. package/src/math/matrix2.js +1 -1
  34. package/src/math/matrix3.js +67 -66
  35. package/src/math/observable_vector2.js +1 -1
  36. package/src/math/observable_vector3.js +1 -1
  37. package/src/math/vector2.js +1 -1
  38. package/src/math/vector3.js +1 -1
  39. package/src/particles/emitter.js +130 -430
  40. package/src/particles/particle.js +53 -53
  41. package/src/particles/settings.js +310 -0
  42. package/src/physics/body.js +67 -51
  43. package/src/physics/bounds.js +8 -9
  44. package/src/physics/world.js +1 -1
  45. package/src/polyfill/console.js +7 -7
  46. package/src/polyfill/index.js +7 -0
  47. package/src/polyfill/performance.js +20 -0
  48. package/src/polyfill/requestAnimationFrame.js +10 -10
  49. package/src/renderable/collectable.js +9 -2
  50. package/src/renderable/colorlayer.js +1 -1
  51. package/src/renderable/container.js +1 -1
  52. package/src/renderable/imagelayer.js +3 -3
  53. package/src/renderable/renderable.js +1 -1
  54. package/src/renderable/sprite.js +2 -3
  55. package/src/renderable/trigger.js +10 -4
  56. package/src/state/stage.js +1 -1
  57. package/src/state/state.js +8 -8
  58. package/src/system/device.js +148 -133
  59. package/src/system/event.js +10 -10
  60. package/src/system/pooling.js +156 -149
  61. package/src/system/timer.js +1 -1
  62. package/src/text/bitmaptext.js +1 -1
  63. package/src/text/text.js +1 -1
  64. package/src/utils/agent.js +4 -4
  65. package/src/utils/function.js +2 -2
  66. package/src/utils/utils.js +10 -5
  67. package/src/video/canvas/canvas_renderer.js +104 -36
  68. package/src/video/renderer.js +28 -16
  69. package/src/video/texture.js +1 -1
  70. package/src/video/video.js +11 -11
  71. package/src/video/webgl/glshader.js +30 -194
  72. package/src/video/webgl/utils/attributes.js +16 -0
  73. package/src/video/webgl/utils/precision.js +11 -0
  74. package/src/video/webgl/utils/program.js +58 -0
  75. package/src/video/webgl/utils/string.js +16 -0
  76. package/src/video/webgl/utils/uniforms.js +87 -0
  77. package/src/video/webgl/webgl_compositor.js +1 -14
  78. package/src/video/webgl/webgl_renderer.js +129 -186
  79. package/src/particles/particlecontainer.js +0 -95
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "melonjs",
3
- "version": "10.6.1",
3
+ "version": "10.8.0",
4
4
  "description": "melonJS Game Engine",
5
5
  "homepage": "http://www.melonjs.org/",
6
6
  "keywords": [
@@ -38,7 +38,8 @@
38
38
  "engines": {
39
39
  "node": ">= 12"
40
40
  },
41
- "main": "dist/melonjs.js",
41
+ "main": "dist/melonjs.module.js",
42
+ "type": "module",
42
43
  "module": "dist/melonjs.module.js",
43
44
  "files": [
44
45
  "dist/melonjs.js",
@@ -54,6 +55,7 @@
54
55
  ],
55
56
  "dependencies": {
56
57
  "@teppeis/multimaps": "^2.0.0",
58
+ "core-js": "^3.22.5",
57
59
  "earcut": "2.2.3",
58
60
  "eventemitter3": "^4.0.7",
59
61
  "howler": "2.2.3"
@@ -61,34 +63,34 @@
61
63
  "devDependencies": {
62
64
  "@rollup/plugin-buble": "^0.21.3",
63
65
  "@rollup/plugin-commonjs": "^22.0.0",
64
- "@rollup/plugin-node-resolve": "^13.2.1",
66
+ "@rollup/plugin-node-resolve": "^13.3.0",
65
67
  "@rollup/plugin-replace": "^4.0.0",
66
68
  "@types/offscreencanvas": "^2019.6.4",
67
69
  "cheerio": "^1.0.0-rc.10",
68
70
  "del-cli": "^4.0.1",
69
- "eslint": "^8.14.0",
70
- "eslint-plugin-jsdoc": "^39.2.8",
71
- "jasmine-core": "^4.1.0",
71
+ "eslint": "^8.15.0",
72
+ "eslint-plugin-jsdoc": "^39.2.9",
73
+ "jasmine-core": "^4.1.1",
72
74
  "jsdoc": "^3.6.10",
73
- "karma": "^6.3.19",
75
+ "karma": "^6.3.20",
74
76
  "karma-chrome-launcher": "^3.1.1",
75
77
  "karma-coverage": "^2.2.0",
76
78
  "karma-html-detailed-reporter": "^2.1.0",
77
- "karma-jasmine": "^5.0.0",
79
+ "karma-jasmine": "^5.0.1",
78
80
  "karma-nyan-reporter": "0.2.5",
79
81
  "qs": "^6.10.3",
80
- "rollup": "^2.70.2",
82
+ "rollup": "^2.73.0",
81
83
  "rollup-plugin-bundle-size": "^1.0.3",
82
84
  "rollup-plugin-string": "^3.0.0",
83
- "terser": "^5.12.1",
84
- "typescript": "^4.6.3"
85
+ "terser": "^5.13.1",
86
+ "typescript": "^4.6.4"
85
87
  },
86
88
  "scripts": {
87
89
  "build": "npm run lint && rollup -c --silent",
88
90
  "dist": " npm run build && npm run minify && mkdirp dist && cp -f build/*.js dist/ && npm run types",
89
91
  "minify": "terser build/melonjs.js --compress --mangle --comments '/(?:^!|@(?:license|preserve|cc_on))/' --output build/melonjs.min.js",
90
92
  "lint": "eslint src rollup.config.js",
91
- "test": "npm run build && karma start tests/karma.conf.js",
93
+ "test": "npm run build && karma start tests/karma.conf.cjs",
92
94
  "doc": "mkdirp docs && jsdoc -c jsdoc_conf.json",
93
95
  "release": "npm run dist && npm publish --access public",
94
96
  "clean": "del-cli --force build/*.js dist/*.js dist/*.d.ts docs src/**/*.d.ts",
@@ -7,7 +7,7 @@ import Matrix3d from "./../math/matrix3.js";
7
7
  import Rect from "./../geometries/rectangle.js";
8
8
  import { renderer } from "./../video/video.js";
9
9
  import * as event from "./../system/event.js";
10
- import * as pool from "./../system/pooling.js";
10
+ import pool from "./../system/pooling.js";
11
11
  import Renderable from "./../renderable/renderable.js";
12
12
  import {clamp, toBeCloseTo} from "./../math/math.js";
13
13
  import { world } from "./../game.js";
@@ -1,8 +1,7 @@
1
- import Vector2d from "./../math/vector2.js";
1
+ import pool from "./../system/pooling.js";
2
2
  import Renderable from "./../renderable/renderable.js";
3
3
  import Sprite from "./../renderable/sprite.js";
4
4
  import Body from "./../physics/body.js";
5
- import Polygon from "./../geometries/poly.js";
6
5
 
7
6
 
8
7
  /**
@@ -102,11 +101,11 @@ class Entity extends Renderable {
102
101
  */
103
102
  // initialize the default body
104
103
  if (typeof settings.shapes === "undefined") {
105
- settings.shapes = new Polygon(0, 0, [
106
- new Vector2d(0, 0),
107
- new Vector2d(this.width, 0),
108
- new Vector2d(this.width, this.height),
109
- new Vector2d(0, this.height)
104
+ settings.shapes = pool.pull("Polygon", 0, 0, [
105
+ pool.pull("Vector2d", 0, 0),
106
+ pool.pull("Vector2d", this.width, 0),
107
+ pool.pull("Vector2d", this.width, this.height),
108
+ pool.pull("Vector2d", 0, this.height)
110
109
  ]);
111
110
  }
112
111
  this.body = new Body(this, settings.shapes, this.onBodyUpdate.bind(this));
package/src/game.js CHANGED
@@ -94,7 +94,7 @@ export let sortOn = "z";
94
94
  * @name lastUpdate
95
95
  * @memberof game
96
96
  */
97
- export let lastUpdate = window.performance.now();
97
+ export let lastUpdate = globalThis.performance.now();
98
98
 
99
99
  /**
100
100
  * Fired when a level is fully loaded and all entities instantiated. <br>
@@ -188,7 +188,7 @@ export function update(time, stage) {
188
188
  accumulatorUpdateDelta = (timer.interpolation) ? updateDelta : Math.max(updateDelta, updateAverageDelta);
189
189
 
190
190
  while (accumulator >= accumulatorUpdateDelta || timer.interpolation) {
191
- lastUpdateStart = window.performance.now();
191
+ lastUpdateStart = globalThis.performance.now();
192
192
 
193
193
  // game update event
194
194
  if (state.isPaused() !== true) {
@@ -198,7 +198,7 @@ export function update(time, stage) {
198
198
  // update all objects (and pass the elapsed time since last frame)
199
199
  isDirty = stage.update(updateDelta) || isDirty;
200
200
 
201
- lastUpdate = window.performance.now();
201
+ lastUpdate = globalThis.performance.now();
202
202
  updateAverageDelta = lastUpdate - lastUpdateStart;
203
203
 
204
204
  accumulator -= accumulatorUpdateDelta;
@@ -223,7 +223,7 @@ export function draw(stage) {
223
223
 
224
224
  if (renderer.isContextValid === true && (isDirty || isAlwaysDirty)) {
225
225
  // publish notification
226
- event.emit(event.GAME_BEFORE_DRAW, window.performance.now());
226
+ event.emit(event.GAME_BEFORE_DRAW, globalThis.performance.now());
227
227
 
228
228
  // prepare renderer to draw a new frame
229
229
  renderer.clear();
@@ -238,6 +238,6 @@ export function draw(stage) {
238
238
  renderer.flush();
239
239
 
240
240
  // publish notification
241
- event.emit(event.GAME_AFTER_DRAW, window.performance.now());
241
+ event.emit(event.GAME_AFTER_DRAW, globalThis.performance.now());
242
242
  }
243
243
  };
@@ -1,5 +1,4 @@
1
- import Vector2d from "./../math/vector2.js";
2
- import * as pool from "./../system/pooling.js";
1
+ import pool from "./../system/pooling.js";
3
2
 
4
3
  /**
5
4
  * @classdesc
@@ -18,9 +17,9 @@ class Ellipse {
18
17
  * @public
19
18
  * @type {Vector2d}
20
19
  * @name pos
21
- * @memberof Ellipse#
20
+ * @memberof Ellipse.prototype
22
21
  */
23
- this.pos = new Vector2d();
22
+ this.pos = pool.pull("Vector2d");
24
23
 
25
24
  /**
26
25
  * The bounding rectangle for this shape
@@ -33,7 +32,7 @@ class Ellipse {
33
32
  * @public
34
33
  * @type {number}
35
34
  * @name radius
36
- * @memberof Ellipse
35
+ * @memberof Ellipse.prototype
37
36
  */
38
37
  this.radius = NaN;
39
38
 
@@ -42,27 +41,27 @@ class Ellipse {
42
41
  * @public
43
42
  * @type {Vector2d}
44
43
  * @name radiusV
45
- * @memberof Ellipse#
44
+ * @memberof Ellipse.prototype
46
45
  */
47
- this.radiusV = new Vector2d();
46
+ this.radiusV = pool.pull("Vector2d");
48
47
 
49
48
  /**
50
49
  * Radius squared, for pythagorean theorom
51
50
  * @public
52
51
  * @type {Vector2d}
53
52
  * @name radiusSq
54
- * @memberof Ellipse#
53
+ * @memberof Ellipse.prototype
55
54
  */
56
- this.radiusSq = new Vector2d();
55
+ this.radiusSq = pool.pull("Vector2d");
57
56
 
58
57
  /**
59
58
  * x/y scaling ratio for ellipse
60
59
  * @public
61
60
  * @type {Vector2d}
62
61
  * @name ratio
63
- * @memberof Ellipse#
62
+ * @memberof Ellipse.prototype
64
63
  */
65
- this.ratio = new Vector2d();
64
+ this.ratio = pool.pull("Vector2d");
66
65
 
67
66
  // the shape type
68
67
  this.shapeType = "Ellipse";
@@ -1,4 +1,4 @@
1
- import Vector2d from "./../math/vector2.js";
1
+ import pool from "./../system/pooling.js";
2
2
  import Polygon from "./poly.js";
3
3
 
4
4
  /**
@@ -76,11 +76,11 @@ class Line extends Polygon {
76
76
 
77
77
  // Calculate the edges/normals
78
78
  if (edges[0] === undefined) {
79
- edges[0] = new Vector2d();
79
+ edges[0] = pool.pull("Vector2d");
80
80
  }
81
81
  edges[0].copy(points[1]).sub(points[0]);
82
82
  if (normals[0] === undefined) {
83
- normals[0] = new Vector2d();
83
+ normals[0] = pool.pull("Vector2d");
84
84
  }
85
85
  normals[0].copy(edges[0]).perp().normalize();
86
86
 
@@ -0,0 +1,319 @@
1
+ import pool from "./../system/pooling.js";
2
+ import { TAU } from "./../math/math.js";
3
+ import earcut from "earcut";
4
+
5
+ /**
6
+ * @classdesc
7
+ * a simplified path2d implementation, supporting only one path
8
+ */
9
+ class Path2D {
10
+ constructor() {
11
+ /**
12
+ * the points defining the current path
13
+ * @public
14
+ * @type {Vector2d[]}
15
+ * @name points
16
+ * @memberof Path2D#
17
+ */
18
+ this.points = [];
19
+
20
+ /**
21
+ * space between interpolated points for quadratic and bezier curve approx. in pixels.
22
+ * @public
23
+ * @type {number}
24
+ * @name arcResolution
25
+ * @default 5
26
+ * @memberof Path2D#
27
+ */
28
+ this.arcResolution = 5;
29
+
30
+ /* @ignore */
31
+ this.vertices = [];
32
+ }
33
+
34
+ /**
35
+ * begin a new path
36
+ * @name beginPath
37
+ * @memberof Path2D.prototype
38
+ * @function
39
+ */
40
+ beginPath() {
41
+ // empty the cache and recycle all vectors
42
+ this.points.forEach((point) => {
43
+ pool.push(point);
44
+ });
45
+ this.points.length = 0;
46
+ }
47
+
48
+ /**
49
+ * causes the point of the pen to move back to the start of the current path.
50
+ * It tries to draw a straight line from the current point to the start.
51
+ * If the shape has already been closed or has only one point, this function does nothing.
52
+ * @name closePath
53
+ * @memberof Path2D.prototype
54
+ * @function
55
+ */
56
+ closePath() {
57
+ var points = this.points;
58
+ if (points.length > 1 && !points[points.length-1].equals(points[0])) {
59
+ points.push(pool.pull("Vector2d", points[0].x, points[0].y));
60
+ }
61
+ }
62
+
63
+ /**
64
+ * triangulate the shape defined by this path into an array of triangles
65
+ * @name triangulatePath
66
+ * @memberof Path2D.prototype
67
+ * @function
68
+ * @returns {Vector2d[]}
69
+ */
70
+ triangulatePath() {
71
+ var i = 0;
72
+ var points = this.points;
73
+ var vertices = this.vertices;
74
+ var indices = earcut(points.flatMap(p => [p.x, p.y]));
75
+
76
+ // calculate all vertices
77
+ for (i = 0; i < indices.length; i++ ) {
78
+ if (typeof vertices[i] === "undefined") {
79
+ // increase cache buffer if necessary
80
+ vertices[i] = pool.pull("Vector2d");
81
+ }
82
+ vertices[i].set(points[indices[i]].x, points[indices[i]].y);
83
+ }
84
+
85
+ // recycle overhead from a previous triangulation
86
+ while (vertices.length > indices.length) {
87
+ pool.push(vertices[vertices.length-1]);
88
+ vertices.length -= 1;
89
+ }
90
+
91
+ return vertices;
92
+ }
93
+
94
+ /**
95
+ * moves the starting point of the current path to the (x, y) coordinates.
96
+ * @name moveTo
97
+ * @memberof Path2D.prototype
98
+ * @function
99
+ * @param {number} x the x-axis (horizontal) coordinate of the point.
100
+ * @param {number} y the y-axis (vertical) coordinate of the point.
101
+ */
102
+ moveTo(x, y) {
103
+ this.points.push(pool.pull("Vector2d", x, y));
104
+ }
105
+
106
+ /**
107
+ * connects the last point in the current patch to the (x, y) coordinates with a straight line.
108
+ * @name lineTo
109
+ * @memberof Path2D.prototype
110
+ * @function
111
+ * @param {number} x the x-axis coordinate of the line's end point.
112
+ * @param {number} y the y-axis coordinate of the line's end point.
113
+ */
114
+ lineTo(x, y) {
115
+ this.points.push(pool.pull("Vector2d", x, y));
116
+ }
117
+
118
+ /**
119
+ * adds an arc to the current path which is centered at (x, y) position with the given radius,
120
+ * starting at startAngle and ending at endAngle going in the given direction by counterclockwise (defaulting to clockwise).
121
+ * @name arc
122
+ * @memberof Path2D.prototype
123
+ * @function
124
+ * @param {number} x the horizontal coordinate of the arc's center.
125
+ * @param {number} y the vertical coordinate of the arc's center.
126
+ * @param {number} radius the arc's radius. Must be positive.
127
+ * @param {number} startAngle the angle at which the arc starts in radians, measured from the positive x-axis.
128
+ * @param {number} endAngle the angle at which the arc ends in radians, measured from the positive x-axis.
129
+ * @param {boolean} [anticlockwise=false] an optional boolean value. If true, draws the arc counter-clockwise between the start and end angles.
130
+ */
131
+ arc(x, y, radius, startAngle, endAngle, anticlockwise = false) {
132
+ var points = this.points;
133
+ // based on from https://github.com/karellodewijk/canvas-webgl/blob/master/canvas-webgl.js
134
+ //bring angles all in [0, 2*PI] range
135
+ if (startAngle === endAngle) return;
136
+ var fullCircle = anticlockwise ? Math.abs(startAngle-endAngle) >= (TAU) : Math.abs(endAngle-startAngle) >= (TAU);
137
+
138
+ startAngle = startAngle % (TAU);
139
+ endAngle = endAngle % (TAU);
140
+
141
+ if (startAngle < 0) startAngle += TAU;
142
+ if (endAngle < 0) endAngle += TAU;
143
+
144
+ if (startAngle >= endAngle) {
145
+ endAngle+= TAU;
146
+ }
147
+
148
+ var diff = endAngle - startAngle;
149
+ var direction = 1;
150
+ if (anticlockwise) {
151
+ direction = -1;
152
+ diff = TAU - diff;
153
+ }
154
+
155
+ if (fullCircle) diff = TAU;
156
+
157
+ var length = diff * radius;
158
+ var nr_of_interpolation_points = length / this.arcResolution;
159
+ var dangle = diff / nr_of_interpolation_points;
160
+
161
+ var angle = startAngle;
162
+ for (var j = 0; j < nr_of_interpolation_points; j++) {
163
+ points.push(pool.pull("Vector2d", x + radius * Math.cos(angle), y + radius * Math.sin(angle)));
164
+ angle += direction * dangle;
165
+ }
166
+ points.push(pool.pull("Vector2d", x + radius * Math.cos(endAngle), y + radius * Math.sin(endAngle)));
167
+ }
168
+
169
+ /**
170
+ * adds a circular arc to the path with the given control points and radius, connected to the previous point by a straight line.
171
+ * @name arcTo
172
+ * @memberof Path2D.prototype
173
+ * @function
174
+ * @param {number} x the x-axis coordinate of the first control point.
175
+ * @param {number} y the y-axis coordinate of the first control point.
176
+ * @param {number} x the x-axis coordinate of the second control point.
177
+ * @param {number} y the y-axis coordinate of the second control point.
178
+ * @param {number} radius the arc's radius. Must be positive.
179
+ */
180
+ arcTo(x1, y1, x2, y2, radius) {
181
+ var points = this.points;
182
+ // based on from https://github.com/karellodewijk/canvas-webgl/blob/master/canvas-webgl.js
183
+ var x0 = points[points.length-1].x, y0 = points[points.length-1].y;
184
+
185
+ //a = -incoming vector, b = outgoing vector to x1, y1
186
+ var a = [x0 - x1, y0 - y1];
187
+ var b = [x2 - x1, y2 - y1];
188
+
189
+ //normalize
190
+ var l_a = Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2));
191
+ var l_b = Math.sqrt(Math.pow(b[0], 2) + Math.pow(b[1], 2));
192
+ a[0] /= l_a; a[1] /= l_a; b[0] /= l_b; b[1] /= l_b;
193
+ var angle = Math.atan2(a[1], a[0]) - Math.atan2(b[1], b[0]);
194
+
195
+ //work out tangent points using tan(θ) = opposite / adjacent; angle/2 because hypotenuse is the bisection of a,b
196
+ var tan_angle_div2 = Math.tan(angle/2);
197
+ var adj_l = (radius/tan_angle_div2);
198
+
199
+ var tangent_point1 = [x1 + a[0] * adj_l, y1 + a[1] * adj_l];
200
+ var tangent_point2 = [x1 + b[0] * adj_l, y1 + b[1] * adj_l];
201
+
202
+ points.push(pool.pull("Vector2d", tangent_point1[0], tangent_point1[1]));
203
+
204
+ var bisec = [(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0];
205
+ var bisec_l = Math.sqrt(Math.pow(bisec[0], 2) + Math.pow(bisec[1], 2));
206
+ bisec[0] /= bisec_l; bisec[1] /= bisec_l;
207
+
208
+ var hyp_l = Math.sqrt(Math.pow(radius, 2) + Math.pow(adj_l, 2));
209
+ var center = [x1 + hyp_l * bisec[0], y1 + hyp_l * bisec[1]];
210
+
211
+ var startAngle = Math.atan2(tangent_point1[1] - center[1], tangent_point1[0] - center[0]);
212
+ var endAngle = Math.atan2(tangent_point2[1] - center[1], tangent_point2[0] - center[0]);
213
+
214
+ this.arc(center[0], center[1], radius, startAngle, endAngle);
215
+ }
216
+
217
+ /**
218
+ * adds an elliptical arc to the path which is centered at (x, y) position with the radii radiusX and radiusY
219
+ * starting at startAngle and ending at endAngle going in the given direction by counterclockwise.
220
+ * @name ellipse
221
+ * @memberof Path2D.prototype
222
+ * @function
223
+ * @param {number} x the x-axis (horizontal) coordinate of the ellipse's center.
224
+ * @param {number} y the y-axis (vertical) coordinate of the ellipse's center.
225
+ * @param {number} radiusX the ellipse's major-axis radius. Must be non-negative.
226
+ * @param {number} radiusY the ellipse's minor-axis radius. Must be non-negative.
227
+ * @param {number} rotation the rotation of the ellipse, expressed in radians.
228
+ * @param {number} startAngle the angle at which the ellipse starts, measured clockwise from the positive x-axis and expressed in radians.
229
+ * @param {number} endAngle the angle at which the ellipse ends, measured clockwise from the positive x-axis and expressed in radians.
230
+ * @param {boolean} [anticlockwise=false] an optional boolean value which, if true, draws the ellipse counterclockwise (anticlockwise).
231
+ */
232
+ ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise = false) {
233
+ var points = this.points;
234
+ // based on from https://github.com/karellodewijk/canvas-webgl/blob/master/canvas-webgl.js
235
+ if (startAngle === endAngle) return;
236
+ var fullCircle = anticlockwise ? Math.abs(startAngle-endAngle) >= (TAU) : Math.abs(endAngle-startAngle) >= (TAU);
237
+
238
+ //bring angles all in [0, 2*PI] range
239
+ startAngle = startAngle % (TAU);
240
+ endAngle = endAngle % (TAU);
241
+ if (startAngle < 0) startAngle += TAU;
242
+ if (endAngle < 0) endAngle += TAU;
243
+
244
+ if (startAngle>=endAngle) {
245
+ endAngle += TAU;
246
+ }
247
+
248
+ var diff = endAngle - startAngle;
249
+
250
+ var direction = 1;
251
+ if (anticlockwise) {
252
+ direction = -1;
253
+ diff = TAU - diff;
254
+ }
255
+
256
+ if (fullCircle) diff = TAU;
257
+
258
+ var length = (diff * radiusX + diff * radiusY) / 2;
259
+ var nr_of_interpolation_points = length / this.arcResolution;
260
+ var dangle = diff / nr_of_interpolation_points;
261
+
262
+ var angle = startAngle;
263
+ var cos_rotation = Math.cos(rotation);
264
+ var sin_rotation = Math.sin(rotation);
265
+ for (var j = 0; j < nr_of_interpolation_points; j++) {
266
+ var _x1 = radiusX * Math.cos(angle);
267
+ var _y1 = radiusY * Math.sin(angle);
268
+ var _x2 = x + _x1 * cos_rotation - _y1 * sin_rotation;
269
+ var _y2 = y + _x1 * sin_rotation + _y1 * cos_rotation;
270
+ points.push(pool.pull("Vector2d", _x2, _y2));
271
+ angle += direction * dangle;
272
+ }
273
+ //var x1 = radiusX * Math.cos(endAngle);
274
+ //var y1 = radiusY * Math.sin(endAngle);
275
+ //points.push(pool.pull("Vector2d", x + x1 * cos_rotation - y1 * sin_rotation, y + x1 * sin_rotation + y1 * cos_rotation));
276
+ }
277
+
278
+ /**
279
+ * creates a path for a rectangle at position (x, y) with a size that is determined by width and height.
280
+ * @name rect
281
+ * @memberof Path2D.prototype
282
+ * @function
283
+ * @param {number} x the x-axis coordinate of the rectangle's starting point.
284
+ * @param {number} y the y-axis coordinate of the rectangle's starting point.
285
+ * @param {number} width the rectangle's width. Positive values are to the right, and negative to the left.
286
+ * @param {number} height the rectangle's height. Positive values are down, and negative are up.
287
+ */
288
+ rect(x, y, width, height) {
289
+ this.moveTo(x, y);
290
+ this.lineTo(x + width, y);
291
+ this.lineTo(x + width, y + height);
292
+ this.lineTo(x, y + height);
293
+ this.lineTo(x, y);
294
+ }
295
+
296
+ /**
297
+ * adds an rounded rectangle to the current path.
298
+ * @name roundRect
299
+ * @memberof Path2D.prototype
300
+ * @function
301
+ * @param {number} x the x-axis coordinate of the rectangle's starting point.
302
+ * @param {number} y the y-axis coordinate of the rectangle's starting point.
303
+ * @param {number} width the rectangle's width. Positive values are to the right, and negative to the left.
304
+ * @param {number} height the rectangle's height. Positive values are down, and negative are up.
305
+ * @param {number} radius the arc's radius to draw the borders. Must be positive.
306
+ */
307
+ roundRect(x, y, width, height, radius) {
308
+ this.moveTo(x + radius, y);
309
+ this.lineTo(x + width - radius, y);
310
+ this.arcTo(x + width, y, x + width, y + radius, radius);
311
+ this.lineTo(x + width, y + height - radius);
312
+ this.arcTo(x + width, y + height, x + width - radius, y + height, radius);
313
+ this.lineTo(x + radius, y + height);
314
+ this.arcTo(x, y + height, x, y + height - radius, radius);
315
+ this.lineTo(x, y + radius);
316
+ this.arcTo(x, y, x + radius, y, radius);
317
+ }
318
+ }
319
+ export default Path2D;
@@ -1,6 +1,6 @@
1
1
  import earcut from "earcut";
2
2
  import Vector2d from "./../math/vector2.js";
3
- import * as pool from "./../system/pooling.js";
3
+ import pool from "./../system/pooling.js";
4
4
 
5
5
  /**
6
6
  * @classdesc
@@ -25,16 +25,16 @@ class Polygon {
25
25
  * @public
26
26
  * @type {Vector2d}
27
27
  * @name pos
28
- * @memberof Polygon#
28
+ * @memberof Polygon.prototype
29
29
  */
30
- this.pos = new Vector2d();
30
+ this.pos = pool.pull("Vector2d");
31
31
 
32
32
  /**
33
33
  * The bounding rectangle for this shape
34
34
  * @ignore
35
35
  * @type {Bounds}
36
36
  * @name _bounds
37
- * @memberof Polygon#
37
+ * @memberof Polygon.prototype
38
38
  */
39
39
  this._bounds;
40
40
 
@@ -44,7 +44,7 @@ class Polygon {
44
44
  * @public
45
45
  * @type {Vector2d[]}
46
46
  * @name points
47
- * @memberof Polygon#
47
+ * @memberof Polygon.prototype
48
48
  */
49
49
  this.points = [];
50
50
 
@@ -117,13 +117,13 @@ class Polygon {
117
117
  if (typeof vertices[0] === "object") {
118
118
  // array of {x,y} object
119
119
  vertices.forEach((vertice) => {
120
- this.points.push(new Vector2d(vertice.x, vertice.y));
120
+ this.points.push(pool.pull("Vector2d", vertice.x, vertice.y));
121
121
  });
122
122
 
123
123
  } else {
124
124
  // it's a flat array
125
125
  for (var p = 0; p < vertices.length; p += 2) {
126
- this.points.push(new Vector2d(vertices[p], vertices[p + 1]));
126
+ this.points.push(pool.pull("Vector2d", vertices[p], vertices[p + 1]));
127
127
  }
128
128
  }
129
129
  } else {
@@ -258,12 +258,12 @@ class Polygon {
258
258
  // Calculate the edges/normals
259
259
  for (i = 0; i < len; i++) {
260
260
  if (edges[i] === undefined) {
261
- edges[i] = new Vector2d();
261
+ edges[i] = pool.pull("Vector2d");
262
262
  }
263
263
  edges[i].copy(points[(i + 1) % len]).sub(points[i]);
264
264
 
265
265
  if (normals[i] === undefined) {
266
- normals[i] = new Vector2d();
266
+ normals[i] = pool.pull("Vector2d");
267
267
  }
268
268
  normals[i].copy(edges[i]).perp().normalize();
269
269
  }
@@ -379,14 +379,14 @@ class Polygon {
379
379
  /**
380
380
  * Shifts the Polygon to the given position vector.
381
381
  * @name shift
382
- * @memberof Polygon
382
+ * @memberof Polygon.prototype
383
383
  * @function
384
384
  * @param {Vector2d} position
385
385
  */
386
386
  /**
387
387
  * Shifts the Polygon to the given x, y position.
388
388
  * @name shift
389
- * @memberof Polygon
389
+ * @memberof Polygon.prototype
390
390
  * @function
391
391
  * @param {number} x
392
392
  * @param {number} y