1
0
Fork 0
freesewing/plugins/plugin-annotations/src/pleat.mjs
2023-10-18 17:27:30 +02:00

115 lines
2.8 KiB
JavaScript

/*
* Defaults for the pleat macro
*/
const macroDefaults = {
classes: {
arrow: 'note',
from: 'note',
to: 'note dashed',
},
id: 'pleat',
margin: 35,
reverse: false,
}
// Export defs
export const pleatDefs = [
{
name: 'pleat',
def: (scale) => `
<marker id="pleatTo" markerWidth="10" markerHeight="6" orient="auto" refY="3" refX="10">
<path d="M 10,3 L 0,0 C 2,2 2,4 0,6 z" class="fill-note note" transform="scale(${scale})" />
</marker>
`,
},
]
/*
* The rmpleat macro
*/
const rmpleat = (id = macroDefaults.id, { store, part }) =>
store.removeMacroNodes(id, 'rmpleat', part)
/*
* The pleat macro
*/
const pleat = function (config, { paths, Path, log, Point, complete, scale, store }) {
/*
* Don't add a pleat when complete is false, unless force is true
*/
if (!complete && !config.force) return
/*
* Merge macro defaults with user-provided config to create the macro config (mc)
*/
const mc = {
...macroDefaults,
...config,
classes: macroDefaults.classes,
}
if (config.classes) mc.classes = { ...mc.classes, ...config.classes }
/*
* Make sure mc.from and mc.to are Point instances
*/
if (!mc.from || typeof mc.from.attr !== 'function') {
log.warn(`Pleat macro called without a valid from point. Using (0,0) for from.`)
mc.from = new Point(0, 0)
}
if (!mc.to || typeof mc.to.attr !== 'function') {
log.warn(`Pleat macro called without a valid to point. Using (666,666) for to.`)
mc.to = new Point(666, 666)
}
/*
* Get the list of IDs
* Initialize the verticle cadence
*/
const ids = store.generateMacroIds(['from', 'to', 'arrow'], mc.id)
const toIn = mc.to.shift(mc.from.shiftTowards(mc.to, 0.1).angle(mc.to) + 90, mc.margin * scale)
const fromIn = mc.from.shift(
mc.from.shiftTowards(mc.to, 0.1).angle(mc.from) + 270,
mc.margin * scale
)
/*
* Pleat line from
*/
paths[ids.from] = new Path()
.move(mc.from)
.line(fromIn)
.attr('class', mc.reverse ? mc.classes.to : mc.classes.from)
/*
* Pleat line to
*/
paths[ids.to] = new Path()
.move(mc.to)
.line(toIn)
.attr('class', mc.reverse ? mc.classes.from : mc.classes.to)
/*
* Pleat line arrow
*/
paths[ids.arrow] = mc.reverse
? new Path()
.move(mc.to.shiftFractionTowards(toIn, 0.25))
.line(mc.from.shiftFractionTowards(toIn, 0.25))
: new Path()
.move(mc.from.shiftFractionTowards(fromIn, 0.25))
.line(mc.to.shiftFractionTowards(fromIn, 0.25))
paths[ids.arrow].attr('class', mc.classes.arrow).attr('marker-end', 'url(#pleatTo)')
/*
* Store all IDs in the store so we can remove this macro with rmpleat
*/
store.storeMacroIds(mc.id, { paths: ids })
/*
* Returning ids is a best practice for FreeSewing macros
*/
return store.getMacroIds(mc.id)
}
// Export macros
export const pleatMacros = { pleat, rmpleat }