paperjs-offset 1.0.7 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -2
- package/README.md +683 -41
- package/dist/index.cjs +769 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +760 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +773 -392
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -0
- package/dist/types/index.d.ts +22 -15
- package/dist/types/offset_core.d.ts +20 -0
- package/package.json +56 -26
- package/build.js +0 -57
- package/demo/demo.css +0 -16
- package/demo/demo.js +0 -125
- package/demo/index.html +0 -12
- package/demo/paperjs-offset.js +0 -660
- package/demo/paperjs-offset.min.js +0 -1
- package/dist/index.es5.js +0 -391
- package/dist/index.es5.js.map +0 -1
- package/dist/lib/bundle.js +0 -10
- package/dist/lib/bundle.js.map +0 -1
- package/dist/lib/index.js +0 -51
- package/dist/lib/index.js.map +0 -1
- package/dist/lib/offset.js +0 -347
- package/dist/lib/offset.js.map +0 -1
- package/dist/paperjs-offset.js +0 -660
- package/dist/paperjs-offset.min.js +0 -1
- package/dist/types/bundle.d.ts +0 -9
- package/dist/types/offset.d.ts +0 -5
- package/public/preview.jpg +0 -0
- package/src/bundle.ts +0 -18
- package/src/index.ts +0 -56
- package/src/offset.ts +0 -356
- package/tsconfig.json +0 -20
- package/tslint.json +0 -17
package/demo/index.html
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<html>
|
|
2
|
-
<head>
|
|
3
|
-
<title>Demo</title>
|
|
4
|
-
<link rel="stylesheet" href="./demo.css"/>
|
|
5
|
-
<script type="text/javascript" src="https://cdn.bootcss.com/paper.js/0.12.0/paper-core.min.js"></script>
|
|
6
|
-
<script type="text/javascript" src="./paperjs-offset.js"></script>
|
|
7
|
-
<script type="text/javascript" src="./demo.js"></script>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<canvas width="1200" height="768"></canvas>
|
|
11
|
-
</body>
|
|
12
|
-
</html>
|
package/demo/paperjs-offset.js
DELETED
|
@@ -1,660 +0,0 @@
|
|
|
1
|
-
(function (paper) {
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
paper = paper && Object.prototype.hasOwnProperty.call(paper, 'default') ? paper['default'] : paper;
|
|
5
|
-
|
|
6
|
-
var Arrayex;
|
|
7
|
-
(function (Arrayex) {
|
|
8
|
-
function Delete(array, item, deleteAll) {
|
|
9
|
-
if (deleteAll === void 0) { deleteAll = false; }
|
|
10
|
-
if (deleteAll) {
|
|
11
|
-
var predicate = (typeof item === 'function') ? (function (v, i, a) { return !item(v, i, a); }) : (function (v) { return v !== item; });
|
|
12
|
-
var afterDeleted = array.filter(predicate);
|
|
13
|
-
if (afterDeleted.length < array.length) {
|
|
14
|
-
array.splice.apply(array, [0, array.length].concat(afterDeleted));
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
var index = (typeof item === 'function') ? array.findIndex(item) : array.indexOf(item);
|
|
20
|
-
if (index > -1) {
|
|
21
|
-
array.splice(index, 1);
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
Arrayex.Delete = Delete;
|
|
28
|
-
function BatchDelete(array, items, deleteAll) {
|
|
29
|
-
if (deleteAll === void 0) { deleteAll = false; }
|
|
30
|
-
items.forEach(function (item) { Delete(array, item, deleteAll); });
|
|
31
|
-
}
|
|
32
|
-
Arrayex.BatchDelete = BatchDelete;
|
|
33
|
-
function Replace(array, item, newItem, replaceAll) {
|
|
34
|
-
if (replaceAll === void 0) { replaceAll = false; }
|
|
35
|
-
if (array != null) {
|
|
36
|
-
var replaced = false;
|
|
37
|
-
do {
|
|
38
|
-
var index = (typeof item === 'function') ? array.findIndex(item) : array.indexOf(item);
|
|
39
|
-
if (index > -1) {
|
|
40
|
-
array[index] = newItem;
|
|
41
|
-
replaced = true;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
} while (replaceAll);
|
|
47
|
-
return replaced;
|
|
48
|
-
}
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
Arrayex.Replace = Replace;
|
|
52
|
-
function Find(array, predicate) {
|
|
53
|
-
var index = array.findIndex(predicate);
|
|
54
|
-
return array[index];
|
|
55
|
-
}
|
|
56
|
-
Arrayex.Find = Find;
|
|
57
|
-
/**
|
|
58
|
-
* IncludeSome() determines if at least one element in items is included in array.
|
|
59
|
-
*/
|
|
60
|
-
function IncludeSome(array, items) {
|
|
61
|
-
return items.some(function (item) { return array.includes(item); });
|
|
62
|
-
}
|
|
63
|
-
Arrayex.IncludeSome = IncludeSome;
|
|
64
|
-
function OrderedInsert(orderedArray, item, compare, unique) {
|
|
65
|
-
if (unique === void 0) { unique = false; }
|
|
66
|
-
var index = orderedArray.findIndex(function (val) { return compare(val, item) >= 0; });
|
|
67
|
-
if (index > -1) {
|
|
68
|
-
var exist = compare(orderedArray[index], item) === 0;
|
|
69
|
-
orderedArray.splice(index, (exist && unique) ? 1 : 0, item);
|
|
70
|
-
return index;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
orderedArray.push(item);
|
|
74
|
-
return orderedArray.length - 1;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
Arrayex.OrderedInsert = OrderedInsert;
|
|
78
|
-
/**
|
|
79
|
-
* InsertBefore() inserts an item into array before nextItem. insert to the head of array if nextItem doesn't exist.
|
|
80
|
-
*/
|
|
81
|
-
function InsertBefore(array, item, nextItem) {
|
|
82
|
-
var index = array.indexOf(nextItem);
|
|
83
|
-
array.splice(Math.max(index, 0), 0, item);
|
|
84
|
-
}
|
|
85
|
-
Arrayex.InsertBefore = InsertBefore;
|
|
86
|
-
/**
|
|
87
|
-
* InsertAfter() inserts an item into array after prevItem. insert to the tail of array if prevItem doesn't exist.
|
|
88
|
-
*/
|
|
89
|
-
function InsertAfter(array, item, prevItem) {
|
|
90
|
-
var index = array.indexOf(prevItem);
|
|
91
|
-
if (index > -1) {
|
|
92
|
-
array.splice(index + 1, 0, item);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
array.push(prevItem);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
Arrayex.InsertAfter = InsertAfter;
|
|
99
|
-
function ApproximateIndex(array, value, epsilon) {
|
|
100
|
-
if (epsilon === void 0) { epsilon = 0; }
|
|
101
|
-
return array.findIndex(function (val) { return Math.abs(val - value) <= epsilon; });
|
|
102
|
-
}
|
|
103
|
-
Arrayex.ApproximateIndex = ApproximateIndex;
|
|
104
|
-
function Approximate(array, value, epsilon) {
|
|
105
|
-
if (epsilon === void 0) { epsilon = 0; }
|
|
106
|
-
var index = array.findIndex(function (val) { return Math.abs(val - value) <= epsilon; });
|
|
107
|
-
return array[index];
|
|
108
|
-
}
|
|
109
|
-
Arrayex.Approximate = Approximate;
|
|
110
|
-
function Empty(n) {
|
|
111
|
-
return n > 0 ? new Array(n).fill(null) : [];
|
|
112
|
-
}
|
|
113
|
-
Arrayex.Empty = Empty;
|
|
114
|
-
function Create(n, creator) {
|
|
115
|
-
return n > 0 ? new Array(n).fill(null).map(function (_, index) { return creator(index, n); }) : [];
|
|
116
|
-
}
|
|
117
|
-
Arrayex.Create = Create;
|
|
118
|
-
/**
|
|
119
|
-
* Repeat() repeats each element in sequence n times. e.g. Repeat(2, 1, 2) => [1, 1, 2, 2]
|
|
120
|
-
*/
|
|
121
|
-
function Repeat(n) {
|
|
122
|
-
var sequence = [];
|
|
123
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
124
|
-
sequence[_i - 1] = arguments[_i];
|
|
125
|
-
}
|
|
126
|
-
return Create(n * sequence.length, function (index) { return sequence[Math.floor(index / n)]; });
|
|
127
|
-
}
|
|
128
|
-
Arrayex.Repeat = Repeat;
|
|
129
|
-
/**
|
|
130
|
-
* RepeatSequence() repeats full sequence n tims. e.g. Repeat(2, 1, 2) => [1, 2, 1, 2]
|
|
131
|
-
*/
|
|
132
|
-
function RepeatSequence(n) {
|
|
133
|
-
var sequence = [];
|
|
134
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
135
|
-
sequence[_i - 1] = arguments[_i];
|
|
136
|
-
}
|
|
137
|
-
return Create(n * sequence.length, function (index) { return sequence[index % sequence.length]; });
|
|
138
|
-
}
|
|
139
|
-
Arrayex.RepeatSequence = RepeatSequence;
|
|
140
|
-
/**
|
|
141
|
-
* AnalyzePeriod() returns the minimum preriod of give array.
|
|
142
|
-
*/
|
|
143
|
-
function AnalyzePeriod(array) {
|
|
144
|
-
var threshold = Math.ceil(Math.sqrt(array.length));
|
|
145
|
-
for (var length_1 = 1; length_1 <= threshold; ++length_1) {
|
|
146
|
-
if (array.length % length_1 === 0) {
|
|
147
|
-
var sample = array.slice(0, length_1);
|
|
148
|
-
var validate = true;
|
|
149
|
-
for (var i = length_1; i < array.length; ++i) {
|
|
150
|
-
if (array[i] !== sample[i % length_1]) {
|
|
151
|
-
validate = false;
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (validate) {
|
|
156
|
-
return sample;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return array;
|
|
161
|
-
}
|
|
162
|
-
Arrayex.AnalyzePeriod = AnalyzePeriod;
|
|
163
|
-
/**
|
|
164
|
-
* Flat() flats the array inside an array.
|
|
165
|
-
*/
|
|
166
|
-
function Flat(array, keepNull) {
|
|
167
|
-
if (keepNull === void 0) { keepNull = false; }
|
|
168
|
-
var flat = Array.prototype.concat.apply([], array);
|
|
169
|
-
return keepNull ? flat : flat.filter(function (v) { return v != null; });
|
|
170
|
-
}
|
|
171
|
-
Arrayex.Flat = Flat;
|
|
172
|
-
/**
|
|
173
|
-
* Divide() divides a given array into partitions, each partition contains count element except the last one.
|
|
174
|
-
*/
|
|
175
|
-
function Divide(array, count) {
|
|
176
|
-
var divides = [];
|
|
177
|
-
for (var i = 0; i < array.length; i += count) {
|
|
178
|
-
divides.push(array.slice(i, i + count));
|
|
179
|
-
}
|
|
180
|
-
return divides;
|
|
181
|
-
}
|
|
182
|
-
Arrayex.Divide = Divide;
|
|
183
|
-
/**
|
|
184
|
-
* Group() groups items in array into a directMap by a groupBy function.
|
|
185
|
-
*/
|
|
186
|
-
function Group(array, groupBy) {
|
|
187
|
-
var groups = {};
|
|
188
|
-
array.forEach(function (item, index) {
|
|
189
|
-
var group = groupBy(item, index, array);
|
|
190
|
-
if (!groups[group]) {
|
|
191
|
-
groups[group] = [];
|
|
192
|
-
}
|
|
193
|
-
groups[group].push(item);
|
|
194
|
-
});
|
|
195
|
-
return groups;
|
|
196
|
-
}
|
|
197
|
-
Arrayex.Group = Group;
|
|
198
|
-
/**
|
|
199
|
-
* Sample() samples a subarray of count in array.
|
|
200
|
-
*/
|
|
201
|
-
function Sample(array, count, random) {
|
|
202
|
-
if (random === void 0) { random = false; }
|
|
203
|
-
if (count >= array.length) {
|
|
204
|
-
return array;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
if (!random) {
|
|
208
|
-
var interval_1 = array.length / count;
|
|
209
|
-
return Create(count, function (index) { return array[Math.round(interval_1 * index)]; });
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
var result = [];
|
|
213
|
-
var copy = array.slice();
|
|
214
|
-
for (var i = 0; i < count; ++i) {
|
|
215
|
-
var item = copy.splice(Math.round(copy.length * Math.random()), 1)[0];
|
|
216
|
-
result.push(item);
|
|
217
|
-
}
|
|
218
|
-
return result;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
Arrayex.Sample = Sample;
|
|
223
|
-
function Union(arrays, unique) {
|
|
224
|
-
if (unique === void 0) { unique = false; }
|
|
225
|
-
var union = Array.prototype.concat.apply([], arrays);
|
|
226
|
-
return unique ? Array.from(new Set(union)) : union;
|
|
227
|
-
}
|
|
228
|
-
Arrayex.Union = Union;
|
|
229
|
-
function Intersect(arrays) {
|
|
230
|
-
if (arrays.length > 1) {
|
|
231
|
-
var intersectMap_1 = new Map();
|
|
232
|
-
arrays[0].forEach(function (v) { return intersectMap_1.set(v, 1); });
|
|
233
|
-
for (var i = 1; i < arrays.length; ++i) {
|
|
234
|
-
arrays[i].forEach(function (item) {
|
|
235
|
-
if (intersectMap_1.has(item)) {
|
|
236
|
-
intersectMap_1.set(item, intersectMap_1.get(item) + 1);
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
var intersect_1 = new Array();
|
|
241
|
-
intersectMap_1.forEach(function (value, item) {
|
|
242
|
-
if (value === arrays.length) {
|
|
243
|
-
intersect_1.push(item);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
return intersect_1;
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
return arrays[0] || [];
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
Arrayex.Intersect = Intersect;
|
|
253
|
-
function Subtract(array, arrays) {
|
|
254
|
-
if (arrays.length > 1) {
|
|
255
|
-
var union_1 = new Set(Array.prototype.concat.apply([], arrays));
|
|
256
|
-
return array.filter(function (item) { return !union_1.has(item); });
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
return array || [];
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
Arrayex.Subtract = Subtract;
|
|
263
|
-
function NonNull(array) {
|
|
264
|
-
return array.filter(function (i) { return i != null; });
|
|
265
|
-
}
|
|
266
|
-
Arrayex.NonNull = NonNull;
|
|
267
|
-
})(Arrayex || (Arrayex = {}));
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Offset the start/terminal segment of a bezier curve
|
|
271
|
-
* @param segment segment to offset
|
|
272
|
-
* @param curve curve to offset
|
|
273
|
-
* @param handleNormal the normal of the the line formed of two handles
|
|
274
|
-
* @param offset offset value
|
|
275
|
-
*/
|
|
276
|
-
function offsetSegment(segment, curve, handleNormal, offset) {
|
|
277
|
-
var isFirst = segment.curve === curve;
|
|
278
|
-
// get offset vector
|
|
279
|
-
var offsetVector = (curve.getNormalAtTime(isFirst ? 0 : 1)).multiply(offset);
|
|
280
|
-
// get offset point
|
|
281
|
-
var point = segment.point.add(offsetVector);
|
|
282
|
-
var newSegment = new paper.Segment(point);
|
|
283
|
-
// handleOut for start segment & handleIn for terminal segment
|
|
284
|
-
var handle = (isFirst ? 'handleOut' : 'handleIn');
|
|
285
|
-
newSegment[handle] = segment[handle].add(handleNormal.subtract(offsetVector).divide(2));
|
|
286
|
-
return newSegment;
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Adaptive offset a curve by repeatly apply the approximation proposed by Tiller and Hanson.
|
|
290
|
-
* @param curve curve to offset
|
|
291
|
-
* @param offset offset value
|
|
292
|
-
*/
|
|
293
|
-
function adaptiveOffsetCurve(curve, offset) {
|
|
294
|
-
var hNormal = (new paper.Curve(curve.segment1.handleOut.add(curve.segment1.point), new paper.Point(0, 0), new paper.Point(0, 0), curve.segment2.handleIn.add(curve.segment2.point))).getNormalAtTime(0.5).multiply(offset);
|
|
295
|
-
var segment1 = offsetSegment(curve.segment1, curve, hNormal, offset);
|
|
296
|
-
var segment2 = offsetSegment(curve.segment2, curve, hNormal, offset);
|
|
297
|
-
// divide && re-offset
|
|
298
|
-
var offsetCurve = new paper.Curve(segment1, segment2);
|
|
299
|
-
// if the offset curve is not self intersected, divide it
|
|
300
|
-
if (offsetCurve.getIntersections(offsetCurve).length === 0) {
|
|
301
|
-
var threshold = Math.min(Math.abs(offset) / 10, 1);
|
|
302
|
-
var midOffset = offsetCurve.getPointAtTime(0.5).getDistance(curve.getPointAtTime(0.5));
|
|
303
|
-
if (Math.abs(midOffset - Math.abs(offset)) > threshold) {
|
|
304
|
-
var subCurve = curve.divideAtTime(0.5);
|
|
305
|
-
if (subCurve != null) {
|
|
306
|
-
return adaptiveOffsetCurve(curve, offset).concat(adaptiveOffsetCurve(subCurve, offset));
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
return [segment1, segment2];
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Create a round join segment between two adjacent segments.
|
|
314
|
-
*/
|
|
315
|
-
function makeRoundJoin(segment1, segment2, originPoint, radius) {
|
|
316
|
-
var through = segment1.point.subtract(originPoint).add(segment2.point.subtract(originPoint))
|
|
317
|
-
.normalize(Math.abs(radius)).add(originPoint);
|
|
318
|
-
var arc = new paper.Path.Arc({ from: segment1.point, to: segment2.point, through: through, insert: false });
|
|
319
|
-
segment1.handleOut = arc.firstSegment.handleOut;
|
|
320
|
-
segment2.handleIn = arc.lastSegment.handleIn;
|
|
321
|
-
return arc.segments.length === 3 ? arc.segments[1] : null;
|
|
322
|
-
}
|
|
323
|
-
function det(p1, p2) {
|
|
324
|
-
return p1.x * p2.y - p1.y * p2.x;
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Get the intersection point of point based lines
|
|
328
|
-
*/
|
|
329
|
-
function getPointLineIntersections(p1, p2, p3, p4) {
|
|
330
|
-
var l1 = p1.subtract(p2);
|
|
331
|
-
var l2 = p3.subtract(p4);
|
|
332
|
-
var dl1 = det(p1, p2);
|
|
333
|
-
var dl2 = det(p3, p4);
|
|
334
|
-
return new paper.Point(dl1 * l2.x - l1.x * dl2, dl1 * l2.y - l1.y * dl2).divide(det(l1, l2));
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Connect two adjacent bezier curve, each curve is represented by two segments,
|
|
338
|
-
* create different types of joins or simply removal redundant segment.
|
|
339
|
-
*/
|
|
340
|
-
function connectAdjacentBezier(segments1, segments2, origin, joinType, offset, limit) {
|
|
341
|
-
var curve1 = new paper.Curve(segments1[0], segments1[1]);
|
|
342
|
-
var curve2 = new paper.Curve(segments2[0], segments2[1]);
|
|
343
|
-
var intersection = curve1.getIntersections(curve2);
|
|
344
|
-
var distance = segments1[1].point.getDistance(segments2[0].point);
|
|
345
|
-
if (origin.isSmooth()) {
|
|
346
|
-
segments2[0].handleOut = segments2[0].handleOut.project(origin.handleOut);
|
|
347
|
-
segments2[0].handleIn = segments1[1].handleIn.project(origin.handleIn);
|
|
348
|
-
segments2[0].point = segments1[1].point.add(segments2[0].point).divide(2);
|
|
349
|
-
segments1.pop();
|
|
350
|
-
}
|
|
351
|
-
else {
|
|
352
|
-
if (intersection.length === 0) {
|
|
353
|
-
if (distance > Math.abs(offset) * 0.1) {
|
|
354
|
-
// connect
|
|
355
|
-
switch (joinType) {
|
|
356
|
-
case 'miter':
|
|
357
|
-
var join = getPointLineIntersections(curve1.point2, curve1.point2.add(curve1.getTangentAtTime(1)), curve2.point1, curve2.point1.add(curve2.getTangentAtTime(0)));
|
|
358
|
-
// prevent sharp angle
|
|
359
|
-
var joinOffset = Math.max(join.getDistance(curve1.point2), join.getDistance(curve2.point1));
|
|
360
|
-
if (joinOffset < Math.abs(offset) * limit) {
|
|
361
|
-
segments1.push(new paper.Segment(join));
|
|
362
|
-
}
|
|
363
|
-
break;
|
|
364
|
-
case 'round':
|
|
365
|
-
var mid = makeRoundJoin(segments1[1], segments2[0], origin.point, offset);
|
|
366
|
-
if (mid) {
|
|
367
|
-
segments1.push(mid);
|
|
368
|
-
}
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
else {
|
|
373
|
-
segments2[0].handleIn = segments1[1].handleIn;
|
|
374
|
-
segments1.pop();
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
else {
|
|
378
|
-
var second1 = curve1.divideAt(intersection[0]);
|
|
379
|
-
if (second1) {
|
|
380
|
-
var join = second1.segment1;
|
|
381
|
-
var second2 = curve2.divideAt(curve2.getIntersections(curve1)[0]);
|
|
382
|
-
join.handleOut = second2 ? second2.segment1.handleOut : segments2[0].handleOut;
|
|
383
|
-
segments1.pop();
|
|
384
|
-
segments2[0] = join;
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
segments2[0].handleIn = segments1[1].handleIn;
|
|
388
|
-
segments1.pop();
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* Connect all the segments together.
|
|
395
|
-
*/
|
|
396
|
-
function connectBeziers(rawSegments, join, source, offset, limit) {
|
|
397
|
-
var originSegments = source.segments;
|
|
398
|
-
var first = rawSegments[0].slice();
|
|
399
|
-
for (var i = 0; i < rawSegments.length - 1; ++i) {
|
|
400
|
-
connectAdjacentBezier(rawSegments[i], rawSegments[i + 1], originSegments[i + 1], join, offset, limit);
|
|
401
|
-
}
|
|
402
|
-
if (source.closed) {
|
|
403
|
-
connectAdjacentBezier(rawSegments[rawSegments.length - 1], first, originSegments[0], join, offset, limit);
|
|
404
|
-
rawSegments[0][0] = first[0];
|
|
405
|
-
}
|
|
406
|
-
return rawSegments;
|
|
407
|
-
}
|
|
408
|
-
function reduceSingleChildCompoundPath(path) {
|
|
409
|
-
if (path.children.length === 1) {
|
|
410
|
-
path = path.children[0];
|
|
411
|
-
path.remove(); // remove from parent, this is critical, or the style attributes will be ignored
|
|
412
|
-
}
|
|
413
|
-
return path;
|
|
414
|
-
}
|
|
415
|
-
/** Normalize a path, always clockwise, non-self-intersection, ignore really small components, and no one-component compound path. */
|
|
416
|
-
function normalize(path, areaThreshold) {
|
|
417
|
-
if (areaThreshold === void 0) { areaThreshold = 0.01; }
|
|
418
|
-
if (path.closed) {
|
|
419
|
-
var ignoreArea_1 = Math.abs(path.area * areaThreshold);
|
|
420
|
-
if (!path.clockwise) {
|
|
421
|
-
path.reverse();
|
|
422
|
-
}
|
|
423
|
-
path = path.unite(path, { insert: false });
|
|
424
|
-
if (path instanceof paper.CompoundPath) {
|
|
425
|
-
path.children.filter(function (c) { return Math.abs(c.area) < ignoreArea_1; }).forEach(function (c) { return c.remove(); });
|
|
426
|
-
if (path.children.length === 1) {
|
|
427
|
-
return reduceSingleChildCompoundPath(path);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
return path;
|
|
432
|
-
}
|
|
433
|
-
function isSameDirection(partialPath, fullPath) {
|
|
434
|
-
var offset1 = partialPath.segments[0].location.offset;
|
|
435
|
-
var offset2 = partialPath.segments[Math.max(1, Math.floor(partialPath.segments.length / 2))].location.offset;
|
|
436
|
-
var sampleOffset = (offset1 + offset2) / 3;
|
|
437
|
-
var originOffset1 = fullPath.getNearestLocation(partialPath.getPointAt(sampleOffset)).offset;
|
|
438
|
-
var originOffset2 = fullPath.getNearestLocation(partialPath.getPointAt(2 * sampleOffset)).offset;
|
|
439
|
-
return originOffset1 < originOffset2;
|
|
440
|
-
}
|
|
441
|
-
/** Remove self intersection when offset is negative by point direction dectection. */
|
|
442
|
-
function removeIntersection(path) {
|
|
443
|
-
var newPath = path.unite(path, { insert: false });
|
|
444
|
-
if (newPath instanceof paper.CompoundPath) {
|
|
445
|
-
newPath.children.filter(function (c) {
|
|
446
|
-
if (c.segments.length > 1) {
|
|
447
|
-
return !isSameDirection(c, path);
|
|
448
|
-
}
|
|
449
|
-
else {
|
|
450
|
-
return true;
|
|
451
|
-
}
|
|
452
|
-
}).forEach(function (c) { return c.remove(); });
|
|
453
|
-
return reduceSingleChildCompoundPath(newPath);
|
|
454
|
-
}
|
|
455
|
-
return path;
|
|
456
|
-
}
|
|
457
|
-
function getSegments(path) {
|
|
458
|
-
if (path instanceof paper.CompoundPath) {
|
|
459
|
-
return Arrayex.Flat(path.children.map(function (c) { return c.segments; }));
|
|
460
|
-
}
|
|
461
|
-
else {
|
|
462
|
-
return path.segments;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Remove impossible segments in negative offset condition.
|
|
467
|
-
*/
|
|
468
|
-
function removeOutsiders(newPath, path) {
|
|
469
|
-
var segments = getSegments(newPath).slice();
|
|
470
|
-
segments.forEach(function (segment) {
|
|
471
|
-
if (!path.contains(segment.point)) {
|
|
472
|
-
segment.remove();
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
function preparePath(path, offset) {
|
|
477
|
-
var source = path.clone({ insert: false });
|
|
478
|
-
source.reduce({});
|
|
479
|
-
if (!path.clockwise) {
|
|
480
|
-
source.reverse();
|
|
481
|
-
offset = -offset;
|
|
482
|
-
}
|
|
483
|
-
return [source, offset];
|
|
484
|
-
}
|
|
485
|
-
function offsetSimpleShape(path, offset, join, limit) {
|
|
486
|
-
var _a;
|
|
487
|
-
var source;
|
|
488
|
-
_a = preparePath(path, offset), source = _a[0], offset = _a[1];
|
|
489
|
-
var curves = source.curves.slice();
|
|
490
|
-
var raws = Arrayex.Divide(Arrayex.Flat(curves.map(function (curve) { return adaptiveOffsetCurve(curve, offset); })), 2);
|
|
491
|
-
var segments = Arrayex.Flat(connectBeziers(raws, join, source, offset, limit));
|
|
492
|
-
var newPath = removeIntersection(new paper.Path({ segments: segments, insert: false, closed: path.closed }));
|
|
493
|
-
newPath.reduce({});
|
|
494
|
-
if (source.closed && ((source.clockwise && offset < 0) || (!source.clockwise && offset > 0))) {
|
|
495
|
-
removeOutsiders(newPath, path);
|
|
496
|
-
}
|
|
497
|
-
// recovery path
|
|
498
|
-
if (source.clockwise !== path.clockwise) {
|
|
499
|
-
newPath.reverse();
|
|
500
|
-
}
|
|
501
|
-
return normalize(newPath);
|
|
502
|
-
}
|
|
503
|
-
function makeRoundCap(from, to, offset) {
|
|
504
|
-
var origin = from.point.add(to.point).divide(2);
|
|
505
|
-
var normal = to.point.subtract(from.point).rotate(-90, new paper.Point(0, 0)).normalize(offset);
|
|
506
|
-
var through = origin.add(normal);
|
|
507
|
-
var arc = new paper.Path.Arc({ from: from.point, to: to.point, through: through, insert: false });
|
|
508
|
-
return arc.segments;
|
|
509
|
-
}
|
|
510
|
-
function connectSide(outer, inner, offset, cap) {
|
|
511
|
-
if (outer instanceof paper.CompoundPath) {
|
|
512
|
-
var cs = outer.children.map(function (c) { return ({ c: c, a: Math.abs(c.area) }); });
|
|
513
|
-
cs = cs.sort(function (c1, c2) { return c2.a - c1.a; });
|
|
514
|
-
outer = cs[0].c;
|
|
515
|
-
}
|
|
516
|
-
var oSegments = outer.segments.slice();
|
|
517
|
-
var iSegments = inner.segments.slice();
|
|
518
|
-
switch (cap) {
|
|
519
|
-
case 'round':
|
|
520
|
-
var heads = makeRoundCap(iSegments[iSegments.length - 1], oSegments[0], offset);
|
|
521
|
-
var tails = makeRoundCap(oSegments[oSegments.length - 1], iSegments[0], offset);
|
|
522
|
-
var result = new paper.Path({ segments: heads.concat(oSegments, tails, iSegments), closed: true, insert: false });
|
|
523
|
-
result.reduce({});
|
|
524
|
-
return result;
|
|
525
|
-
default: return new paper.Path({ segments: oSegments.concat(iSegments), closed: true, insert: false });
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
function offsetSimpleStroke(path, offset, join, cap, limit) {
|
|
529
|
-
offset = path.clockwise ? offset : -offset;
|
|
530
|
-
var positiveOffset = offsetSimpleShape(path, offset, join, limit);
|
|
531
|
-
var negativeOffset = offsetSimpleShape(path, -offset, join, limit);
|
|
532
|
-
if (path.closed) {
|
|
533
|
-
return positiveOffset.subtract(negativeOffset, { insert: false });
|
|
534
|
-
}
|
|
535
|
-
else {
|
|
536
|
-
var inner = negativeOffset;
|
|
537
|
-
var holes = new Array();
|
|
538
|
-
if (negativeOffset instanceof paper.CompoundPath) {
|
|
539
|
-
holes = negativeOffset.children.filter(function (c) { return c.closed; });
|
|
540
|
-
holes.forEach(function (h) { return h.remove(); });
|
|
541
|
-
inner = negativeOffset.children[0];
|
|
542
|
-
}
|
|
543
|
-
inner.reverse();
|
|
544
|
-
var final = connectSide(positiveOffset, inner, offset, cap);
|
|
545
|
-
if (holes.length > 0) {
|
|
546
|
-
for (var _i = 0, holes_1 = holes; _i < holes_1.length; _i++) {
|
|
547
|
-
var hole = holes_1[_i];
|
|
548
|
-
final = final.subtract(hole, { insert: false });
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
return final;
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
function offsetPath(path, offset, join, limit) {
|
|
555
|
-
var nonSIPath = path.unite(path, { insert: false });
|
|
556
|
-
var result = nonSIPath;
|
|
557
|
-
if (nonSIPath instanceof paper.Path) {
|
|
558
|
-
result = offsetSimpleShape(nonSIPath, offset, join, limit);
|
|
559
|
-
}
|
|
560
|
-
else {
|
|
561
|
-
var children = Arrayex.Flat(nonSIPath.children.map(function (c) {
|
|
562
|
-
if (c.segments.length > 1) {
|
|
563
|
-
if (!isSameDirection(c, path)) {
|
|
564
|
-
c.reverse();
|
|
565
|
-
}
|
|
566
|
-
var offseted = offsetSimpleShape(c, offset, join, limit);
|
|
567
|
-
offseted = normalize(offseted);
|
|
568
|
-
if (offseted.clockwise !== c.clockwise) {
|
|
569
|
-
offseted.reverse();
|
|
570
|
-
}
|
|
571
|
-
if (offseted instanceof paper.CompoundPath) {
|
|
572
|
-
offseted.applyMatrix = true;
|
|
573
|
-
return offseted.children;
|
|
574
|
-
}
|
|
575
|
-
else {
|
|
576
|
-
return offseted;
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
else {
|
|
580
|
-
return null;
|
|
581
|
-
}
|
|
582
|
-
}), false);
|
|
583
|
-
result = new paper.CompoundPath({ children: children, insert: false });
|
|
584
|
-
}
|
|
585
|
-
result.copyAttributes(nonSIPath, false);
|
|
586
|
-
result.remove();
|
|
587
|
-
return result;
|
|
588
|
-
}
|
|
589
|
-
function offsetStroke(path, offset, join, cap, limit) {
|
|
590
|
-
var nonSIPath = path.unite(path, { insert: false });
|
|
591
|
-
var result = nonSIPath;
|
|
592
|
-
if (nonSIPath instanceof paper.Path) {
|
|
593
|
-
result = offsetSimpleStroke(nonSIPath, offset, join, cap, limit);
|
|
594
|
-
}
|
|
595
|
-
else {
|
|
596
|
-
var children = Arrayex.Flat(nonSIPath.children.map(function (c) {
|
|
597
|
-
return offsetSimpleStroke(c, offset, join, cap, limit);
|
|
598
|
-
}));
|
|
599
|
-
result = children.reduce(function (c1, c2) { return c1.unite(c2, { insert: false }); });
|
|
600
|
-
}
|
|
601
|
-
result.strokeWidth = 0;
|
|
602
|
-
result.fillColor = nonSIPath.strokeColor;
|
|
603
|
-
result.shadowBlur = nonSIPath.shadowBlur;
|
|
604
|
-
result.shadowColor = nonSIPath.shadowColor;
|
|
605
|
-
result.shadowOffset = nonSIPath.shadowOffset;
|
|
606
|
-
return result;
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
var PaperOffset = /** @class */ (function () {
|
|
610
|
-
function PaperOffset() {
|
|
611
|
-
}
|
|
612
|
-
PaperOffset.offset = function (path, offset, options) {
|
|
613
|
-
options = options || {};
|
|
614
|
-
var newPath = offsetPath(path, offset, options.join || 'miter', options.limit || 10);
|
|
615
|
-
if (options.insert === undefined) {
|
|
616
|
-
options.insert = true;
|
|
617
|
-
}
|
|
618
|
-
if (options.insert) {
|
|
619
|
-
(path.parent || paper.project.activeLayer).addChild(newPath);
|
|
620
|
-
}
|
|
621
|
-
return newPath;
|
|
622
|
-
};
|
|
623
|
-
PaperOffset.offsetStroke = function (path, offset, options) {
|
|
624
|
-
options = options || {};
|
|
625
|
-
var newStroke = offsetStroke(path, offset, options.join || 'miter', options.cap || 'butt', options.limit || 10);
|
|
626
|
-
if (options.insert === undefined) {
|
|
627
|
-
options.insert = true;
|
|
628
|
-
}
|
|
629
|
-
if (options.insert) {
|
|
630
|
-
(path.parent || paper.project.activeLayer).addChild(newStroke);
|
|
631
|
-
}
|
|
632
|
-
return newStroke;
|
|
633
|
-
};
|
|
634
|
-
return PaperOffset;
|
|
635
|
-
}());
|
|
636
|
-
/**
|
|
637
|
-
* @deprecated EXTEND existing paper module is not recommend anymore
|
|
638
|
-
*/
|
|
639
|
-
function ExtendPaperJs(paperNs) {
|
|
640
|
-
paperNs.Path.prototype.offset = function (offset, options) {
|
|
641
|
-
return PaperOffset.offset(this, offset, options);
|
|
642
|
-
};
|
|
643
|
-
paperNs.Path.prototype.offsetStroke = function (offset, options) {
|
|
644
|
-
return PaperOffset.offsetStroke(this, offset, options);
|
|
645
|
-
};
|
|
646
|
-
paperNs.CompoundPath.prototype.offset = function (offset, options) {
|
|
647
|
-
return PaperOffset.offset(this, offset, options);
|
|
648
|
-
};
|
|
649
|
-
paperNs.CompoundPath.prototype.offsetStroke = function (offset, options) {
|
|
650
|
-
return PaperOffset.offsetStroke(this, offset, options);
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
ExtendPaperJs(paper);
|
|
655
|
-
window.PaperOffset = {
|
|
656
|
-
offset: PaperOffset.offset,
|
|
657
|
-
offsetStroke: PaperOffset.offsetStroke,
|
|
658
|
-
};
|
|
659
|
-
|
|
660
|
-
}(paper));
|