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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 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)
  43. .setProperty(shape, 'rotation', rot)
  44. .setProperty(shape, 'point', nextPoint)
  45. return [id, shape]
  46. })
  47. )
  48. const nextboundsRotation = (PI2 + (data.boundsRotation - PI2 / 4)) % PI2
  49. history.execute(
  50. data,
  51. new Command({
  52. name: 'translate_shapes',
  53. category: 'canvas',
  54. do(data) {
  55. const { shapes } = getPage(data, currentPageId)
  56. for (let id in nextShapes) {
  57. const shape = shapes[id]
  58. getShapeUtils(shape)
  59. .setProperty(shape, 'rotation', nextShapes[id].rotation)
  60. .setProperty(shape, 'point', nextShapes[id].point)
  61. }
  62. data.boundsRotation = nextboundsRotation
  63. },
  64. undo(data) {
  65. const { shapes } = getPage(data, currentPageId)
  66. for (let id in initialShapes) {
  67. const { point, rotation } = initialShapes[id]
  68. const shape = shapes[id]
  69. const utils = getShapeUtils(shape)
  70. utils
  71. .setProperty(shape, 'rotation', rotation)
  72. .setProperty(shape, 'point', point)
  73. }
  74. data.boundsRotation = boundsRotation
  75. },
  76. })
  77. )
  78. }