
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@beoe/pan-zoom
Advanced tools
Small JS library to add pan and zoom functionality to SVG (inline or image). It supports gestures for all types of devices:
intention | mouse | trackpad/touchpad | touchscren |
---|---|---|---|
pan | click + move | click + move | two finger drag |
zoom | Ctrl + wheel | pinch | pinch |
reset | double click | double click | double tap |
scroll | wheel | two finger drag | one finger drag |
Pay attention:
pan(dx, dy)
and zoom(scale)
There are two flavors:
If you have container element in HTML:
import { PanZoom } from "@beoe/pan-zoom";
document.querySelectorAll(".beoe").forEach((container) => {
const element = container.firstElementChild;
if (!element) return;
new PanZoom({ element, container }).on();
});
If you don't have container element in HTML:
import { PanZoom } from "@beoe/pan-zoom";
document.querySelectorAll("svg, img[src$='.svg' i]").forEach((element) => {
if (element.parentElement?.tagName === "PICTURE") {
element = element.parentElement;
}
const container = document.createElement("div");
container.className = "beoe";
element.replaceWith(container);
container.append(element);
new PanZoom({ element, container }).on();
});
Additionally following CSS is required:
.beoe {
overflow: hidden;
touch-action: pan-x pan-y;
user-select: none;
cursor: grab;
}
.beoe svg,
.beoe img {
/* need to center smaller images to fix bug in zoom functionality */
margin: auto;
display: block;
/* need to fit bigger images */
max-width: 100%;
height: auto;
}
.beoe img {
pointer-events: none;
}
Instance methods:
on()
- adds event listenersoff()
- removes event listenerspan(dx, dy)
- pans imagezoom(scale)
- zooms imagereset()
- resets pan and zoom valuesimport "@beoe/pan-zoom/css/PanZoomUi.css";
import { PanZoomUi } from "@beoe/pan-zoom";
document.querySelectorAll(".beoe").forEach((container) => {
const element = container.firstElementChild;
if (!element) return;
new PanZoomUi({ element, container }).on();
});
Additionally, CSS to style UI required (for example with Tailwind):
.beoe .buttons {
@apply inline-flex;
}
.beoe .zoom-in {
@apply bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-l;
}
.beoe .reset {
@apply bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4;
}
.beoe .zoom-out {
@apply bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-r;
}
You can configure HTML classes used by UI:
const classes = {
zoomIn: "zoom-in",
reset: "reset",
zoomOut: "zoom-out",
buttons: "buttons",
tsWarning: "touchscreen-warning",
tsWarningActive: "active",
};
new PanZoomUi({ element, container, classes });
and message with instructions for the touchscreen:
const message = "Use two fingers to pan and zoom";
new PanZoomUi({ element, container, message });
Be aware that some CSS will cause pixelation of SVG on zoom (bug in Safari), for example:
will-change: transform;
transform: matrix3d(...);
transition-property: transform;
(it setles after animation, though)There are a lot of solutions for this task:
spread by Tomas Knopp from Noun Project (CC BY 3.0)
FAQs
Pan and zoom for SVG images
The npm package @beoe/pan-zoom receives a total of 176 weekly downloads. As such, @beoe/pan-zoom popularity was classified as not popular.
We found that @beoe/pan-zoom demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.