You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transform-session.ts 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { Data, Edge, Corner } from 'types'
  2. import * as vec from 'utils/vec'
  3. import BaseSession from './base-session'
  4. import commands from 'state/commands'
  5. import { current } from 'immer'
  6. import { getShapeUtils } from 'lib/shape-utils'
  7. import {
  8. getBoundsCenter,
  9. getBoundsFromPoints,
  10. getCommonBounds,
  11. getPage,
  12. getRelativeTransformedBoundingBox,
  13. getSelectedShapes,
  14. getShapes,
  15. getTransformedBoundingBox,
  16. } from 'utils/utils'
  17. export default class TransformSession extends BaseSession {
  18. scaleX = 1
  19. scaleY = 1
  20. transformType: Edge | Corner
  21. origin: number[]
  22. snapshot: TransformSnapshot
  23. constructor(data: Data, transformType: Corner | Edge, point: number[]) {
  24. super(data)
  25. this.origin = point
  26. this.transformType = transformType
  27. this.snapshot = getTransformSnapshot(data, transformType)
  28. }
  29. update(data: Data, point: number[], isAspectRatioLocked = false) {
  30. const { transformType } = this
  31. const { shapeBounds, initialBounds, isAllAspectRatioLocked } = this.snapshot
  32. const { shapes } = getPage(data)
  33. const newBoundingBox = getTransformedBoundingBox(
  34. initialBounds,
  35. transformType,
  36. vec.vec(this.origin, point),
  37. data.boundsRotation,
  38. isAspectRatioLocked || isAllAspectRatioLocked
  39. )
  40. this.scaleX = newBoundingBox.scaleX
  41. this.scaleY = newBoundingBox.scaleY
  42. // Now work backward to calculate a new bounding box for each of the shapes.
  43. for (let id in shapeBounds) {
  44. const { initialShape, initialShapeBounds, transformOrigin } =
  45. shapeBounds[id]
  46. const newShapeBounds = getRelativeTransformedBoundingBox(
  47. newBoundingBox,
  48. initialBounds,
  49. initialShapeBounds,
  50. this.scaleX < 0,
  51. this.scaleY < 0
  52. )
  53. const shape = shapes[id]
  54. getShapeUtils(shape).transform(shape, newShapeBounds, {
  55. type: this.transformType,
  56. initialShape,
  57. scaleX: this.scaleX,
  58. scaleY: this.scaleY,
  59. transformOrigin,
  60. })
  61. }
  62. }
  63. cancel(data: Data) {
  64. const { currentPageId, shapeBounds } = this.snapshot
  65. const page = getPage(data, currentPageId)
  66. for (let id in shapeBounds) {
  67. const shape = page.shapes[id]
  68. const { initialShape, initialShapeBounds, transformOrigin } =
  69. shapeBounds[id]
  70. getShapeUtils(shape).transform(shape, initialShapeBounds, {
  71. type: this.transformType,
  72. initialShape,
  73. scaleX: 1,
  74. scaleY: 1,
  75. transformOrigin,
  76. })
  77. }
  78. }
  79. complete(data: Data) {
  80. if (!this.snapshot.hasUnlockedShapes) return
  81. commands.transform(
  82. data,
  83. this.snapshot,
  84. getTransformSnapshot(data, this.transformType),
  85. this.scaleX,
  86. this.scaleY
  87. )
  88. }
  89. }
  90. export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
  91. const cData = current(data)
  92. const { currentPageId } = cData
  93. const initialShapes = getSelectedShapes(cData).filter(
  94. (shape) => !shape.isLocked
  95. )
  96. const hasUnlockedShapes = initialShapes.length > 0
  97. const isAllAspectRatioLocked = initialShapes.every(
  98. (shape) => shape.isAspectRatioLocked
  99. )
  100. const shapesBounds = Object.fromEntries(
  101. initialShapes.map((shape) => [
  102. shape.id,
  103. getShapeUtils(shape).getBounds(shape),
  104. ])
  105. )
  106. const boundsArr = Object.values(shapesBounds)
  107. const commonBounds = getCommonBounds(...boundsArr)
  108. const initialInnerBounds = getBoundsFromPoints(boundsArr.map(getBoundsCenter))
  109. // Return a mapping of shapes to bounds together with the relative
  110. // positions of the shape's bounds within the common bounds shape.
  111. return {
  112. type: transformType,
  113. hasUnlockedShapes,
  114. isAllAspectRatioLocked,
  115. currentPageId,
  116. initialBounds: commonBounds,
  117. shapeBounds: Object.fromEntries(
  118. initialShapes.map((shape) => {
  119. const initialShapeBounds = shapesBounds[shape.id]
  120. const ic = getBoundsCenter(initialShapeBounds)
  121. let ix = (ic[0] - initialInnerBounds.minX) / initialInnerBounds.width
  122. let iy = (ic[1] - initialInnerBounds.minY) / initialInnerBounds.height
  123. return [
  124. shape.id,
  125. {
  126. initialShape: shape,
  127. initialShapeBounds,
  128. transformOrigin: [ix, iy],
  129. },
  130. ]
  131. })
  132. ),
  133. }
  134. }
  135. export type TransformSnapshot = ReturnType<typeof getTransformSnapshot>
  136. // const transformOrigins = {
  137. // [Edge.Top]: [0.5, 1],
  138. // [Edge.Right]: [0, 0.5],
  139. // [Edge.Bottom]: [0.5, 0],
  140. // [Edge.Left]: [1, 0.5],
  141. // [Corner.TopLeft]: [1, 1],
  142. // [Corner.TopRight]: [0, 1],
  143. // [Corner.BottomLeft]: [1, 0],
  144. // [Corner.BottomRight]: [0, 0],
  145. // }
  146. // const origin = transformOrigins[this.transformType]