Skip to content

Commit 5e6c3bb

Browse files
committed
YES WE FIXED ZOOMING!!!
1 parent 4359cf2 commit 5e6c3bb

4 files changed

Lines changed: 81 additions & 15 deletions

File tree

src/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export const bottomMargin = 0.075*pageHeight; // margin at bottom of page
1010
export const pageSpace = 0.02*pageHeight; // space between pages
1111
export let noteSize = 0.03*width;
1212

13+
export const zoomFactor = 0.2; // how much to zoom in/out
14+
export const zoomMin = 0.75;
15+
export const zoomMax = 5;
16+
1317
// Setup
1418
export let titleSize = 0.05*width;
1519
export let titleY = 0.1*pageHeight;

src/main.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { createMeasure, createPageGroup } from './renderer.js';
1111
const canvas = SVG().addTo(CONFIG.container)
1212
.size(CONFIG.screenWidth, CONFIG.screenHeight)
1313
.viewbox(-CONFIG.autoMargin, -CONFIG.autoMargin, CONFIG.screenWidth, CONFIG.screenHeight);
14-
canvas.panZoom({ zoomMin: 0.25, zoomMax: 5, zoomFactor: 0.1 });
14+
canvas.panZoom({
15+
wheelZoom: false, // custom logic instead
16+
panning: true
17+
});
1518

1619
setupEditorUI(); // Initialize UI
1720
const musicLayer = canvas.group().id("music-layer");
@@ -105,13 +108,65 @@ document.querySelectorAll(".icon").forEach(icon => {
105108

106109
// Adding Measures
107110
document.getElementById("add-measure-btn").addEventListener("click", () => {
108-
for (let i=0; i < 150; ++i) addMeasure();
111+
for (let i=0; i < 400; ++i) addMeasure();
109112
addMeasure();
110113
});
111114

112115
// Zooming
113-
document.getElementById("zoom-in").addEventListener("click", () => canvas.zoom(canvas.zoom() * 1.2));
114-
document.getElementById("zoom-out").addEventListener("click", () => canvas.zoom(canvas.zoom() / 1.2));
116+
function getZoom(zoom) { // keep zoom within boundaries
117+
return Math.min(Math.max(zoom, CONFIG.zoomMin), CONFIG.zoomMax);
118+
}
119+
document.getElementById("zoom-in").addEventListener("click", () => canvas.zoom(getZoom(canvas.zoom() * (1+CONFIG.zoomFactor))));
120+
document.getElementById("zoom-out").addEventListener("click", () => canvas.zoom(getZoom(canvas.zoom() / (1+CONFIG.zoomFactor))));
121+
canvas.on("wheel", e => {
122+
e.preventDefault();
123+
124+
let newY = 0; // default initialize to 0 (we change it later)
125+
if (e.ctrlKey) { // zoom
126+
const zFactor = 1+CONFIG.zoomFactor;
127+
let currZoom = canvas.zoom();
128+
const newZoom = e.deltaY < 0? currZoom*zFactor : currZoom/zFactor;
129+
const point = canvas.point(e.clientX, e.clientY); // centers zoom around mouse
130+
canvas.zoom(getZoom(newZoom), point);
131+
132+
// check if we zoomed past any boundaries
133+
let vb = canvas.viewbox();
134+
currZoom = canvas.zoom(); // update variables
135+
newY = vb.y;
136+
const totalHeight = musicLayer.bbox().height; // height of all pages
137+
const margin = CONFIG.autoMargin/currZoom;
138+
const minY = -margin; // highest possible height
139+
let maxY = Math.max(minY, totalHeight + margin - vb.h); // lowest possible height relative to the top of the viewbox
140+
141+
if (vb.h >= totalHeight + (margin * 2) || newY < minY) { // zoomed out too far
142+
newY = minY;
143+
} else if (newY > maxY) {
144+
newY = maxY;
145+
}
146+
canvas.viewbox(vb.x, newY, vb.w, vb.h);
147+
148+
} else { // manual scrolling
149+
const vb = canvas.viewbox();
150+
const zoom = canvas.zoom(); // use zoom so that you don't scroll by a ton when zoomed into a small area
151+
152+
// deltaY pos=down, negative=up
153+
const scrollAmount = e.deltaY / zoom;
154+
let newY = vb.y + scrollAmount;
155+
156+
// limit how far they can scroll vertically
157+
const totalHeight = musicLayer.bbox().height; // height of all pages
158+
const margin = CONFIG.autoMargin/zoom;
159+
const minY = -margin; // highest possible height
160+
let maxY = Math.max(minY, totalHeight + margin - vb.h); // lowest possible height relative to the top of the viewbox
161+
162+
if (newY < minY) {
163+
newY = minY;
164+
} else if (newY > maxY) {
165+
newY = maxY;
166+
}
167+
canvas.viewbox(vb.x, newY, vb.w, vb.h); // min x coord, min y coord, width, height
168+
}
169+
}, { passive: false }); // tells browser im preventing default behavior
115170

116171
// Preview
117172
document.getElementById("preview-btn").addEventListener("click", () => {

src/renderer.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,20 @@ export function createPageGroup(pageIndex, container) {
5959

6060
const pageGroup = container.group()
6161
.id(`page-${pageIndex}`)
62-
.translate(0, yOffset); // move is unreliabel for groups!
62+
.translate(0, yOffset); // move is unreliable for groups!
6363

6464
pageGroup.rect(CONFIG.width, CONFIG.pageHeight)
6565
.fill("white")
6666
.stroke({ color: "#ccc", width: 0.25 })
6767
.back();
6868

69-
// expand SVG canvas height (fixed) so we can scroll to the new page
70-
const newHeight = yOffset + CONFIG.pageHeight + CONFIG.pageSpace;
71-
canvas.size(CONFIG.screenWidth, newHeight); // canvas svg
72-
canvas.viewbox(-CONFIG.autoMargin, -CONFIG.autoMargin, CONFIG.screenWidth, newHeight);
69+
// DON'T expand SVG canvas height (fixed). Only the vb changes when we scroll down
70+
const newHeight = yOffset + CONFIG.pageHeight + CONFIG.pageSpace + CONFIG.autoMargin;
71+
const vb = canvas.viewbox();
72+
console.log("Previous VB: " + vb);
73+
// canvas.viewbox(-CONFIG.autoMargin, -CONFIG.autoMargin, CONFIG.screenWidth, newHeight-CONFIG.pageSpace+CONFIG.autoMargin);
74+
// console.log("New VB: " + canvas.viewbox());
75+
// canvas.viewbox(-CONFIG.autoMargin, -CONFIG.autoMargin, CONFIG.screenWidth, newHeight);
7376

7477
// ⭐⭐ change later? set viewbox to the created page?
7578

src/style.css

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,26 @@ button:active {
211211
display: flex;
212212
gap: 2px;
213213
position: absolute;
214-
top: 0;
215-
right: 16px;
214+
top: 2px;
215+
right: 3px;
216216
}
217217

218218
#zoom-controls button {
219219
border: 1px solid gray;
220220
padding: 0 5px;
221221
}
222222

223+
/* Hide scrollbar for Chrome, Safari and Opera */
224+
#sheet-container::-webkit-scrollbar {
225+
display: none;
226+
}
227+
223228
#sheet-container {
224229
flex: 1;
225-
display: flex;
226-
flex-direction: column;
227-
align-items: center;
228-
gap: 3vw;
230+
height: 100vh;
229231
overflow-y: scroll;
232+
-ms-overflow-style: none; /* IE and Edge */
233+
scrollbar-width: none; /* Firefox */
230234
}
231235

232236
#sheet-container > svg {

0 commit comments

Comments
 (0)