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.

draw-session.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { current } from 'immer'
  2. import { Data, DrawShape } from 'types'
  3. import BaseSession from './base-session'
  4. import { getShapeUtils } from 'lib/shape-utils'
  5. import { getPage, getShape, updateParents } from 'utils/utils'
  6. import * as vec from 'utils/vec'
  7. import commands from 'state/commands'
  8. let prevEndPoint: number[]
  9. export default class BrushSession extends BaseSession {
  10. origin: number[]
  11. previous: number[]
  12. last: number[]
  13. points: number[][]
  14. snapshot: DrawSnapshot
  15. isLocked: boolean
  16. lockedDirection: 'horizontal' | 'vertical'
  17. constructor(data: Data, id: string, point: number[], isLocked = false) {
  18. super(data)
  19. this.origin = point
  20. this.previous = point
  21. this.last = point
  22. this.points = []
  23. this.snapshot = getDrawSnapshot(data, id)
  24. const page = getPage(data)
  25. const shape = page.shapes[id]
  26. getShapeUtils(shape).translateTo(shape, point)
  27. }
  28. update = (
  29. data: Data,
  30. point: number[],
  31. pressure: number,
  32. isLocked = false
  33. ) => {
  34. const { snapshot } = this
  35. const delta = vec.vec(this.origin, point)
  36. if (isLocked) {
  37. if (!this.isLocked && this.points.length > 1) {
  38. this.isLocked = true
  39. const returning = [...this.previous]
  40. if (Math.abs(delta[0]) < Math.abs(delta[1])) {
  41. this.lockedDirection = 'vertical'
  42. returning[0] = this.origin[0]
  43. } else {
  44. this.lockedDirection = 'horizontal'
  45. returning[1] = this.origin[1]
  46. }
  47. this.previous = returning
  48. this.points.push(vec.sub(returning, this.origin))
  49. }
  50. } else {
  51. if (this.isLocked) {
  52. this.isLocked = false
  53. }
  54. }
  55. if (this.isLocked) {
  56. if (this.lockedDirection === 'vertical') {
  57. point[0] = this.origin[0]
  58. } else {
  59. point[1] = this.origin[1]
  60. }
  61. }
  62. point = vec.med(this.previous, point)
  63. const next = [...vec.sub(point, this.origin), pressure]
  64. // Don't add duplicate points
  65. if (vec.isEqual(this.last, next)) return
  66. this.points.push(next)
  67. this.last = next
  68. this.previous = point
  69. const shape = getShape(data, snapshot.id) as DrawShape
  70. getShapeUtils(shape).setProperty(shape, 'points', [...this.points])
  71. updateParents(data, [shape.id])
  72. }
  73. cancel = (data: Data) => {
  74. const { snapshot } = this
  75. const shape = getShape(data, snapshot.id) as DrawShape
  76. getShapeUtils(shape).setProperty(shape, 'points', snapshot.points)
  77. updateParents(data, [shape.id])
  78. }
  79. complete = (data: Data) => {
  80. const { snapshot } = this
  81. const page = getPage(data)
  82. const shape = page.shapes[snapshot.id] as DrawShape
  83. getShapeUtils(shape).onSessionComplete(shape)
  84. commands.draw(data, this.snapshot.id)
  85. }
  86. }
  87. export function getDrawSnapshot(data: Data, shapeId: string) {
  88. const page = getPage(current(data))
  89. const { points } = page.shapes[shapeId] as DrawShape
  90. return {
  91. id: shapeId,
  92. points,
  93. }
  94. }
  95. export type DrawSnapshot = ReturnType<typeof getDrawSnapshot>