Skip to content

Commit a534629

Browse files
committed
Added toJSON functions to all Curve types and Path
1 parent 649fa88 commit a534629

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

v3/src/paths/Path.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ var Path = new Class({
8383
// function EllipseCurve (x, y, xRadius, yRadius, startAngle, endAngle, clockwise, rotation)
8484
// },
8585

86+
toJSON: function ()
87+
{
88+
var out = [];
89+
90+
for (var i = 0; i < this.curves.length; i++)
91+
{
92+
out.push(this.curves[i].toJSON());
93+
}
94+
95+
return {
96+
type: 'Path',
97+
x: this.startPoint.x,
98+
y: this.startPoint.y,
99+
autoClose: this.autoClose,
100+
curves: out
101+
}
102+
},
103+
86104
add: function (curve)
87105
{
88106
this.curves.push(curve);

v3/src/paths/curves/cubicbezier/CubicBezierCurve.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ var CubicBezierCurve = new Class({
7272

7373
// So you can chain graphics calls
7474
return graphics;
75+
},
76+
77+
toJSON: function ()
78+
{
79+
return {
80+
type: 'CubicBezierCurve',
81+
points: [
82+
this.p0.x, this.p0.y,
83+
this.p1.x, this.p1.y,
84+
this.p2.x, this.p2.y,
85+
this.p3.x, this.p3.y
86+
]
87+
};
7588
}
7689

7790
});

v3/src/paths/curves/ellipse/EllipseCurve.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
22

3-
var Curve = require('../Curve');
43
var Class = require('../../../utils/Class');
4+
var Curve = require('../Curve');
55
var DegToRad = require('../../../math/DegToRad');
6+
var RadToDeg = require('../../../math/RadToDeg');
67
var Vector2 = require('../../../math/Vector2');
78

89
// Phaser.Curves.Ellipse
@@ -114,6 +115,21 @@ var EllipseCurve = new Class({
114115
return out.set(x, y);
115116
},
116117

118+
toJSON: function ()
119+
{
120+
return {
121+
type: 'EllipseCurve',
122+
x: this.p0.x,
123+
y: this.p0.y,
124+
xRadius: this._xRadius,
125+
yRadius: this._yRadius,
126+
startAngle: RadToDeg(this._startAngle),
127+
endAngle: RadToDeg(this._endAngle),
128+
clockwise: this._clockwise,
129+
rotation: RadToDeg(this._rotation)
130+
};
131+
},
132+
117133
x: {
118134
get: function ()
119135
{

v3/src/paths/curves/line/LineCurve.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ var LineCurve = new Class({
7373

7474
// So you can chain graphics calls
7575
return graphics;
76+
},
77+
78+
toJSON: function ()
79+
{
80+
return {
81+
type: 'LineCurve',
82+
points: [
83+
this.p0.x, this.p0.y,
84+
this.p1.x, this.p1.y
85+
]
86+
};
7687
}
7788

7889
});

v3/src/paths/curves/spline/SplineCurve.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ var SplineCurve = new Class({
6060
var p3 = points[(intPoint > points.length - 3) ? points.length - 1 : intPoint + 2];
6161

6262
return out.set(CatmullRom(weight, p0.x, p1.x, p2.x, p3.x), CatmullRom(weight, p0.y, p1.y, p2.y, p3.y));
63+
},
64+
65+
toJSON: function ()
66+
{
67+
var points = [];
68+
69+
for (var i = 0; i < this.points.length; i++)
70+
{
71+
points.push(this.points[i].x);
72+
points.push(this.points[i].y);
73+
}
74+
75+
return {
76+
type: 'SplineCurve',
77+
points: points
78+
};
6379
}
6480

6581
});

0 commit comments

Comments
 (0)