@stringsync/vexml
Version:
MusicXML to Vexflow
49 lines (48 loc) • 1.23 kB
JavaScript
import * as util from '../util';
import { Point } from '../spatial';
import { Stack } from '../util';
/**
* Pen is a position tracker.
*
* The term "cursor" is overloaded in the codebase, so this is a simple alternative.
*/
export class Pen {
positions = new Stack();
constructor(initialPosition = Point.origin()) {
this.positions.push(initialPosition);
}
get x() {
return this.position().x;
}
get y() {
return this.position().y;
}
position() {
const position = this.positions.peek();
util.assertDefined(position);
return position;
}
moveTo(point) {
this.positions.pop();
this.positions.push(new Point(point.x, point.y));
}
moveBy(opts) {
const dx = opts.dx ?? 0;
const dy = opts.dy ?? 0;
const position = this.position();
this.moveTo({ x: position.x + dx, y: position.y + dy });
}
clone() {
const pen = new Pen();
pen.positions = this.positions.clone();
return pen;
}
save() {
this.positions.push(this.position());
}
restore() {
if (this.positions.size() > 1) {
this.positions.pop();
}
}
}