選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

draw-session.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 } 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. points: number[][]
  13. snapshot: DrawSnapshot
  14. isLocked: boolean
  15. lockedDirection: 'horizontal' | 'vertical'
  16. constructor(data: Data, id: string, point: number[], isLocked = false) {
  17. super(data)
  18. this.origin = point
  19. this.previous = point
  20. this.points = []
  21. this.snapshot = getDrawSnapshot(data, id)
  22. const page = getPage(data)
  23. const shape = page.shapes[id]
  24. getShapeUtils(shape).translateTo(shape, point)
  25. }
  26. update = (data: Data, point: number[], isLocked = false) => {
  27. const { snapshot } = this
  28. const delta = vec.vec(this.origin, point)
  29. if (isLocked) {
  30. if (!this.isLocked && this.points.length > 1) {
  31. this.isLocked = true
  32. const returning = [...this.previous]
  33. if (Math.abs(delta[0]) < Math.abs(delta[1])) {
  34. this.lockedDirection = 'vertical'
  35. returning[0] = this.origin[0]
  36. } else {
  37. this.lockedDirection = 'horizontal'
  38. returning[1] = this.origin[1]
  39. }
  40. this.previous = returning
  41. this.points.push(vec.sub(returning, this.origin))
  42. }
  43. } else {
  44. if (this.isLocked) {
  45. this.isLocked = false
  46. }
  47. }
  48. if (this.isLocked) {
  49. if (this.lockedDirection === 'vertical') {
  50. point[0] = this.origin[0]
  51. } else {
  52. point[1] = this.origin[1]
  53. }
  54. }
  55. if (this.previous) {
  56. point = vec.med(this.previous, point)
  57. }
  58. prevEndPoint = [...point]
  59. const next = vec.sub(point, this.origin)
  60. this.points.push(next)
  61. this.previous = point
  62. const page = getPage(data)
  63. const shape = page.shapes[snapshot.id] as DrawShape
  64. getShapeUtils(shape).setProperty(shape, 'points', [...this.points])
  65. }
  66. cancel = (data: Data) => {
  67. const { snapshot } = this
  68. const page = getPage(data)
  69. const shape = page.shapes[snapshot.id] as DrawShape
  70. getShapeUtils(shape).setProperty(shape, 'points', snapshot.points)
  71. }
  72. complete = (data: Data) => {
  73. if (this.points.length > 1) {
  74. let minX = Infinity
  75. let minY = Infinity
  76. const pts = [...this.points]
  77. for (let pt of pts) {
  78. minX = Math.min(pt[0], minX)
  79. minY = Math.min(pt[1], minY)
  80. }
  81. for (let pt of pts) {
  82. pt[0] -= minX
  83. pt[1] -= minY
  84. }
  85. const { snapshot } = this
  86. const page = getPage(data)
  87. const shape = page.shapes[snapshot.id] as DrawShape
  88. getShapeUtils(shape)
  89. .setProperty(shape, 'points', pts)
  90. .translateTo(shape, vec.add(shape.point, [minX, minY]))
  91. }
  92. commands.draw(data, this.snapshot.id, this.points)
  93. }
  94. }
  95. export function getDrawSnapshot(data: Data, shapeId: string) {
  96. const page = getPage(current(data))
  97. const { points } = page.shapes[shapeId] as DrawShape
  98. return {
  99. id: shapeId,
  100. points,
  101. }
  102. }
  103. export type DrawSnapshot = ReturnType<typeof getDrawSnapshot>