首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >调整围绕其原点旋转的SVG的大小

调整围绕其原点旋转的SVG的大小
EN

Stack Overflow用户
提问于 2018-05-18 09:13:20
回答 1查看 1.1K关注 0票数 2

我有一个矩形的svg,可以围绕2d平面拖动,围绕它自己的原点旋转和调整大小。

代码语言:javascript
复制
class SVG extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      x: 100,
      y: 100,
      width: 50,
      height: 50,
      angle: 0,
      focusedElement: null
    }
  }
  
  handleMouseDown = (e) => {
    const focusedElement = e.target.getAttribute('data-element-type')
    this.setState({focusedElement})
  }
  
  handleMouseMove = (e) => {
    const {focusedElement} = this.state 
    if (!focusedElement) return
    else if (focusedElement === 'rectangle') this.moveRectangle(e)
    else if (focusedElement === 'resize') this.resizeRectangle(e)
    else if (focusedElement === 'rotate') this.rotateRectangle(e)
  }
  
  handleMouseUp = () => {
    this.setState({focusedElement: null})
  }
  
  moveRectangle = (e) => {
    const {width, height} = this.state
    
    this.setState({
      x: e.clientX - width / 2,
      y: e.clientY - height / 2
    })
  }
  
  resizeRectangle = (e) => {
    const {x, y} = this.state
    this.setState({
      width: e.clientX - x,
      height: e.clientY - y
    })
  }
  
  rotateRectangle = (e) => {
    const {x, y, width, height} = this.state
    const origin = {
      x: x + (width / 2),
      y: y + (height / 2),
    }
    const angle = Math.atan2(
      e.clientY - origin.y, 
      e.clientX - origin.x
    ) * 180 / Math.PI
    
    this.setState({angle})
  }
  
  render() {
    const {width, height, x, y, angle} = this.state
    
    return (
      <svg
        viewPort="0 0 300 300"
        style={{width: 300, height: 300, backgroundColor: '#999'}}
        onMouseUp={this.handleMouseUp}
        onMouseMove={this.handleMouseMove}
        onMouseDown={this.handleMouseDown}
      >
        <g
          transform={`
            translate(${x}, ${y})
            rotate(${angle}, ${(width / 2)}, ${(height / 2)})
          `}
        >
           
          <rect 
            width={width}
            height={height}
            fill="salmon"
            data-element-type="rectangle"
          />

          <rect 
            width={10}
            height={10}
            x={width - 10}
            y={height - 10}
            data-element-type="resize"
            fill="black"
          />

          <circle 
            r="7"
            cx={width + 7}
            cy={height / 2}
            data-element-type="rotate"
            fill="blue"
          />

        </g>
      </svg>
    )
  }
}

ReactDOM.render(<SVG />, document.getElementById('app'))
代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

</head>
<body>
  <div id="app"></div>
</body>
</html>

单击并拖动主体可以围绕平面移动,右侧的蓝色圆圈将旋转,右下角的正方形将调整大小

调整大小、在平面周围移动以及从0度旋转都可以按需工作

在发生旋转后尝试调整大小时会出现问题,svg的widthheight会发生更改,就像没有旋转一样

我的问题是,如何缩放形状的widthheightxy,以便实现更像photoshop的用户体验,或者http://editor.method.ac/如何处理旋转元素的大小调整?

以下是JSBin https://jsbin.com/mapumif/edit?js,output中的完整示例

注意: JSBin似乎有错误,所以如果它不能立即渲染,请将"Run with JS“按钮mash 10倍左右

我使用react组件来保持状态,但是任何解决方案都是非常受欢迎的

一如既往,我们非常感谢您的任何见解,感谢您的关注。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 07:02:48

此解决方案的工作方式是使用矩阵转换在调整高度和宽度时考虑形状的角度,而cos和sin函数则考虑由调整宽度和高度引起的坐标更改。

代码语言:javascript
复制
resizeRectangle = (e) => {
const {x, y, angle, width, height} = this.state

const point = this.svg.createSVGPoint()
point.x = e.clientX
point.y = e.clientY


const rotatedPoint = point.matrixTransform(
  this.rect.getScreenCTM().inverse()     
)

const widthDifference = rotatedPoint.x - width
const heightDifference = rotatedPoint.y - height

const widthOriginMovementRight = widthDifference * Math.cos(angle * Math.PI / 180) 
const widthOriginMovementDown = widthDifference * Math.sin(angle * Math.PI / 180)
const heightOriginMovementRight = heightDifference * Math.cos((angle+90) * Math.PI / 180)
const heightOriginMovementDown = heightDifference * Math.sin((angle+90) * Math.PI / 180)
const sumMovementX = widthOriginMovementRight + heightOriginMovementRight - widthDifference
const sumMovementY = widthOriginMovementDown + heightOriginMovementDown - heightDifference


this.setState({
  width: rotatedPoint.x,
  height: rotatedPoint.y,
  x: x + (sumMovementX /2) , 
  y: y + (sumMovementY /2)
})

用于查找旋转点的相同技术也必须引入到呈现逻辑中

代码语言:javascript
复制
 render() {
const {width, height, x, y, angle, focusedElement, start} = this.state


if (this.svg) {
  const point = this.svg.createSVGPoint()
  point.x = x 
  point.y = y
  var rotatedPoint = point.matrixTransform(
    this.rect.getScreenCTM().inverse()
  )
}

在return语句中

代码语言:javascript
复制
 return (
  <div>
  <svg
    ref={node => this.svg = node}
    viewPort="0 0 300 300"
    style={{width: 300, height: 300, backgroundColor: '#999'}}
    onMouseUp={this.handleMouseUp}
    onMouseMove={this.handleMouseMove}
    onMouseDown={this.handleMouseDown}
  >

    <g
      transform={
       ((!focusedElement && !!rotatedPoint) || focusedElement === 'resize') ?
        `
         translate(${x}, ${y})
         rotate(${angle})
         translate(${-rotatedPoint.x}, ${-rotatedPoint.y})

       `
        :
       `
         translate(${x}, ${y})
         rotate(${angle}, ${(width / 2)}, ${(height / 2)})
       `
      }
      ref={node => this.rect = node}
    >    
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50402466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档