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

rotate-resize

v0.0.5

Published

市面上很多 Web 应用需要对某个元素进行拖转缩放旋转等操作。在这个元素旋转角度是 0 的情况下,大部分应用都能很好的处理。

Downloads

3

Readme

旋转拖拽算法

市面上很多 Web 应用需要对某个元素进行拖转缩放旋转等操作。在这个元素旋转角度是 0 的情况下,大部分应用都能很好的处理。

然而在旋转角度不是 0 的情况下,拖拽效果特别不自然。比如按住一个角控制点拖拽缩放一个元素时,本应该固定的对边点会到处乱跑。rotate-resize 包含一些算法用来实现这个交互的数值计算。

示例代码

Live Demo

安装

npm install rotate-resize

使用

import rotateResize from 'rotate-resize'

约定

我们使用形如 { x, y, w, h, r } 这样的对象来确定页面中一个矩形的大小、位置、旋转角度。其中旋转角度的取值是[-180, 180]。

另外每个矩形有九个控制点,如下图所示

                   rotate
                   |
lt-----------------ct-----------------rt
|                                      |
|                                      |
|                                      |
|                                      |
lm                                    rm
|                                      |
|                                      |
|                                      |
|                                      |
lb-----------------cb-----------------rb

使用

import rotateResize from 'rotate-resize'

在拖转缩放或者旋转时,都要从鼠标按住控制点开始。此时要记录三个变量:

  1. 记录鼠标其实点 mouseStart
  2. 鼠标按住的控制点 adjustType(也即要进行操作的类型);
  3. 初始矩形的位置、大小、旋转数据({ x, y, w, h, r }rectStart

type SideAdjustType = 'ct' | 'rm' | 'cb' | 'lm'
type CornerAdjustType = 'lt' | 'rt' | 'lb' | 'rb'
type RotateAdjustType = 'rotate'
type MoveAdjustType = 'move'

type Rect = {
  x: number,
  y: number,
  w: number,
  h: number,
  r: number   // 范围是[-180, 180]
}
type Position = {
  x: number,
  y: number
}

// mouseStart 是鼠标按下时的位置
// mouseEnd 是在更新时鼠标的最新位置

// 旋转
const resultRect: Rect = rotateResize(
  adjustType: RotateAdjustType,
  mouseStart: Position,
  mouseEnd: Position,
  rectStart: Rect
)

// 按住角控制点同时改变矩形的宽高
const resultRect: Rect = rotateResize(
  adjustType: CornerAdjustType,
  mouseStart: Position,
  mouseEnd: Position,
  rectStart: Rect
)

// 按住边控制点改变矩形的宽或者高
const resultRect: Rect = rotateResize(
  adjustType: SideAdjustType,
  mouseStart: Position,
  mouseEnd: Position,
  rectStart: Rect
)

// 按住角控制点同时改变矩形的宽高,维持固定宽高比,通常是检测是否按住了 shift 键
const resultRect: Rect = rotateResize(
  adjustType: CornerAdjustType,
  mouseStart: Position,
  mouseEnd: Position,
  rectStart: Rect,
  fixedRatio: boolean
)

// 按住边控制点改变矩形的宽或者高,维持固定宽高比,通常是检测是否按住了 shift 键
const resultRect: Rect = rotateResize(
  adjustType: SideAdjustType,
  mouseStart: Position,
  mouseEnd: Position,
  rectStart: Rect,
  fixedRatio: boolean
)

// 移动一个矩形
const resultRect: Rect = rotateResize(
  adjustType: MoveAdjustType,
  mouseStart: Position,
  mouseEnd: Position,
  rectStart: Rect
)