Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

rotate-ccw.ts 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import {
  5. getBoundsCenter,
  6. getCommonBounds,
  7. getPage,
  8. getSelectedShapes,
  9. } from 'utils/utils'
  10. import * as vec from 'utils/vec'
  11. import { getShapeUtils } from 'lib/shape-utils'
  12. const PI2 = Math.PI * 2
  13. export default function rotateCcwCommand(data: Data) {
  14. const { currentPageId, boundsRotation } = data
  15. const page = getPage(data)
  16. const initialShapes = Object.fromEntries(
  17. getSelectedShapes(data).map((shape) => {
  18. const bounds = getShapeUtils(shape).getBounds(shape)
  19. return [
  20. shape.id,
  21. {
  22. rotation: shape.rotation,
  23. point: [...shape.point],
  24. center: getBoundsCenter(bounds),
  25. bounds,
  26. },
  27. ]
  28. })
  29. )
  30. const commonBoundsCenter = getBoundsCenter(
  31. getCommonBounds(...Object.values(initialShapes).map((b) => b.bounds))
  32. )
  33. const nextShapes = Object.fromEntries(
  34. Object.entries(initialShapes).map(([id, { point, center }]) => {
  35. const shape = { ...page.shapes[id] }
  36. const offset = vec.sub(center, point)
  37. const nextPoint = vec.sub(
  38. vec.rotWith(center, commonBoundsCenter, -(PI2 / 4)),
  39. offset
  40. )
  41. const rot = (PI2 + (shape.rotation - PI2 / 4)) % PI2
  42. getShapeUtils(shape).rotateTo(shape, rot).translateTo(shape, nextPoint)
  43. return [id, shape]
  44. })
  45. )
  46. const nextboundsRotation = (PI2 + (data.boundsRotation - PI2 / 4)) % PI2
  47. history.execute(
  48. data,
  49. new Command({
  50. name: 'translate_shapes',
  51. category: 'canvas',
  52. do(data) {
  53. const { shapes } = getPage(data, currentPageId)
  54. for (let id in nextShapes) {
  55. const shape = shapes[id]
  56. getShapeUtils(shape)
  57. .rotateTo(shape, nextShapes[id].rotation)
  58. .translateTo(shape, nextShapes[id].point)
  59. }
  60. data.boundsRotation = nextboundsRotation
  61. },
  62. undo(data) {
  63. const { shapes } = getPage(data, currentPageId)
  64. for (let id in initialShapes) {
  65. const { point, rotation } = initialShapes[id]
  66. const shape = shapes[id]
  67. const utils = getShapeUtils(shape)
  68. utils.rotateTo(shape, rotation).translateTo(shape, point)
  69. }
  70. data.boundsRotation = boundsRotation
  71. },
  72. })
  73. )
  74. }