Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-02-12 21:46:22
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import Parse from '@dc-modules/parse/Parse'
  8. import { DomUtil, Util } from '@dc-modules/utils'
  9. import { MouseEventType } from '@dc-modules/event'
  10. import { isBetween } from '@dc-modules/math'
  11. import { Transform } from '@dc-modules/transform'
  12. import Overlay from '../Overlay'
  13. class DivIcon extends Overlay {
  14. constructor(position, content) {
  15. super()
  16. this._delegate = DomUtil.create('div', 'div-icon')
  17. this._position = Parse.parsePosition(position)
  18. this._delegate.setAttribute('id', this._id)
  19. Util.merge(this._delegate.style, {
  20. position: 'absolute',
  21. top: '0',
  22. left: '0'
  23. })
  24. this.content = content
  25. this._state = State.INITIALIZED
  26. }
  27. get type() {
  28. return Overlay.getOverlayType('div_icon')
  29. }
  30. set show(show) {
  31. this._show = show
  32. this._delegate.style.visibility = this._show ? 'visible' : 'hidden'
  33. return this
  34. }
  35. get show() {
  36. return this._show
  37. }
  38. set position(position) {
  39. this._position = Parse.parsePosition(position)
  40. return this
  41. }
  42. get position() {
  43. return this._position
  44. }
  45. set content(content) {
  46. if (content && typeof content === 'string') {
  47. this._delegate.innerHTML = content
  48. } else if (content && content instanceof Element) {
  49. while (this._delegate.hasChildNodes()) {
  50. this._delegate.removeChild(this._delegate.firstChild)
  51. }
  52. this._delegate.appendChild(content)
  53. }
  54. return this
  55. }
  56. get content() {
  57. return this._delegate.childNodes || []
  58. }
  59. /**
  60. * Updates style
  61. * @param windowCoord
  62. * @param distance
  63. * @param isFront
  64. * @private
  65. */
  66. _updateStyle(windowCoord, distance, isFront) {
  67. if (!this._show || !windowCoord) {
  68. return
  69. }
  70. // set translate
  71. let x = windowCoord.x - this._delegate.offsetWidth / 2
  72. let y = windowCoord.y - this._delegate.offsetHeight / 2
  73. let translate3d = `translate3d(${Math.round(x)}px,${Math.round(y)}px, 0)`
  74. // set scale
  75. let scale3d = 'scale3d(1,1,1)'
  76. let scaleByDistance = this._style.scaleByDistance
  77. if (distance && scaleByDistance) {
  78. let nearValue = scaleByDistance.nearValue
  79. let farValue = scaleByDistance.farValue
  80. let f = distance / scaleByDistance.far
  81. if (distance < scaleByDistance.near) {
  82. scale3d = `scale3d(${nearValue},${nearValue},1)`
  83. } else if (distance > scaleByDistance.far) {
  84. scale3d = `scale3d(${farValue},${farValue},1)`
  85. } else {
  86. let scale = farValue + f * (nearValue - farValue)
  87. scale3d = `scale3d(${scale},${scale},1)`
  88. }
  89. }
  90. // set condition
  91. let isDisplay = true
  92. let distanceDisplayCondition = this._style.distanceDisplayCondition
  93. if (distance && distanceDisplayCondition) {
  94. isDisplay = isBetween(
  95. distance,
  96. distanceDisplayCondition.near,
  97. distanceDisplayCondition.far
  98. )
  99. }
  100. // update style
  101. this._delegate.style.transform = `${translate3d} ${scale3d}`
  102. this._delegate.style.visibility =
  103. isDisplay && isFront ? 'visible' : 'hidden'
  104. }
  105. /**
  106. *
  107. * @param layer
  108. * @returns {boolean}
  109. * @private
  110. */
  111. _onAdd(layer) {
  112. this._layer = layer
  113. this._layer.delegate.appendChild(this._delegate)
  114. let params = {
  115. layer: layer,
  116. overlay: this,
  117. position: Transform.transformWGS84ToCartesian(this._position)
  118. }
  119. this._delegate.addEventListener('click', () => {
  120. this._overlayEvent.fire(MouseEventType.CLICK, params)
  121. })
  122. this._delegate.addEventListener('mouseover', () => {
  123. this._overlayEvent.fire(MouseEventType.MOUSE_OVER, params)
  124. })
  125. this._delegate.addEventListener('mouseout', () => {
  126. this._overlayEvent.fire(MouseEventType.MOUSE_OUT, params)
  127. })
  128. this._state = State.ADDED
  129. }
  130. /**
  131. *
  132. * @private
  133. */
  134. _onRemove() {
  135. if (this._layer) {
  136. this._layer.delegate.removeChild(this._delegate)
  137. this._state = State.REMOVED
  138. }
  139. }
  140. /**
  141. * Sets text
  142. * @param text
  143. * @param textStyle
  144. * @returns {DivIcon}
  145. */
  146. setLabel(text, textStyle) {
  147. return this
  148. }
  149. /**
  150. * Sets style
  151. * @param style
  152. * @returns {DivIcon}
  153. */
  154. setStyle(style) {
  155. if (!style || Object.keys(style).length === 0) {
  156. return this
  157. }
  158. this._style = style
  159. this._style.className &&
  160. DomUtil.addClass(this._delegate, this._style.className)
  161. return this
  162. }
  163. /**
  164. * Parse from entity
  165. * @param entity
  166. * @param content
  167. * @returns {DivIcon}
  168. */
  169. static fromEntity(entity, content) {
  170. let divIcon
  171. let now = Cesium.JulianDate.now()
  172. let position = Transform.transformCartesianToWGS84(
  173. entity.position.getValue(now)
  174. )
  175. divIcon = new DivIcon(position, content)
  176. if (entity.billboard) {
  177. divIcon.attr = {
  178. ...entity?.properties?.getValue(now)
  179. }
  180. }
  181. return divIcon
  182. }
  183. }
  184. Overlay.registerType('div_icon')
  185. export default DivIcon