Skip to content

Commit

Permalink
Shift rect elements using transform attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
wergo committed Dec 19, 2024
1 parent dbe56ae commit 65ade63
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions scoreWarper.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class ScoreWarper {
'path', // for slur, barline, (stem handled by note, staff lines ignored)
'use[x]', // for many elements
'text[x]',
// 'rect[x]',
'rect[x]',
// 'ellipse', // not for dots, for what?
// 'circle', // for what?
];
Expand Down Expand Up @@ -185,7 +185,7 @@ class ScoreWarper {
let notehead = note.querySelector('g.notehead');
let noteheadBB = notehead?.getBBox();
let noteX = noteheadBB.x + xShift; // + noteHeadBB.width / 2;
this.#translate(note, onsetSVGx - noteX, true);
this.#translate(note, onsetSVGx - noteX);
} else {
console.debug('No note element found: ', id);
}
Expand Down Expand Up @@ -713,23 +713,22 @@ class ScoreWarper {

// rect, use, text, ellipse, circle
else {
// console.debug('Potentiall shift ', item);
// if not within these elements, shift element
if (
!item.closest('.chord') &&
!item.closest('.note') &&
!item.closest('.arpeg') &&
!item.closest('.rest') &&
!item.closest('.hairpin')
) {
// not within
console.debug('Shift ', item, 'inside ', item.parentElement);
let attribute = 'x';
if (item.nodeName == 'ellipse' || item.nodeName == 'circle') {
attribute = 'cx';
}
let x = parseFloat(item.getAttribute(attribute));
let xShift = warpingFunction[Math.round(x)];
item.setAttribute(attribute, x + xShift);
this.#translate(item, xShift);
}
}
});
Expand All @@ -742,7 +741,7 @@ class ScoreWarper {
* @param {number} delta
* @param {Boolean} useExisting
*/
#translate(item, delta, useExisting = true) {
#translate(item, delta) {
let existingX = 0;
let transformList = item.transform.baseVal; // SVGTransformList

Expand All @@ -755,7 +754,7 @@ class ScoreWarper {
// trList.forEach((currTrans, i) => {
if (currTrans.type === SVGTransform.SVG_TRANSFORM_TRANSLATE) {
// console.debug('TRANSLATION FOUND: ', item);
if (useExisting) existingX = currTrans.matrix.e;
existingX = currTrans.matrix.e;
break;
}
}
Expand Down

0 comments on commit 65ade63

Please sign in to comment.