|
| 1 | +# React-resizable-rotatable-draggable-rectangle |
| 2 | + |
| 3 | + |
| 4 | +A react widget that can be resized and rotated via a handler. |
| 5 | + |
| 6 | +### Installation |
| 7 | + |
| 8 | +`npm install --save react-resizable-rotatable-draggable` |
| 9 | + |
| 10 | +### Usage |
| 11 | + |
| 12 | +```javascript |
| 13 | +import React, { Component } from 'react' |
| 14 | +import ResizableRect from 'react-resizable-rotatable-draggable' |
| 15 | + |
| 16 | +class App extends Component { |
| 17 | + constructor() { |
| 18 | + super() |
| 19 | + this.state = { |
| 20 | + width: 100, |
| 21 | + height: 100, |
| 22 | + top: 100, |
| 23 | + left: 100, |
| 24 | + rotateAngle: 0 |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + handleResize = (style, isShiftKey, type) => { |
| 29 | + // type is a string and it shows which resize-handler you clicked |
| 30 | + // e.g. if you clicked top-right handler, then type is 'tr' |
| 31 | + let { top, left, width, height } = style |
| 32 | + top = Math.round(top) |
| 33 | + left = Math.round(left) |
| 34 | + width = Math.round(width) |
| 35 | + height = Math.round(height) |
| 36 | + this.setState({ |
| 37 | + top, |
| 38 | + left, |
| 39 | + width, |
| 40 | + height |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + handleRotate = (rotateAngle) => { |
| 45 | + this.setState({ |
| 46 | + rotateAngle |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + handleDrag = (deltaX, deltaY) => { |
| 51 | + this.setState({ |
| 52 | + left: this.state.left + deltaX, |
| 53 | + top: this.state.top + deltaY |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + render() { |
| 58 | + const {width, top, left, height, rotateAngle} = this.state |
| 59 | + return ( |
| 60 | + <div className="App"> |
| 61 | + <ResizableRect |
| 62 | + left={left} |
| 63 | + top={top} |
| 64 | + width={width} |
| 65 | + height={height} |
| 66 | + rotateAngle={rotateAngle} |
| 67 | + // aspectRatio={false} |
| 68 | + // minWidth={10} |
| 69 | + // minHeight={10} |
| 70 | + zoomable='n, w, s, e, nw, ne, se, sw' |
| 71 | + // rotatable={true} |
| 72 | + // onRotateStart={this.handleRotateStart} |
| 73 | + onRotate={this.handleRotate} |
| 74 | + // onRotateEnd={this.handleRotateEnd} |
| 75 | + // onResizeStart={this.handleResizeStart} |
| 76 | + onResize={this.handleResize} |
| 77 | + // onResizeEnd={this.handleUp} |
| 78 | + // onDragStart={this.handleDragStart} |
| 79 | + onDrag={this.handleDrag} |
| 80 | + // onDragEnd={this.handleDragEnd} |
| 81 | + /> |
| 82 | + </div> |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export default App |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | +### Props |
| 92 | + |
| 93 | +| Props | Type | Default | Example | |
| 94 | +|:-----------:|:-----------------------:|:-------:|:-------------------------------------:| |
| 95 | +|left | number.isRequired | // | 10 | |
| 96 | +|top | number.isRequired | // | 10 | |
| 97 | +|width | number.isRequired | // | 100 | |
| 98 | +|height | number.isRequired | // | 100 | |
| 99 | +|rotateAngle | number | 0 | 0 | |
| 100 | +|rotatable | bool | true | true | |
| 101 | +|zoomable | string | '' | 'n, w, s, e, nw, ne, se, sw' | |
| 102 | +|minWidth | number | 10 | 0 | |
| 103 | +|minHeight | number | 10 | 0 | |
| 104 | +|aspectRatio | number (width / height) | // | 1(which makes the rectangle a square) | |
| 105 | +|onRotateStart| func | // | // | |
| 106 | +|onRotate | func | // | (rotateAngle) | |
| 107 | +|onRotateEnd | func | // | // | |
| 108 | +|onResizeStart| func | // | // | |
| 109 | +|onResize | func | // | (style, isShiftKey, type) | |
| 110 | +|onResizeEnd | func | // | // | |
| 111 | +|onDragStart | func | // | // | |
| 112 | +|onDrag | func | // | (deltaX, deltaY) | |
| 113 | +|onDragEnd | func | // | // | |
0 commit comments