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.

BounceLabelPrimitive.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Overlay from '../Overlay'
  8. import LabelPrimitive from './LabelPrimitive'
  9. const DEF_STYLE = {
  10. maxOffsetY: 10,
  11. offsetAmount: 0.1
  12. }
  13. class BounceLabelPrimitive extends LabelPrimitive {
  14. constructor(position, text) {
  15. super(position, text)
  16. this._currentOffset = new Cesium.Cartesian2(0, 0)
  17. this._isUp = true
  18. this._state = State.INITIALIZED
  19. }
  20. get type() {
  21. return Overlay.getOverlayType('bounce_label_primitive')
  22. }
  23. /**
  24. *
  25. * @private
  26. */
  27. _addedHook() {
  28. if (!this._delegate || !this._layer) {
  29. return
  30. }
  31. this._delegate.layerId = this._layer?.layerId
  32. this._delegate.overlayId = this._id
  33. this._layer.delegate.add(this)
  34. }
  35. /**
  36. *
  37. * @private
  38. */
  39. _removedHook() {
  40. if (!this._layer) {
  41. return
  42. }
  43. this._layer.delegate.remove(this)
  44. }
  45. update() {
  46. if (!this._show) {
  47. return
  48. }
  49. let maxOffsetY = this._style?.maxOffsetY || DEF_STYLE.maxOffsetY
  50. let offsetAmount = this._style?.offsetAmount || DEF_STYLE.offsetAmount
  51. if (this._currentOffset.y >= 0) {
  52. this._isUp = true
  53. } else if (this._currentOffset.y <= -maxOffsetY) {
  54. this._isUp = false
  55. }
  56. this._currentOffset.y += offsetAmount * (this._isUp ? -1 : 1)
  57. this._delegate.pixelOffset = this._currentOffset
  58. }
  59. /**
  60. * @return {*}
  61. */
  62. destroy() {
  63. return Cesium.destroyObject(this)
  64. }
  65. }
  66. Overlay.registerType('bounce_label_primitive')
  67. export default BounceLabelPrimitive