Skip to content

Commit 776b5cd

Browse files
committed
docs: introduce / simplify documentation
1 parent 143d36a commit 776b5cd

2 files changed

Lines changed: 78 additions & 30 deletions

File tree

intersect.d.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
* // { x: 50, y: 50, segment1: 1, segment2: 1, t1: 0.5, t2: 0.5 }
2929
* // ];
3030
*
31-
* @param {String|Array<PathComponent>} path1
32-
* @param {String|Array<PathComponent>} path2
33-
* @param {Boolean} [justCount=false]
31+
* @param {Path} path1
32+
* @param {Path} path2
33+
* @param {boolean} [justCount=false]
3434
*
35-
* @return {Array<Intersection>|Number}
35+
* @return {Intersection[]|number}
3636
*/
3737
declare function findPathIntersections(path1: Path, path2: Path, justCount: true): number;
3838
declare function findPathIntersections(path1: Path, path2: Path, justCount: false): Intersection[];
@@ -72,12 +72,13 @@ export function parsePath(path: Path): PathComponent[];
7272
/**
7373
* A string in the form of 'M150,150m0,-18a18,18,0,1,1,0,36a18,18,0,1,1,0,-36z'
7474
* or something like:
75+
*
7576
* [
76-
* ['M', 1, 2],
77-
* ['m', 0, -2],
78-
* ['a', 1, 1, 0, 1, 1, 0, 2 * 1],
79-
* ['a', 1, 1, 0, 1, 1, 0, -2 * 1],
80-
* ['z']
77+
* ['M', 1, 2],
78+
* ['m', 0, -2],
79+
* ['a', 1, 1, 0, 1, 1, 0, 2 * 1],
80+
* ['a', 1, 1, 0, 1, 1, 0, -2 * 1],
81+
* ['z']
8182
* ]
8283
*/
8384
declare type Path = string | PathComponent[];

intersect.js

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* @see https://github.com/adobe-webplatform/Snap.svg/blob/master/src/path.js
55
*/
66

7+
/**
8+
* @typedef { import('./intersect.js').PathComponent } PathComponent
9+
* @typedef { import('./intersect.js').Path } Path
10+
*/
11+
712
/* eslint no-fallthrough: "off" */
813

914
var p2s = /,?([a-z]),?/gi,
@@ -69,6 +74,13 @@ function cacher(f) {
6974
return newf;
7075
}
7176

77+
/**
78+
* Parse SVG path string and return an array of path components.
79+
*
80+
* @param {string} pathString
81+
*
82+
* @return {PathComponent[]}
83+
*/
7284
function parsePathString(pathString) {
7385

7486
if (!pathString) {
@@ -105,6 +117,13 @@ function parsePathString(pathString) {
105117
return pathComponents;
106118
}
107119

120+
/**
121+
* Checks if a path is already in absolute format.
122+
* An absolute path has all uppercase commands.
123+
*
124+
* @param {PathComponent[]} pathComponents
125+
* @return {boolean}
126+
*/
108127
function isPathAbsolute(pathComponents) {
109128

110129
for (var i = 0; i < pathComponents.length; i++) {
@@ -117,13 +136,17 @@ function isPathAbsolute(pathComponents) {
117136
return true;
118137
}
119138

120-
function isPathCurve(pathArray) {
121-
if (!isArray(pathArray) || !pathArray.length) {
122-
return false;
123-
}
124-
125-
for (var i = 0; i < pathArray.length; i++) {
126-
var command = pathArray[i][0];
139+
/**
140+
* Checks if a path is already in curve format.
141+
* A curve path only contains 'M' and 'C' commands.
142+
*
143+
* @param {PathComponent[]} pathComponents
144+
*
145+
* @return {boolean}
146+
*/
147+
function isPathCurve(pathComponents) {
148+
for (var i = 0; i < pathComponents.length; i++) {
149+
var command = pathComponents[i][0];
127150
if (command !== 'M' && command !== 'C') {
128151
return false;
129152
}
@@ -155,8 +178,13 @@ function pathToString() {
155178
return this.join(',').replace(p2s, '$1');
156179
}
157180

158-
function pathClone(pathArray) {
159-
var res = clone(pathArray);
181+
/**
182+
* @param {PathComponent[]} pathComponents
183+
*
184+
* @return {PathComponent[]}
185+
*/
186+
function pathClone(pathComponents) {
187+
var res = clone(pathComponents);
160188
res.toString = pathToString;
161189
return res;
162190
}
@@ -389,11 +417,11 @@ function findBezierIntersections(bez1, bez2, justCount) {
389417
* // { x: 50, y: 50, segment1: 1, segment2: 1, t1: 0.5, t2: 0.5 }
390418
* // ]
391419
*
392-
* @param {String|Array<PathDef>} path1
393-
* @param {String|Array<PathDef>} path2
394-
* @param {Boolean} [justCount=false]
420+
* @param {Path} path1
421+
* @param {Path} path2
422+
* @param {boolean} [justCount=false]
395423
*
396-
* @return {Array<Intersection>|Number}
424+
* @return {Intersection[]|number}
397425
*/
398426
export default function findPathIntersections(path1, path2, justCount) {
399427
path1 = pathToCurve(path1);
@@ -461,14 +489,26 @@ export default function findPathIntersections(path1, path2, justCount) {
461489
return res;
462490
}
463491

492+
/**
493+
* Test if path is a set of path components [ ['M', 0, 0 ], ['L', 100, 100], ...].
494+
*
495+
* @param {Path} path
496+
*
497+
* @return {boolean}
498+
*/
464499
function isPathComponents(path) {
465500
return isArray(path) && isArray(path[0]);
466501
}
467502

468-
function pathToAbsolute(pathArray) {
503+
/**
504+
* @param {PathComponent[]} pathComponents
505+
*
506+
* @return {PathComponent[]}
507+
*/
508+
function pathToAbsolute(pathComponents) {
469509

470-
if (isPathAbsolute(pathArray)) {
471-
return pathArray;
510+
if (isPathAbsolute(pathComponents)) {
511+
return pathComponents;
472512
}
473513

474514
var res = [],
@@ -479,18 +519,18 @@ function pathToAbsolute(pathArray) {
479519
start = 0,
480520
pa0;
481521

482-
if (pathArray[0][0] == 'M') {
483-
x = +pathArray[0][1];
484-
y = +pathArray[0][2];
522+
if (pathComponents[0][0] == 'M') {
523+
x = +pathComponents[0][1];
524+
y = +pathComponents[0][2];
485525
mx = x;
486526
my = y;
487527
start++;
488528
res[0] = [ 'M', x, y ];
489529
}
490530

491-
for (var r, pa, i = start, ii = pathArray.length; i < ii; i++) {
531+
for (var r, pa, i = start, ii = pathComponents.length; i < ii; i++) {
492532
res.push(r = []);
493-
pa = pathArray[i];
533+
pa = pathComponents[i];
494534
pa0 = pa[0];
495535

496536
if (pa0 != pa0.toUpperCase()) {
@@ -768,6 +808,13 @@ function curveBBox(x0, y0, x1, y1, x2, y2, x3, y3) {
768808
};
769809
}
770810

811+
/**
812+
* Convert path to a curve.
813+
*
814+
* @param {Path} path
815+
*
816+
* @return {PathComponent[]}
817+
*/
771818
function pathToCurve(path) {
772819

773820
if (!isPathComponents(path)) {

0 commit comments

Comments
 (0)