-
-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathpath.js
1523 lines (1306 loc) · 37.8 KB
/
path.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Commands } from './utils/path-commands.js';
import { Collection } from './collection.js';
import { lerp, mod, decomposeMatrix } from './utils/math.js';
import {
getComponentOnCubicBezier,
getCurveBoundingBox,
getCurveFromPoints,
} from './utils/curves.js';
import {
contains,
getIdByLength,
getCurveLength,
getSubdivisions,
getEffectFromObject,
} from './utils/shape.js';
import { _ } from './utils/underscore.js';
import { Shape } from './shape.js';
import { Events } from './events.js';
import { Vector } from './vector.js';
import { Anchor } from './anchor.js';
import { Gradient } from './effects/gradient.js';
import { LinearGradient } from './effects/linear-gradient.js';
import { RadialGradient } from './effects/radial-gradient.js';
import { Texture } from './effects/texture.js';
// Constants
const min = Math.min,
max = Math.max,
ceil = Math.ceil,
floor = Math.floor;
const vector = new Vector();
/**
* @name Two.Path
* @class
* @extends Two.Shape
* @param {Two.Anchor[]} [vertices] - A list of {@link Two.Anchor}s that represent the order and coordinates to construct the rendered shape.
* @param {Boolean} [closed=false] - Describes whether the shape is closed or open.
* @param {Boolean} [curved=false] - Describes whether the shape automatically calculates bezier handles for each vertex.
* @param {Boolean} [manual=false] - Describes whether the developer controls how vertices are plotted or if Two.js automatically plots coordinates based on closed and curved booleans.
* @description This is the primary primitive class for creating all drawable shapes in Two.js. Unless specified methods return their instance of `Two.Path` for the purpose of chaining.
*/
export class Path extends Shape {
/**
* @name Two.Path#_flagVertices
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#vertices} need updating.
*/
_flagVertices = true;
/**
* @name Two.Path#_flagLength
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#length} needs updating.
*/
_flagLength = true;
/**
* @name Two.Path#_flagFill
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#fill} needs updating.
*/
_flagFill = true;
/**
* @name Two.Path#_flagStroke
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#stroke} needs updating.
*/
_flagStroke = true;
/**
* @name Two.Path#_flagLinewidth
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#linewidth} needs updating.
*/
_flagLinewidth = true;
/**
* @name Two.Path#_flagOpacity
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#opacity} needs updating.
*/
_flagOpacity = true;
/**
* @name Two.Path#_flagVisible
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#visible} needs updating.
*/
_flagVisible = true;
/**
* @name Two.Path#_flagCap
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#cap} needs updating.
*/
_flagCap = true;
/**
* @name Two.Path#_flagJoin
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#join} needs updating.
*/
_flagJoin = true;
/**
* @name Two.Path#_flagMiter
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#miter} needs updating.
*/
_flagMiter = true;
/**
* @name Two.Path#_flagMask
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#mask} needs updating.
*/
_flagMask = false;
/**
* @name Two.Path#_flagClip
* @private
* @property {Boolean} - Determines whether the {@link Two.Path#clip} needs updating.
*/
_flagClip = false;
// Underlying Properties
/**
* @name Two.Path#_length
* @private
* @see {@link Two.Path#length}
*/
_length = 0;
/**
* @name Two.Path#_fill
* @private
* @see {@link Two.Path#fill}
*/
_fill = '#fff';
/**
* @name Two.Path#_stroke
* @private
* @see {@link Two.Path#stroke}
*/
_stroke = '#000';
/**
* @name Two.Path#_linewidth
* @private
* @see {@link Two.Path#linewidth}
*/
_linewidth = 1;
/**
* @name Two.Path#_opacity
* @private
* @see {@link Two.Path#opacity}
*/
_opacity = 1.0;
/**
* @name Two.Path#_visible
* @private
* @see {@link Two.Path#visible}
*/
_visible = true;
/**
* @name Two.Path#_cap
* @private
* @see {@link Two.Path#cap}
*/
_cap = 'round';
/**
* @name Two.Path#_join
* @private
* @see {@link Two.Path#join}
*/
_join = 'round';
/**
* @name Two.Path#_miter
* @private
* @see {@link Two.Path#miter}
*/
_miter = 4;
/**
* @name Two.Path#_closed
* @private
* @see {@link Two.Path#closed}
*/
_closed = true;
/**
* @name Two.Path#_curved
* @private
* @see {@link Two.Path#curved}
*/
_curved = false;
/**
* @name Two.Path#_automatic
* @private
* @see {@link Two.Path#automatic}
*/
_automatic = true;
/**
* @name Two.Path#_beginning
* @private
* @see {@link Two.Path#beginning}
*/
_beginning = 0;
/**
* @name Two.Path#_ending
* @private
* @see {@link Two.Path#ending}
*/
_ending = 1.0;
/**
* @name Two.Path#_mask
* @private
* @see {@link Two.Path#mask}
*/
_mask = null;
/**
* @name Two.Path#_clip
* @private
* @see {@link Two.Path#clip}
*/
_clip = false;
/**
* @name Two.Path#_dashes
* @private
* @see {@link Two.Path#dashes}
*/
_dashes = null;
constructor(vertices, closed, curved, manual) {
super();
for (let prop in proto) {
Object.defineProperty(this, prop, proto[prop]);
}
this._renderer.type = 'path';
this._renderer.flagVertices = FlagVertices.bind(this);
this._renderer.bindVertices = BindVertices.bind(this);
this._renderer.unbindVertices = UnbindVertices.bind(this);
this._renderer.flagFill = FlagFill.bind(this);
this._renderer.flagStroke = FlagStroke.bind(this);
this._renderer.vertices = [];
this._renderer.collection = [];
/**
* @name Two.Path#closed
* @property {Boolean} - Determines whether a final line is drawn between the final point in the `vertices` array and the first point.
*/
this.closed = !!closed;
/**
* @name Two.Path#curved
* @property {Boolean} - When the path is `automatic = true` this boolean determines whether the lines between the points are curved or not.
*/
this.curved = !!curved;
/**
* @name Two.Path#beginning
* @property {Number} - Number between zero and one to state the beginning of where the path is rendered.
* @description {@link Two.Path#beginning} is a percentage value that represents at what percentage into the path should the renderer start drawing.
* @nota-bene This is great for animating in and out stroked paths in conjunction with {@link Two.Path#ending}.
*/
this.beginning = 0;
/**
* @name Two.Path#ending
* @property {Number} - Number between zero and one to state the ending of where the path is rendered.
* @description {@link Two.Path#ending} is a percentage value that represents at what percentage into the path should the renderer start drawing.
* @nota-bene This is great for animating in and out stroked paths in conjunction with {@link Two.Path#beginning}.
*/
this.ending = 1;
// Style properties
/**
* @name Two.Path#fill
* @property {(String|Two.Gradient|Two.Texture)} - The value of what the path should be filled in with.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/color_value} for more information on CSS's colors as `String`.
*/
this.fill = '#fff';
/**
* @name Two.Path#stroke
* @property {(String|Two.Gradient|Two.Texture)} - The value of what the path should be outlined in with.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/color_value} for more information on CSS's colors as `String`.
*/
this.stroke = '#000';
/**
* @name Two.Path#linewidth
* @property {Number} - The thickness in pixels of the stroke.
*/
this.linewidth = 1.0;
/**
* @name Two.Path#opacity
* @property {Number} - The opaqueness of the path.
* @nota-bene Can be used in conjunction with CSS Colors that have an alpha value.
*/
this.opacity = 1.0;
/**
* @name Two.Path#className
* @property {String} - A class to be applied to the element to be compatible with CSS styling.
* @nota-bene Only available for the SVG renderer.
*/
this.className = '';
/**
* @name Two.Path#visible
* @property {Boolean} - Display the path or not.
* @nota-bene For {@link Two.CanvasRenderer} and {@link Two.WebGLRenderer} when set to false all updating is disabled improving performance dramatically with many objects in the scene.
*/
this.visible = true;
/**
* @name Two.Path#cap
* @property {String}
* @see {@link https://www.w3.org/TR/SVG11/painting.html#StrokeLinecapProperty}
*/
this.cap = 'butt'; // Default of Adobe Illustrator
/**
* @name Two.Path#join
* @property {String}
* @see {@link https://www.w3.org/TR/SVG11/painting.html#StrokeLinejoinProperty}
*/
this.join = 'miter'; // Default of Adobe Illustrator
/**
* @name Two.Path#miter
* @property {String}
* @see {@link https://www.w3.org/TR/SVG11/painting.html#StrokeMiterlimitProperty}
*/
this.miter = 4; // Default of Adobe Illustrator
/**
* @name Two.Path#vertices
* @property {Two.Anchor[]} - An ordered list of anchor points for rendering the path.
* @description A list of {@link Two.Anchor} objects that consist of what form the path takes.
* @nota-bene The array when manipulating is actually a {@link Two.Collection}.
*/
this.vertices = vertices;
/**
* @name Two.Path#automatic
* @property {Boolean} - Determines whether or not Two.js should calculate curves, lines, and commands automatically for you or to let the developer manipulate them for themselves.
*/
this.automatic = !manual;
/**
* @name Two.Path#dashes
* @property {Number[]} - Array of numbers. Odd indices represent dash length. Even indices represent dash space.
* @description A list of numbers that represent the repeated dash length and dash space applied to the stroke of the text.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray} for more information on the SVG stroke-dasharray attribute.
*/
this.dashes = [];
/**
* @name Two.Path#dashes#offset
* @property {Number} - A number in pixels to offset {@link Two.Path#dashes} display.
*/
this.dashes.offset = 0;
}
/**
* @name Two.Path.Properties
* @property {String[]} - A list of properties that are on every {@link Two.Path}.
*/
static Properties = [
'fill',
'stroke',
'linewidth',
'opacity',
'visible',
'cap',
'join',
'miter',
'closed',
'curved',
'automatic',
'beginning',
'ending',
'dashes',
];
static Utils = {
getCurveLength,
};
/**
* @name Two.Path.fromObject
* @function
* @param {Object} obj - Object notation of a {@link Two.Path} to create a new instance
* @returns {Two.Path}
* @description Create a new {@link Two.Path} from an object notation of a {@link Two.Path}.
* @nota-bene Works in conjunction with {@link Two.Path#toObject}
*/
static fromObject(obj) {
const fill =
typeof obj.fill === 'string' ? obj.fill : getEffectFromObject(obj.fill);
const stroke =
typeof obj.stroke === 'string'
? obj.stroke
: getEffectFromObject(obj.stroke);
const path = new Path().copy({ ...obj, fill, stroke });
if ('id' in obj) {
path.id = obj.id;
}
return path;
}
/**
* @name Two.Path#copy
* @function
* @param {Two.Path} path - The reference {@link Two.Path}
* @description Copy the properties of one {@link Two.Path} onto another.
*/
copy(path) {
super.copy.call(this, path);
this.vertices = [];
for (let j = 0; j < path.vertices.length; j++) {
const v = path.vertices[j];
if (v instanceof Anchor) {
this.vertices.push(path.vertices[j].clone());
} else {
this.vertices.push(new Anchor().copy(v));
}
}
for (let i = 0; i < Path.Properties.length; i++) {
const k = Path.Properties[i];
if (k in path) {
this[k] = path[k];
}
}
return this;
}
/**
* @name Two.Path#clone
* @function
* @param {Two.Group} [parent] - The parent group or scene to add the clone to.
* @returns {Two.Path}
* @description Create a new instance of {@link Two.Path} with the same properties of the current path.
*/
clone(parent) {
const clone = new Path();
for (let j = 0; j < this.vertices.length; j++) {
clone.vertices.push(this.vertices[j].clone());
}
for (let i = 0; i < Path.Properties.length; i++) {
const k = Path.Properties[i];
clone[k] = this[k];
}
clone.className = this.className;
clone.translation.copy(this.translation);
clone.rotation = this.rotation;
clone.scale = this.scale;
clone.skewX = this.skewX;
clone.skewY = this.skewY;
if (this.matrix.manual) {
clone.matrix.copy(this.matrix);
}
if (parent) {
parent.add(clone);
}
return clone._update();
}
/**
* @name Two.Path#toObject
* @function
* @returns {Object}
* @description Return a JSON compatible plain object that represents the path.
* @nota-bene Works in conjunction with {@link Two.Path.fromObject}
*/
toObject() {
const result = super.toObject.call(this);
result.renderer.type = 'path';
result.vertices = this.vertices.map((v) => v.toObject());
_.each(
Path.Properties,
(k) => {
if (typeof this[k] !== 'undefined') {
if (this[k].toObject) {
result[k] = this[k].toObject();
} else {
result[k] = this[k];
}
}
},
this
);
return result;
}
/**
* @name Two.Path#noFill
* @function
* @description Short hand method to set fill to `none`.
*/
noFill() {
this.fill = 'none';
return this;
}
/**
* @name Two.Path#noStroke
* @function
* @description Short hand method to set stroke to `none`.
*/
noStroke() {
this.stroke = 'none';
this.linewidth = 0;
return this;
}
/**
* @name Two.Path#corner
* @function
* @description Orient the vertices of the shape to the upper left-hand corner of the path.
*/
corner() {
const rect = this.getBoundingClientRect(true);
const hw = rect.width / 2;
const hh = rect.height / 2;
const cx = rect.left + rect.width / 2;
const cy = rect.top + rect.height / 2;
for (let i = 0; i < this.vertices.length; i++) {
const v = this.vertices[i];
v.x -= cx;
v.y -= cy;
v.x += hw;
v.y += hh;
}
if (this.mask) {
this.mask.translation.x -= cx;
this.mask.translation.x += hw;
this.mask.translation.y -= cy;
this.mask.translation.y += hh;
}
return this;
}
/**
* @name Two.Path#center
* @function
* @description Orient the vertices of the shape to the center of the path.
*/
center() {
const rect = this.getBoundingClientRect(true);
const cx = rect.left + rect.width / 2 - this.translation.x;
const cy = rect.top + rect.height / 2 - this.translation.y;
for (let i = 0; i < this.vertices.length; i++) {
const v = this.vertices[i];
v.x -= cx;
v.y -= cy;
}
if (this.mask) {
this.mask.translation.x -= cx;
this.mask.translation.y -= cy;
}
return this;
}
/**
* @name Two.Path#getBoundingClientRect
* @function
* @param {Boolean} [shallow=false] - Describes whether to calculate off local matrix or world matrix.
* @returns {Object} - Returns object with top, left, right, bottom, width, height attributes.
* @description Return an object with top, left, right, bottom, width, and height parameters of the path.
*/
getBoundingClientRect(shallow) {
let matrix, border, l, i, v0, v1;
let left = Infinity,
right = -Infinity,
top = Infinity,
bottom = -Infinity;
// TODO: Update this to not __always__ update. Just when it needs to.
this._update(true);
matrix = shallow ? this.matrix : this.worldMatrix;
border = (this.linewidth || 0) / 2;
l = this._renderer.vertices.length;
if (
this.linewidth > 0 ||
(this.stroke && !/(transparent|none)/i.test(this.stroke))
) {
if (this.matrix.manual) {
const { scaleX, scaleY } = decomposeMatrix(
matrix.elements[0],
matrix.elements[3],
matrix.elements[1],
matrix.elements[4],
matrix.elements[2],
matrix.elements[5]
);
if (typeof scaleX === 'number' && typeof scaleY === 'number') {
border = (Math.max(scaleX, scaleY) * (this.linewidth || 0)) / 2;
}
} else {
border *=
typeof this.scale === 'number'
? this.scale
: Math.max(this.scale.x, this.scale.y);
}
}
if (l <= 0) {
return {
width: 0,
height: 0,
};
}
for (i = 0; i < l; i++) {
v1 = this._renderer.vertices[i];
// If i = 0, then this "wraps around" to the last vertex. Otherwise, it's the previous vertex.
// This is important for handling cyclic paths.
v0 = this._renderer.vertices[(i + l - 1) % l];
const [v0x, v0y] = matrix.multiply(v0.x, v0.y);
const [v1x, v1y] = matrix.multiply(v1.x, v1.y);
if (v0.controls && v1.controls) {
let rx = v0.controls.right.x;
let ry = v0.controls.right.y;
if (v0.relative) {
rx += v0.x;
ry += v0.y;
}
let [c0x, c0y] = matrix.multiply(rx, ry);
let lx = v1.controls.left.x;
let ly = v1.controls.left.y;
if (v1.relative) {
lx += v1.x;
ly += v1.y;
}
let [c1x, c1y] = matrix.multiply(lx, ly);
const bb = getCurveBoundingBox(v0x, v0y, c0x, c0y, c1x, c1y, v1x, v1y);
top = min(bb.min.y - border, top);
left = min(bb.min.x - border, left);
right = max(bb.max.x + border, right);
bottom = max(bb.max.y + border, bottom);
} else {
if (i <= 1) {
top = min(v0y - border, top);
left = min(v0x - border, left);
right = max(v0x + border, right);
bottom = max(v0y + border, bottom);
}
top = min(v1y - border, top);
left = min(v1x - border, left);
right = max(v1x + border, right);
bottom = max(v1y + border, bottom);
}
}
return {
top: top,
left: left,
right: right,
bottom: bottom,
width: right - left,
height: bottom - top,
};
}
/**
* @name Two.Path#getPointAt
* @function
* @param {Number} t - Percentage value describing where on the {@link Two.Path} to estimate and assign coordinate values.
* @param {Two.Vector} [obj] - Object to apply calculated x, y to. If none available returns new `Object`.
* @returns {Object}
* @description Given a float `t` from 0 to 1, return a point or assign a passed `obj`'s coordinates to that percentage on this {@link Two.Path}'s curve.
*/
getPointAt(t, obj) {
let ia, ib, result;
let x, x1, x2, x3, x4, y, y1, y2, y3, y4, left, right;
let target = this.length * Math.min(Math.max(t, 0), 1);
const length = this.vertices.length;
const last = length - 1;
let a = null;
let b = null;
for (let i = 0, l = this._lengths.length, sum = 0; i < l; i++) {
if (sum + this._lengths[i] >= target) {
if (this._closed) {
ia = mod(i, length);
ib = mod(i - 1, length);
if (i === 0) {
ia = ib;
ib = i;
}
} else {
ia = i;
ib = Math.min(Math.max(i - 1, 0), last);
}
a = this.vertices[ia];
b = this.vertices[ib];
target -= sum;
if (this._lengths[i] !== 0) {
t = target / this._lengths[i];
} else {
t = 0;
}
break;
}
sum += this._lengths[i];
}
if (a === null || b === null) {
return null;
}
if (!a) {
return b;
} else if (!b) {
return a;
}
right = b.controls && b.controls.right;
left = a.controls && a.controls.left;
x1 = b.x;
y1 = b.y;
x2 = (right || b).x;
y2 = (right || b).y;
x3 = (left || a).x;
y3 = (left || a).y;
x4 = a.x;
y4 = a.y;
if (right && b.relative) {
x2 += b.x;
y2 += b.y;
}
if (left && a.relative) {
x3 += a.x;
y3 += a.y;
}
x = getComponentOnCubicBezier(t, x1, x2, x3, x4);
y = getComponentOnCubicBezier(t, y1, y2, y3, y4);
// Higher order points for control calculation.
const t1x = lerp(x1, x2, t);
const t1y = lerp(y1, y2, t);
const t2x = lerp(x2, x3, t);
const t2y = lerp(y2, y3, t);
const t3x = lerp(x3, x4, t);
const t3y = lerp(y3, y4, t);
// Calculate the returned points control points.
const brx = lerp(t1x, t2x, t);
const bry = lerp(t1y, t2y, t);
const alx = lerp(t2x, t3x, t);
const aly = lerp(t2y, t3y, t);
if (_.isObject(obj)) {
obj.x = x;
obj.y = y;
if (obj instanceof Anchor) {
obj.controls.left.x = brx;
obj.controls.left.y = bry;
obj.controls.right.x = alx;
obj.controls.right.y = aly;
if (!(typeof obj.relative === 'boolean') || obj.relative) {
obj.controls.left.x -= x;
obj.controls.left.y -= y;
obj.controls.right.x -= x;
obj.controls.right.y -= y;
}
}
obj.t = t;
return obj;
}
result = new Anchor(
x,
y,
brx - x,
bry - y,
alx - x,
aly - y,
this._curved ? Commands.curve : Commands.line
);
result.t = t;
return result;
}
/**
* @name Two.Path#plot
* @function
* @description Based on closed / curved and sorting of vertices plot where all points should be and where the respective handles should be too.
* @nota-bene While this method is public it is internally called by {@link Two.Path#_update} when `automatic = true`.
*/
plot() {
if (this.curved) {
getCurveFromPoints(this._collection, this.closed);
return this;
}
for (let i = 0; i < this._collection.length; i++) {
this._collection[i].command = i === 0 ? Commands.move : Commands.line;
}
return this;
}
/**
* @name Two.Path#subdivide
* @function
* @param {Number} limit - How many times to recurse subdivisions.
* @description Insert a {@link Two.Anchor} at the midpoint between every item in {@link Two.Path#vertices}.
*/
subdivide(limit) {
// TODO: DRYness (function below)
this._update();
const last = this.vertices.length - 1;
const closed =
this._closed || this.vertices[last]._command === Commands.close;
let b = this.vertices[last];
let points = [],
verts;
_.each(
this.vertices,
function (a, i) {
if (i <= 0 && !closed) {
b = a;
return;
}
if (a.command === Commands.move) {
points.push(new Anchor(b.x, b.y));
if (i > 0) {
points[points.length - 1].command = Commands.line;
}
b = a;
return;
}
verts = getSubdivisions(a, b, limit);
points = points.concat(verts);
// Assign commands to all the verts
_.each(verts, function (v, i) {
if (i <= 0 && b.command === Commands.move) {
v.command = Commands.move;
} else {
v.command = Commands.line;
}
});
if (i >= last) {
// TODO: Add check if the two vectors in question are the same values.
if (this._closed && this._automatic) {
b = a;
verts = getSubdivisions(a, b, limit);
points = points.concat(verts);
// Assign commands to all the verts
_.each(verts, function (v, i) {
if (i <= 0 && b.command === Commands.move) {
v.command = Commands.move;
} else {
v.command = Commands.line;
}
});
} else if (closed) {
points.push(new Anchor(a.x, a.y));
}
points[points.length - 1].command = closed
? Commands.close
: Commands.line;
}
b = a;
},
this
);
this._automatic = false;
this._curved = false;
this.vertices = points;
return this;
}
/**
* @name Two.Path#_updateLength
* @function
* @private
* @param {Number} [limit] -
* @param {Boolean} [silent=false] - If set to `true` then the path isn't updated before calculation. Useful for internal use.
* @description Recalculate the {@link Two.Path#length} value.
*/
_updateLength(limit, silent) {
// TODO: DRYness (function above)
if (!silent) {
this._update();
}
const length = this.vertices.length;
const last = length - 1;
const closed = false; //this._closed || this.vertices[last]._command === Commands.close;
let b = this.vertices[last];
let sum = 0;
if (typeof this._lengths === 'undefined') {
this._lengths = [];
}
_.each(
this.vertices,
function (a, i) {
if ((i <= 0 && !closed) || a.command === Commands.move) {
b = a;
this._lengths[i] = 0;
return;
}
this._lengths[i] = getCurveLength(a, b, limit);
sum += this._lengths[i];