@vpmedia/phaser 1.0.1 → 1.0.2

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 (107) hide show
  1. package/dist/phaser.cjs.LICENSE.txt +1 -1
  2. package/dist/phaser.js.LICENSE.txt +1 -1
  3. package/package.json +2 -3
  4. package/src/index.js +99 -0
  5. package/src/phaser/core/animation.js +355 -0
  6. package/src/phaser/core/animation_manager.js +238 -0
  7. package/src/phaser/core/animation_parser.js +130 -0
  8. package/src/phaser/core/array_set.js +108 -0
  9. package/src/phaser/core/cache.js +558 -0
  10. package/src/phaser/core/const.js +106 -0
  11. package/src/phaser/core/device.js +67 -0
  12. package/src/phaser/core/device_util.js +386 -0
  13. package/src/phaser/core/dom.js +207 -0
  14. package/src/phaser/core/event_manager.js +243 -0
  15. package/src/phaser/core/factory.js +74 -0
  16. package/src/phaser/core/frame.js +75 -0
  17. package/src/phaser/core/frame_data.js +84 -0
  18. package/src/phaser/core/frame_util.js +31 -0
  19. package/src/phaser/core/game.js +412 -0
  20. package/src/phaser/core/input.js +401 -0
  21. package/src/phaser/core/input_button.js +102 -0
  22. package/src/phaser/core/input_handler.js +687 -0
  23. package/src/phaser/core/input_mouse.js +289 -0
  24. package/src/phaser/core/input_mspointer.js +197 -0
  25. package/src/phaser/core/input_pointer.js +427 -0
  26. package/src/phaser/core/input_touch.js +157 -0
  27. package/src/phaser/core/loader.js +946 -0
  28. package/src/phaser/core/loader_parser.js +105 -0
  29. package/src/phaser/core/raf.js +46 -0
  30. package/src/phaser/core/raf_fb.js +75 -0
  31. package/src/phaser/core/raf_to.js +34 -0
  32. package/src/phaser/core/scale_manager.js +806 -0
  33. package/src/phaser/core/scene.js +66 -0
  34. package/src/phaser/core/scene_manager.js +310 -0
  35. package/src/phaser/core/signal.js +175 -0
  36. package/src/phaser/core/signal_binding.js +69 -0
  37. package/src/phaser/core/sound.js +538 -0
  38. package/src/phaser/core/sound_manager.js +365 -0
  39. package/src/phaser/core/stage.js +108 -0
  40. package/src/phaser/core/time.js +203 -0
  41. package/src/phaser/core/timer.js +276 -0
  42. package/src/phaser/core/timer_event.js +21 -0
  43. package/src/phaser/core/tween.js +329 -0
  44. package/src/phaser/core/tween_data.js +258 -0
  45. package/src/phaser/core/tween_easing.js +316 -0
  46. package/src/phaser/core/tween_manager.js +185 -0
  47. package/src/phaser/core/world.js +18 -0
  48. package/src/phaser/display/bitmap_text.js +322 -0
  49. package/src/phaser/display/button.js +194 -0
  50. package/src/phaser/display/canvas/buffer.js +36 -0
  51. package/src/phaser/display/canvas/graphics.js +227 -0
  52. package/src/phaser/display/canvas/masker.js +39 -0
  53. package/src/phaser/display/canvas/pool.js +121 -0
  54. package/src/phaser/display/canvas/renderer.js +123 -0
  55. package/src/phaser/display/canvas/tinter.js +141 -0
  56. package/src/phaser/display/canvas/util.js +151 -0
  57. package/src/phaser/display/display_object.js +597 -0
  58. package/src/phaser/display/graphics.js +723 -0
  59. package/src/phaser/display/graphics_data.js +27 -0
  60. package/src/phaser/display/graphics_data_util.js +14 -0
  61. package/src/phaser/display/group.js +227 -0
  62. package/src/phaser/display/image.js +288 -0
  63. package/src/phaser/display/sprite_batch.js +15 -0
  64. package/src/phaser/display/sprite_util.js +248 -0
  65. package/src/phaser/display/text.js +1089 -0
  66. package/src/phaser/display/webgl/abstract_filter.js +25 -0
  67. package/src/phaser/display/webgl/base_texture.js +68 -0
  68. package/src/phaser/display/webgl/blend_manager.js +35 -0
  69. package/src/phaser/display/webgl/earcut.js +647 -0
  70. package/src/phaser/display/webgl/earcut_node.js +28 -0
  71. package/src/phaser/display/webgl/fast_sprite_batch.js +242 -0
  72. package/src/phaser/display/webgl/filter_manager.js +46 -0
  73. package/src/phaser/display/webgl/filter_texture.js +61 -0
  74. package/src/phaser/display/webgl/graphics.js +618 -0
  75. package/src/phaser/display/webgl/graphics_data.js +42 -0
  76. package/src/phaser/display/webgl/mask_manager.js +36 -0
  77. package/src/phaser/display/webgl/render_texture.js +81 -0
  78. package/src/phaser/display/webgl/renderer.js +234 -0
  79. package/src/phaser/display/webgl/shader/complex.js +74 -0
  80. package/src/phaser/display/webgl/shader/fast.js +97 -0
  81. package/src/phaser/display/webgl/shader/normal.js +225 -0
  82. package/src/phaser/display/webgl/shader/primitive.js +72 -0
  83. package/src/phaser/display/webgl/shader/strip.js +77 -0
  84. package/src/phaser/display/webgl/shader_manager.js +89 -0
  85. package/src/phaser/display/webgl/sprite_batch.js +320 -0
  86. package/src/phaser/display/webgl/stencil_manager.js +170 -0
  87. package/src/phaser/display/webgl/texture.js +117 -0
  88. package/src/phaser/display/webgl/texture_util.js +32 -0
  89. package/src/phaser/display/webgl/util.js +74 -0
  90. package/src/phaser/geom/circle.js +186 -0
  91. package/src/phaser/geom/ellipse.js +65 -0
  92. package/src/phaser/geom/line.js +190 -0
  93. package/src/phaser/geom/matrix.js +147 -0
  94. package/src/phaser/geom/point.js +164 -0
  95. package/src/phaser/geom/polygon.js +141 -0
  96. package/src/phaser/geom/rectangle.js +306 -0
  97. package/src/phaser/geom/rounded_rectangle.js +36 -0
  98. package/src/phaser/geom/util/circle.js +115 -0
  99. package/src/phaser/geom/util/ellipse.js +30 -0
  100. package/src/phaser/geom/util/line.js +130 -0
  101. package/src/phaser/geom/util/matrix.js +48 -0
  102. package/src/phaser/geom/util/point.js +276 -0
  103. package/src/phaser/geom/util/polygon.js +24 -0
  104. package/src/phaser/geom/util/rectangle.js +212 -0
  105. package/src/phaser/geom/util/rounded_rectangle.js +28 -0
  106. package/src/phaser/util/math.js +279 -0
  107. package/src/phaser/util/string.js +26 -0
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @author Mat Groves http://matgroves.com/ @Doormat23
5
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
6
+ */
7
+
8
+ /**
9
+ *
10
+ */
11
+ export function initDefaultShaders() {
12
+ }
13
+
14
+ /**
15
+ *
16
+ * @param gl
17
+ * @param shaderSrc
18
+ * @param shaderType
19
+ */
20
+ export function compileShader(gl, shaderSrc, shaderType) {
21
+ let src = shaderSrc;
22
+ if (Array.isArray(shaderSrc)) {
23
+ src = shaderSrc.join('\n');
24
+ }
25
+ const shader = gl.createShader(shaderType);
26
+ gl.shaderSource(shader, src);
27
+ gl.compileShader(shader);
28
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
29
+ console.log(gl.getShaderInfoLog(shader));
30
+ return null;
31
+ }
32
+ return shader;
33
+ }
34
+
35
+ /**
36
+ *
37
+ * @param gl
38
+ * @param shaderSrc
39
+ */
40
+ export function compileVertexShader(gl, shaderSrc) {
41
+ return compileShader(gl, shaderSrc, gl.VERTEX_SHADER);
42
+ }
43
+
44
+ /**
45
+ *
46
+ * @param gl
47
+ * @param shaderSrc
48
+ */
49
+ export function compileFragmentShader(gl, shaderSrc) {
50
+ return compileShader(gl, shaderSrc, gl.FRAGMENT_SHADER);
51
+ }
52
+
53
+ /**
54
+ *
55
+ * @param gl
56
+ * @param vertexSrc
57
+ * @param fragmentSrc
58
+ */
59
+ export function compileProgram(gl, vertexSrc, fragmentSrc) {
60
+ const fragmentShader = compileFragmentShader(gl, fragmentSrc);
61
+ const vertexShader = compileVertexShader(gl, vertexSrc);
62
+
63
+ const shaderProgram = gl.createProgram();
64
+
65
+ gl.attachShader(shaderProgram, vertexShader);
66
+ gl.attachShader(shaderProgram, fragmentShader);
67
+ gl.linkProgram(shaderProgram);
68
+
69
+ if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
70
+ console.log(gl.getProgramInfoLog(shaderProgram));
71
+ console.log('Could not initialise shaders');
72
+ }
73
+ return shaderProgram;
74
+ }
@@ -0,0 +1,186 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Point from './point';
7
+ import Rectangle from './rectangle';
8
+ import { distance } from '../util/math';
9
+ import { clone, contains, circumferencePoint } from './util/circle';
10
+ import { GEOM_CIRCLE } from '../core/const';
11
+
12
+ export default class {
13
+
14
+ constructor(x = 0, y = 0, diameter = 0) {
15
+ this.x = x;
16
+ this.y = y;
17
+ this._diameter = diameter;
18
+ this._radius = 0;
19
+ if (diameter > 0) {
20
+ this._radius = diameter * 0.5;
21
+ }
22
+ this.type = GEOM_CIRCLE;
23
+ }
24
+
25
+ circumference() {
26
+ return 2 * (Math.PI * this._radius);
27
+ }
28
+
29
+ random(output = null) {
30
+ const result = output || new Point();
31
+ const t = 2 * Math.PI * Math.random();
32
+ const u = Math.random() + Math.random();
33
+ const r = (u > 1) ? 2 - u : u;
34
+ const x = r * Math.cos(t);
35
+ const y = r * Math.sin(t);
36
+ result.x = this.x + (x * this.radius);
37
+ result.y = this.y + (y * this.radius);
38
+ return result;
39
+ }
40
+
41
+ getBounds() {
42
+ return new Rectangle(this.x - this.radius, this.y - this.radius, this.diameter, this.diameter);
43
+ }
44
+
45
+ setTo(x, y, diameter) {
46
+ this.x = x;
47
+ this.y = y;
48
+ this._diameter = diameter;
49
+ this._radius = diameter * 0.5;
50
+ return this;
51
+ }
52
+
53
+ copyFrom(source) {
54
+ return this.setTo(source.x, source.y, source.diameter);
55
+ }
56
+
57
+ copyTo(dest) {
58
+ dest.x = this.x;
59
+ dest.y = this.y;
60
+ dest.diameter = this._diameter;
61
+ return dest;
62
+ }
63
+
64
+ distance(dest, round = false) {
65
+ const d = distance(this.x, this.y, dest.x, dest.y);
66
+ return round ? Math.round(d) : d;
67
+ }
68
+
69
+ clone() {
70
+ return clone(this);
71
+ }
72
+
73
+ contains(x, y) {
74
+ return contains(this, x, y);
75
+ }
76
+
77
+ circumferencePoint(angle, asDegrees, out) {
78
+ return circumferencePoint(this, angle, asDegrees, out);
79
+ }
80
+
81
+ offset(dx, dy) {
82
+ this.x += dx;
83
+ this.y += dy;
84
+ return this;
85
+ }
86
+
87
+ offsetPoint(point) {
88
+ return this.offset(point.x, point.y);
89
+ }
90
+
91
+ toString() {
92
+ return '[{Circle (x=' + this.x + ' y=' + this.y + ' diameter=' + this.diameter + ' radius=' + this.radius + ')}]';
93
+ }
94
+
95
+ get diameter() {
96
+ return this._diameter;
97
+ }
98
+
99
+ set diameter(value) {
100
+ if (value > 0) {
101
+ this._diameter = value;
102
+ this._radius = value * 0.5;
103
+ }
104
+ }
105
+
106
+ get radius() {
107
+ return this._radius;
108
+ }
109
+
110
+ set radius(value) {
111
+ if (value > 0) {
112
+ this._radius = value;
113
+ this._diameter = value * 2;
114
+ }
115
+ }
116
+
117
+ get left() {
118
+ return this.x - this._radius;
119
+ }
120
+
121
+ set left(value) {
122
+ if (value > this.x) {
123
+ this._radius = 0;
124
+ this._diameter = 0;
125
+ } else {
126
+ this.radius = this.x - value;
127
+ }
128
+ }
129
+
130
+ get right() {
131
+ return this.x + this._radius;
132
+ }
133
+
134
+ set right(value) {
135
+ if (value < this.x) {
136
+ this._radius = 0;
137
+ this._diameter = 0;
138
+ } else {
139
+ this.radius = value - this.x;
140
+ }
141
+ }
142
+
143
+ get top() {
144
+ return this.y - this._radius;
145
+ }
146
+
147
+ set top(value) {
148
+ if (value > this.y) {
149
+ this._radius = 0;
150
+ this._diameter = 0;
151
+ } else {
152
+ this.radius = this.y - value;
153
+ }
154
+ }
155
+
156
+ get bottom() {
157
+ return this.y + this._radius;
158
+ }
159
+
160
+ set bottom(value) {
161
+ if (value < this.y) {
162
+ this._radius = 0;
163
+ this._diameter = 0;
164
+ } else {
165
+ this.radius = value - this.y;
166
+ }
167
+ }
168
+
169
+ get area() {
170
+ if (this._radius > 0) {
171
+ return Math.PI * this._radius * this._radius;
172
+ }
173
+ return 0;
174
+ }
175
+
176
+ get empty() {
177
+ return (this._diameter === 0);
178
+ }
179
+
180
+ set empty(value) {
181
+ if (value === true) {
182
+ this.setTo(0, 0, 0);
183
+ }
184
+ }
185
+
186
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Point from './point';
7
+ import Rectangle from './rectangle';
8
+ import { contains } from './util/ellipse';
9
+ import { GEOM_ELLIPSE } from '../core/const';
10
+
11
+ export default class {
12
+
13
+ constructor(x = 0, y = 0, width = 0, height = 0) {
14
+ this.x = x;
15
+ this.y = y;
16
+ this.width = width;
17
+ this.height = height;
18
+ this.type = GEOM_ELLIPSE;
19
+ }
20
+
21
+ setTo(x, y, width, height) {
22
+ this.x = x;
23
+ this.y = y;
24
+ this.width = width;
25
+ this.height = height;
26
+ return this;
27
+ }
28
+
29
+ getBounds() {
30
+ return new Rectangle(this.x - this.width, this.y - this.height, this.width, this.height);
31
+ }
32
+
33
+ copyFrom(source) {
34
+ return this.setTo(source.x, source.y, source.width, source.height);
35
+ }
36
+
37
+ copyTo(dest) {
38
+ dest.x = this.x;
39
+ dest.y = this.y;
40
+ dest.width = this.width;
41
+ dest.height = this.height;
42
+ return dest;
43
+ }
44
+
45
+ contains(x, y) {
46
+ return contains(this, x, y);
47
+ }
48
+
49
+ random(output = null) {
50
+ const result = output || new Point();
51
+ const p = Math.random() * Math.PI * 2;
52
+ const r = Math.random();
53
+ result.x = Math.sqrt(r) * Math.cos(p);
54
+ result.y = Math.sqrt(r) * Math.sin(p);
55
+ result.x = this.x + (result.x * this.width / 2.0);
56
+ result.y = this.y + (result.y * this.height / 2.0);
57
+ return result;
58
+ }
59
+
60
+ toString() {
61
+ return '[{Ellipse (x=' + this.x + ' y=' + this.y + ' width=' + this.width + ' height=' + this.height + ')}]';
62
+ }
63
+
64
+
65
+ }
@@ -0,0 +1,190 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Point from './point';
7
+ import { clone, intersectsPoints, reflect } from './util/line';
8
+ import { wrap } from '../util/math';
9
+ import { GEOM_LINE } from '../core/const';
10
+
11
+ export default class {
12
+
13
+ constructor(x1 = 0, y1 = 0, x2 = 0, y2 = 0) {
14
+ this.start = new Point(x1, y1);
15
+ this.end = new Point(x2, y2);
16
+ this.type = GEOM_LINE;
17
+ }
18
+
19
+ setTo(x1, y1, x2, y2) {
20
+ this.start.setTo(x1, y1);
21
+ this.end.setTo(x2, y2);
22
+ return this;
23
+ }
24
+
25
+ fromSprite(startSprite, endSprite, useCenter = false) {
26
+ if (useCenter) {
27
+ return this.setTo(startSprite.center.x, startSprite.center.y, endSprite.center.x, endSprite.center.y);
28
+ }
29
+ return this.setTo(startSprite.x, startSprite.y, endSprite.x, endSprite.y);
30
+ }
31
+
32
+ fromAngle(x, y, angle, length) {
33
+ this.start.setTo(x, y);
34
+ this.end.setTo(x + (Math.cos(angle) * length), y + (Math.sin(angle) * length));
35
+ return this;
36
+ }
37
+
38
+ rotate(angle, asDegrees = false) {
39
+ const cx = (this.start.x + this.end.x) / 2;
40
+ const cy = (this.start.y + this.end.y) / 2;
41
+ this.start.rotate(cx, cy, angle, asDegrees);
42
+ this.end.rotate(cx, cy, angle, asDegrees);
43
+ return this;
44
+ }
45
+
46
+ rotateAround(x, y, angle, asDegrees = false) {
47
+ this.start.rotate(x, y, angle, asDegrees);
48
+ this.end.rotate(x, y, angle, asDegrees);
49
+ return this;
50
+ }
51
+
52
+ intersects(line, asSegment, result) {
53
+ return intersectsPoints(this.start, this.end, line.start, line.end, asSegment, result);
54
+ }
55
+
56
+ reflect(line) {
57
+ return reflect(this, line);
58
+ }
59
+
60
+ midPoint(output = null) {
61
+ const result = output || new Point();
62
+ result.x = (this.start.x + this.end.x) / 2;
63
+ result.y = (this.start.y + this.end.y) / 2;
64
+ return result;
65
+ }
66
+
67
+ centerOn(x, y) {
68
+ const cx = (this.start.x + this.end.x) / 2;
69
+ const cy = (this.start.y + this.end.y) / 2;
70
+ const tx = x - cx;
71
+ const ty = y - cy;
72
+ this.start.add(tx, ty);
73
+ this.end.add(tx, ty);
74
+ }
75
+
76
+ pointOnLine(x, y) {
77
+ return ((x - this.start.x) * (this.end.y - this.start.y) === (this.end.x - this.start.x) * (y - this.start.y));
78
+ }
79
+
80
+ pointOnSegment(x, y) {
81
+ const xMin = Math.min(this.start.x, this.end.x);
82
+ const xMax = Math.max(this.start.x, this.end.x);
83
+ const yMin = Math.min(this.start.y, this.end.y);
84
+ const yMax = Math.max(this.start.y, this.end.y);
85
+ return (this.pointOnLine(x, y) && (x >= xMin && x <= xMax) && (y >= yMin && y <= yMax));
86
+ }
87
+
88
+ random(output = null) {
89
+ const result = output || new Point();
90
+ const t = Math.random();
91
+ result.x = this.start.x + t * (this.end.x - this.start.x);
92
+ result.y = this.start.y + t * (this.end.y - this.start.y);
93
+ return result;
94
+ }
95
+
96
+ coordinatesOnLine(stepRate = 1, results = []) {
97
+ let x1 = Math.round(this.start.x);
98
+ let y1 = Math.round(this.start.y);
99
+ const x2 = Math.round(this.end.x);
100
+ const y2 = Math.round(this.end.y);
101
+ const dx = Math.abs(x2 - x1);
102
+ const dy = Math.abs(y2 - y1);
103
+ const sx = (x1 < x2) ? 1 : -1;
104
+ const sy = (y1 < y2) ? 1 : -1;
105
+ let err = dx - dy;
106
+ results.push([x1, y1]);
107
+ let i = 1;
108
+ while (!((x1 === x2) && (y1 === y2))) {
109
+ const e2 = err << 1;
110
+ if (e2 > -dy) {
111
+ err -= dy;
112
+ x1 += sx;
113
+ }
114
+ if (e2 < dx) {
115
+ err += dx;
116
+ y1 += sy;
117
+ }
118
+ if (i % stepRate === 0) {
119
+ results.push([x1, y1]);
120
+ }
121
+ i += 1;
122
+ }
123
+ return results;
124
+ }
125
+
126
+ clone() {
127
+ return clone(this);
128
+ }
129
+
130
+ get length() {
131
+ return Math.sqrt((this.end.x - this.start.x) * (this.end.x - this.start.x) + (this.end.y - this.start.y) * (this.end.y - this.start.y));
132
+ }
133
+
134
+ get angle() {
135
+ return Math.atan2(this.end.y - this.start.y, this.end.x - this.start.x);
136
+ }
137
+
138
+ get slope() {
139
+ return (this.end.y - this.start.y) / (this.end.x - this.start.x);
140
+ }
141
+
142
+ get perpSlope() {
143
+ return -((this.end.x - this.start.x) / (this.end.y - this.start.y));
144
+ }
145
+
146
+ get x() {
147
+ return Math.min(this.start.x, this.end.x);
148
+ }
149
+
150
+ get y() {
151
+ return Math.min(this.start.y, this.end.y);
152
+ }
153
+
154
+ get left() {
155
+ return Math.min(this.start.x, this.end.x);
156
+ }
157
+
158
+ get right() {
159
+ return Math.max(this.start.x, this.end.x);
160
+ }
161
+
162
+ get top() {
163
+ return Math.min(this.start.y, this.end.y);
164
+ }
165
+
166
+ get bottom() {
167
+ return Math.max(this.start.y, this.end.y);
168
+ }
169
+
170
+ get width() {
171
+ return Math.abs(this.start.x - this.end.x);
172
+ }
173
+
174
+ get height() {
175
+ return Math.abs(this.start.y - this.end.y);
176
+ }
177
+
178
+ get normalX() {
179
+ return Math.cos(this.angle - 1.5707963267948966);
180
+ }
181
+
182
+ get normalY() {
183
+ return Math.sin(this.angle - 1.5707963267948966);
184
+ }
185
+
186
+ get normalAngle() {
187
+ return wrap(this.angle - 1.5707963267948966, -Math.PI, Math.PI);
188
+ }
189
+
190
+ }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Point from './point';
7
+ import { clone } from './util/matrix';
8
+ import { GEOM_MATRIX } from '../core/const';
9
+
10
+ export default class {
11
+
12
+ constructor(a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0) {
13
+ this.a = a;
14
+ this.b = b;
15
+ this.c = c;
16
+ this.d = d;
17
+ this.tx = tx;
18
+ this.ty = ty;
19
+ this.type = GEOM_MATRIX;
20
+ }
21
+
22
+ fromArray(array) {
23
+ return this.setTo(array[0], array[1], array[3], array[4], array[2], array[5]);
24
+ }
25
+
26
+ setTo(a, b, c, d, tx, ty) {
27
+ this.a = a;
28
+ this.b = b;
29
+ this.c = c;
30
+ this.d = d;
31
+ this.tx = tx;
32
+ this.ty = ty;
33
+ return this;
34
+ }
35
+
36
+ clone() {
37
+ return clone(this);
38
+ }
39
+
40
+ copyTo(matrix) {
41
+ matrix.copyFrom(this);
42
+ return matrix;
43
+ }
44
+
45
+ copyFrom(matrix) {
46
+ this.a = matrix.a;
47
+ this.b = matrix.b;
48
+ this.c = matrix.c;
49
+ this.d = matrix.d;
50
+ this.tx = matrix.tx;
51
+ this.ty = matrix.ty;
52
+ return this;
53
+ }
54
+
55
+ toArray(transpose = false, output = null) {
56
+ const result = output || new Float32Array(9);
57
+ if (transpose) {
58
+ result[0] = this.a;
59
+ result[1] = this.b;
60
+ result[2] = 0;
61
+ result[3] = this.c;
62
+ result[4] = this.d;
63
+ result[5] = 0;
64
+ result[6] = this.tx;
65
+ result[7] = this.ty;
66
+ result[8] = 1;
67
+ } else {
68
+ result[0] = this.a;
69
+ result[1] = this.c;
70
+ result[2] = this.tx;
71
+ result[3] = this.b;
72
+ result[4] = this.d;
73
+ result[5] = this.ty;
74
+ result[6] = 0;
75
+ result[7] = 0;
76
+ result[8] = 1;
77
+ }
78
+ return result;
79
+ }
80
+
81
+ apply(pos, output = null) {
82
+ const result = output || new Point();
83
+ result.x = this.a * pos.x + this.c * pos.y + this.tx;
84
+ result.y = this.b * pos.x + this.d * pos.y + this.ty;
85
+ return result;
86
+ }
87
+
88
+ applyInverse(pos, output = null) {
89
+ const result = output || new Point();
90
+ const id = 1 / (this.a * this.d + this.c * -this.b);
91
+ const x = pos.x;
92
+ const y = pos.y;
93
+ result.x = this.d * id * x + -this.c * id * y + (this.ty * this.c - this.tx * this.d) * id;
94
+ result.y = this.a * id * y + -this.b * id * x + (-this.ty * this.a + this.tx * this.b) * id;
95
+ return result;
96
+ }
97
+
98
+ translate(x, y) {
99
+ this.tx += x;
100
+ this.ty += y;
101
+ return this;
102
+ }
103
+
104
+ scale(x, y) {
105
+ this.a *= x;
106
+ this.d *= y;
107
+ this.c *= x;
108
+ this.b *= y;
109
+ this.tx *= x;
110
+ this.ty *= y;
111
+ return this;
112
+ }
113
+
114
+ rotate(angle) {
115
+ const cos = Math.cos(angle);
116
+ const sin = Math.sin(angle);
117
+ const a1 = this.a;
118
+ const c1 = this.c;
119
+ const tx1 = this.tx;
120
+ this.a = a1 * cos - this.b * sin;
121
+ this.b = a1 * sin + this.b * cos;
122
+ this.c = c1 * cos - this.d * sin;
123
+ this.d = c1 * sin + this.d * cos;
124
+ this.tx = tx1 * cos - this.ty * sin;
125
+ this.ty = tx1 * sin + this.ty * cos;
126
+ return this;
127
+ }
128
+
129
+ append(matrix) {
130
+ const a1 = this.a;
131
+ const b1 = this.b;
132
+ const c1 = this.c;
133
+ const d1 = this.d;
134
+ this.a = matrix.a * a1 + matrix.b * c1;
135
+ this.b = matrix.a * b1 + matrix.b * d1;
136
+ this.c = matrix.c * a1 + matrix.d * c1;
137
+ this.d = matrix.c * b1 + matrix.d * d1;
138
+ this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx;
139
+ this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty;
140
+ return this;
141
+ }
142
+
143
+ identity() {
144
+ return this.setTo(1, 0, 0, 1, 0, 0);
145
+ }
146
+
147
+ }