Skip to content

Commit fcccdd0

Browse files
committed
horizontal zoom + panning limits
1 parent 5e6c3bb commit fcccdd0

1 file changed

Lines changed: 74 additions & 5 deletions

File tree

src/main.js

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ document.getElementById("zoom-out").addEventListener("click", () => canvas.zoom(
121121
canvas.on("wheel", e => {
122122
e.preventDefault();
123123

124-
let newY = 0; // default initialize to 0 (we change it later)
125124
if (e.ctrlKey) { // zoom
126125
const zFactor = 1+CONFIG.zoomFactor;
127126
let currZoom = canvas.zoom();
@@ -132,20 +131,52 @@ canvas.on("wheel", e => {
132131
// check if we zoomed past any boundaries
133132
let vb = canvas.viewbox();
134133
currZoom = canvas.zoom(); // update variables
135-
newY = vb.y;
134+
let newY = vb.y;
135+
let newX = vb.x;
136+
136137
const totalHeight = musicLayer.bbox().height; // height of all pages
138+
const totalWidth = musicLayer.bbox().width;
137139
const margin = CONFIG.autoMargin/currZoom;
140+
138141
const minY = -margin; // highest possible height
139142
let maxY = Math.max(minY, totalHeight + margin - vb.h); // lowest possible height relative to the top of the viewbox
140-
141143
if (vb.h >= totalHeight + (margin * 2) || newY < minY) { // zoomed out too far
142144
newY = minY;
143145
} else if (newY > maxY) {
144146
newY = maxY;
145147
}
146-
canvas.viewbox(vb.x, newY, vb.w, vb.h);
148+
149+
const minX = -margin;
150+
let maxX = Math.max(minX, totalWidth + margin - vb.w);
151+
if (vb.w >= totalWidth + (margin * 2) || newX < minX) {
152+
newX = minX;
153+
} else if (newX > maxX) {
154+
newX = maxX;
155+
}
156+
157+
canvas.viewbox(newX, newY, vb.w, vb.h);
147158

148-
} else { // manual scrolling
159+
} else if (e.shiftKey) { // manual horizontal scrolling
160+
const vb = canvas.viewbox();
161+
const zoom = canvas.zoom(); // use zoom so that you don't scroll by a ton when zoomed into a small area
162+
163+
const scrollAmount = e.deltaY / zoom;
164+
let newX = vb.x + scrollAmount;
165+
166+
// limit how far they can scroll horizontally
167+
const totalWidth = musicLayer.bbox().width; // width of pages
168+
const margin = CONFIG.autoMargin/zoom;
169+
const minX = -margin;
170+
let maxX = Math.max(minX, totalWidth + margin - vb.w);
171+
172+
if (newX < minX) {
173+
newX = minX;
174+
} else if (newX > maxX) {
175+
newX = maxX;
176+
}
177+
178+
canvas.viewbox(newX, vb.y, vb.w, vb.h); // min x coord, min y coord, width, height
179+
} else { // manual vertical scrolling
149180
const vb = canvas.viewbox();
150181
const zoom = canvas.zoom(); // use zoom so that you don't scroll by a ton when zoomed into a small area
151182

@@ -168,6 +199,44 @@ canvas.on("wheel", e => {
168199
}
169200
}, { passive: false }); // tells browser im preventing default behavior
170201

202+
// Panning
203+
canvas.on("panning", function(e) {
204+
let vb = e.detail.box; // contains info of what panZoom wants to change
205+
const zoom = canvas.zoom();
206+
let updated = false; // checks if boundary has been broken (if we need to prevent the panning)
207+
208+
const margin = CONFIG.autoMargin / zoom;
209+
const totalHeight = musicLayer.bbox().height; // total height of all pages
210+
const totalWidth = musicLayer.bbox().width; // total width of your page setup
211+
212+
const minY = -margin;
213+
let maxY = Math.max(minY, totalHeight + margin - vb.h);
214+
let newY = vb.y;
215+
if (vb.h >= totalHeight + (margin * 2) || newY < minY) {
216+
newY = minY;
217+
updated = true;
218+
} else if (newY > maxY) {
219+
newY = maxY;
220+
updated = true;
221+
}
222+
223+
const minX = -margin;
224+
let maxX = Math.max(minX, totalWidth + margin - vb.w);
225+
let newX = vb.x;
226+
if (vb.w >= totalWidth + (margin * 2) || newX < minX) {
227+
newX = minX;
228+
updated = true;
229+
} else if (newX > maxX) {
230+
newX = maxX;
231+
updated = true;
232+
}
233+
234+
if (updated) {
235+
e.preventDefault();
236+
canvas.viewbox(newX, newY, vb.w, vb.h);
237+
}
238+
})
239+
171240
// Preview
172241
document.getElementById("preview-btn").addEventListener("click", () => {
173242
alert("Preview functionality in progress.\nWhen complete, it will be a popup showing all individual pages of sheet music without drop zones occupying any space. The preview will ideally have proper spacing for different note durations and different spacing for each horizontal line of music so that all lines start and end at the same x values (properly lined up)");

0 commit comments

Comments
 (0)