Creating a zoomable SVG typically involves manipulating the SVG's viewBox attribute to change the area of the image being displayed, often done by utilizing JavaScript to handle user interactions such as mouse scroll or touch gestures. Below is a basic example showing how you can achieve a zoomable SVG using HTML, CSS, and JavaScript.
- HTML Setup
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zoomable SVG</title>
<style>
#svg-container {
width: 100%;
height: 500px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="svg-container">
<!-- Inline SVG here -->
<svg id="mySVG" width="500" height="500" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
<!-- SVG Content -->
</svg>
</div>
<script src="zoom-svg.js"></script>
</body>
</html>
- JavaScript Interaction: Create a file named
zoom-svg.js and add the following code:
document.addEventListener('DOMContentLoaded', function () {
const svg = document.getElementById('mySVG');
let viewBox = { x: 0, y: 0, w: 500, h: 500 };
// Function to zoom and update the viewBox
function zoom(delta, mouseX, mouseY) {
// Calculate the zoom factor
const zoomFactor = 0.1;
const widthReduction = viewBox.w * zoomFactor * delta;
const heightReduction = viewBox.h * zoomFactor * delta;
// Update the viewBox properties
viewBox.w -= widthReduction;
viewBox.h -= heightReduction;
viewBox.x += widthReduction / 2;
viewBox.y += heightReduction / 2;
// Update the viewBox attribute
svg.setAttribute('viewBox', `${viewBox.x} ${viewBox.y} ${viewBox.w} ${viewBox.h}`);
}
// Mouse wheel event
svg.addEventListener('wheel', function (event) {
event.preventDefault();
const delta = event.deltaY > 0 ? 1 : -1; // Zoom direction
zoom(delta, event.offsetX, event.offsetY); // Zoom in at the mouse position
});
});
- CSS: Add style to ensure the SVG is responsive.
#svg-container {
width: 100%;
height: 500px; /* Set to desired fixed height */
overflow: hidden;
border: 1px solid #ccc; /* Optional: for visual boundary */
}
svg {
display: block;
width: 100%;
height: 100%;
cursor: pointer; /* Optional: Change cursor on hover over the SVG */
}
The JavaScript code provided here sets up an event listener for the DOMContentLoaded event to make sure the script runs only after the SVG is loaded in the DOM. It then establishes a zoom function that adjusts the view box according to the mouse wheel event. The zoom function calculates the new view box dimensions based on the direction the mouse wheel moved and adjusts the view accordingly.
The event.deltaY attribute indicates the direction of the mouse wheel scroll (up or down), and the zoom function is called to adjust the view box size. The new SVG view box is then applied to the viewBox attribute using the setAttribute method.
Make sure that the SVG content is placed within the svg element of your HTML document.
These code examples provide the basic functionality to zoom in and out on an SVG using the mouse wheel. Additional features, such as panning and zooming limits, can also be added for enhanced interactivity.