1
0
Fork 0
freesewing/plugins/plugin-annotations/src/grainline.mjs

57 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-02-20 22:58:05 +00:00
import { name, version } from '../data.mjs'
const markers = `
<marker orient="auto" refY="4.0" refX="10.0" id="grainlineFrom" style="overflow:visible;" markerWidth="12" markerHeight="8">
<path class="note fill-note" d="M 0,4 L 12,0 C 10,2 10,6 12,8 z" />
</marker>
<marker orient="auto" refY="4.0" refX="2.0" id="grainlineTo" style="overflow:visible;" markerWidth="12" markerHeight="8">
<path class="note fill-note" d="M 12,4 L 0,0 C 2,2 2,6 0,8 z" />
</marker>`
const dflts = { text: 'grainline' }
export const grainline = {
name,
version,
hooks: {
preRender: (svg) => {
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
},
},
macros: {
grainline: function (so = {}, { points, paths, Path, complete, setGrain }) {
if (so === false) {
delete points.grainlineFrom
delete points.grainlineTo
delete paths.grainline
setGrain(90) // Restoring default
return true
}
so = {
...dflts,
...so,
}
// setGrain relies on plugin-cutlist
if (typeof setGrain === 'function') {
setGrain(so.from.angle(so.to))
}
if (complete) {
points.grainlineFrom = so.from.shiftFractionTowards(so.to, 0.05)
points.grainlineTo = so.to.shiftFractionTowards(so.from, 0.05)
paths.grainline = new Path()
.move(points.grainlineFrom)
.line(points.grainlineTo)
.attr('class', 'note')
.attr('marker-start', 'url(#grainlineFrom)')
.attr('marker-end', 'url(#grainlineTo)')
.attr('data-text', so.text)
.attr('data-text-class', 'center fill-note')
}
},
},
}
// More specifically named exports
// export const grainlinePlugin = plugin
// export const pluginGrainline = plugin