-
Notifications
You must be signed in to change notification settings - Fork 0
/
point-along-path.js
56 lines (46 loc) · 1.59 KB
/
point-along-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
var collide = require('line-circle-collision')
var curve = require('./point-along-curve')
module.exports = function(path, closed, point, radius, nearest) {
var steps = 100
if (path.length <= 1)
return -1
for (var i=0; i<path.length; i++) {
var cur = path[i]
var last = null
var hit = false
var curvePoint = null
if (!closed && i===0)
continue
if (closed && i===0) {
last = path[path.length-1]
} else
last = path[i-1]
if (!last.curve && !cur.curve) {
hit = collide(last.position, cur.position, point, radius, nearest)
} else {
if (last.curve && cur.curve) {
var c1 = last.controls[1],
c2 = cur.controls[0]
curvePoint = curve.bezier(last.position, c1, c2, cur.position, point, radius, steps)
} else {
if (closed && i===0 && last.curve) {
var c1 = last.controls[1]
curvePoint = curve.quadratic(last.position, c1, cur.position, point, radius, steps)
} else {
var c1 = last.curve
? last.controls[1]
: cur.controls[0]
curvePoint = curve.quadratic(last.position, c1, cur.position, point, radius, steps)
}
}
}
if (curvePoint) {
nearest[0] = curvePoint[0]
nearest[1] = curvePoint[1]
hit = true
}
if (hit)
return i
}
return -1
}