forked from jonobr1/two.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanchor.js
272 lines (253 loc) · 6.98 KB
/
anchor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { Commands } from './utils/path-commands.js';
import { Events } from './events.js';
import { Vector } from './vector.js';
import { toFixed } from './utils/math.js';
/**
* @class
* @name Two.Anchor
* @param {Number} [x=0] - The x position of the root anchor point.
* @param {Number} [y=0] - The y position of the root anchor point.
* @param {Number} [ax=0] - The x position of the left handle point.
* @param {Number} [ay=0] - The y position of the left handle point.
* @param {Number} [bx=0] - The x position of the right handle point.
* @param {Number} [by=0] - The y position of the right handle point.
* @param {String} [command=Two.Commands.move] - The command to describe how to render. Applicable commands are {@link Two.Commands}
* @extends Two.Vector
* @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.
*/
export class Anchor extends Vector {
controls = {
left: new Vector(),
right: new Vector(),
};
_command = Commands.move;
_relative = true;
_rx = 0;
_ry = 0;
_xAxisRotation = 0;
_largeArcFlag = 0;
_sweepFlag = 1;
constructor(
x = 0,
y = 0,
ax = 0,
ay = 0,
bx = 0,
by = 0,
command = Commands.move
) {
super(x, y);
for (let prop in proto) {
Object.defineProperty(this, prop, proto[prop]);
}
this.command = command;
this.relative = true;
const broadcast = Anchor.makeBroadcast(this);
this.controls.left
.set(ax, ay)
.addEventListener(Events.Types.change, broadcast);
this.controls.right
.set(bx, by)
.addEventListener(Events.Types.change, broadcast);
}
static makeBroadcast(scope) {
return broadcast;
function broadcast() {
if (scope._bound) {
scope.dispatchEvent(Events.Types.change);
}
}
}
/**
* @name Two.Anchor.fromObject
* @function
* @param {Object} obj - Object notation of a {@link Two.Anchor} to create a new instance
* @returns {Two.Anchor}
* @description Create a new {@link Two.Anchor} from an object notation of a {@link Two.Anchor}.
* @nota-bene Works in conjunction with {@link Two.Anchor#toObject}
*/
static fromObject(obj) {
return new Anchor().copy(obj);
}
/**
* @name Two.Anchor#copy
* @function
* @param {Two.Anchor} v - The anchor to apply values to.
* @description Copy the properties of one {@link Two.Anchor} onto another.
*/
copy(v) {
this.x = v.x;
this.y = v.y;
if (typeof v.command === 'string') {
this.command = v.command;
}
if (v.controls) {
if (v.controls.left) {
this.controls.left.copy(v.controls.left);
}
if (v.controls.right) {
this.controls.right.copy(v.controls.right);
}
}
if (typeof v.relative === 'boolean') {
this.relative = v.relative;
}
if (typeof v.rx === 'number') {
this.rx = v.rx;
}
if (typeof v.ry === 'number') {
this.ry = v.ry;
}
if (typeof v.xAxisRotation === 'number') {
this.xAxisRotation = v.xAxisRotation;
}
if (typeof v.largeArcFlag === 'number') {
this.largeArcFlag = v.largeArcFlag;
}
if (typeof v.sweepFlag === 'number') {
this.sweepFlag = v.sweepFlag;
}
return this;
}
/**
* @name Two.Anchor#clone
* @function
* @returns {Two.Anchor}
* @description Create a new {@link Two.Anchor}, set all its values to the current instance and return it for use.
*/
clone() {
return new Anchor().copy(this);
}
/**
* @name Two.Anchor#toObject
* @function
* @returns {Object} - An object with properties filled out to mirror {@link Two.Anchor}.
* @description Create a JSON compatible plain object of the current instance. Intended for use with storing values in a database.
* @nota-bene Works in conjunction with {@link Two.Anchor.fromObject}
*/
toObject() {
return {
x: toFixed(this.x),
y: toFixed(this.y),
command: this.command,
relative: this.relative,
controls: {
left: this.controls.left.toObject(),
right: this.controls.right.toObject(),
},
rx: toFixed(this.rx),
ry: toFixed(this.ry),
xAxisRotation: toFixed(this.xAxisRotation),
largeArcFlag: toFixed(this.largeArcFlag),
sweepFlag: toFixed(this.sweepFlag),
};
}
/**
* @name Two.Anchor#toString
* @function
* @returns {String} - A String with comma-separated values reflecting the various values on the current instance.
* @description Create a string form of the current instance. Intended for use with storing values in a database. This is lighter to store than the JSON compatible {@link Two.Anchor#toObject}.
*/
toString() {
return JSON.stringify(this.toObject());
}
}
const proto = {
command: {
enumerable: true,
get: function () {
return this._command;
},
set: function (command) {
if (this._command !== command) {
this._command = command;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
relative: {
enumerable: true,
get: function () {
return this._relative;
},
set: function (relative) {
if (this._relative !== !!relative) {
this._relative = !!relative;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
rx: {
enumerable: true,
get: function () {
return this._rx;
},
set: function (rx) {
if (this._rx !== rx) {
this._rx = rx;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
ry: {
enumerable: true,
get: function () {
return this._ry;
},
set: function (ry) {
if (this._ry !== ry) {
this._ry = ry;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
xAxisRotation: {
enumerable: true,
get: function () {
return this._xAxisRotation;
},
set: function (xAxisRotation) {
if (this._xAxisRotation !== xAxisRotation) {
this._xAxisRotation = xAxisRotation;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
largeArcFlag: {
enumerable: true,
get: function () {
return this._largeArcFlag;
},
set: function (largeArcFlag) {
if (this._largeArcFlag !== largeArcFlag) {
this._largeArcFlag = largeArcFlag;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
sweepFlag: {
get: function () {
return this._sweepFlag;
},
set: function (sweepFlag) {
if (this._sweepFlag !== sweepFlag) {
this._sweepFlag = sweepFlag;
if (this._bound) {
this.dispatchEvent(Events.Types.change);
}
}
},
},
};