2021-04-11 17:16:42 +02:00
|
|
|
const name = (n, so) => `${so.prefix}${n}${so.suffix}`
|
|
|
|
|
2021-04-18 13:16:37 +02:00
|
|
|
const drawBartack = (points, self) => {
|
2021-04-11 17:16:42 +02:00
|
|
|
let path = new self.Path().move(points.path1[0])
|
|
|
|
for (let i in points.path1) {
|
|
|
|
if (points.path1[i]) path = path.line(points.path1[i])
|
|
|
|
if (points.path2[i]) path = path.line(points.path2[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:16:37 +02:00
|
|
|
const getPoints = (path, so) => {
|
2021-04-11 17:16:42 +02:00
|
|
|
let path1 = path.offset(so.width / 2)
|
|
|
|
let path2 = path.offset(so.width / -2)
|
2021-04-18 13:16:37 +02:00
|
|
|
let len = path1.length()
|
2021-04-11 17:16:42 +02:00
|
|
|
let len2 = path2.length()
|
|
|
|
|
|
|
|
// Make sure path1 is always the longest one
|
2021-04-18 13:16:37 +02:00
|
|
|
if (len2 > len) {
|
2021-04-11 17:16:42 +02:00
|
|
|
let tmp = path2
|
|
|
|
path2 = path1
|
|
|
|
path1 = tmp
|
2021-04-18 13:16:37 +02:00
|
|
|
len = len2
|
2021-04-11 17:16:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let points = {
|
|
|
|
path1: [path1.start()],
|
|
|
|
path2: [path2.start()]
|
|
|
|
}
|
2021-04-18 13:16:37 +02:00
|
|
|
let steps = Math.ceil((len / so.width) * so.density)
|
2021-04-11 17:16:42 +02:00
|
|
|
for (let i = 1; i < steps; i++) {
|
|
|
|
points.path1.push(path1.shiftFractionAlong((1 / steps) * i))
|
|
|
|
points.path2.push(path2.shiftFractionAlong((1 / steps) * i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return points
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:16:37 +02:00
|
|
|
const bartackPath = (path, so, self) => (path ? drawBartack(getPoints(path, so), self) : null)
|
2021-04-11 17:16:42 +02:00
|
|
|
|
|
|
|
export default function bartack(so, self) {
|
|
|
|
const defaults = {
|
|
|
|
width: 3,
|
|
|
|
length: 15,
|
|
|
|
density: 3,
|
|
|
|
angle: 0,
|
|
|
|
prefix: '',
|
|
|
|
suffix: '',
|
|
|
|
anchor: false,
|
|
|
|
path: false,
|
|
|
|
from: false,
|
|
|
|
to: false,
|
|
|
|
start: 0,
|
|
|
|
end: 1,
|
|
|
|
bartackAlong: false,
|
|
|
|
bartackFractionAlong: false
|
|
|
|
}
|
|
|
|
so = { ...defaults, ...so }
|
|
|
|
|
|
|
|
let guide = false
|
|
|
|
|
|
|
|
if (so.anchor)
|
|
|
|
// Anchor + angle + length
|
|
|
|
guide = new self.Path().move(so.anchor).line(so.anchor.shift(so.angle, so.length))
|
|
|
|
else if (so.from && so.to)
|
|
|
|
// From to
|
|
|
|
guide = new self.Path().move(so.from).line(so.to)
|
|
|
|
else if (so.path) {
|
|
|
|
// Along path
|
|
|
|
let start = false
|
|
|
|
let end = false
|
|
|
|
if (so.bartackAlong) {
|
|
|
|
if (so.start > 0) start = so.path.shiftAlong(so.start)
|
2021-04-18 13:19:12 +02:00
|
|
|
end = so.path.shiftAlong(so.end)
|
2021-04-11 17:16:42 +02:00
|
|
|
} else if (so.bartackFractionAlong) {
|
|
|
|
if (so.start > 0) start = so.path.shiftFractionAlong(so.start)
|
|
|
|
if (so.end < 1) end = so.path.shiftFractionAlong(so.end)
|
|
|
|
}
|
|
|
|
if (start && end) guide = so.path.split(start).pop().split(end).shift()
|
|
|
|
else if (start) guide = so.path.split(start).pop()
|
|
|
|
else if (end) guide = so.path.split(end).shift()
|
|
|
|
else guide = so.path.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
self.paths[name('bartack', so)] = bartackPath(guide, so, self).attr('class', 'stroke-sm bartack')
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|