Skip to content

Commit c1e4c7c

Browse files
committed
SVG length scales with scaleFactor
1 parent 140c5fd commit c1e4c7c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/stream.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,8 @@ export class Stream<ElementType extends base.Music21Object = base.Music21Object>
24742474
* if height is undefined will use
24752475
* `this.renderOptions.height`. If still undefined, will use
24762476
* `this.estimateStreamHeight()`
2477+
*
2478+
* Estimated widths and heights are multiplied by this.renderOptions.scaleFactor.
24772479
*/
24782480
createNewDOM(
24792481
width?: number|string,
@@ -2504,7 +2506,10 @@ export class Stream<ElementType extends base.Music21Object = base.Music21Object>
25042506
const computedWidth
25052507
= this.estimateStaffLength()
25062508
+ this.renderOptions.staffPadding;
2507-
newCanvasOrDIV.setAttribute('width', computedWidth.toString());
2509+
newCanvasOrDIV.setAttribute(
2510+
'width',
2511+
(computedWidth * this.renderOptions.scaleFactor.x).toString()
2512+
);
25082513
}
25092514
if (height !== undefined) {
25102515
newCanvasOrDIV.setAttribute('height', height.toString());

tests/moduleTests/stream.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,26 @@ export default function tests() {
442442
assert.ok(true);
443443
});
444444

445+
test('music21.stream.Stream.createNewDOM', assert => {
446+
const s = music21.tinyNotation.TinyNotation('4/4 c2 c2 c2 c2');
447+
s.renderOptions.scaleFactor = {x: 1.0, y: 1.0};
448+
449+
const where1 = s.createNewDOM();
450+
const full_height = parseInt(where1.getAttribute('height'));
451+
const full_width = parseInt(where1.getAttribute('width'));
452+
453+
// sanity check
454+
assert.ok(full_height > 0);
455+
assert.ok(full_width > 0);
456+
457+
s.renderOptions.scaleFactor = {x: 0.5, y: 0.5};
458+
const where2 = s.createNewDOM();
459+
const half_height = parseInt(where2.getAttribute('height'));
460+
const half_width = parseInt(where2.getAttribute('width'));
461+
assert.equal(full_height / 2, half_height);
462+
assert.equal(full_width / 2, half_width);
463+
});
464+
445465
test('music21.stream.Stream.getElementsByClass', assert => {
446466
const s = new music21.stream.Stream();
447467
const n1 = new music21.note.Note('C#5');

0 commit comments

Comments
 (0)