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.

DrawTool.ts 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Utils, TLPointerEventHandler } from '@tldraw/core'
  2. import { Draw } from '~state/shapes'
  3. import { SessionType, TDShapeType } from '~types'
  4. import { BaseTool, Status } from '../BaseTool'
  5. export class DrawTool extends BaseTool {
  6. type = TDShapeType.Draw as const
  7. /* ----------------- Event Handlers ----------------- */
  8. onPointerDown: TLPointerEventHandler = (info) => {
  9. if (this.status !== Status.Idle) return
  10. const {
  11. currentPoint,
  12. appState: { currentPageId, currentStyle },
  13. } = this.app
  14. const childIndex = this.getNextChildIndex()
  15. const id = Utils.uniqueId()
  16. const newShape = Draw.create({
  17. id,
  18. parentId: currentPageId,
  19. childIndex,
  20. point: [...currentPoint, info.pressure || 0.5],
  21. style: { ...currentStyle },
  22. })
  23. this.app.patchCreate([newShape])
  24. this.app.startSession(SessionType.Draw, id)
  25. this.setStatus(Status.Creating)
  26. }
  27. onPointerMove: TLPointerEventHandler = () => {
  28. if (this.status === Status.Creating) {
  29. this.app.updateSession()
  30. }
  31. }
  32. onPointerUp: TLPointerEventHandler = () => {
  33. if (this.status === Status.Creating) {
  34. this.app.completeSession()
  35. }
  36. this.setStatus(Status.Idle)
  37. }
  38. }