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.

hacks.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { DrawShape, PointerInfo } from 'types'
  2. import { setToArray } from 'utils'
  3. import tld from 'utils/tld'
  4. import { freeze } from 'immer'
  5. import session from './session'
  6. import state from './state'
  7. import vec from 'utils/vec'
  8. import * as Session from './sessions'
  9. /**
  10. * While a user is drawing with the draw tool, we want to update the shape without
  11. * going through the trouble of updating the entire state machine. Speciifcally, we
  12. * do not want to push the change through immer. Instead, we'll push the change
  13. * directly to the state using `forceData`.
  14. * @param info
  15. */
  16. export function fastDrawUpdate(info: PointerInfo): void {
  17. const data = { ...state.data }
  18. session.update<Session.DrawSession>(
  19. data,
  20. tld.screenToWorld(info.point, data),
  21. info.pressure,
  22. info.shiftKey
  23. )
  24. const selectedId = setToArray(tld.getSelectedIds(data))[0]
  25. const shape = data.document.pages[data.currentPageId].shapes[
  26. selectedId
  27. ] as DrawShape
  28. ;(data.document.pages[data.currentPageId].shapes[selectedId] as DrawShape) = {
  29. ...shape,
  30. points: [...shape.points],
  31. }
  32. state.forceData(freeze(data))
  33. }
  34. export function fastPanUpdate(delta: number[]): void {
  35. const data = { ...state.data }
  36. const camera = tld.getCurrentCamera(data)
  37. camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
  38. data.pageStates[data.currentPageId].camera = { ...camera }
  39. state.forceData(freeze(data))
  40. }
  41. export function fastZoomUpdate(point: number[], delta: number): void {
  42. const data = { ...state.data }
  43. const camera = tld.getCurrentCamera(data)
  44. const next = camera.zoom - (delta / 100) * camera.zoom
  45. const p0 = tld.screenToWorld(point, data)
  46. camera.zoom = tld.getCameraZoom(next)
  47. const p1 = tld.screenToWorld(point, data)
  48. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  49. data.pageStates[data.currentPageId].camera = { ...camera }
  50. state.forceData(freeze(data))
  51. }
  52. export function fastPinchCamera(
  53. point: number[],
  54. delta: number[],
  55. distanceDelta: number
  56. ): void {
  57. const data = { ...state.data }
  58. const camera = tld.getCurrentCamera(data)
  59. camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
  60. const next = camera.zoom - (distanceDelta / 350) * camera.zoom
  61. const p0 = tld.screenToWorld(point, data)
  62. camera.zoom = tld.getCameraZoom(next)
  63. const p1 = tld.screenToWorld(point, data)
  64. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  65. const pageState = data.pageStates[data.currentPageId]
  66. pageState.camera = { ...camera }
  67. data.pageStates[data.currentPageId] = { ...pageState }
  68. state.forceData(freeze(data))
  69. }
  70. export function fastBrushSelect(point: number[]): void {
  71. const data = { ...state.data }
  72. session.update<Session.BrushSession>(data, tld.screenToWorld(point, data))
  73. state.forceData(freeze(data))
  74. }
  75. export function fastTranslate(info: PointerInfo): void {
  76. const data = { ...state.data }
  77. session.update<Session.TranslateSession>(
  78. data,
  79. tld.screenToWorld(info.point, data),
  80. info.shiftKey,
  81. info.altKey
  82. )
  83. state.forceData(freeze(data))
  84. }
  85. export function fastTransform(info: PointerInfo): void {
  86. const data = { ...state.data }
  87. session.update<Session.TransformSession | Session.TransformSingleSession>(
  88. data,
  89. tld.screenToWorld(info.point, data),
  90. info.shiftKey
  91. )
  92. state.forceData(freeze(data))
  93. }