UNPKG

@thi.ng/axidraw

Version:

Minimal AxiDraw plotter/drawing machine controller for Node.js

82 lines (81 loc) 1.86 kB
import { isNumber } from "@thi.ng/checks/is-number"; import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; import { cossin } from "@thi.ng/math/angle"; import { mix } from "@thi.ng/math/mix"; import { jitter } from "@thi.ng/vectors/jitter"; import { madd2 } from "@thi.ng/vectors/madd"; import { maddN2 } from "@thi.ng/vectors/maddn"; import { MOVE } from "./commands.js"; import { dip } from "./dip.js"; const linearPalette = (opts) => { const $opts = { pos: [0, 0], repeat: 1, jitter: 0, ...opts }; const dipOpts = __dipOpts($opts); return (id) => { if (id < 0 || id >= $opts.num) { illegalArgs(`invalid palette index: ${id}`); } return [ MOVE( jitter( null, maddN2([], $opts.step, id, $opts.pos), $opts.jitter ) ), ...dip($opts.repeat, dipOpts) ]; }; }; const radialPalette = (opts) => { const $opts = { repeat: 1, jitter: 0, startTheta: 0, endTheta: Math.PI * 2, ...opts }; const radius = isNumber($opts.radius) ? [$opts.radius, $opts.radius] : $opts.radius; const dipOpts = __dipOpts($opts); return (id) => { if (id < 0 || id >= $opts.num) { illegalArgs(`invalid palette index: ${id}`); } return [ MOVE( jitter( null, madd2( null, cossin( mix( $opts.startTheta, $opts.endTheta, id / $opts.num ) ), radius, $opts.pos ), $opts.jitter ) ), ...dip($opts.repeat, dipOpts) ]; }; }; const __dipOpts = (opts) => ({ down: opts.down, downDelay: opts.downDelay, up: opts.up, upDelay: opts.upDelay, commands: opts.commands }); export { linearPalette, radialPalette };