Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DivIcon.js 5.0KB

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