melonjs 10.7.0 → 10.9.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.
- package/dist/melonjs.js +1488 -666
- package/dist/melonjs.min.js +4 -4
- package/dist/melonjs.module.d.ts +929 -202
- package/dist/melonjs.module.js +1575 -777
- package/package.json +9 -9
- package/src/camera/camera2d.js +1 -1
- package/src/entity/entity.js +6 -7
- package/src/geometries/ellipse.js +10 -11
- package/src/geometries/line.js +3 -3
- package/src/geometries/path2d.js +319 -0
- package/src/geometries/poly.js +11 -11
- package/src/geometries/rectangle.js +15 -15
- package/src/geometries/roundrect.js +164 -0
- package/src/index.js +5 -1
- package/src/input/gamepad.js +2 -2
- package/src/input/pointerevent.js +1 -1
- package/src/lang/deprecated.js +1 -1
- package/src/level/tiled/TMXLayer.js +1 -1
- package/src/level/tiled/TMXObject.js +9 -12
- package/src/level/tiled/TMXTileMap.js +23 -4
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
- package/src/loader/loader.js +4 -4
- package/src/loader/loadingscreen.js +1 -1
- package/src/math/color.js +1 -1
- package/src/math/matrix2.js +1 -1
- package/src/math/matrix3.js +1 -1
- package/src/math/observable_vector2.js +1 -1
- package/src/math/observable_vector3.js +1 -1
- package/src/math/vector2.js +1 -1
- package/src/math/vector3.js +1 -1
- package/src/particles/emitter.js +23 -14
- package/src/particles/particle.js +3 -2
- package/src/physics/body.js +67 -51
- package/src/physics/bounds.js +8 -9
- package/src/physics/world.js +1 -1
- package/src/polyfill/index.js +1 -0
- package/src/polyfill/roundrect.js +235 -0
- package/src/renderable/collectable.js +9 -2
- package/src/renderable/colorlayer.js +1 -1
- package/src/renderable/container.js +1 -1
- package/src/renderable/imagelayer.js +1 -1
- package/src/renderable/renderable.js +2 -2
- package/src/renderable/sprite.js +2 -3
- package/src/renderable/trigger.js +10 -4
- package/src/state/stage.js +1 -1
- package/src/state/state.js +1 -1
- package/src/system/device.js +10 -8
- package/src/system/pooling.js +156 -149
- package/src/text/bitmaptext.js +1 -1
- package/src/text/text.js +1 -1
- package/src/utils/utils.js +2 -2
- package/src/video/canvas/canvas_renderer.js +83 -39
- package/src/video/renderer.js +36 -16
- package/src/video/texture.js +1 -1
- package/src/video/webgl/glshader.js +29 -193
- package/src/video/webgl/utils/attributes.js +16 -0
- package/src/video/webgl/utils/precision.js +11 -0
- package/src/video/webgl/utils/program.js +58 -0
- package/src/video/webgl/utils/string.js +16 -0
- package/src/video/webgl/utils/uniforms.js +87 -0
- package/src/video/webgl/webgl_compositor.js +1 -14
- package/src/video/webgl/webgl_renderer.js +124 -182
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melonjs",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.9.0",
|
|
4
4
|
"description": "melonJS Game Engine",
|
|
5
5
|
"homepage": "http://www.melonjs.org/",
|
|
6
6
|
"keywords": [
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
],
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@teppeis/multimaps": "^2.0.0",
|
|
58
|
-
"core-js": "^3.22.
|
|
58
|
+
"core-js": "^3.22.5",
|
|
59
59
|
"earcut": "2.2.3",
|
|
60
60
|
"eventemitter3": "^4.0.7",
|
|
61
61
|
"howler": "2.2.3"
|
|
@@ -66,20 +66,20 @@
|
|
|
66
66
|
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
67
67
|
"@rollup/plugin-replace": "^4.0.0",
|
|
68
68
|
"@types/offscreencanvas": "^2019.6.4",
|
|
69
|
-
"cheerio": "^1.0.0-rc.
|
|
69
|
+
"cheerio": "^1.0.0-rc.11",
|
|
70
70
|
"del-cli": "^4.0.1",
|
|
71
|
-
"eslint": "^8.
|
|
72
|
-
"eslint-plugin-jsdoc": "^39.
|
|
73
|
-
"jasmine-core": "^4.1.
|
|
71
|
+
"eslint": "^8.16.0",
|
|
72
|
+
"eslint-plugin-jsdoc": "^39.3.0",
|
|
73
|
+
"jasmine-core": "^4.1.1",
|
|
74
74
|
"jsdoc": "^3.6.10",
|
|
75
|
-
"karma": "^6.3.
|
|
75
|
+
"karma": "^6.3.20",
|
|
76
76
|
"karma-chrome-launcher": "^3.1.1",
|
|
77
77
|
"karma-coverage": "^2.2.0",
|
|
78
78
|
"karma-html-detailed-reporter": "^2.1.0",
|
|
79
|
-
"karma-jasmine": "^5.0.
|
|
79
|
+
"karma-jasmine": "^5.0.1",
|
|
80
80
|
"karma-nyan-reporter": "0.2.5",
|
|
81
81
|
"qs": "^6.10.3",
|
|
82
|
-
"rollup": "^2.
|
|
82
|
+
"rollup": "^2.74.1",
|
|
83
83
|
"rollup-plugin-bundle-size": "^1.0.3",
|
|
84
84
|
"rollup-plugin-string": "^3.0.0",
|
|
85
85
|
"terser": "^5.13.1",
|
package/src/camera/camera2d.js
CHANGED
|
@@ -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
|
|
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";
|
package/src/entity/entity.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
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 =
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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));
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
64
|
+
this.ratio = pool.pull("Vector2d");
|
|
66
65
|
|
|
67
66
|
// the shape type
|
|
68
67
|
this.shapeType = "Ellipse";
|
package/src/geometries/line.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
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] =
|
|
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] =
|
|
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;
|
package/src/geometries/poly.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import earcut from "earcut";
|
|
2
2
|
import Vector2d from "./../math/vector2.js";
|
|
3
|
-
import
|
|
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 =
|
|
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(
|
|
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(
|
|
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] =
|
|
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] =
|
|
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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pool from "./../system/pooling.js";
|
|
2
2
|
import Polygon from "./poly.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -16,10 +16,10 @@ class Rect extends Polygon {
|
|
|
16
16
|
constructor(x, y, w, h) {
|
|
17
17
|
// parent constructor
|
|
18
18
|
super(x, y, [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
pool.pull("Vector2d", 0, 0), // 0, 0
|
|
20
|
+
pool.pull("Vector2d", w, 0), // 1, 0
|
|
21
|
+
pool.pull("Vector2d", w, h), // 1, 1
|
|
22
|
+
pool.pull("Vector2d", 0, h) // 0, 1
|
|
23
23
|
]);
|
|
24
24
|
this.shapeType = "Rectangle";
|
|
25
25
|
}
|
|
@@ -63,7 +63,7 @@ class Rect extends Polygon {
|
|
|
63
63
|
* @public
|
|
64
64
|
* @type {number}
|
|
65
65
|
* @name left
|
|
66
|
-
* @memberof Rect
|
|
66
|
+
* @memberof Rect.prototype
|
|
67
67
|
*/
|
|
68
68
|
get left() {
|
|
69
69
|
return this.pos.x;
|
|
@@ -74,7 +74,7 @@ class Rect extends Polygon {
|
|
|
74
74
|
* @public
|
|
75
75
|
* @type {number}
|
|
76
76
|
* @name right
|
|
77
|
-
* @memberof Rect
|
|
77
|
+
* @memberof Rect.prototype
|
|
78
78
|
*/
|
|
79
79
|
get right() {
|
|
80
80
|
var w = this.width;
|
|
@@ -86,7 +86,7 @@ class Rect extends Polygon {
|
|
|
86
86
|
* @public
|
|
87
87
|
* @type {number}
|
|
88
88
|
* @name top
|
|
89
|
-
* @memberof Rect
|
|
89
|
+
* @memberof Rect.prototype
|
|
90
90
|
*/
|
|
91
91
|
get top() {
|
|
92
92
|
return this.pos.y;
|
|
@@ -97,7 +97,7 @@ class Rect extends Polygon {
|
|
|
97
97
|
* @public
|
|
98
98
|
* @type {number}
|
|
99
99
|
* @name bottom
|
|
100
|
-
* @memberof Rect
|
|
100
|
+
* @memberof Rect.prototype
|
|
101
101
|
*/
|
|
102
102
|
get bottom() {
|
|
103
103
|
var h = this.height;
|
|
@@ -109,7 +109,7 @@ class Rect extends Polygon {
|
|
|
109
109
|
* @public
|
|
110
110
|
* @type {number}
|
|
111
111
|
* @name width
|
|
112
|
-
* @memberof Rect
|
|
112
|
+
* @memberof Rect.prototype
|
|
113
113
|
*/
|
|
114
114
|
get width() {
|
|
115
115
|
return this.points[2].x;
|
|
@@ -125,7 +125,7 @@ class Rect extends Polygon {
|
|
|
125
125
|
* @public
|
|
126
126
|
* @type {number}
|
|
127
127
|
* @name height
|
|
128
|
-
* @memberof Rect
|
|
128
|
+
* @memberof Rect.prototype
|
|
129
129
|
*/
|
|
130
130
|
get height() {
|
|
131
131
|
return this.points[2].y;
|
|
@@ -141,7 +141,7 @@ class Rect extends Polygon {
|
|
|
141
141
|
* @public
|
|
142
142
|
* @type {number}
|
|
143
143
|
* @name centerX
|
|
144
|
-
* @memberof Rect
|
|
144
|
+
* @memberof Rect.prototype
|
|
145
145
|
*/
|
|
146
146
|
get centerX() {
|
|
147
147
|
if (isFinite(this.width)) {
|
|
@@ -159,7 +159,7 @@ class Rect extends Polygon {
|
|
|
159
159
|
* @public
|
|
160
160
|
* @type {number}
|
|
161
161
|
* @name centerY
|
|
162
|
-
* @memberof Rect
|
|
162
|
+
* @memberof Rect.prototype
|
|
163
163
|
*/
|
|
164
164
|
get centerY() {
|
|
165
165
|
if (isFinite(this.height)) {
|
|
@@ -301,7 +301,7 @@ class Rect extends Polygon {
|
|
|
301
301
|
/**
|
|
302
302
|
* Returns true if the rectangle contains the given point
|
|
303
303
|
* @name contains
|
|
304
|
-
* @memberof Rect
|
|
304
|
+
* @memberof Rect.prototype
|
|
305
305
|
* @function
|
|
306
306
|
* @param {Vector2d} point
|
|
307
307
|
* @returns {boolean} true if contains
|
|
@@ -370,7 +370,7 @@ class Rect extends Polygon {
|
|
|
370
370
|
* @returns {Polygon} a new Polygon that represents this rectangle.
|
|
371
371
|
*/
|
|
372
372
|
toPolygon() {
|
|
373
|
-
return
|
|
373
|
+
return pool.pull("Polygon",
|
|
374
374
|
this.pos.x, this.pos.y, this.points
|
|
375
375
|
);
|
|
376
376
|
}
|