-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Path.js
2967 lines (2836 loc) · 113 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
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name Path
*
* @class The path item represents a path in a Paper.js project.
*
* @extends PathItem
*/
// DOCS: Explain that path matrix is always applied with each transformation.
var Path = PathItem.extend(/** @lends Path# */{
_class: 'Path',
_serializeFields: {
segments: [],
closed: false
},
/**
* Creates a new path item and places it at the top of the active layer.
*
* @name Path#initialize
* @param {Segment[]} [segments] An array of segments (or points to be
* converted to segments) that will be added to the path
* @return {Path} the newly created path
*
* @example
* // Create an empty path and add segments to it:
* var path = new Path();
* path.strokeColor = 'black';
* path.add(new Point(30, 30));
* path.add(new Point(100, 100));
*
* @example
* // Create a path with two segments:
* var segments = [new Point(30, 30), new Point(100, 100)];
* var path = new Path(segments);
* path.strokeColor = 'black';
*/
/**
* Creates a new path item from an object description and places it at the
* top of the active layer.
*
* @name Path#initialize
* @param {Object} object an object containing properties to be set on the
* path
* @return {Path} the newly created path
*
* @example {@paperscript}
* var path = new Path({
* segments: [[20, 20], [80, 80], [140, 20]],
* fillColor: 'black',
* closed: true
* });
*
* @example {@paperscript}
* var path = new Path({
* segments: [[20, 20], [80, 80], [140, 20]],
* strokeColor: 'red',
* strokeWidth: 20,
* strokeCap: 'round',
* selected: true
* });
*/
/**
* Creates a new path item from SVG path-data and places it at the top of
* the active layer.
*
* @name Path#initialize
* @param {String} pathData the SVG path-data that describes the geometry
* of this path
* @return {Path} the newly created path
*
* @example {@paperscript}
* var pathData = 'M100,50c0,27.614-22.386,50-50,50S0,77.614,0,50S22.386,0,50,0S100,22.386,100,50';
* var path = new Path(pathData);
* path.fillColor = 'red';
*/
initialize: function Path(arg) {
this._closed = false;
this._segments = [];
// Increased on every change of segments, so CurveLocation knows when to
// update its internally cached values.
this._version = 0;
// arg can either be an object literal containing properties to be set
// on the path, a list of segments to be set, or the first of multiple
// arguments describing separate segments.
// If it is an array, it can also be a description of a point, so
// check its first entry for object as well.
// But first see if segments are directly passed at all. If not, try
// _set(arg).
var args = arguments,
segments = Array.isArray(arg)
? typeof arg[0] === 'object'
? arg
: args
// See if it behaves like a segment or a point, but filter out
// rectangles, as accepted by some Path.Constructor constructors.
: arg && (arg.size === undefined && (arg.x !== undefined
|| arg.point !== undefined))
? args
: null;
// Always call setSegments() to initialize a few related variables.
if (segments && segments.length > 0) {
// This sets _curves and _segmentSelection too!
this.setSegments(segments);
} else {
this._curves = undefined; // For hidden class optimization
this._segmentSelection = 0;
if (!segments && typeof arg === 'string') {
this.setPathData(arg);
// Erase for _initialize() call below.
arg = null;
}
}
// Only pass on arg as props if it wasn't consumed for segments already.
this._initialize(!segments && arg);
},
_equals: function(item) {
return this._closed === item._closed
&& Base.equals(this._segments, item._segments);
},
copyContent: function(source) {
this.setSegments(source._segments);
this._closed = source._closed;
},
_changed: function _changed(flags) {
_changed.base.call(this, flags);
if (flags & /*#=*/ChangeFlag.GEOMETRY) {
this._length = this._area = undefined;
if (flags & /*#=*/ChangeFlag.SEGMENTS) {
this._version++; // See CurveLocation
} else if (this._curves) {
// Only notify all curves if we're not told that only segments
// have changed and took already care of notifications.
for (var i = 0, l = this._curves.length; i < l; i++)
this._curves[i]._changed();
}
} else if (flags & /*#=*/ChangeFlag.STROKE) {
// TODO: We could preserve the purely geometric bounds that are not
// affected by stroke: _bounds.bounds and _bounds.handleBounds
this._bounds = undefined;
}
},
getStyle: function() {
// If this path is part of a compound-path, return the parent's style.
var parent = this._parent;
return (parent instanceof CompoundPath ? parent : this)._style;
},
/**
* The segments contained within the path.
*
* @bean
* @type Segment[]
*/
getSegments: function() {
return this._segments;
},
setSegments: function(segments) {
var fullySelected = this.isFullySelected(),
length = segments && segments.length;
this._segments.length = 0;
this._segmentSelection = 0;
// Calculate new curves next time we call getCurves()
this._curves = undefined;
if (length) {
// See if the last segment is a boolean describing the path's closed
// state. This is part of the shorter segment array notation that
// can also be nested to create compound-paths out of one array.
var last = segments[length - 1];
if (typeof last === 'boolean') {
this.setClosed(last);
length--;
}
this._add(Segment.readList(segments, 0, {}, length));
}
// Preserve fullySelected state.
// TODO: Do we still need this?
if (fullySelected)
this.setFullySelected(true);
},
/**
* The first Segment contained within the path.
*
* @bean
* @type Segment
*/
getFirstSegment: function() {
return this._segments[0];
},
/**
* The last Segment contained within the path.
*
* @bean
* @type Segment
*/
getLastSegment: function() {
return this._segments[this._segments.length - 1];
},
/**
* The curves contained within the path.
*
* @bean
* @type Curve[]
*/
getCurves: function() {
var curves = this._curves,
segments = this._segments;
if (!curves) {
var length = this._countCurves();
curves = this._curves = new Array(length);
for (var i = 0; i < length; i++)
curves[i] = new Curve(this, segments[i],
// Use first segment for segment2 of closing curve
segments[i + 1] || segments[0]);
}
return curves;
},
/**
* The first Curve contained within the path.
*
* @bean
* @type Curve
*/
getFirstCurve: function() {
return this.getCurves()[0];
},
/**
* The last Curve contained within the path.
*
* @bean
* @type Curve
*/
getLastCurve: function() {
var curves = this.getCurves();
return curves[curves.length - 1];
},
/**
* Specifies whether the path is closed. If it is closed, Paper.js connects
* the first and last segments.
*
* @bean
* @type Boolean
*
* @example {@paperscript}
* var myPath = new Path();
* myPath.strokeColor = 'black';
* myPath.add(new Point(50, 75));
* myPath.add(new Point(100, 25));
* myPath.add(new Point(150, 75));
*
* // Close the path:
* myPath.closed = true;
*/
isClosed: function() {
return this._closed;
},
setClosed: function(closed) {
// On-the-fly conversion to boolean:
if (this._closed != (closed = !!closed)) {
this._closed = closed;
// Update _curves length
if (this._curves) {
var length = this._curves.length = this._countCurves();
// If we were closing this path, we need to add a new curve now
if (closed)
this._curves[length - 1] = new Curve(this,
this._segments[length - 1], this._segments[0]);
}
// Use SEGMENTS notification instead of GEOMETRY since curves are
// up-to-date and don't need notification.
this._changed(/*#=*/Change.SEGMENTS);
}
}
}, /** @lends Path# */{
// Enforce creation of beans, as bean getters have hidden parameters.
// See #getPathData() below.
beans: true,
getPathData: function(_matrix, _precision) {
// NOTE: #setPathData() is defined in PathItem.
var segments = this._segments,
length = segments.length,
f = new Formatter(_precision),
coords = new Array(6),
first = true,
curX, curY,
prevX, prevY,
inX, inY,
outX, outY,
parts = [];
function addSegment(segment, skipLine) {
segment._transformCoordinates(_matrix, coords);
curX = coords[0];
curY = coords[1];
if (first) {
parts.push('M' + f.pair(curX, curY));
first = false;
} else {
inX = coords[2];
inY = coords[3];
if (inX === curX && inY === curY
&& outX === prevX && outY === prevY) {
// l = relative lineto:
if (!skipLine) {
var dx = curX - prevX,
dy = curY - prevY;
parts.push(
dx === 0 ? 'v' + f.number(dy)
: dy === 0 ? 'h' + f.number(dx)
: 'l' + f.pair(dx, dy));
}
} else {
// c = relative curveto:
parts.push('c' + f.pair(outX - prevX, outY - prevY)
+ ' ' + f.pair( inX - prevX, inY - prevY)
+ ' ' + f.pair(curX - prevX, curY - prevY));
}
}
prevX = curX;
prevY = curY;
outX = coords[4];
outY = coords[5];
}
if (!length)
return '';
for (var i = 0; i < length; i++)
addSegment(segments[i]);
// Close path by drawing first segment again
if (this._closed && length > 0) {
addSegment(segments[0], true);
parts.push('z');
}
return parts.join('');
},
// TODO: Consider adding getSubPath(a, b), returning a part of the current
// path, with the added benefit that b can be < a, and closed looping is
// taken into account.
isEmpty: function() {
return !this._segments.length;
},
_transformContent: function(matrix) {
var segments = this._segments,
coords = new Array(6);
for (var i = 0, l = segments.length; i < l; i++)
segments[i]._transformCoordinates(matrix, coords, true);
return true;
},
/**
* Private method that adds segments to the segment list. It assumes that
* the passed object is an array of segments already and does not perform
* any checks. If a curves list was requested, it will be kept in sync with
* the segments list automatically.
*/
_add: function(segs, index) {
// Local short-cuts:
var segments = this._segments,
curves = this._curves,
amount = segs.length,
append = index == null,
index = append ? segments.length : index;
// Scan through segments to add first, convert if necessary and set
// _path and _index references on them.
for (var i = 0; i < amount; i++) {
var segment = segs[i];
// If the segments belong to another path already, clone them before
// adding:
if (segment._path)
segment = segs[i] = segment.clone();
segment._path = this;
segment._index = index + i;
// If parts of this segment are selected, adjust the internal
// _segmentSelection now
if (segment._selection)
this._updateSelection(segment, 0, segment._selection);
}
if (append) {
// Append them all at the end.
Base.push(segments, segs);
} else {
// Insert somewhere else
segments.splice.apply(segments, [index, 0].concat(segs));
// Adjust the indices of the segments above.
for (var i = index + amount, l = segments.length; i < l; i++)
segments[i]._index = i;
}
// Keep the curves list in sync all the time in case it was requested
// already.
if (curves) {
var total = this._countCurves(),
// If we're adding a new segment to the end of an open path,
// we need to step one index down to get its curve.
start = index > 0 && index + amount - 1 === total ? index - 1
: index,
insert = start,
end = Math.min(start + amount, total);
if (segs._curves) {
// Reuse removed curves.
curves.splice.apply(curves, [start, 0].concat(segs._curves));
insert += segs._curves.length;
}
// Insert new curves, but do not initialize their segments yet,
// since #_adjustCurves() handles all that for us.
for (var i = insert; i < end; i++)
curves.splice(i, 0, new Curve(this, null, null));
// Adjust segments for the curves before and after the removed ones
this._adjustCurves(start, end);
}
// Use SEGMENTS notification instead of GEOMETRY since curves are kept
// up-to-date by _adjustCurves() and don't need notification.
this._changed(/*#=*/Change.SEGMENTS);
return segs;
},
/**
* Adjusts segments of curves before and after inserted / removed segments.
*/
_adjustCurves: function(start, end) {
var segments = this._segments,
curves = this._curves,
curve;
for (var i = start; i < end; i++) {
curve = curves[i];
curve._path = this;
curve._segment1 = segments[i];
curve._segment2 = segments[i + 1] || segments[0];
curve._changed();
}
// If it's the first segment, correct the last segment of closed
// paths too:
if (curve = curves[this._closed && !start ? segments.length - 1
: start - 1]) {
curve._segment2 = segments[start] || segments[0];
curve._changed();
}
// Fix the segment after the modified range, if it exists
if (curve = curves[end]) {
curve._segment1 = segments[end];
curve._changed();
}
},
/**
* Returns the amount of curves this path item is supposed to have, based
* on its amount of #segments and #closed state.
*/
_countCurves: function() {
var length = this._segments.length;
// Reduce length by one if it's an open path:
return !this._closed && length > 0 ? length - 1 : length;
},
// DOCS: find a way to document the variable segment parameters of Path#add
/**
* Adds one or more segments to the end of the {@link #segments} array of
* this path.
*
* @param {...(Segment|Point|Number[])} segment the segment or point to be
* added.
* @return {Segment|Segment[]} the added segment(s). This is not necessarily
* the same object, e.g. if the segment to be added already belongs to
* another path.
*
* @example {@paperscript}
* // Adding segments to a path using point objects:
* var path = new Path({
* strokeColor: 'black'
* });
*
* // Add a segment at {x: 30, y: 75}
* path.add(new Point(30, 75));
*
* // Add two segments in one go at {x: 100, y: 20}
* // and {x: 170, y: 75}:
* path.add(new Point(100, 20), new Point(170, 75));
*
* @example {@paperscript}
* // Adding segments to a path using arrays containing number pairs:
* var path = new Path({
* strokeColor: 'black'
* });
*
* // Add a segment at {x: 30, y: 75}
* path.add([30, 75]);
*
* // Add two segments in one go at {x: 100, y: 20}
* // and {x: 170, y: 75}:
* path.add([100, 20], [170, 75]);
*
* @example {@paperscript}
* // Adding segments to a path using objects:
* var path = new Path({
* strokeColor: 'black'
* });
*
* // Add a segment at {x: 30, y: 75}
* path.add({x: 30, y: 75});
*
* // Add two segments in one go at {x: 100, y: 20}
* // and {x: 170, y: 75}:
* path.add({x: 100, y: 20}, {x: 170, y: 75});
*
* @example {@paperscript}
* // Adding a segment with handles to a path:
* var path = new Path({
* strokeColor: 'black'
* });
*
* path.add(new Point(30, 75));
*
* // Add a segment with handles:
* var point = new Point(100, 20);
* var handleIn = new Point(-50, 0);
* var handleOut = new Point(50, 0);
* var added = path.add(new Segment(point, handleIn, handleOut));
*
* // Select the added segment, so we can see its handles:
* added.selected = true;
*
* path.add(new Point(170, 75));
*/
add: function(segment1 /*, segment2, ... */) {
var args = arguments;
return args.length > 1 && typeof segment1 !== 'number'
// addSegments
? this._add(Segment.readList(args))
// addSegment
: this._add([ Segment.read(args) ])[0];
},
/**
* Inserts one or more segments at a given index in the list of this path's
* segments.
*
* @param {Number} index the index at which to insert the segment
* @param {Segment|Point} segment the segment or point to be inserted.
* @return {Segment} the added segment. This is not necessarily the same
* object, e.g. if the segment to be added already belongs to another path
*
* @example {@paperscript}
* // Inserting a segment:
* var myPath = new Path();
* myPath.strokeColor = 'black';
* myPath.add(new Point(50, 75));
* myPath.add(new Point(150, 75));
*
* // Insert a new segment into myPath at index 1:
* myPath.insert(1, new Point(100, 25));
*
* // Select the segment which we just inserted:
* myPath.segments[1].selected = true;
*
* @example {@paperscript}
* // Inserting multiple segments:
* var myPath = new Path();
* myPath.strokeColor = 'black';
* myPath.add(new Point(50, 75));
* myPath.add(new Point(150, 75));
*
* // Insert two segments into myPath at index 1:
* myPath.insert(1, [80, 25], [120, 25]);
*
* // Select the segments which we just inserted:
* myPath.segments[1].selected = true;
* myPath.segments[2].selected = true;
*/
insert: function(index, segment1 /*, segment2, ... */) {
var args = arguments;
return args.length > 2 && typeof segment1 !== 'number'
// insertSegments
? this._add(Segment.readList(args, 1), index)
// insertSegment
: this._add([ Segment.read(args, 1) ], index)[0];
},
addSegment: function(/* segment */) {
return this._add([ Segment.read(arguments) ])[0];
},
insertSegment: function(index /*, segment */) {
return this._add([ Segment.read(arguments, 1) ], index)[0];
},
/**
* Adds an array of segments (or types that can be converted to segments)
* to the end of the {@link #segments} array.
*
* @param {Segment[]} segments
* @return {Segment[]} an array of the added segments. These segments are
* not necessarily the same objects, e.g. if the segment to be added already
* belongs to another path
*
* @example {@paperscript}
* // Adding an array of Point objects:
* var path = new Path({
* strokeColor: 'black'
* });
* var points = [new Point(30, 50), new Point(170, 50)];
* path.addSegments(points);
*
* @example {@paperscript}
* // Adding an array of [x, y] arrays:
* var path = new Path({
* strokeColor: 'black'
* });
* var array = [[30, 75], [100, 20], [170, 75]];
* path.addSegments(array);
*
* @example {@paperscript}
* // Adding segments from one path to another:
*
* var path = new Path({
* strokeColor: 'black'
* });
* path.addSegments([[30, 75], [100, 20], [170, 75]]);
*
* var path2 = new Path();
* path2.strokeColor = 'red';
*
* // Add the second and third segments of path to path2:
* path2.add(path.segments[1], path.segments[2]);
*
* // Move path2 30pt to the right:
* path2.position.x += 30;
*/
addSegments: function(segments) {
return this._add(Segment.readList(segments));
},
/**
* Inserts an array of segments at a given index in the path's
* {@link #segments} array.
*
* @param {Number} index the index at which to insert the segments
* @param {Segment[]} segments the segments to be inserted
* @return {Segment[]} an array of the added segments. These segments are
* not necessarily the same objects, e.g. if the segment to be added already
* belongs to another path
*/
insertSegments: function(index, segments) {
return this._add(Segment.readList(segments), index);
},
/**
* Removes the segment at the specified index of the path's
* {@link #segments} array.
*
* @param {Number} index the index of the segment to be removed
* @return {Segment} the removed segment
*
* @example {@paperscript}
* // Removing a segment from a path:
*
* // Create a circle shaped path at { x: 80, y: 50 }
* // with a radius of 35:
* var path = new Path.Circle({
* center: new Point(80, 50),
* radius: 35,
* strokeColor: 'black'
* });
*
* // Remove its second segment:
* path.removeSegment(1);
*
* // Select the path, so we can see its segments:
* path.selected = true;
*/
removeSegment: function(index) {
return this.removeSegments(index, index + 1)[0] || null;
},
/**
* Removes all segments from the path's {@link #segments} array.
*
* @name Path#removeSegments
* @alias Path#clear
* @function
* @return {Segment[]} an array containing the removed segments
*/
/**
* Removes the segments from the specified `from` index to the `to` index
* from the path's {@link #segments} array.
*
* @param {Number} from the beginning index, inclusive
* @param {Number} [to=segments.length] the ending index, exclusive
* @return {Segment[]} an array containing the removed segments
*
* @example {@paperscript}
* // Removing segments from a path:
*
* // Create a circle shaped path at { x: 80, y: 50 }
* // with a radius of 35:
* var path = new Path.Circle({
* center: new Point(80, 50),
* radius: 35,
* strokeColor: 'black'
* });
*
* // Remove the segments from index 1 till index 2:
* path.removeSegments(1, 2);
*
* // Select the path, so we can see its segments:
* path.selected = true;
*/
removeSegments: function(start, end, _includeCurves) {
start = start || 0;
end = Base.pick(end, this._segments.length);
var segments = this._segments,
curves = this._curves,
count = segments.length, // segment count before removal
removed = segments.splice(start, end - start),
amount = removed.length;
if (!amount)
return removed;
// Update selection state accordingly
for (var i = 0; i < amount; i++) {
var segment = removed[i];
if (segment._selection)
this._updateSelection(segment, segment._selection, 0);
// Clear the indices and path references of the removed segments
segment._index = segment._path = null;
}
// Adjust the indices of the segments above.
for (var i = start, l = segments.length; i < l; i++)
segments[i]._index = i;
// Keep curves in sync
if (curves) {
// If we're removing the last segment, remove the last curve (the
// one to the left of the segment, not to the right, as normally).
// Also take into account closed paths, which have one curve more
// than segments.
var index = start > 0 && end === count + (this._closed ? 1 : 0)
? start - 1
: start,
curves = curves.splice(index, amount);
// Unlink the removed curves from the path.
for (var i = curves.length - 1; i >= 0; i--)
curves[i]._path = null;
// Return the removed curves as well, if we're asked to include
// them, but exclude the first curve, since that's shared with the
// previous segment and does not connect the returned segments.
if (_includeCurves)
removed._curves = curves.slice(1);
// Adjust segments for the curves before and after the removed ones
this._adjustCurves(index, index);
}
// Use SEGMENTS notification instead of GEOMETRY since curves are kept
// up-to-date by _adjustCurves() and don't need notification.
this._changed(/*#=*/Change.SEGMENTS);
return removed;
},
// DOCS Path#clear()
clear: '#removeSegments',
/**
* Checks if any of the curves in the path have curve handles set.
*
* @return {Boolean} {@true if the path has curve handles set}
* @see Segment#hasHandles()
* @see Curve#hasHandles()
*/
hasHandles: function() {
var segments = this._segments;
for (var i = 0, l = segments.length; i < l; i++) {
if (segments[i].hasHandles())
return true;
}
return false;
},
/**
* Clears the path's handles by setting their coordinates to zero,
* turning the path into a polygon (or a polyline if it isn't closed).
*/
clearHandles: function() {
var segments = this._segments;
for (var i = 0, l = segments.length; i < l; i++)
segments[i].clearHandles();
},
/**
* The approximate length of the path.
*
* @bean
* @type Number
*/
getLength: function() {
if (this._length == null) {
var curves = this.getCurves(),
length = 0;
for (var i = 0, l = curves.length; i < l; i++)
length += curves[i].getLength();
this._length = length;
}
return this._length;
},
/**
* The area that the path's geometry is covering. Self-intersecting paths
* can contain sub-areas that cancel each other out.
*
* @bean
* @type Number
*/
getArea: function() {
var area = this._area;
if (area == null) {
var segments = this._segments,
closed = this._closed;
area = 0;
for (var i = 0, l = segments.length; i < l; i++) {
var last = i + 1 === l;
area += Curve.getArea(Curve.getValues(
segments[i], segments[last ? 0 : i + 1],
// If this is the last curve and the last is not closed,
// connect with a straight curve and ignore the handles.
null, last && !closed));
}
this._area = area;
}
return area;
},
/**
* Specifies whether an path is selected and will also return `true` if the
* path is partially selected, i.e. one or more of its segments is selected.
*
* Paper.js draws the visual outlines of selected items on top of your
* project. This can be useful for debugging, as it allows you to see the
* construction of paths, position of path curves, individual segment points
* and bounding boxes of symbol and raster items.
*
* @bean
* @type Boolean
* @see Project#selectedItems
* @see Segment#selected
* @see Point#selected
*
* @example {@paperscript}
* // Selecting an item:
* var path = new Path.Circle({
* center: [80, 50],
* radius: 35
* });
* path.selected = true; // Select the path
*
* @example {@paperscript}
* // A path is selected, if one or more of its segments is selected:
* var path = new Path.Circle({
* center: [80, 50],
* radius: 35
* });
*
* // Select the second segment of the path:
* path.segments[1].selected = true;
*
* // If the path is selected (which it is), set its fill color to red:
* if (path.selected) {
* path.fillColor = 'red';
* }
*
*/
/**
* Specifies whether the path and all its segments are selected. Cannot be
* `true` on an empty path.
*
* @bean
* @type Boolean
*
* @example {@paperscript}
* // A path is fully selected, if all of its segments are selected:
* var path = new Path.Circle({
* center: [80, 50],
* radius: 35
* });
* path.fullySelected = true;
*
* var path2 = new Path.Circle({
* center: [180, 50],
* radius: 35
* });
*
* // Deselect the second segment of the second path:
* path2.segments[1].selected = false;
*
* // If the path is fully selected (which it is),
* // set its fill color to red:
* if (path.fullySelected) {
* path.fillColor = 'red';
* }
*
* // If the second path is fully selected (which it isn't, since we just
* // deselected its second segment),
* // set its fill color to red:
* if (path2.fullySelected) {
* path2.fillColor = 'red';
* }
*/
isFullySelected: function() {
var length = this._segments.length;
return this.isSelected() && length > 0 && this._segmentSelection
=== length * /*#=*/SegmentSelection.ALL;
},
setFullySelected: function(selected) {
// No need to call _selectSegments() when selected is false, since
// #setSelected() does that for us
if (selected)
this._selectSegments(true);
this.setSelected(selected);
},
setSelection: function setSelection(selection) {
// Deselect all segments when path is marked as not selected
if (!(selection & /*#=*/ItemSelection.ITEM))
this._selectSegments(false);
setSelection.base.call(this, selection);
},
_selectSegments: function(selected) {
var segments = this._segments,
length = segments.length,
selection = selected ? /*#=*/SegmentSelection.ALL : 0;
this._segmentSelection = selection * length;
for (var i = 0; i < length; i++)
segments[i]._selection = selection;
},
_updateSelection: function(segment, oldSelection, newSelection) {
segment._selection = newSelection;
var selection = this._segmentSelection += newSelection - oldSelection;
// Set this path as selected in case we have selected segments. Do not
// unselect if we're down to 0, as the path itself can still remain
// selected even when empty.
if (selection > 0)
this.setSelected(true);
},
/**
* Divides the path on the curve at the given offset or location into two
* curves, by inserting a new segment at the given location.
*
* @param {Number|CurveLocation} location the offset or location on the
* path at which to divide the existing curve by inserting a new segment
* @return {Segment} the newly inserted segment if the location is valid,
* `null` otherwise
* @see Curve#divideAt(location)
*/
divideAt: function(location) {
var loc = this.getLocationAt(location),
curve;
return loc && (curve = loc.getCurve().divideAt(loc.getCurveOffset()))
? curve._segment1
: null;
},
/**
* Splits the path at the given offset or location. After splitting, the
* path will be open. If the path was open already, splitting will result in
* two paths.
*
* @param {Number|CurveLocation} location the offset or location at which to
* split the path
* @return {Path} the newly created path after splitting, if any
*
* @example {@paperscript}
* var path = new Path.Circle({
* center: view.center,
* radius: 40,
* strokeColor: 'black'