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.

BounceBillboardPrimitive.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-06-03 21:06:17
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import { Util } from '@dc-modules/utils'
  8. import Overlay from '../Overlay'
  9. import BillboardPrimitive from './BillboardPrimitive'
  10. const DEF_STYLE = {
  11. maxOffsetY: 10,
  12. offsetAmount: 0.1
  13. }
  14. class BounceBillboardPrimitive extends BillboardPrimitive {
  15. constructor(position, icon) {
  16. super(position, icon)
  17. this._currentOffset = new Cesium.Cartesian2(0, 0)
  18. this._isUp = true
  19. this.type = Overlay.getOverlayType('bounce_billboard_primitive')
  20. this._state = State.INITIALIZED
  21. }
  22. /**
  23. *
  24. * @param layer
  25. * @private
  26. */
  27. _onAdd(layer) {
  28. if (!layer) {
  29. return
  30. }
  31. this._layer = layer
  32. this._mountedHook && this._mountedHook()
  33. if (this._layer?.delegate?.add && this._delegate) {
  34. this._delegate = this._layer.billboards.add(this._delegate)
  35. this._layer.delegate.add(this)
  36. }
  37. this._addedHook && this._addedHook()
  38. this._state = State.ADDED
  39. }
  40. _onRemove() {
  41. if (!this._layer || !this._delegate) {
  42. return
  43. }
  44. if (this._layer?.delegate?.remove) {
  45. this._layer.billboards.remove(this._delegate)
  46. this._layer.delegate.remove(this)
  47. }
  48. this._removedHook && this._removedHook()
  49. this._state = State.REMOVED
  50. }
  51. /**
  52. *
  53. */
  54. update() {
  55. if (!this._show) {
  56. return
  57. }
  58. let maxOffsetY = this._style?.maxOffsetY || DEF_STYLE.maxOffsetY
  59. let offsetAmount = this._style?.offsetAmount || DEF_STYLE.offsetAmount
  60. if (this._currentOffset.y >= 0) {
  61. this._isUp = true
  62. } else if (this._currentOffset.y <= -maxOffsetY) {
  63. this._isUp = false
  64. }
  65. this._currentOffset.y += offsetAmount * (this._isUp ? -1 : 1)
  66. this._delegate.pixelOffset = this._currentOffset
  67. }
  68. /**
  69. *
  70. * @param style
  71. * @returns {BounceBillboardPrimitive}
  72. */
  73. setStyle(style) {
  74. if (!style || Object.keys(style).length === 0) {
  75. return this
  76. }
  77. delete style['position'] &&
  78. delete style['image'] &&
  79. delete style['width'] &&
  80. delete style['height']
  81. this._style = style
  82. Util.merge(this._delegate, this._style)
  83. return this
  84. }
  85. }
  86. Overlay.registerType('bounce_billboard_primitive')
  87. export default BounceBillboardPrimitive