2022-09-09 12:13:52 -05:00
|
|
|
import { version, name } from '../data.mjs'
|
2021-04-11 17:16:42 +02:00
|
|
|
|
2022-08-28 11:58:04 +02:00
|
|
|
// Method that draws the actual bartack
|
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])
|
2021-11-21 17:20:24 +01:00
|
|
|
for (const i in points.path1) {
|
2021-04-11 17:16:42 +02:00
|
|
|
if (points.path1[i]) path = path.line(points.path1[i])
|
|
|
|
if (points.path2[i]) path = path.line(points.path2[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2022-08-28 11:58:04 +02:00
|
|
|
// Helper method to generate the points to draw on later
|
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()],
|
2021-04-24 10:16:31 +02:00
|
|
|
path2: [path2.start()],
|
2021-04-11 17:16:42 +02:00
|
|
|
}
|
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,
|
2021-04-24 10:16:31 +02:00
|
|
|
bartackFractionAlong: false,
|
2021-04-11 17:16:42 +02:00
|
|
|
}
|
|
|
|
so = { ...defaults, ...so }
|
|
|
|
|
2022-09-04 19:41:55 +02:00
|
|
|
// Handle negative angle
|
|
|
|
if (so.angle < 0) so.angle = 360 + (so.angle % -360)
|
|
|
|
|
2021-04-11 17:16:42 +02:00
|
|
|
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
|
2022-01-16 17:33:48 +01:00
|
|
|
if (so.bartackAlong) guide = so.path.clone()
|
|
|
|
else if (so.bartackFractionAlong) {
|
|
|
|
if (so.start === so.end) return null
|
|
|
|
if (so.start > so.end) {
|
|
|
|
const newEnd = so.start
|
|
|
|
so.start = so.end
|
|
|
|
so.end = newEnd
|
|
|
|
}
|
2021-04-11 17:16:42 +02:00
|
|
|
if (so.start > 0) start = so.path.shiftFractionAlong(so.start)
|
|
|
|
if (so.end < 1) end = so.path.shiftFractionAlong(so.end)
|
2022-01-16 17:33:48 +01:00
|
|
|
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()
|
2021-04-11 17:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 21:13:35 +02:00
|
|
|
self.paths[`${so.prefix}bartack${so.suffix}`] = bartackPath(guide, so, self).attr(
|
|
|
|
'class',
|
|
|
|
'stroke-sm stroke-mark'
|
|
|
|
)
|
2021-04-11 17:16:42 +02:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2022-08-28 11:58:04 +02:00
|
|
|
|
|
|
|
// The actual plugin
|
|
|
|
export const plugin = {
|
|
|
|
name,
|
|
|
|
version,
|
|
|
|
macros: {
|
|
|
|
bartack: function (so) {
|
|
|
|
const self = this
|
|
|
|
return bartack(so, self)
|
|
|
|
},
|
|
|
|
bartackAlong: function (so) {
|
|
|
|
const self = this
|
|
|
|
so.bartackFractionAlong = false
|
|
|
|
so.bartackAlong = true
|
|
|
|
so.anchor = false
|
|
|
|
so.from = false
|
|
|
|
so.to = false
|
|
|
|
return bartack(so, self)
|
|
|
|
},
|
|
|
|
bartackFractionAlong: function (so) {
|
|
|
|
const self = this
|
|
|
|
so.bartackFractionAlong = true
|
|
|
|
so.bartackAlong = false
|
|
|
|
so.anchor = false
|
|
|
|
so.from = false
|
|
|
|
so.to = false
|
|
|
|
return bartack(so, self)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// More specifically named exports
|
|
|
|
export const bartackPlugin = plugin
|
|
|
|
export const pluginBartack = plugin
|