npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smooth-zoom

v1.5.0

Published

A light weight javascript library for zooming images.

Downloads

942

Readme

Smooth Zoom

License NPM Version Known Vulnerabilities NPM Downloads Dependencies

A light weight javascript library for zooming images like Medium, Google Photos and more.

Zoom example

👀 Demo

Install

# npm
npm i smooth-zoom
# pnpm
pnpm i smooth-zoom
# yarn
yarn add smooth-zoom

Usage

Import with script tag

<script src="node_modules/smooth-zoom/dist/zoom.min.js"></script>
<!-- or -->
<script src="https://cdn.jsdelivr.net/npm/smooth-zoom@latest/dist/zoom.min.js"></script>

Import as module

import Zoom from "smooth-zoom";

And calling it with any css-selector, HTMLElement (or Array of it), and NodeList will make your image zoomed on click.

// Css Selector
Zoom(".zoomable");

// HTMLElement || HTMLElement[]
Zoom(document.querySelector(".zoomable"));

Zoom([
    document.querySelector(".foo"),
    ...document.querySelectorAll(".zoomable"),
]);

// NodeList
Zoom(document.querySelectorAll(".zoomable"));

And you can simply use this with custom hook in react.

import { useRef } from "react";
import Zoom from "smooth-zoom";

export default function useZoom() {
    const zoom = useRef(Zoom());

    return {
        attach: zoom.current.attach,
        detach: zoom.current.detach,
    };
}

Add custom hook like above,

import { ImgHTMLAttributes, useEffect, useRef } from "react";
import { useZoom } from "@hooks";

export default function ZoomableImage(
    props: ImgHTMLAttributes<HTMLImageElement>
) {
    const { attach, detach } = useZoom();
    const imageRef = useRef<HTMLImageElement | null>(null);

    useEffect(() => {
        const { current: image } = imageRef;

        attach(image);

        return () => {
            detach(image);
        };
    }, []);

    return <img {...props} ref={imageRef} />;
}

And create a component like above.

Options

Zoom(".zoomable", {
    background: "auto",
    onTransitionEnd: (img) => {
        img.src = img.src.replace(/-[0-9]+\.jpg/, ".jpg");
    },
});

| Property | Type | Default | Description | | --------------- | ------------------------------- | ------------------- | ------------------------------------------------------------------------------------ | | background | string | rgba(0, 0, 0, 0.95) | Image's background color.Use auto to get average color of the image | | useMaximumSize | boolean | true | Find the longest width through image's natural width and srcset attribute | | onClick | (img: HTMLImageElement) => void | undefined | Function that fires on click. Note that img is original image element. | | onTransitionEnd | (img: HTMLImageElement) => void | undefined | Function that fires after zoom animation. Note that img is cloned image element. |

Methods

zoom(img: HTMLImageElement) => void

Zoom-in an image

const zoom = Zoom();

zoom.zoom(document.querySelector("img"));

attach(target: string | HTMLElement | NodeList | HTMLElement[]) => void

Make the target zoomed on click

const zoom = Zoom(".zoomable", {
    background: "rgba(0, 0, 0, .3)",
});

zoom.attach(".more-elements");

detach(target: string | HTMLElement | NodeList | HTMLElement[]) => void

Remove an event listener from the target

zoom.detach(".more-elements");