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.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-31 19:45:32
  4. */
  5. import { PlotEventType } from '@dc-modules/event'
  6. class Draw {
  7. constructor(style) {
  8. this._style = style
  9. this._viewer = undefined
  10. this._layer = undefined
  11. this._delegate = undefined
  12. this._options = {}
  13. this._positions = []
  14. }
  15. get drawTool() {
  16. return this._viewer.drawTool
  17. }
  18. /**
  19. * The hook for mount layer
  20. * Subclasses need to be overridden
  21. * @private
  22. */
  23. _mountedHook() {}
  24. /**
  25. *
  26. * @param position
  27. * @private
  28. */
  29. _onDrawAnchor(position) {}
  30. /**
  31. *
  32. * @param position
  33. * @private
  34. */
  35. _onAnchorMoving(position) {
  36. this._positions.pop()
  37. this._positions.push(position)
  38. this._options.onCalc && this._options.onCalc(this._positions)
  39. }
  40. /**
  41. *
  42. * @private
  43. */
  44. _onDrawStop() {
  45. this._unbindEvent()
  46. this._viewer.drawTool.deactivate()
  47. this._options.onDrawStop && this._options.onDrawStop(this._delegate)
  48. }
  49. /**
  50. *
  51. * @private
  52. */
  53. _bindEvent() {
  54. this.drawTool.on(PlotEventType.DRAW_ANCHOR, this._onDrawAnchor, this)
  55. this.drawTool.on(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
  56. this.drawTool.on(PlotEventType.DRAW_STOP, this._onDrawStop, this)
  57. }
  58. /**
  59. *
  60. * @private
  61. */
  62. _unbindEvent() {
  63. this.drawTool.off(PlotEventType.DRAW_ANCHOR, this._onDrawAnchor, this)
  64. this.drawTool.off(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
  65. this.drawTool.off(PlotEventType.DRAW_STOP, this._onDrawStop, this)
  66. }
  67. /**
  68. *
  69. * @param measure
  70. * @param options
  71. * @returns {Draw}
  72. */
  73. start(measure, options) {
  74. this._viewer = measure.viewer
  75. this._layer = measure.layer
  76. this._options = options
  77. this._viewer.editTool.deactivate()
  78. this._viewer.drawTool.activate(options)
  79. this._mountedHook()
  80. this._unbindEvent()
  81. this._bindEvent()
  82. return this
  83. }
  84. }
  85. export default Draw