Skip to content

Commit

Permalink
fix notehead bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed Mar 23, 2023
1 parent 660856f commit 57fe2dc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/notehead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Stave } from './stave';
import { Stem } from './stem';
import { Tables } from './tables';
import { Category } from './typeguard';
import { defined, log, RuntimeError } from './util';
import { defined, log } from './util';

// eslint-disable-next-line
function L(...args: any[]) {
Expand Down Expand Up @@ -232,9 +232,6 @@ export class NoteHead extends Note {

/** Get the `BoundingBox` for the `NoteHead`. */
getBoundingBox(): BoundingBox {
if (!this.preFormatted) {
throw new RuntimeError('UnformattedNote', "Can't call getBoundingBox on an unformatted note.");
}
const spacing = this.checkStave().getSpacingBetweenLines();
const half_spacing = spacing / 2;
const min_y = this.y - half_spacing;
Expand Down
2 changes: 1 addition & 1 deletion src/stavenote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export class StaveNote extends StemmableNote {

const stave = this.stave;
if (stave) {
this._noteHeads.forEach((head) => head.setStave(stave));
this.setStave(stave);
}
this.calcNoteDisplacements();
return this;
Expand Down
6 changes: 3 additions & 3 deletions tests/notehead_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,16 @@ function basicBoundingBoxes(options: TestOptions, contextBuilder: ContextBuilder
const formatter = new Formatter();
const voice = new Voice(Flow.TIME4_4).setStrict(false);

const nh1 = new NoteHead({ duration: '4', line: 3 });
const nh2 = new NoteHead({ duration: '2', line: 2.5 });
const nh1 = new StaveNote({ keys: ['b/4'], duration: '4' });
const nh2 = new StaveNote({ keys: ['a/4'], duration: '2' });
const nh3 = new NoteHead({ duration: '1', line: 0 });

voice.addTickables([nh1, nh2, nh3]);
formatter.joinVoices([voice]).formatToStave([voice], stave);

voice.draw(ctx, stave);

for (const bb of [nh1.getBoundingBox(), nh2.getBoundingBox(), nh3.getBoundingBox()]) {
for (const bb of [nh1.noteHeads[0].getBoundingBox(), nh2.noteHeads[0].getBoundingBox(), nh3.getBoundingBox()]) {
ctx.rect(bb.getX(), bb.getY(), bb.getW(), bb.getH());
}
ctx.stroke();
Expand Down

0 comments on commit 57fe2dc

Please sign in to comment.