slideDoor.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import Door, { DoorProps } from './door/index'
  2. import { SVGURI } from '../constant/Element'
  3. import { lineDis, verticalLine, getLineDisSelectPoint } from '../geometry'
  4. interface SlideDoorProps extends DoorProps {
  5. clipColor?: string,
  6. within: number,
  7. bwithin: number
  8. }
  9. /**
  10. * @category core
  11. * @subcategory CAD_Architecture
  12. */
  13. class SlideDoor extends Door {
  14. layer: SVGPathElement
  15. clip1: SVGPathElement
  16. clip2: SVGPathElement
  17. path1: SVGPathElement
  18. path2: SVGPathElement
  19. left: SVGRectElement
  20. right: SVGRectElement
  21. constructor({points, foorWidth = 6, foorColor = 'rgba(255,255,255,0.5)', clipColor = 'rgba(0,0,0,1)', linecap = "square", within = 0, ...args}: SlideDoorProps) {
  22. points[0].fillColor = 'rgba(0,0,0,0)'
  23. points[1].fillColor = 'rgba(0,0,0,0)'
  24. super({...args, points, foorWidth, foorColor, within })
  25. this.clipColor = clipColor
  26. }
  27. setHoverStyle() {
  28. this.clipColor = 'rgba(243, 255, 0, 0.8)'
  29. this.foorColor = 'rgba(243, 255, 0, 0.8)'
  30. }
  31. setUnHoverStyle() {
  32. this.clipColor = 'rgba(0,0,0,1)'
  33. this.foorColor = 'rgba(255,255,255,0.5)'
  34. }
  35. grentNode() {
  36. let node = document.createElementNS(SVGURI, 'g')
  37. this.clip1 = document.createElementNS(SVGURI, 'path')
  38. node.appendChild(this.clip1)
  39. this.clip2 = document.createElementNS(SVGURI, 'path')
  40. node.appendChild(this.clip2)
  41. this.path1 = document.createElementNS(SVGURI, 'path')
  42. node.appendChild(this.path1)
  43. this.path2 = document.createElementNS(SVGURI, 'path')
  44. node.appendChild(this.path2)
  45. this.left = document.createElementNS(SVGURI, 'rect')
  46. this.left.setAttribute('width', '0.00001')
  47. this.left.setAttribute('height', '0.00001')
  48. node.appendChild(this.left)
  49. this.right = document.createElementNS(SVGURI, 'rect')
  50. this.right.setAttribute('width', '0.00001')
  51. this.right.setAttribute('height', '0.00001')
  52. node.appendChild(this.right)
  53. this.nextTick(() => {
  54. node.appendChild(this.linePoints[0].real)
  55. node.appendChild(this.linePoints[1].real)
  56. this.linePoints.forEach(point => {
  57. point.changeSelect = (isSelect) => {
  58. isSelect && this.changeSelect(isSelect)
  59. }
  60. })
  61. })
  62. return node
  63. }
  64. update() {
  65. // if (!this.path1 || !this.clip1 || !this.path2)
  66. let width = this.foorWidth * this.multiple
  67. let lineWidth = lineDis({points: this.linePoints})
  68. let padding = 2 * this.multiple
  69. let lr = lineWidth * 0.6
  70. let p1, p2;
  71. if (this.within) {
  72. [p1, p2] = this.linePoints
  73. } else {
  74. [p2, p1] = this.linePoints
  75. }
  76. if (isNaN(p1.x)) return;
  77. let leftr = getLineDisSelectPoint({points: this.linePoints}, p1, lr)
  78. let leftrc = getLineDisSelectPoint({points: this.linePoints}, p1, lr - padding)
  79. let rightl = getLineDisSelectPoint({points: this.linePoints}, p2, lr)
  80. let rightlc = getLineDisSelectPoint({points: this.linePoints}, p2, lr - padding)
  81. let vv = verticalLine({points: this.linePoints})
  82. let lrwidth = width / 2
  83. let marginWidth = lrwidth - padding
  84. let margin = ((width - lrwidth) / 2) - 0.0001 * this.multiple
  85. let startl = {
  86. x: p1.x + vv.x * margin,
  87. y: p1.y + vv.y * margin
  88. }
  89. let endl = {
  90. x: leftr.x + vv.x * margin,
  91. y: leftr.y + vv.y * margin,
  92. }
  93. let endlc = {
  94. x: leftrc.x + vv.x * margin,
  95. y: leftrc.y + vv.y * margin,
  96. }
  97. let startr = {
  98. x: p2.x - vv.x * margin,
  99. y: p2.y - vv.y * margin
  100. }
  101. let endr = {
  102. x: rightl.x - vv.x * margin,
  103. y: rightl.y - vv.y * margin,
  104. }
  105. let endrc = {
  106. x: rightlc.x - vv.x * margin,
  107. y: rightlc.y - vv.y * margin,
  108. }
  109. this.path1.setAttribute('stroke', this.foorColor)
  110. this.path1.setAttribute('stroke-width', lrwidth.toString())
  111. this.path1.setAttribute('d', `M ${startl.x} ${startl.y} L ${endl.x} ${endl.y}`)
  112. this.clip1.setAttribute('stroke', this.clipColor)
  113. this.clip1.setAttribute('stroke-width', (lrwidth - this.bwithin * this.multiple) .toString())
  114. this.clip1.setAttribute('d', `M ${startl.x} ${startl.y} L ${endlc.x} ${endlc.y}`)
  115. this.path2.setAttribute('stroke', this.foorColor)
  116. this.path2.setAttribute('stroke-width', (lrwidth).toString())
  117. this.path2.setAttribute('d', `M ${startr.x} ${startr.y} L ${endr.x} ${endr.y}`)
  118. this.clip2.setAttribute('stroke', this.clipColor)
  119. this.clip2.setAttribute('stroke-width', (lrwidth - this.bwithin * this.multiple) .toString())
  120. this.clip2.setAttribute('d', `M ${startr.x} ${startr.y} L ${endrc.x} ${endrc.y}`)
  121. this.left.setAttribute('x', p1.x.toString())
  122. this.left.setAttribute('y', p1.y.toString())
  123. this.left.setAttribute('stroke-width', width.toString())
  124. this.left.setAttribute('stroke', 'rgba(0,0,0,0)')
  125. this.right.setAttribute('x', p2.x.toString())
  126. this.right.setAttribute('y', p2.y.toString())
  127. this.right.setAttribute('stroke-width', width.toString())
  128. this.right.setAttribute('stroke', 'rgba(0,0,0,0)')
  129. this.attachment.update()
  130. }
  131. }
  132. export default SlideDoor