dragger.js 31.7 KB
Newer Older
huahua committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
/**
 * @description: 拖拽类
 * @param {
 *  canRotate: true, 可旋转
 *  canZoom: true, 可缩放
 *  canPull: true, 可拉升
 *  canMove: true, 可平移
 *  showAngle: false, 展示角度
 *  showPosition: false, 展示位置
 *  isScale: true 是否等比例缩放
 * } 
 * @return:  
 */
const Drag = {
	
  constructor (obj) {
	console.log('---------')
    this.id = obj.id
    this.initParameter(obj)
  },
  /**
   * @description: 初始化参数 
   * @param {type} 
   * @return: 
   */
  initParameter (obj) {
    this.canRotate = obj.canRotate === undefined ? true : obj.canRotate
    this.canZoom = obj.canZoom === undefined ? true : obj.canZoom
    this.canPull = obj.canPull === undefined ? true : obj.canPull
    this.canMove = obj.canMove === undefined ? true : obj.canMove
    this.showBorder = obj.showBorder === undefined ? true : obj.showBorder
    this.showAngle = obj.showAngle
    this.showPosition = obj.showPosition
    this.isScale = obj.isScale === undefined ? true : obj.isScale
    this.container = obj.container ? document.getElementById(obj.container) : document.body
    this.targetObj = document.getElementById(obj.id)
    this.initPos()
    this.initPannel()
    this.initEvent()
  },
  /**
   * @description: 判断是否有pannel面板
   * @param {type} 
   * @return: 
   */
  initPannel () {
    this.pannelDom = document.querySelector('#pannel')
    if (!this.pannelDom) {
      this.pannelDom = document.createElement('div')
      this.pannelDom.id = 'pannel'
      this.container.appendChild(this.pannelDom)
    } else {
      this.pannelDom.innerHTML = ''
    }
    this.initPannelDom()
  },
  /**
   * @description: 初始化事件
   * @param {type} 
   * @return: 
   */
initEvent () {
  function throttle(fn, interval) {
    let canRun = true;
    return function () {
      if (!canRun) return;
      canRun = false;
      setTimeout(() => {
        fn.apply(this, arguments);
        canRun = true;
      }, interval);
    };
  }
  let that = this
  document.addEventListener('mousemove', throttle(function (e) {
    e.preventDefault && e.preventDefault()
    that.moveChange(e, that.targetObj)
  }, 10))
  document.addEventListener('mouseup', e => {
    this.moveLeave(this.targetObj)
  })
  // this.container.addEventListener('mousedown', e => {
  //   this.mergeBox.flag = true
  //   this.initPannel()
  //   this.mergeBox.initPos = {
  //     x: Math.floor(e.clientX),
  //     y: Math.floor(e.clientY)
  //   }
  //   this.container.addEventListener('mousemove', e => {
  //     if (this.mergeBox.flag) {
  //       this.mergeBox.left = this.mergeBox.initPos.x
  //       this.mergeBox.top = this.mergeBox.initPos.y
  //       this.pannelDom.style.left = `${this.mergeBox.initPos.x}px`
  //       this.pannelDom.style.top = `${this.mergeBox.initPos.y}px`
  //       this.pannelDom.style.width = `${Math.floor(e.clientX) - this.mergeBox.initPos.x}px`
  //       this.pannelDom.style.height = `${Math.floor(e.clientY) - this.mergeBox.initPos.y}px`
  //     }
  //   })
  // })
  if (this.canMove) {
    // 外层给this.pannelDom添加mousedown事件,是在所有实例化结束后,panneldom被展示在最后一个实例化对象上,鼠标按下它时,触发moveInit事件
    this.pannelDom.onmousedown = e => {
      e.stopPropagation()
      this.moveInit(9, e, this.targetObj)
    }
    this.targetObj.onmousedown = e => {
      e.stopPropagation()
      this.moveInit(9, e, this.targetObj)
      this.initPannel()
      // 在点击其他未被选中元素时,pannel定位到该元素上,重写pannelDom事件,因为此时的this.pannelDom已经根据新的目标元素被重写
      this.pannelDom.onmousedown= e => {
        this.moveInit(9, e, this.targetObj)
      }
    }
  }
},
  /**
   * @description: 初始化Pannel内部dom结构
   * @param {} 
   * @return: null
   */
  initPannelDom () {
    this.pannelDom.style.left = `${this.left}px`
    this.pannelDom.style.top = `${this.top}px`
    this.pannelDom.style.width = `${this.width}px`
    this.pannelDom.style.height = `${this.height}px`
    this.pannelDom.style.transform = `rotate(${this.angle}deg)`
    if (this.canRotate) {
      let rotatePoint = document.createElement('span')
      rotatePoint.className = `${this.id}-dragger-rotate-point dragger-rotate-point`
      if (this.showAngle) {
        this.angleDom = document.createElement('span')
        this.angleDom.className = `dragger-rotate-angle ${this.id}-dragger-rotate-angle`
        this.angleDom.style.display = 'none'
        this.pannelDom.appendChild(this.angleDom)
      }
      this.pannelDom.appendChild(rotatePoint)
      rotatePoint.addEventListener('mousedown', e => {
        e.stopPropagation()
        this.moveInit(0, e, this.targetObj)
      })
    }
    if (this.showBorder) {
      for (let i = 1; i < 5; i++) {
        let baseLine = document.createElement('span')
        baseLine.className = `${this.id}-dragger-base-line dragger-base-line dragger-line${i}`
        this.pannelDom.appendChild(baseLine)
      }
    }
    if (this.canZoom) {
      for (let i = 1; i < 5; i++) {
        let zoomPoint = document.createElement('span')
        zoomPoint.className = `${this.id}-dragger-base-piont dragger-base-piont dragger-zoom${i}`
        this.pannelDom.appendChild(zoomPoint)
        zoomPoint.addEventListener('mousedown', e => {
          e.stopPropagation()
          this.moveInit(i, e, this.targetObj)
        })
      }
    }
    if (this.canPull) {
      for (let i = 5; i < 9; i++) {
        let pullPoint = document.createElement('span')
        pullPoint.className = `${this.id}-dragger-base-piont dragger-base-piont dragger-pull${i}`
        this.pannelDom.appendChild(pullPoint)
        pullPoint.addEventListener('mousedown', e => {
          e.stopPropagation()
          this.moveInit(i, e, this.targetObj)
        })
      }
    }
    if (this.canMove && this.showPosition) {
      this.positionDom = document.createElement('span')
      this.positionDom.className = 'dragger-position'
      this.pannelDom.appendChild(this.positionDom)
      this.positionDom.style.display = 'none'
    }
  },
  /**
   * @description: 初始化targetObj的位置
   * @param {type} 
   * @return: 
   */
  initPos () {
    this.left = this.targetObj.offsetLeft
    this.top = this.targetObj.offsetTop
    this.width = this.targetObj.offsetWidth
    this.height = this.targetObj.offsetHeight
    this.angle = this.getRotate(this.targetObj)
    // 记录初始盒子位置右下
    this.rightBottomPoint = {
      x: this.width + this.left,
      y: this.height + this.top
    }
    // 记录初始盒子右上角位置
    this.rightTopPoint = {
      x: this.width + this.left,
      y: this.top
    } 
    // 记录左上角的位置
    this.leftTopPoint = {
      x: this.left,
      y: this.top
    }
    // 左下
    this.leftBottomPoint = {
      x: this.left,
      y: this.top + this.height
    }
    // 左中
    this.leftMiddlePoint = {
      x: this.left,
      y: this.top  + this.height / 2
    }
    // 右中
    this.rightMiddlePoint = {
      x: this.left  + this.width,
      y: this.top  + this.height / 2
    }
    // 上中
    this.topMiddlePoint = {
      x: this.left  + this.width / 2,
      y: this.top
    }
    // 下中
    this.bottomMiddlePoint= {
      x: this.left  + this.width / 2,
      y: this.top  + this.height
    }
    // 记录中心位置
    this.centerPos = {
      x: this.left + this.width / 2,
      y: this.top + this.height / 2
    }
  },
  moveInit (type, e, target) {
    this.type = Number(type)
    this.mouseInit = {
      x: Math.floor(e.clientX),
      y: Math.floor(e.clientY)
    }
    this.scale = target.offsetWidth / target.offsetHeight
    this.initAngle = this.angle
    this.initRightBottomPoint = this.rightBottomPoint
    this.initRightTopPoint = this.rightTopPoint
    this.initLeftTopPoint = this.leftTopPoint
    this.initLeftBottomPoint = this.leftBottomPoint
    this.initLeftMiddlePoint = this.leftMiddlePoint
    this.initRightMiddlePoint = this.rightMiddlePoint
    this.initTopMiddlePoint = this.topMiddlePoint
    this.initBottomMiddlePoint = this.bottomMiddlePoint
    this.initCenterPos = this.centerPos
    this.initPosition = {
      x: this.left,
      y: this.top
    }
    if (type === 0) {
      this.rotateFlag = true
      this.preRadian = Math.atan2(this.mouseInit.y - this.centerPos.y, this.mouseInit.x - this.centerPos.x)
    } else if (type < 10) {
      this.canChange = true
    }
  },
  moveChange (e, target) {
    let newHeight, newWidth, newRightBottomPoint, newLeftTopPoint, newLeftBottomPoint, newRightTopPoint, rotateCurrentPos
    switch (this.type) {
      case 0:
        if (this.rotateFlag) {
          this.rotateCurrent = {
            x: Math.floor(e.clientX),
            y: Math.floor(e.clientY)
          }
          this.curRadian = Math.atan2(this.rotateCurrent.y - this.centerPos.y, this.rotateCurrent.x - this.centerPos.x)
          this.tranformRadian = this.curRadian - this.preRadian
          this.angle = this.getRotate(target) +  Math.round(this.tranformRadian * 180 / Math.PI)
          target.style.transform = `rotate(${this.angle}deg)`
          this.pannelDom.style.transform = `rotate(${this.angle}deg)`
          if (this.showAngle) {
            this.angleDom.innerHTML = this.angle
            this.angleDom.style.display = 'block'
          }
          this.preRadian = this.curRadian
          // 重新计算旋转后四个点的坐标变化
          let disAngle = this.angle - this.initAngle
          this.rightBottomPoint = this.getRotatedPoint(this.initRightBottomPoint, this.centerPos, disAngle)
          this.rightTopPoint = this.getRotatedPoint(this.initRightTopPoint, this.centerPos, disAngle)
          this.leftTopPoint = this.getRotatedPoint(this.initLeftTopPoint, this.centerPos, disAngle)
          this.leftBottomPoint = this.getRotatedPoint(this.initLeftBottomPoint, this.centerPos, disAngle)
          this.leftMiddlePoint = this.getRotatedPoint(this.initLeftMiddlePoint, this.centerPos, disAngle)
          this.rightMiddlePoint = this.getRotatedPoint(this.initRightMiddlePoint, this.centerPos, disAngle)
          this.topMiddlePoint = this.getRotatedPoint(this.initTopMiddlePoint, this.centerPos, disAngle)
          this.bottomMiddlePoint = this.getRotatedPoint(this.initBottomMiddlePoint, this.centerPos, disAngle)
        }
      case 1:
        if (this.canChange) {
          
          this.centerPos = {
            x: Math.floor((e.clientX + this.rightBottomPoint.x) / 2),
            y: Math.floor((e.clientY + this.rightMiddlePoint.y) / 2)
          }

          // 计算旋转为水平角度的两点坐标
          newLeftTopPoint = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.centerPos, -this.initAngle)
          newRightBottomPoint = this.getRotatedPoint(this.rightBottomPoint, this.centerPos, -this.initAngle)

          console.log('-----------------')
          console.log(this.centerPos)
          console.log(this.initAngle)
          console.log(newLeftTopPoint)
          console.log(newRightBottomPoint)


          newWidth = newRightBottomPoint.x - newLeftTopPoint.x
          newHeight = newRightBottomPoint.y - newLeftTopPoint.y
          if (this.isScale) {
            if (newWidth / newHeight > this.scale) {
              newLeftTopPoint.x = newLeftTopPoint.x + Math.abs(newWidth - newHeight * this.scale)
              newWidth = newHeight * this.scale
            } else {
              newLeftTopPoint.y = newLeftTopPoint.y + Math.abs(newHeight - newWidth / this.scale)
              newHeight = newWidth / this.scale
            }
            // 计算出左上角等比角度变换后水平坐标后,再计算旋转后的角度
            var rotateLeftTopPoint = this.getRotatedPoint(newLeftTopPoint, this.centerPos, this.initAngle)
            this.centerPos = {
              x: Math.floor((rotateLeftTopPoint.x + this.rightBottomPoint.x) / 2),
              y: Math.floor((rotateLeftTopPoint.y + this.rightBottomPoint.y) / 2)
            }
            newLeftTopPoint = this.getRotatedPoint(rotateLeftTopPoint, this.centerPos, -this.initAngle)
            newRightBottomPoint = this.getRotatedPoint(this.rightBottomPoint, this.centerPos, -this.initAngle)
            newWidth = newRightBottomPoint.x - newLeftTopPoint.x
            newHeight = newRightBottomPoint.y - newLeftTopPoint.y
          }
          if (newWidth <= 12) {
            newWidth = 12
            newHeight = Math.floor(newWidth / this.scale)
            newLeftTopPoint.x = newRightBottomPoint.x - newWidth
            newLeftTopPoint.y = newRightBottomPoint.y - newHeight
          }
          if (newHeight <= 12) {
            newHeight = 12
            newWidth = Math.floor(newHeight * this.scale)
            newLeftTopPoint.y = newRightBottomPoint.y - newHeight
            newLeftTopPoint.x = newRightBottomPoint.x - newWidth
          }
          if (newHeight > 12 && newWidth > 12) {
            this.left = newLeftTopPoint.x
            this.top = newLeftTopPoint.y
            this.width = newWidth
            this.height = newHeight
            target.style.left = `${this.left}px`
            target.style.top = `${this.top}px`
            target.style.width = `${this.width}px`
            target.style.height = `${this.height}px`
            this.pannelDom.style.left = `${this.left}px`
            this.pannelDom.style.top = `${this.top}px`
            this.pannelDom.style.width = `${this.width}px`
            this.pannelDom.style.height = `${this.height}px`
          } 
        }
        break;
      case 2:
        if (this.canChange) {
          this.centerPos = {
            x: Math.floor((e.clientX + this.rightTopPoint.x) / 2),
            y: Math.floor((e.clientY + this.rightTopPoint.y) / 2)
          }
          newLeftBottomPoint =  this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.centerPos, -this.initAngle)
          newRightTopPoint = this.getRotatedPoint(this.rightTopPoint, this.centerPos, -this.initAngle)
          newWidth = newRightTopPoint.x - newLeftBottomPoint.x
          newHeight = newLeftBottomPoint.y - newRightTopPoint.y
          if (this.isScale) {
            if (newWidth / newHeight > this.scale) {
              newLeftBottomPoint.x = newLeftBottomPoint.x + Math.abs(newWidth - newHeight * this.scale)
              newWidth = newHeight * this.scale
            } else {
              newLeftBottomPoint.y = newLeftBottomPoint.y - Math.abs(newHeight - newWidth / this.scale)
              newHeight = newWidth / this.scale
            }
            var rotatedLeftBottomPoint = this.getRotatedPoint(newLeftBottomPoint, this.centerPos, this.initAngle)
            this.centerPos = {
              x: Math.floor((rotatedLeftBottomPoint.x + this.rightTopPoint.x) / 2),
              y: Math.floor((rotatedLeftBottomPoint.y + this.rightTopPoint.y) / 2)
            }
            newLeftBottomPoint = this.getRotatedPoint(rotatedLeftBottomPoint, this.centerPos, -this.initAngle)
            newRightTopPoint = this.getRotatedPoint(this.rightTopPoint, this.centerPos, -this.initAngle)
            newWidth = newRightTopPoint.x - newLeftBottomPoint.x
            newHeight = newLeftBottomPoint.y - newRightTopPoint.y
          }
          if (newHeight <= 12) {
            newHeight = 12
            newWidth = Math.floor(newHeight * this.scale)
            newLeftBottomPoint = {
              x: newRightTopPoint.x - newWidth,
              y: newRightTopPoint.y + newHeight
            }
          }
          if (newWidth <= 12) {
            newWidth = 12
            newHeight = Math.floor(newWidth / this.scale)
            newLeftBottomPoint = {
              x: newRightTopPoint.x - newWidth,
              y: newRightTopPoint.y + newHeight
            }
          }
          if (newHeight > 12 && newHeight > 12) {
            this.left = newLeftBottomPoint.x
            this.top = newRightTopPoint.y
            this.width = newWidth
            this.height = newHeight
            target.style.left = `${this.left}px`
            target.style.top = `${this.top}px`
            target.style.width = `${this.width}px`
            target.style.height = `${this.height}px`
            this.pannelDom.style.left = `${this.left}px`
            this.pannelDom.style.top = `${this.top}px`
            this.pannelDom.style.width = `${this.width}px`
            this.pannelDom.style.height = `${this.height}px`
          }
        }
        break;
      case 3:
        if (this.canChange) {
          this.centerPos = {
            x: Math.floor((e.clientX + this.leftBottomPoint.x) / 2),
            y: Math.floor((e.clientY + this.leftBottomPoint.y) / 2)
          }
          newRightTopPoint = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.centerPos, -this.initAngle)
          newLeftBottomPoint = this.getRotatedPoint(this.leftBottomPoint, this.centerPos, -this.initAngle)
          newWidth = newRightTopPoint.x - newLeftBottomPoint.x
          newHeight = newLeftBottomPoint.y - newRightTopPoint.y
          if (this.isScale) {
            if (newWidth / newHeight > this.scale) {
              newRightTopPoint.x = newRightTopPoint.x - Math.abs(newWidth - newHeight * this.scale)
              newWidth = newHeight * this.scale
            } else {
              newRightTopPoint.y = newRightTopPoint.y + Math.abs(newHeight - newWidth / this.scale)
              newHeight = newWidth / this.scale
            }
            let rotatedRightTopPoint = this.getRotatedPoint(newRightTopPoint, this.centerPos, this.initAngle)
            this.centerPos = {
              x: Math.floor((rotatedRightTopPoint.x + this.leftBottomPoint.x) / 2),
              y: Math.floor((rotatedRightTopPoint.y + this.leftBottomPoint.y) / 2)
            }
            newLeftBottomPoint = this.getRotatedPoint(this.leftBottomPoint, this.centerPos, -this.initAngle)
            newRightTopPoint = this.getRotatedPoint(rotatedRightTopPoint, this.centerPos, -this.initAngle)
            newWidth = newRightTopPoint.x - newLeftBottomPoint.x
            newHeight = newLeftBottomPoint.y - newRightTopPoint.y
          }
          if (newWidth <= 12) {
            newWidth = 12
            newHeight = Math.floor(newWidth / this.scale)
            newRightTopPoint = {
              x: newLeftBottomPoint.x + newWidth,
              y: newLeftBottomPoint.y - newHeight
            }
          }
          if (newHeight <= 12) {
            newHeight = 12
            newWidth = Math.floor(newHeight * this.scale)
            newRightTopPoint = {
              x: newLeftBottomPoint.x + newWidth,
              y: newLeftBottomPoint.y - newHeight
            }
          }
          if (newWidth > 12 && newHeight > 12) {
            this.left = newLeftBottomPoint.x
            this.top = newRightTopPoint.y
            this.width = newWidth
            this.height = newHeight
            target.style.left = `${this.left}px`
            target.style.top = `${this.top}px`
            target.style.width = `${this.width}px`
            target.style.height = `${this.height}px`
            this.pannelDom.style.left = `${this.left}px`
            this.pannelDom.style.top = `${this.top}px`
            this.pannelDom.style.width = `${this.width}px`
            this.pannelDom.style.height = `${this.height}px`
          }
        }
        break;
      case 4:
        if (this.canChange) {
          this.centerPos = {
            x: Math.floor((e.clientX + this.leftTopPoint.x) / 2),
            y: Math.floor((e.clientY + this.leftTopPoint.y) / 2)
          }
          newRightBottomPoint = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.centerPos, -this.initAngle)
          newLeftTopPoint = this.getRotatedPoint(this.leftTopPoint, this.centerPos, -this.initAngle)
          newWidth = newRightBottomPoint.x - newLeftTopPoint.x
          newHeight = newRightBottomPoint.y - newLeftTopPoint.y
          if (this.isScale) {
            if (newWidth / newHeight > this.scale) {
              newRightBottomPoint.x = newRightBottomPoint.x - Math.abs(newWidth - newHeight * this.scale)
              newWidth = newHeight * this.scale
            } else {
              newRightBottomPoint.y = newRightBottomPoint.y - Math.abs(newHeight - newWidth / this.scale)
              newHeight = newWidth / this.scale
            }
            let rotatedRightBottomPoint = this.getRotatedPoint(newRightBottomPoint, this.centerPos, this.initAngle)
            this.centerPos = {
              x: Math.floor((rotatedRightBottomPoint.x + this.leftTopPoint.x) / 2),
              y: Math.floor((rotatedRightBottomPoint.y + this.leftTopPoint.y) / 2)
            }
            newLeftTopPoint = this.getRotatedPoint(this.leftTopPoint, this.centerPos, -this.initAngle)
            newRightBottomPoint = this.getRotatedPoint(rotatedRightBottomPoint, this.centerPos, -this.initAngle)
            newWidth = newRightBottomPoint.x - newLeftTopPoint.x
            newHeight = newRightBottomPoint.y - newLeftTopPoint.y
          }
          if (newWidth <= 12) {
            newWidth = 12
            newHeight = Math.floor(newWidth / this.scale)
            newRightBottomPoint = {
              x: newLeftTopPoint.x + newWidth,
              y: newLeftTopPoint.y + newHeight
            }
          }
          if (newHeight <= 12) {
            newHeight = 12
            newWidth = Math.floor(newHeight * this.scale)
            newRightBottomPoint = {
              x: newLeftTopPoint.x + newWidth,
              y: newLeftTopPoint.y + newHeight
            }
          }
          if (newWidth > 12 && newHeight > 12) {
            this.left = newLeftTopPoint.x
            this.top = newLeftTopPoint.y
            this.width = newWidth
            this.height = newHeight
            target.style.left = `${this.left}px`
            target.style.top = `${this.top}px`
            target.style.width = `${this.width}px`
            target.style.height = `${this.height}px`
            this.pannelDom.style.left = `${this.left}px`
            this.pannelDom.style.top = `${this.top}px`
            this.pannelDom.style.width = `${this.width}px`
            this.pannelDom.style.height = `${this.height}px`
          }
        }
        break;
      case 5:
        if (this.canChange) {
          // 计算出鼠标现在所在的点,经过以bottommiddle点反向旋转后的位置,从而得到其y轴坐标点与topmiddle的x轴坐标结合,求出旋转后图形的topmiddle
          rotateCurrentPos = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.bottomMiddlePoint, -this.initAngle)
          let rotatedTopMiddlePoint = {
            x: this.bottomMiddlePoint.x,
            y: rotateCurrentPos.y
          }
          newHeight = this.bottomMiddlePoint.y - rotatedTopMiddlePoint.y

          // newHeight = Math.sqrt(Math.pow((this.topMiddlePoint.x - this.bottomMiddlePoint.x), 2) + Math.pow((this.topMiddlePoint.y - this.bottomMiddlePoint.y), 2))
          if (newHeight <= 12) {
            newHeight = 12
            rotatedTopMiddlePoint.y = this.bottomMiddlePoint.y - 12
          }
          // 计算转回去的topmiddle点坐标
          this.topMiddlePoint = this.getRotatedPoint(rotatedTopMiddlePoint, this.bottomMiddlePoint, this.initAngle)
          this.centerPos = {
            x: (this.topMiddlePoint.x + this.bottomMiddlePoint.x) / 2,
            y: (this.topMiddlePoint.y + this.bottomMiddlePoint.y) / 2
          }
          this.left = this.centerPos.x - target.offsetWidth / 2
          this.top = this.centerPos.y - newHeight / 2
          this.height = newHeight
          target.style.left = `${this.left}px`
          target.style.height = `${this.height}px`
          target.style.top = `${this.top}px`
          this.pannelDom.style.left = `${this.left}px`
          this.pannelDom.style.height = `${this.height}px`
          this.pannelDom.style.top = `${this.top}px`
        }
        break;
      case 6:
        if (this.canChange) {
          rotateCurrentPos = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.rightMiddlePoint, -this.initAngle)
          let rotatedLeftMiddlePonit = {
            x: rotateCurrentPos.x,
            y: this.rightMiddlePoint.y
          }
          newWidth = this.rightMiddlePoint.x - rotatedLeftMiddlePonit.x
          if (newWidth <= 12) {
            newWidth = 12
            rotatedLeftMiddlePonit.x = this.rightMiddlePoint.x - 12
          }
          this.leftMiddlePoint = this.getRotatedPoint(rotatedLeftMiddlePonit, this.rightMiddlePoint, this.initAngle)
          this.centerPos = {
            x: Math.floor((this.leftMiddlePoint.x + this.rightMiddlePoint.x) / 2),
            y: Math.floor((this.leftMiddlePoint.y + this.rightMiddlePoint.y) / 2)
          }
          this.left = this.centerPos.x - newWidth / 2
          this.top = this.centerPos.y - target.offsetHeight / 2
          this.width = newWidth
          target.style.left = `${this.left}px`
          target.style.top = `${this.top}px`
          target.style.width = `${this.width}px`
          this.pannelDom.style.left = `${this.left}px`
          this.pannelDom.style.top = `${this.top}px`
          this.pannelDom.style.width = `${this.width}px`
        }
        break;
      case 7:
        if (this.canChange) {
          rotateCurrentPos = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.topMiddlePoint, -this.initAngle)
          let rotatedBottomMiddlePoint = {
            x: this.topMiddlePoint.x,
            y: rotateCurrentPos.y
          }
          newHeight = rotatedBottomMiddlePoint.y - this.topMiddlePoint.y
          if (newHeight <= 12) {
            newHeight = 12
            rotatedBottomMiddlePoint.y = this.topMiddlePoint.y - 12
          }
          this.bottomMiddlePoint = this.getRotatedPoint(rotatedBottomMiddlePoint, this.topMiddlePoint, this.initAngle)
          this.centerPos = {
            x: Math.floor((this.bottomMiddlePoint.x + this.topMiddlePoint.x) / 2),
            y: Math.floor((this.bottomMiddlePoint.y + this.topMiddlePoint.y) / 2)
          }
          this.left = this.centerPos.x - target.offsetWidth / 2
          this.top = this.centerPos.y - newHeight / 2
          this.height = newHeight
          target.style.left = `${this.left}px`
          target.style.top = `${this.top}px`
          target.style.height = `${this.height}px`
          this.pannelDom.style.left = `${this.left}px`
          this.pannelDom.style.top = `${this.top}px`
          this.pannelDom.style.height = `${this.height}px`
        }
        break;
      case 8:
        if (this.canChange) {
          // console.log(e.clientX)
          // console.log(e.clientY)
          rotateCurrentPos = this.getRotatedPoint({
            x: e.clientX,
            y: e.clientY
          }, this.leftMiddlePoint, -this.initAngle)

          let rotatedRightMiddlePoint = {
            x: rotateCurrentPos.x,
            y: this.leftMiddlePoint.y
          }

          console.log('----右中--------')
          // console.log(rotateCurrentPos)
          console.log(rotatedRightMiddlePoint)
          console.log(this.leftMiddlePoint)

          newWidth = rotatedRightMiddlePoint.x - this.leftMiddlePoint.x
          if (newWidth <= 12) {
            newWidth = 12
            rotatedRightMiddlePoint.x = this.leftMiddlePoint.x + 12
          }
          this.rightMiddlePoint = this.getRotatedPoint(rotatedRightMiddlePoint, this.leftMiddlePoint, this.initAngle)
          this.centerPos = {
            x: Math.floor((this.leftMiddlePoint.x + this.rightMiddlePoint.x) / 2),
            y: Math.floor((this.leftMiddlePoint.y + this.rightMiddlePoint.y) / 2)
          }
          console.log(this.rightMiddlePoint)
          console.log(this.centerPos)

          this.left = this.centerPos.x - newWidth / 2
          this.top = this.centerPos.y - target.offsetHeight / 2
          this.width = newWidth




          target.style.left = `${this.left}px`
          target.style.top = `${this.top}px`
          target.style.width = `${newWidth}px`
          this.pannelDom.style.left = `${this.left}px`
          this.pannelDom.style.top = `${this.top}px`
          this.pannelDom.style.width = `${this.width}px`
        }
        break;
      case 9:
        if (this.canChange) {
          let dis = {
            x: Math.floor(e.clientX - this.mouseInit.x),
            y: Math.floor(e.clientY - this.mouseInit.y)
          }
          this.left = this.initPosition.x + dis.x
          this.top = this.initPosition.y + dis.y
          target.style.left = `${this.left}px`
          target.style.top = `${this.top}px`
          this.pannelDom.style.left = `${this.left}px`
          this.pannelDom.style.top = `${this.top}px`
          this.pannelDom.style.width = `${this.width}px`
          this.pannelDom.style.height = `${this.height}px`
          this.centerPos = {
            x: this.initCenterPos.x + dis.x,
            y: this.initCenterPos.y + dis.y
          }
          if (this.showPosition) {
            this.positionDom.style.display = 'inline-block'
            this.positionDom.innerHTML = `X: ${this.left} Y: ${this.top}`
          }
        }
        break;
    }
  },
  moveLeave () {
    if (this.canChange || this.rotateFlag) {
      this.rotateFlag = false
      this.canChange = false
      if (this.angleDom) this.angleDom.style.display = 'none'
      if (this.positionDom) this.positionDom.style.display = 'none'
      this.getTransferPosition(this.left, this.top, this.width, this.height, this.angle, this.centerPos)
    }
  },
  getRotate (target) {
    var st = window.getComputedStyle(target, null);
    var tr = st.getPropertyValue("-webkit-transform") ||
    st.getPropertyValue("-moz-transform") ||
    st.getPropertyValue("-ms-transform") ||
    st.getPropertyValue("-o-transform") ||
    st.getPropertyValue("transform") || "FAIL";
      // With rotate(30deg)...
      // matrix(0.866025, 0.5, -0.5, 0.866025, 0px, 0px)
    if (tr !== 'none') {
      var values = tr.split('(')[1].split(')')[0].split(',');
      var a = values[0];
      var b = values[1];
      var c = values[2];
      var d = values[3];
      var scale = Math.sqrt(a * a + b * b);
      // arc sin, convert from radians to degrees, round
      var sin = b / scale;
      // next line works for 30deg but not 130deg (returns 50);
      // var angle = Math.round(Math.asin(sin) * (180/Math.PI));
      var angle = Math.round(Math.atan2(b, a) * (180 / Math.PI))
      if (angle < 0) {
        angle = 360 + angle
      }
      return angle
    } else {
      return 0
    }
  },
  getRotatedPoint (curPos, centerPos, angle) {
    return {
      x: Math.floor((curPos.x - centerPos.x) * Math.cos(Math.PI / 180 * angle) - (curPos.y - centerPos.y) * Math.sin(Math.PI / 180 * angle) + centerPos.x),
      y: Math.floor((curPos.x - centerPos.x) * Math.sin(Math.PI / 180 * angle) + (curPos.y - centerPos.y) * Math.cos(Math.PI / 180 * angle) + centerPos.y)                   
    }
  },
  getTransferPosition (left, top, width, height, angle, center) {
    // 计算变换后的方框四个角的位置
    var a1 = {
      x: left,
      y: top
    }
    var a2 = {
        x: left,
        y: top + height
    }
    var a3 = {
        x: left + width,
        y: top
    }
    var a4 = {
        x: left + width,
        y: top + height
    }
    var a5 = {
        x: left,
        y: top + height / 2
    }
    var a6 = {
        x: left + width,
        y: top + height / 2
    }
    var a7 = {
        x: left + width / 2,
        y: top
    }
    var a8 = {
        x: left + width / 2,
        y: top + height
    }
    this.leftTopPoint = this.getRotatedPoint(a1, center, angle)
    this.leftBottomPoint = this.getRotatedPoint(a2, center, angle)
    this.rightTopPoint = this.getRotatedPoint(a3, center, angle)
    this.rightBottomPoint = this.getRotatedPoint(a4, center, angle)
    this.leftMiddlePoint = this.getRotatedPoint(a5, center, angle)
    this.rightMiddlePoint = this.getRotatedPoint(a6, center, angle)
    this.topMiddlePoint = this.getRotatedPoint(a7, center, angle)
    this.bottomMiddlePoint = this.getRotatedPoint(a8, center, angle)
  }
}

export default Drag