GithubHelp home page GithubHelp logo

mockingbot / react-resizable-rotatable-draggable Goto Github PK

View Code? Open in Web Editor NEW
262.0 9.0 90.0 1.46 MB

A rectangle react component which can be resized and rotated

Home Page: https://mockingbot.github.io/react-resizable-rotatable-draggable/

License: MIT License

JavaScript 100.00%

react-resizable-rotatable-draggable's Introduction

React-resizable-rotatable-draggable-rectangle

NPM JavaScript Style Guide

A react widget that can be resized and rotated via a handler.

Installation

npm install --save react-resizable-rotatable-draggable

Then you will need to install peer dependency

npm install --save styled-components

Usage

import React, { Component } from 'react'
import ResizableRect from 'react-resizable-rotatable-draggable'

class App extends Component {
  constructor() {
    super()
    this.state = {
      width: 100,
      height: 100,
      top: 100,
      left: 100,
      rotateAngle: 0
    }
  }

  handleResize = (style, isShiftKey, type) => {
    // type is a string and it shows which resize-handler you clicked
    // e.g. if you clicked top-right handler, then type is 'tr'
    let { top, left, width, height } = style
    top = Math.round(top)
    left = Math.round(left)
    width = Math.round(width)
    height = Math.round(height)
    this.setState({
      top,
      left,
      width,
      height
    })
  }

  handleRotate = (rotateAngle) => {
    this.setState({
      rotateAngle
    })
  }

  handleDrag = (deltaX, deltaY) => {
    this.setState({
      left: this.state.left + deltaX,
      top: this.state.top + deltaY
    })
  }

  render() {
    const {width, top, left, height, rotateAngle} = this.state
    return (
      <div className="App">
        <ResizableRect
          left={left}
          top={top}
          width={width}
          height={height}
          rotateAngle={rotateAngle}
          // aspectRatio={false}
          // minWidth={10}
          // minHeight={10}
          zoomable='n, w, s, e, nw, ne, se, sw'
          // rotatable={true}
          // onRotateStart={this.handleRotateStart}
          onRotate={this.handleRotate}
          // onRotateEnd={this.handleRotateEnd}
          // onResizeStart={this.handleResizeStart}
          onResize={this.handleResize}
          // onResizeEnd={this.handleUp}
          // onDragStart={this.handleDragStart}
          onDrag={this.handleDrag}
          // onDragEnd={this.handleDragEnd}
        />
      </div>
    )
  }
}

export default App

Props

Props Type Default Example
left number.isRequired 10
top number.isRequired 10
width number.isRequired 100
height number.isRequired 100
rotateAngle number 0 0
rotatable bool true true
zoomable string '' 'n, w, s, e, nw, ne, se, sw'
minWidth number 10 0
minHeight number 10 0
aspectRatio number (width / height) 1(which makes the rectangle a square)
onRotateStart func
onRotate func (rotateAngle)
onRotateEnd func
onResizeStart func
onResize func (style, isShiftKey, type)
onResizeEnd func
onDragStart func
onDrag func (deltaX, deltaY)
onDragEnd func

License

MIT © MockingBot

react-resizable-rotatable-draggable's People

Contributors

dependabot[bot] avatar dr-js avatar iny7 avatar pugson avatar ramirobg94 avatar weiwang120 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-resizable-rotatable-draggable's Issues

Can't create custom styles for Component

Hi! Great library.

I noticed the <StyledRect> component doesn't inherit a className from it's parent and it creates it's own style property. This makes styling the component difficult as you are reliant on the classNames provided by the library. It also means you can't extend the existing styled-component which is a bummer.

Add argument in handleResize or handleDrag method

I have multiple elements to be resizable and draggable. So in the resizable method and draggable method, is there any way to add an index as an argument to be used as the index in the arrays of all the elements?

Consider removing dependency to styled-components

While styled-components is great, I would like to argue that library like this would be much better off just by manipulating inline styles directly.

Reasoning:

  • There are many css-in-js libs and styled-components is just one of them. The majority of the potential user base cannot use this library because of this dependency. Basically just people who already use styled-components are willing to accept this as dependency.
  • Styled-components seems to add quite little value in this context as you basically just need to manipulate the size and shape of the object (as far as I understand). No reason why this couldn't be done inline.

Of course I can always fork and remove the dependency for myself, but I just wanted to put this here in case this was not very intentional choice.

ps. Otherwise the lib seems really nice and it solves exactly the issue I'm facing right now.

react 17+ support

while trying to install the package npm issued the error:

Could not resolve dependency:
peer react@"^16" from [email protected]

any plan to support higher versions?
i'm currently using
"react": "^17.0.2"

Not an issue more of curiosity

What're the calculations you are using when rotation is applied when dragging one of the click handlers?

Say you're calculating the top right drag handler in a similar way with this sudo code

newWidth = shapeWidth - (cursorX - cursorStartX);
newHeight = shapeHeight - (cursorY - cursorStartY);
newLeft = cursorStartX + (cursorX - cursorStartX);
newTop = cursorStartY + (cursorY - cursorStartY);

When you apply rotation how are you factoring the rotation?

How to add child elements to ResizableRect ?

I want to display an element inside ResizableRect , and drag-drop resize facility to that child element ....how to do that ?

here what i have tried , but it is only showing rectangle , not button inside it.

<ResizableRect 
    ...
>
   <button className="btn btn-primary">Button</button>
</ResizableRect>

怎么在组件内添加数据?

这样添加无效:

   <ResizableRect
          left={left}
          top={top}
          width={width}
          height={height}
          rotateAngle={rotateAngle}
          zoomable='n, w, s, e, nw, ne, se, sw'
          onRotate={this.handleRotate}
          onResize={this.handleResize}
          onDrag={this.handleDrag}
        >

          <div>content<div> 

      </ResizableRect>

结果:

content 不显示

修改了下源码,操作成功,特来感谢下开源团队!

TypeError: Cannot read property 'getBoundingClientRect' of undefined

I have created sample app using create-react-app , and copied your code from src and paste it .
resize and drag drop is working proper but , I got below error on Rotate :
screen shot 2018-10-16 at 14 26 39

package.json versions :

"prop-types": "^15.6.2",
  "react": "^16.5.2",
  "react-dom": "^16.5.2",
  "react-scripts": "2.0.5",
  "styled-components": "^4.0.0"

It is working in "react-scripts": "2.0.3", but as i changed "react-scripts": "2.0.5", it gives me error

Warning: Prop `className` did not match. Server: "StyledRect-sc-1uso172-0 eRDgKF rect single-resizer" Client: "StyledRect-sc-1uso172-0 gblcbk rect single-resizer"

Hello,
im using this module with Next.js and react and i get this error
: Warning: Prop className did not match. Server: "StyledRect-sc-1uso172-0 eRDgKF rect single-resizer" Client: "StyledRect-sc-1uso172-0 gblcbk rect single-resizer"

when i first load the page, also the class gblcbk is not added on the ResizableRect component,
but wehn i make a change on my code and then save, it renders properly,
so basically for the first load i dont get the compoent with this class : gblcbk
any ideas how i can resolve this ?
thanks

Snapping/Snap to grid

Any chance of getting snapping added to this? I have tried a few things but nothing seems to work.
It would be nice to have a couple of props such as "snap:true/false" and "snapDistance:10".
Apart from that I find this super useful!

Easy method to show or hide 'handles and controls'?

Would be very useful to have to option to easily toggle handles and control visibility but it's not clear that fucntion is readily available.
Consider have a number of images that you want to user to move and resize.
Ideally woudl show no handles until the user clicks on one. This keeps the images clean and signifies when an image is selected and cna be moved or resized.

How to get ‘e’?

Because i need to get ‘e’ to do sth:

  1. e.preventDefault()
  2. if(e.button === 0) …

Transformed parent

Say you wrap "ResizableRect" component in a parent div and apply transform scale(0.5) how would you tell the component to scale the value down?

e.g. the component thinks the mouse position is 100px from the top of the screen but its actually 50px

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.