您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DivIcon.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 style
  62. * @param distance
  63. * @param isFront
  64. * @private
  65. */
  66. _updateStyle(style, distance, isFront) {
  67. if (!this._show) {
  68. return
  69. }
  70. let isDisplay = true
  71. let translate3d = 'translate3d(0,0,0)'
  72. if (style.transform) {
  73. let x = style.transform.x - this._delegate.offsetWidth / 2
  74. let y = style.transform.y - this._delegate.offsetHeight / 2
  75. translate3d = `translate3d(${Math.round(x)}px,${Math.round(y)}px, 0)`
  76. }
  77. let scale3d = 'scale3d(1,1,1)'
  78. let scaleByDistance = this._style.scaleByDistance
  79. if (distance && scaleByDistance) {
  80. let nearValue = scaleByDistance.nearValue
  81. let farValue = scaleByDistance.farValue
  82. let f = distance / scaleByDistance.far
  83. if (distance < scaleByDistance.near) {
  84. scale3d = `scale3d(${nearValue},${nearValue},1)`
  85. } else if (distance > scaleByDistance.far) {
  86. scale3d = `scale3d(${farValue},${farValue},1)`
  87. } else {
  88. let scale = farValue + f * (nearValue - farValue)
  89. scale3d = `scale3d(${scale},${scale},1)`
  90. }
  91. }
  92. let distanceDisplayCondition = this._style.distanceDisplayCondition
  93. if (distance && distanceDisplayCondition) {
  94. isDisplay = isBetween(
  95. distance,
  96. distanceDisplayCondition.near,
  97. distanceDisplayCondition.far
  98. )
  99. }
  100. this._delegate.style.transform = `${translate3d} ${scale3d}`
  101. this._delegate.style.visibility =
  102. isDisplay && isFront ? 'visible' : 'hidden'
  103. }
  104. /**
  105. *
  106. * @param layer
  107. * @returns {boolean}
  108. * @private
  109. */
  110. _onAdd(layer) {
  111. this._layer = layer
  112. this._layer.delegate.appendChild(this._delegate)
  113. let params = {
  114. layer: layer,
  115. overlay: this,
  116. position: Transform.transformWGS84ToCartesian(this._position)
  117. }
  118. this._delegate.addEventListener('click', () => {
  119. this._overlayEvent.fire(MouseEventType.CLICK, params)
  120. })
  121. this._delegate.addEventListener('mouseover', () => {
  122. this._overlayEvent.fire(MouseEventType.MOUSE_OVER, params)
  123. })
  124. this._delegate.addEventListener('mouseout', () => {
  125. this._overlayEvent.fire(MouseEventType.MOUSE_OUT, params)
  126. })
  127. this._state = State.ADDED
  128. }
  129. /**
  130. *
  131. * @private
  132. */
  133. _onRemove() {
  134. if (this._layer) {
  135. this._layer.delegate.removeChild(this._delegate)
  136. this._state = State.REMOVED
  137. }
  138. }
  139. /**
  140. * Sets text
  141. * @param text
  142. * @param textStyle
  143. * @returns {DivIcon}
  144. */
  145. setLabel(text, textStyle) {
  146. return this
  147. }
  148. /**
  149. * Sets style
  150. * @param style
  151. * @returns {DivIcon}
  152. */
  153. setStyle(style) {
  154. if (!style || Object.keys(style).length === 0) {
  155. return this
  156. }
  157. this._style = style
  158. this._style.className &&
  159. DomUtil.addClass(this._delegate, this._style.className)
  160. return this
  161. }
  162. /**
  163. * Parse from entity
  164. * @param entity
  165. * @param content
  166. * @returns {DivIcon}
  167. */
  168. static fromEntity(entity, content) {
  169. let divIcon
  170. let now = Cesium.JulianDate.now()
  171. let position = Transform.transformCartesianToWGS84(
  172. entity.position.getValue(now)
  173. )
  174. divIcon = new DivIcon(position, content)
  175. if (entity.billboard) {
  176. divIcon.attr = {
  177. ...entity?.properties?.getValue(now)
  178. }
  179. }
  180. return divIcon
  181. }
  182. }
  183. Overlay.registerType('div_icon')
  184. export default DivIcon