您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

rotate-session.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Data } 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 {
  7. clampToRotationToSegments,
  8. getBoundsCenter,
  9. getCommonBounds,
  10. getPage,
  11. getSelectedShapes,
  12. getShapeBounds,
  13. } from "utils/utils"
  14. import { getShapeUtils } from "lib/shape-utils"
  15. const PI2 = Math.PI * 2
  16. export default class RotateSession extends BaseSession {
  17. delta = [0, 0]
  18. origin: number[]
  19. snapshot: RotateSnapshot
  20. constructor(data: Data, point: number[]) {
  21. super(data)
  22. this.origin = point
  23. this.snapshot = getRotateSnapshot(data)
  24. }
  25. update(data: Data, point: number[], isLocked: boolean) {
  26. const { boundsCenter, shapes } = this.snapshot
  27. const page = getPage(data)
  28. const a1 = vec.angle(boundsCenter, this.origin)
  29. const a2 = vec.angle(boundsCenter, point)
  30. let rot = a2 - a1
  31. if (isLocked) {
  32. rot = clampToRotationToSegments(rot, 24)
  33. }
  34. data.boundsRotation = (PI2 + (this.snapshot.boundsRotation + rot)) % PI2
  35. for (let { id, center, offset, rotation } of shapes) {
  36. const shape = page.shapes[id]
  37. getShapeUtils(shape)
  38. .rotate(shape, (PI2 + (rotation + rot)) % PI2)
  39. .translate(
  40. shape,
  41. vec.sub(vec.rotWith(center, boundsCenter, rot % PI2), offset)
  42. )
  43. }
  44. }
  45. cancel(data: Data) {
  46. const page = getPage(data, this.snapshot.currentPageId)
  47. for (let { id, point, rotation } of this.snapshot.shapes) {
  48. const shape = page.shapes[id]
  49. getShapeUtils(shape).rotate(shape, rotation).translate(shape, point)
  50. }
  51. }
  52. complete(data: Data) {
  53. commands.rotate(data, this.snapshot, getRotateSnapshot(data))
  54. }
  55. }
  56. export function getRotateSnapshot(data: Data) {
  57. const shapes = getSelectedShapes(current(data))
  58. // A mapping of selected shapes and their bounds
  59. const shapesBounds = Object.fromEntries(
  60. shapes.map((shape) => [shape.id, getShapeBounds(shape)])
  61. )
  62. // The common (exterior) bounds of the selected shapes
  63. const bounds = getCommonBounds(...Object.values(shapesBounds))
  64. const boundsCenter = getBoundsCenter(bounds)
  65. return {
  66. boundsCenter,
  67. currentPageId: data.currentPageId,
  68. boundsRotation: data.boundsRotation,
  69. shapes: shapes.map(({ id, point, rotation }) => {
  70. const bounds = shapesBounds[id]
  71. const offset = [bounds.width / 2, bounds.height / 2]
  72. const center = getBoundsCenter(bounds)
  73. return {
  74. id,
  75. point,
  76. rotation,
  77. offset,
  78. center,
  79. }
  80. }),
  81. }
  82. }
  83. export type RotateSnapshot = ReturnType<typeof getRotateSnapshot>