Certainly! To modify your code so that on mobile devices you can't accidentally zoom in or out of the webpage and can't scroll horizontally or vertically, we'll need to make a few adjustments:
-
Prevent user scaling (zooming) on mobile devices: We can do this by adding a viewport meta tag with user-scalable=no and setting maximum-scale=1 in your HTML.
-
Prevent scrolling: We'll ensure that the overflow is hidden and disable touch actions that cause scrolling.
-
Update touch event listeners: We'll prevent the default touch behaviors by calling e.preventDefault() and specifying { passive: false } when adding the event listeners.
Below is the modified code with these changes incorporated:
Modified svgGrid.js
document.addEventListener('DOMContentLoaded', function () {
const svg = document.getElementById('infiniteGridSVG');
const gridGroup = document.getElementById('grid');
let panX = 0;
let panY = 0;
let scale = 1;
let isPanning = false;
let startX = 0;
let startY = 0;
const gridSize = 50;
// Create the infinite grid
function drawGrid() {
gridGroup.innerHTML = ''; // Clear any previous grid
const width = svg.clientWidth;
const height = svg.clientHeight;
// Draw vertical lines
for (let x = -width; x < width * 2; x += gridSize) {
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', x);
line.setAttribute('y1', -height * 2);
line.setAttribute('x2', x);
line.setAttribute('y2', height * 2);
line.setAttribute('stroke', '#ccc');
line.setAttribute('stroke-width', '1');
gridGroup.appendChild(line);
}
// Draw horizontal lines
for (let y = -height; y < height * 2; y += gridSize) {
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', -width * 2);
line.setAttribute('y1', y);
line.setAttribute('x2', width * 2);
line.setAttribute('y2', y);
line.setAttribute('stroke', '#ccc');
line.setAttribute('stroke-width', '1');
gridGroup.appendChild(line);
}
}
// Handle panning with mouse
svg.addEventListener('mousedown', function (e) {
isPanning = true;
startX = e.clientX - panX;
startY = e.clientY - panY;
});
svg.addEventListener('mousemove', function (e) {
if (!isPanning) return;
panX = e.clientX - startX;
panY = e.clientY - startY;
updateTransform();
});
svg.addEventListener('mouseup', function () {
isPanning = false;
});
svg.addEventListener('mouseleave', function () {
isPanning = false;
});
// Handle touch-based panning
svg.addEventListener('touchstart', function (e) {
e.preventDefault(); // Prevent default touch behaviors
if (e.touches.length === 1) {
isPanning = true;
startX = e.touches[0].clientX - panX;
startY = e.touches[0].clientY - panY;
}
}, { passive: false }); // Set passive to false
svg.addEventListener('touchmove', function (e) {
e.preventDefault(); // Prevent default touch behaviors
if (e.touches.length === 1) {
panX = e.touches[0].clientX - startX;
panY = e.touches[0].clientY - startY;
updateTransform();
}
}, { passive: false }); // Set passive to false
svg.addEventListener('touchend', function () {
isPanning = false;
});
// Handle zooming (with scroll wheel)
svg.addEventListener('wheel', function (e) {
e.preventDefault();
const zoomFactor = e.deltaY > 0 ? 0.9 : 1.1;
scale *= zoomFactor;
updateTransform();
});
// Apply transformations to the grid
function updateTransform() {
gridGroup.setAttribute('transform', `translate(${panX}, ${panY}) scale(${scale})`);
}
window.addEventListener('resize', drawGrid);
drawGrid();
});
Explanation of Changes:
-
Prevent Default Touch Behaviors: We added e.preventDefault(); at the beginning of the touchstart and touchmove event handlers. This prevents the default scrolling and zooming behaviors.
-
Passive Event Listeners: We set { passive: false } when adding the touch event listeners. By default, touch event listeners are passive, which means e.preventDefault() won't work unless passive is set to false.
Modified svgCanvas.html
{% extends "base.html" %}
{% block title %}Family Tree{% endblock %}
{% block head %}
<!-- Prevent user scaling (zooming) on mobile devices -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<style>
/* Prevent scrolling */
html, body {
overflow: hidden;
margin: 0;
padding: 0;
}
/* Disable touch actions like scrolling and zooming */
#svgContainer {
touch-action: none;
}
</style>
{% endblock %}
{% block content %}
<div id="svgContainer" style="width: 100vw; height: 100vh; overflow: hidden;">
<svg id="infiniteGridSVG" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<g id="grid"></g>
</svg>
</div>
<script src="{{ url_for('static', filename='js/svg/svgGrid.js') }}"></script>
{% endblock %}
Explanation of Changes:
-
Viewport Meta Tag: Added <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> inside the {% block head %}. This prevents the user from zooming in or out on mobile devices.
-
CSS to Prevent Scrolling and Touch Actions:
- Added
overflow: hidden; to html and body to prevent scrolling.
- Added
touch-action: none; to #svgContainer to disable touch actions like scrolling and zooming.
-
Moved Inline Styles to CSS: Moved the touch-action CSS property into the <style> block to keep the HTML cleaner.
Additional Notes:
-
Preventing Scrolling: By setting overflow: hidden; on the html and body elements, we ensure that no scrollbars appear and the user cannot scroll the page.
-
Touch-Action Property: The touch-action: none; CSS property on #svgContainer tells the browser not to handle default touch actions (like scrolling or zooming) within this element.
-
Event Listener Options: By specifying { passive: false } in addEventListener, we allow e.preventDefault() to work in touch event handlers.
Summary
The modifications above prevent users on mobile devices from accidentally zooming or scrolling both horizontally and vertically. The combination of the viewport meta tag, CSS properties, and updated event listeners effectively disables default touch gestures that lead to zooming or scrolling, while still allowing your custom panning and zooming functionality within the SVG element.
Feel free to replace your existing svgGrid.js and svgCanvas.html with the modified versions above. If you have any questions or need further assistance, please let me know!