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

draw-session.ts 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, simplify } from 'utils/utils'
  6. import * as vec from 'utils/vec'
  7. import commands from 'state/commands'
  8. export default class BrushSession extends BaseSession {
  9. origin: number[]
  10. previous: number[]
  11. points: number[][]
  12. snapshot: DrawSnapshot
  13. shapeId: string
  14. constructor(data: Data, id: string, point: number[]) {
  15. super(data)
  16. this.shapeId = id
  17. this.origin = point
  18. this.previous = point
  19. this.points = []
  20. this.snapshot = getDrawSnapshot(data, id)
  21. const page = getPage(data)
  22. const shape = page.shapes[id]
  23. getShapeUtils(shape).translateTo(shape, point)
  24. }
  25. update = (data: Data, point: number[]) => {
  26. const { shapeId } = this
  27. const lp = vec.med(this.previous, vec.toPrecision(point))
  28. this.points.push(vec.sub(lp, this.origin))
  29. this.previous = lp
  30. const page = getPage(data)
  31. const shape = page.shapes[shapeId]
  32. getShapeUtils(shape).setPoints!(shape, [...this.points])
  33. }
  34. cancel = (data: Data) => {
  35. const { shapeId, snapshot } = this
  36. const page = getPage(data)
  37. const shape = page.shapes[shapeId]
  38. getShapeUtils(shape).setPoints!(shape, snapshot.points)
  39. }
  40. complete = (data: Data) => {
  41. commands.draw(data, this.shapeId, this.snapshot.points, this.points)
  42. }
  43. }
  44. export function getDrawSnapshot(data: Data, shapeId: string) {
  45. const page = getPage(current(data))
  46. const { points } = page.shapes[shapeId] as DrawShape
  47. return {
  48. points,
  49. }
  50. }
  51. export type DrawSnapshot = ReturnType<typeof getDrawSnapshot>