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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-draggable-ball

v0.1.0

Published

react-draggable-ball;拖拽控制球

Downloads

135

Readme

react-draggable-ball 拖拽控制小球

简介

  • 有时候可能需要那种像手游一样的操作球来控制另一个物体的位置,所以我利用 react-draggable 封装起来做了个包。

  • 控制的物体不一定要可拖动,不可拖动也可以的。

  • https://yehuozhili.github.io/react-draggable-ball/

快速上手

  • demo 中的示例为此示例
import React, { useState } from "react";
import Draggable, { DraggableEvent, DraggableData } from "react-draggable";
import { useGetBall } from "react-draggable-ball";

function App() {
	const [state, setState] = useState({ x: 0, y: 0 });
	const [render, isDrag, , position] = useGetBall(setState, {
		ratioSpeed: { x: 0.5, y: 0.5 },
	});

	return (
		<div className="App">
			<Draggable
				position={state}
				onStop={(e: DraggableEvent, data: DraggableData) => {
					setState({ x: data.x, y: data.y });
				}}
			>
				<div
					style={{
						width: "100px",
						height: "100px",
						backgroundColor: "red",
					}}
				></div>
			</Draggable>

			<div
				style={{
					width: "100px",
					height: "100px",
					backgroundColor: "blue",
					position: "relative",
					transform: `translate(${state.x}px,${state.y}px)`,
				}}
			></div>

			<div>{isDrag}</div>
			<div>
				{state.x},{state.y}
			</div>
			<div>
				{position.x},{position.y}
			</div>
			{render}
		</div>
	);
}

export default App;

api

export interface IOptionsType {
	wrapperStyle?: CSSProperties; //这是外壳样式
	innerStyle?: CSSProperties; // 拖动的小球样式
	intervalDelay?: number; //interval延迟,越大,移动看起来越卡
	ratioSpeed?: IPositionState; //速度比率,用来控制state速度
	draggableProps?: Partial<DraggableProps>; //参考react-draggable属性
	innerContent?: ReactNode; //小球内容物,初始为null
}
export interface IPositionState {
	x: number;
	y: number;
}
//必传setState,用来控制
export function useGetBall(
	setState: React.Dispatch<React.SetStateAction<IPositionState>>,
	options?: IOptionsType
): [
	ReactNode, //这是渲染出来的拖拽小球
	boolean, //这是小球是否在拖拽状态
	React.Dispatch<React.SetStateAction<boolean>>, //这个可以改变是否拖拽状态
	IPositionState, //这是小球在壳里的位置
	React.Dispatch<React.SetStateAction<IPositionState>> //可以改变小球在壳里位置
];