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

use-free-scale

v1.0.4

Published

useFreeScale is a lightweight and high-performance custom React Hook that provides the functionality of free scaling and dragging (compatible with mobile touch events) on HTML elements. It uses touchpad two-finger zoom and mouse dragging for scaling and m

Downloads

33

Readme

useFreeScale

useFreeScale is a lightweight and high-performance custom React Hook that provides the functionality of free scaling and dragging (compatible with mobile touch events) on HTML elements. It uses touchpad two-finger zoom and mouse dragging for scaling and moving operations.

Preview

Preview

Install

npm i use-free-scale

Usage

First, you need to call useFreeScale in your component and pass in a configuration object. This object has two optional properties: scaleStep and customTrans. scaleStep is the scale ratio, and customTrans is a function that takes the current transformation result and the new transformation result and returns a new transformation result.

import { useFreeScale, ITransRes, IRectData } from "use-free-scale";

const customLimitScaleTrans = (
  prev: ITransRes,
  v: ITransRes,
  rects: IRectData
) => {
  if (v.scale <= 0.3 || v.scale >= 3) {
    return prev;
  }
  return v;
};

const customFreeTrans = (_: ITransRes, v: ITransRes) => v;

const containerLimitTrans = (
  prev: ITransRes,
  v: ITransRes,
  rects: IRectData
) => {
  // 限制变换后的子元素不能超出容器
  const { originConatinerRect, originChildRect } = rects;
  // 高度加变换距离绝对值,小于容器高度
  const limitY =
    Math.abs(v.transXY[1]) + ((originChildRect?.height || 0) * v.scale) / 2 <
    (originConatinerRect?.height || 0) / 2;
  // 宽度加变换距离绝对值,小于容器宽度
  const limitX =
    Math.abs(v.transXY[0]) + ((originChildRect?.width || 0) * v.scale) / 2 <
    (originConatinerRect?.width || 0) / 2;

  if (!limitX || !limitY) {
    return prev;
  }
  return v;
};

const { containerRef, childRef, transform } = useFreeScale({
  customTrans: customLimitScaleTrans,
});

Then, you need to assign containerRef and childRef to your container element and child element, respectively. transform is a string that represents the current transformation state. You need to assign it to your child element's style.transform property.

<div ref={containerRef} style={{ width: "100%", height: "100%" }}>
  <div ref={childRef} style={{ transform }}>
    Content
  </div>
</div>

You can also use setRotate, setScale, and setTransXY to manually set the rotation angle, scale ratio, and displacement.

API

useFreeScale(config: IUseFreeScale)

Parameters

  • config: A configuration object with two optional properties.
    • scaleStep: The scale ratio, the default value is 0.1.
    • customTrans: A custom transformation function. It takes two parameters: the current transformation result and the new transformation result, and returns a new transformation result. The default value is an identity function.

Return Value

  • containerRef: A React.RefObject, you need to assign it to your container element.
  • childRef: A React.RefObject, you need to assign it to your child element.
  • transform: A string that represents the current transformation state. You need to assign it to your child element's style.transform property.
  • setRotate: A function that you can use to manually set the rotation angle.
  • setScale: A function that you can use to manually set the scale ratio.
  • setTransXY: A function that you can use to manually set the displacement.

Notes

This Hook can only be used in environments that support wheel events and mousedown, mousemove, mouseup events, such as modern web browsers. In addition, you need to ensure that your container element and child element are both block elements, and the size of the container element is large enough to accommodate the enlargement and movement of the child element.