|
| 1 | +import { rotatePositionRelatively } from './rotate' |
| 2 | +import rectCenter from './rectCenter' |
| 3 | +import symmetryPoint from './symmetryPoint' |
| 4 | +import centerPoint from './centerPoint' |
| 5 | +import computeRotation from './computeRotation' |
| 6 | + |
| 7 | +class Controller { |
| 8 | + constructor(options) { |
| 9 | + this.adjustType = options.adjustType |
| 10 | + this.mouseStart = options.mouseStart |
| 11 | + this.mouseEnd = options.mouseEnd |
| 12 | + this.rectStart = options.rectStart |
| 13 | + this.isFixedRatio = options.isFixedRatio |
| 14 | + |
| 15 | + this._p0 = null |
| 16 | + this._p1 = null |
| 17 | + this._p2 = null |
| 18 | + this._p3 = null |
| 19 | + this._lt = null |
| 20 | + this._rt = null |
| 21 | + this._rb = null |
| 22 | + this._lb = null |
| 23 | + this._ct = null |
| 24 | + this._cb = null |
| 25 | + this._rm = null |
| 26 | + this._lm = null |
| 27 | + this._center = null |
| 28 | + } |
| 29 | + |
| 30 | + moveRect() { |
| 31 | + const [x, y] = ['x', 'y'].map(k => |
| 32 | + this.mouseEnd[k] - this.mouseStart[k] |
| 33 | + ) |
| 34 | + return { |
| 35 | + ...this.rectStart, |
| 36 | + x: this.rectStart.x + x, |
| 37 | + y: this.rectStart.y + y |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + rotateRect() { |
| 42 | + // const shrotcutRotations = [-180, -135, -90, -45, 0, 45, 90, 135, 180] |
| 43 | + const offsetRotation = computeRotation(this.center, mouseStart, mouseEnd) |
| 44 | + let nextRotation = this.rectStart.r + offsetRotation |
| 45 | + if (nextRotation >= 180) { |
| 46 | + nextRotation = nextRotation - 360 |
| 47 | + } |
| 48 | + if (nextRotation <= -180) { |
| 49 | + nextRotation = nextRotation + 360 |
| 50 | + } |
| 51 | + return { |
| 52 | + ...this.rectStart, |
| 53 | + r: nextRotation |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + resizeRect() { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + // key end points |
| 62 | + get center() { |
| 63 | + if (!this._center) { |
| 64 | + this._center = rectCenter(this.rectStart) |
| 65 | + } |
| 66 | + return this._center |
| 67 | + } |
| 68 | + |
| 69 | + get lt () { |
| 70 | + if (!this._p0) { |
| 71 | + const { x, y, r } = this.rectStart |
| 72 | + this._p0 = rotatePositionRelatively({ x, y }, this.center, r) |
| 73 | + } |
| 74 | + return this._p0 |
| 75 | + } |
| 76 | + |
| 77 | + get rt() { |
| 78 | + if (!this._p1) { |
| 79 | + const { x, y, w, r } = this.rectStart |
| 80 | + this._p1 = rotatePositionRelatively( |
| 81 | + { x: x + w, y }, |
| 82 | + this.center, |
| 83 | + r |
| 84 | + ) |
| 85 | + } |
| 86 | + return this._p1 |
| 87 | + } |
| 88 | + |
| 89 | + get rb() { |
| 90 | + if (!this._p2) { |
| 91 | + this._p2 = symmetryPoint(this.p0, this.center) |
| 92 | + } |
| 93 | + return this._p2 |
| 94 | + } |
| 95 | + |
| 96 | + get lb() { |
| 97 | + if (!this._p3) { |
| 98 | + this._p3 = symmetryPoint(this.p1, this.center) |
| 99 | + } |
| 100 | + return this._p3 |
| 101 | + } |
| 102 | + |
| 103 | + get ct() { |
| 104 | + if (!this._ct) { |
| 105 | + this._ct = centerPoint(this.p0, this.p1) |
| 106 | + } |
| 107 | + return this._ct |
| 108 | + } |
| 109 | + |
| 110 | + get cb() { |
| 111 | + if (!this._cb) { |
| 112 | + this._cb = centerPoint(this.p2, this.p3) |
| 113 | + } |
| 114 | + return this._cb |
| 115 | + } |
| 116 | + |
| 117 | + get rm() { |
| 118 | + if (!this._rm) { |
| 119 | + this._rm = centerPoint(this.p1, this.p2) |
| 120 | + } |
| 121 | + return this._rm |
| 122 | + } |
| 123 | + |
| 124 | + get lm() { |
| 125 | + if (!this._lm) { |
| 126 | + this._lm = centerPoint(this.p0, this.p3) |
| 127 | + } |
| 128 | + return this._lm |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments