Skip to content

Commit 1b3a45b

Browse files
authored
Merge pull request #741 from jonobr1/737-from-object
Added fromObject and copy methods to all Two.Elements and descendants
2 parents ec247ac + 36ac68f commit 1b3a45b

File tree

61 files changed

+19248
-13559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+19248
-13559
lines changed

build/two.js

Lines changed: 6385 additions & 5872 deletions
Large diffs are not rendered by default.

build/two.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/two.module.js

Lines changed: 6217 additions & 5687 deletions
Large diffs are not rendered by default.

src/anchor.js

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Commands } from './utils/path-commands.js';
22
import { Events } from './events.js';
33
import { Vector } from './vector.js';
4+
import { toFixed } from './utils/math.js';
45

56
/**
67
* @class
@@ -16,10 +17,9 @@ import { Vector } from './vector.js';
1617
* @description An object that holds 3 {@link Two.Vector}s, the anchor point and its corresponding handles: `left` and `right`. In order to properly describe the bezier curve about the point there is also a command property to describe what type of drawing should occur when Two.js renders the anchors.
1718
*/
1819
export class Anchor extends Vector {
19-
2020
controls = {
2121
left: new Vector(),
22-
right: new Vector()
22+
right: new Vector(),
2323
};
2424
_command = Commands.move;
2525
_relative = true;
@@ -30,8 +30,15 @@ export class Anchor extends Vector {
3030
_largeArcFlag = 0;
3131
_sweepFlag = 1;
3232

33-
constructor(x = 0, y = 0, ax = 0, ay = 0, bx = 0, by = 0, command = Commands.move) {
34-
33+
constructor(
34+
x = 0,
35+
y = 0,
36+
ax = 0,
37+
ay = 0,
38+
bx = 0,
39+
by = 0,
40+
command = Commands.move
41+
) {
3542
super(x, y);
3643

3744
for (let prop in proto) {
@@ -43,11 +50,12 @@ export class Anchor extends Vector {
4350

4451
const broadcast = Anchor.makeBroadcast(this);
4552

46-
this.controls.left.set(ax, ay)
53+
this.controls.left
54+
.set(ax, ay)
4755
.addEventListener(Events.Types.change, broadcast);
48-
this.controls.right.set(bx, by)
56+
this.controls.right
57+
.set(bx, by)
4958
.addEventListener(Events.Types.change, broadcast);
50-
5159
}
5260

5361
static makeBroadcast(scope) {
@@ -59,14 +67,25 @@ export class Anchor extends Vector {
5967
}
6068
}
6169

70+
/**
71+
* @name Two.Anchor.fromObject
72+
* @function
73+
* @param {Object} obj - Object notation of a {@link Two.Anchor} to create a new instance
74+
* @returns {Two.Anchor}
75+
* @description Create a new {@link Two.Anchor} from an object notation of a {@link Two.Anchor}.
76+
* @nota-bene Works in conjunction with {@link Two.Anchor#toObject}
77+
*/
78+
static fromObject(obj) {
79+
return new Anchor().copy(obj);
80+
}
81+
6282
/**
6383
* @name Two.Anchor#copy
6484
* @function
6585
* @param {Two.Anchor} v - The anchor to apply values to.
6686
* @description Copy the properties of one {@link Two.Anchor} onto another.
6787
*/
6888
copy(v) {
69-
7089
this.x = v.x;
7190
this.y = v.y;
7291

@@ -104,7 +123,6 @@ export class Anchor extends Vector {
104123
}
105124

106125
return this;
107-
108126
}
109127

110128
/**
@@ -122,22 +140,23 @@ export class Anchor extends Vector {
122140
* @function
123141
* @returns {Object} - An object with properties filled out to mirror {@link Two.Anchor}.
124142
* @description Create a JSON compatible plain object of the current instance. Intended for use with storing values in a database.
143+
* @nota-bene Works in conjunction with {@link Two.Anchor.fromObject}
125144
*/
126145
toObject() {
127146
return {
128-
x: this.x,
129-
y: this.y,
147+
x: toFixed(this.x),
148+
y: toFixed(this.y),
130149
command: this.command,
131150
relative: this.relative,
132151
controls: {
133152
left: this.controls.left.toObject(),
134-
right: this.controls.right.toObject()
153+
right: this.controls.right.toObject(),
135154
},
136-
rx: this.rx,
137-
ry: this.ry,
138-
xAxisRotation: this.xAxisRotation,
139-
largeArcFlag: this.largeArcFlag,
140-
sweepFlag: this.sweepFlag
155+
rx: toFixed(this.rx),
156+
ry: toFixed(this.ry),
157+
xAxisRotation: toFixed(this.xAxisRotation),
158+
largeArcFlag: toFixed(this.largeArcFlag),
159+
sweepFlag: toFixed(this.sweepFlag),
141160
};
142161
}
143162

@@ -150,105 +169,104 @@ export class Anchor extends Vector {
150169
toString() {
151170
return JSON.stringify(this.toObject());
152171
}
153-
154172
}
155173

156174
const proto = {
157175
command: {
158176
enumerable: true,
159-
get: function() {
177+
get: function () {
160178
return this._command;
161179
},
162-
set: function(command) {
180+
set: function (command) {
163181
if (this._command !== command) {
164182
this._command = command;
165183
if (this._bound) {
166184
this.dispatchEvent(Events.Types.change);
167185
}
168186
}
169-
}
187+
},
170188
},
171189
relative: {
172190
enumerable: true,
173-
get: function() {
191+
get: function () {
174192
return this._relative;
175193
},
176-
set: function(relative) {
194+
set: function (relative) {
177195
if (this._relative !== !!relative) {
178196
this._relative = !!relative;
179197
if (this._bound) {
180198
this.dispatchEvent(Events.Types.change);
181199
}
182200
}
183-
}
201+
},
184202
},
185203
rx: {
186204
enumerable: true,
187-
get: function() {
205+
get: function () {
188206
return this._rx;
189207
},
190-
set: function(rx) {
208+
set: function (rx) {
191209
if (this._rx !== rx) {
192210
this._rx = rx;
193211
if (this._bound) {
194212
this.dispatchEvent(Events.Types.change);
195213
}
196214
}
197-
}
215+
},
198216
},
199217
ry: {
200218
enumerable: true,
201-
get: function() {
219+
get: function () {
202220
return this._ry;
203221
},
204-
set: function(ry) {
222+
set: function (ry) {
205223
if (this._ry !== ry) {
206224
this._ry = ry;
207225
if (this._bound) {
208226
this.dispatchEvent(Events.Types.change);
209227
}
210228
}
211-
}
229+
},
212230
},
213231
xAxisRotation: {
214232
enumerable: true,
215-
get: function() {
233+
get: function () {
216234
return this._xAxisRotation;
217235
},
218-
set: function(xAxisRotation) {
236+
set: function (xAxisRotation) {
219237
if (this._xAxisRotation !== xAxisRotation) {
220238
this._xAxisRotation = xAxisRotation;
221239
if (this._bound) {
222240
this.dispatchEvent(Events.Types.change);
223241
}
224242
}
225-
}
243+
},
226244
},
227245
largeArcFlag: {
228246
enumerable: true,
229-
get: function() {
247+
get: function () {
230248
return this._largeArcFlag;
231249
},
232-
set: function(largeArcFlag) {
250+
set: function (largeArcFlag) {
233251
if (this._largeArcFlag !== largeArcFlag) {
234252
this._largeArcFlag = largeArcFlag;
235253
if (this._bound) {
236254
this.dispatchEvent(Events.Types.change);
237255
}
238256
}
239-
}
257+
},
240258
},
241259
sweepFlag: {
242-
get: function() {
260+
get: function () {
243261
return this._sweepFlag;
244262
},
245-
set: function(sweepFlag) {
263+
set: function (sweepFlag) {
246264
if (this._sweepFlag !== sweepFlag) {
247265
this._sweepFlag = sweepFlag;
248266
if (this._bound) {
249267
this.dispatchEvent(Events.Types.change);
250268
}
251269
}
252-
}
253-
}
270+
},
271+
},
254272
};

0 commit comments

Comments
 (0)