@aibee/crc-bmap 0.0.90 → 0.0.92

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bmap.esm.js CHANGED
@@ -500,7 +500,8 @@ import {
500
500
  Box3,
501
501
  Vector3 as Vector32,
502
502
  BufferGeometry,
503
- LineSegments
503
+ LineSegments,
504
+ Ray
504
505
  } from "three";
505
506
  var defaultOptions = {
506
507
  id: "",
@@ -642,7 +643,7 @@ var Graphic = class extends Object3D {
642
643
  }
643
644
  getPosition() {
644
645
  const center2 = this.getCenter();
645
- center2.setZ(center2.z + this.options.height);
646
+ center2.setZ(center2.z + this.options.height / 2);
646
647
  return center2;
647
648
  }
648
649
  init() {
@@ -685,31 +686,67 @@ var Graphic = class extends Object3D {
685
686
  this.material = [material, material1];
686
687
  return [material, material1];
687
688
  }
689
+ // createMesh(pos: Vector3, color = 0xff0000) {
690
+ // const geometry = new BoxGeometry(1, 1, 1)
691
+ // const material = new MeshBasicMaterial({ color })
692
+ // const mesh = new Mesh(geometry, material)
693
+ // mesh.position.copy(pos)
694
+ // this.context.scene.add(mesh)
695
+ // }
696
+ // createLine(points: Vector3[], color = 0xff0000) {
697
+ // const material = new LineBasicMaterial({
698
+ // color: color
699
+ // });
700
+ // const geometry = new BufferGeometry().setFromPoints(points);
701
+ // const line = new Line( geometry, material );
702
+ // this.context.scene.add(line)
703
+ // }
704
+ getMaxAndMin(pos, dir) {
705
+ const box = new Box3().setFromObject(this);
706
+ const minOrigin = pos.clone().add(dir.clone().multiplyScalar(1e3));
707
+ const raycaster = new Ray(minOrigin, dir.clone().multiplyScalar(-1));
708
+ const min = new Vector32();
709
+ raycaster.intersectBox(box, min);
710
+ const maxOrigin = pos.clone().add(dir.clone().multiplyScalar(-1e3));
711
+ const raycaster2 = new Ray(maxOrigin, dir.clone());
712
+ const max = new Vector32();
713
+ raycaster2.intersectBox(box, max);
714
+ return {
715
+ max,
716
+ min
717
+ };
718
+ }
688
719
  initGradualMaterial() {
689
720
  const { max, min } = new Box3().setFromObject(this);
690
721
  const _max = max.clone().sub(min);
691
722
  const maxValue = Math.max(_max.x, _max.y, _max.z) / 2;
723
+ const pos = this.getPosition();
724
+ const dir = new Vector32(-1, 0.2, 0).normalize();
725
+ const { max: __max, min: _min } = this.getMaxAndMin(pos, dir);
692
726
  const material = this.context.materialFactory.createShaderMaterial({
693
727
  gradualColor: this.options.gradualColor,
694
728
  center: this.getPosition(),
695
729
  maxValue,
696
730
  opacity: this.options.fillOpacity,
697
731
  direction: new Vector32(-1, 0.2, 0),
698
- max,
699
- min
732
+ max: __max,
733
+ min: _min
700
734
  });
701
735
  if (this.options.height <= 0.1) {
702
736
  this.material = material;
703
737
  return material;
704
738
  }
739
+ const center2 = this.getCenter();
740
+ const dir2 = new Vector32(-1, 0.2, 1).normalize();
741
+ const { max: ___max, min: __min } = this.getMaxAndMin(center2, dir2);
705
742
  const material1 = this.context.materialFactory.createShaderMaterial({
706
743
  gradualColor: [darkenColor(this.options.gradualColor[0], this.options.colorFactor), darkenColor(this.options.gradualColor[1], this.options.colorFactor)],
707
744
  center: this.getCenter(),
708
745
  maxValue,
709
746
  opacity: this.options.fillOpacity,
710
747
  direction: new Vector32(-1, 0.2, 1),
711
- max,
712
- min
748
+ max: ___max,
749
+ min: __min
713
750
  });
714
751
  this.material = [material, material1];
715
752
  return [material, material1];
@@ -867,9 +904,11 @@ import { EventDispatcher as EventDispatcher2, Vector3 as Vector35 } from "three"
867
904
 
868
905
  // src/elements/overlay.ts
869
906
  import { Box3 as Box32, EventDispatcher, Vector3 as Vector34 } from "three";
907
+ import { debounce } from "lodash";
870
908
  var defaultOptions2 = {
871
909
  autoUpdate: true,
872
- appendToBody: false
910
+ appendToBody: false,
911
+ autoChangePlacement: false
873
912
  };
874
913
  var Overlay = class extends EventDispatcher {
875
914
  constructor(context, options = {}) {
@@ -881,8 +920,14 @@ var Overlay = class extends EventDispatcher {
881
920
  __publicField(this, "clientPos", { x: 0, y: 0 });
882
921
  __publicField(this, "visible", true);
883
922
  __publicField(this, "options");
923
+ __publicField(this, "placement", "top");
924
+ __publicField(this, "observer", null);
884
925
  __publicField(this, "onUpdate", () => {
885
- this.updatePosition();
926
+ if (this.options.autoChangePlacement) {
927
+ this.usePlacement();
928
+ } else {
929
+ this.updatePosition();
930
+ }
886
931
  });
887
932
  this.options = __spreadValues(__spreadValues({}, defaultOptions2), options);
888
933
  this.registryEvent();
@@ -893,13 +938,175 @@ var Overlay = class extends EventDispatcher {
893
938
  this.context.container.appendChild(this.div);
894
939
  }
895
940
  }
941
+ initObserver() {
942
+ const observer = new MutationObserver(
943
+ debounce(() => {
944
+ this.usePlacement();
945
+ }, 100)
946
+ );
947
+ observer.observe(this.div, {
948
+ childList: true,
949
+ subtree: true,
950
+ attributes: true
951
+ });
952
+ this.observer = observer;
953
+ }
954
+ getPlacementPosition() {
955
+ const { max, min } = new Box32().setFromObject(this.element);
956
+ const centerX = (max.x + min.x) / 2;
957
+ const centerY = (max.y + min.y) / 2;
958
+ return {
959
+ left: new Vector34(min.x, centerY, max.z),
960
+ leftTop: new Vector34(min.x, max.y, max.z),
961
+ top: new Vector34(centerX, max.y, max.z),
962
+ rightTop: new Vector34(max.x, max.y, max.z),
963
+ right: new Vector34(max.x, centerY, max.z),
964
+ rightBottom: new Vector34(max.x, min.y, max.z),
965
+ bottom: new Vector34(centerX, min.y, max.z),
966
+ leftBottom: new Vector34(min.x, min.y, max.z),
967
+ center: new Vector34(centerX, centerY, max.z)
968
+ };
969
+ }
970
+ getPlacementScreenPosition() {
971
+ const { width, height } = this.context.clientSize;
972
+ const positions = this.getPlacementPosition();
973
+ return Object.keys(positions).reduce((obj, key) => {
974
+ const { x, y } = vector3ToDevice(
975
+ positions[key],
976
+ this.context.camera,
977
+ width,
978
+ height
979
+ );
980
+ obj[key] = { x, y };
981
+ return obj;
982
+ }, {});
983
+ }
984
+ // 检查是不是溢出了context.container
985
+ // @return true 表示没有溢出
986
+ checkOverflow(max, min) {
987
+ const { width, height } = this.context.clientSize;
988
+ return min.x > 0 && max.x < width && min.y < height && max.y > 0;
989
+ }
990
+ // 计算适用的placement
991
+ getPlacement() {
992
+ const { clientWidth, clientHeight } = this.div;
993
+ const positions = this.getPlacementScreenPosition();
994
+ const halfWidth = clientWidth / 2;
995
+ const halfHeight = clientHeight / 2;
996
+ const placements = [
997
+ {
998
+ type: "center",
999
+ getBox(x, y) {
1000
+ return {
1001
+ max: { x: halfWidth + x, y: y - clientHeight },
1002
+ min: { x: x - halfWidth, y }
1003
+ };
1004
+ }
1005
+ },
1006
+ {
1007
+ type: "left",
1008
+ getBox(x, y) {
1009
+ return {
1010
+ max: { x, y: y - halfHeight },
1011
+ min: { x: x - clientWidth, y: y + halfHeight }
1012
+ };
1013
+ }
1014
+ },
1015
+ {
1016
+ type: "leftTop",
1017
+ getBox(x, y) {
1018
+ return {
1019
+ max: { x, y: y - clientHeight },
1020
+ min: { x: x - clientWidth, y }
1021
+ };
1022
+ }
1023
+ },
1024
+ {
1025
+ type: "top",
1026
+ getBox(x, y) {
1027
+ return {
1028
+ max: { x: x + halfWidth, y: y - clientHeight },
1029
+ min: { x: x - halfWidth, y }
1030
+ };
1031
+ }
1032
+ },
1033
+ {
1034
+ type: "rightTop",
1035
+ getBox(x, y) {
1036
+ return {
1037
+ max: { x: x + clientWidth, y: y - clientHeight },
1038
+ min: { x, y }
1039
+ };
1040
+ }
1041
+ },
1042
+ {
1043
+ type: "right",
1044
+ getBox(x, y) {
1045
+ return {
1046
+ max: { x: x + clientWidth, y: y - halfHeight },
1047
+ min: { x, y: y + halfHeight }
1048
+ };
1049
+ }
1050
+ },
1051
+ {
1052
+ type: "rightBottom",
1053
+ getBox(x, y) {
1054
+ return {
1055
+ max: { x: x + clientWidth, y },
1056
+ min: { x, y: y + clientHeight }
1057
+ };
1058
+ }
1059
+ },
1060
+ {
1061
+ type: "bottom",
1062
+ getBox(x, y) {
1063
+ return {
1064
+ max: { x: x + halfWidth, y },
1065
+ min: { x: x - halfWidth, y: y + clientHeight }
1066
+ };
1067
+ }
1068
+ },
1069
+ {
1070
+ type: "leftBottom",
1071
+ getBox(x, y) {
1072
+ return {
1073
+ max: { x, y },
1074
+ min: { x: x - clientWidth, y: y + halfHeight }
1075
+ };
1076
+ }
1077
+ }
1078
+ ];
1079
+ for (let i = 0; i < placements.length; i++) {
1080
+ const placement = placements[i];
1081
+ const position = positions[placement.type];
1082
+ const { max, min } = placement.getBox(position.x, position.y);
1083
+ if (this.checkOverflow(max, min)) {
1084
+ return {
1085
+ type: placement.type,
1086
+ position
1087
+ };
1088
+ }
1089
+ }
1090
+ return {
1091
+ type: "center",
1092
+ position: positions.center
1093
+ };
1094
+ }
896
1095
  initDiv() {
897
1096
  const div = document.createElement("div");
898
1097
  div.style.position = "absolute";
899
1098
  return div;
900
1099
  }
1100
+ usePlacement() {
1101
+ const placement = this.getPlacement();
1102
+ this.div.className = `overlay_${placement.type}`;
1103
+ this._updatePosition(placement.position.x, placement.position.y);
1104
+ }
901
1105
  bindElement(element) {
902
1106
  this.element = element;
1107
+ if (this.options.autoChangePlacement) {
1108
+ this.initObserver();
1109
+ }
903
1110
  this.onUpdate();
904
1111
  }
905
1112
  unBindElement() {
@@ -932,14 +1139,9 @@ var Overlay = class extends EventDispatcher {
932
1139
  const { width, height } = this.context.clientSize;
933
1140
  return x >= 0 && x <= width && y >= 0 && y <= height;
934
1141
  }
935
- updatePosition(force = false) {
936
- const vector = this.getPosition();
937
- const { width, height, x: clientX, y: clientY } = this.context.clientSize;
938
- const { x, y } = vector3ToDevice(vector, this.context.camera, width, height);
939
- if (this.clientPos.x === x && this.clientPos.y === y && !force) {
940
- return;
941
- }
1142
+ _updatePosition(x, y) {
942
1143
  this.clientPos = { x, y };
1144
+ const { width, height, x: clientX, y: clientY } = this.context.clientSize;
943
1145
  if (this.options.appendToBody) {
944
1146
  this.div.style.left = `${clientX}px`;
945
1147
  this.div.style.top = `${clientY + height}px`;
@@ -950,6 +1152,20 @@ var Overlay = class extends EventDispatcher {
950
1152
  this.dispatchEvent({ type: "update-position", x, y, width, height });
951
1153
  }
952
1154
  }
1155
+ updatePosition(force = false) {
1156
+ const vector = this.getPosition();
1157
+ const { width, height, x: clientX, y: clientY } = this.context.clientSize;
1158
+ const { x, y } = vector3ToDevice(
1159
+ vector,
1160
+ this.context.camera,
1161
+ width,
1162
+ height
1163
+ );
1164
+ if (this.clientPos.x === x && this.clientPos.y === y && !force) {
1165
+ return;
1166
+ }
1167
+ this._updatePosition(x, y);
1168
+ }
953
1169
  registryEvent() {
954
1170
  this.context.addEventListener("update", this.onUpdate);
955
1171
  }
@@ -957,10 +1173,11 @@ var Overlay = class extends EventDispatcher {
957
1173
  this.context.removeEventListener("update", this.onUpdate);
958
1174
  }
959
1175
  dispose() {
960
- var _a;
1176
+ var _a, _b;
961
1177
  this.unRegistryEvent();
962
1178
  this.unBindElement();
963
- (_a = this.div) == null ? void 0 : _a.remove();
1179
+ (_a = this.observer) == null ? void 0 : _a.disconnect();
1180
+ (_b = this.div) == null ? void 0 : _b.remove();
964
1181
  this.div = null;
965
1182
  }
966
1183
  };
@@ -1316,7 +1533,7 @@ var GraphicLayer = class extends Layer {
1316
1533
  };
1317
1534
 
1318
1535
  // src/layer/poi-layer.ts
1319
- import { debounce } from "lodash";
1536
+ import { debounce as debounce2 } from "lodash";
1320
1537
  var PoiLayer = class extends Layer {
1321
1538
  constructor(context) {
1322
1539
  super(context);
@@ -1329,7 +1546,7 @@ var PoiLayer = class extends Layer {
1329
1546
  });
1330
1547
  });
1331
1548
  this.registryEvent();
1332
- this.debounceCollisionDetection = debounce(this.collisionDetection, 10);
1549
+ this.debounceCollisionDetection = debounce2(this.collisionDetection, 10);
1333
1550
  }
1334
1551
  clear(force = false) {
1335
1552
  this.pois.forEach((item) => {
@@ -2442,7 +2659,14 @@ var HoverHelper = class extends EventDispatcher5 {
2442
2659
  };
2443
2660
 
2444
2661
  // src/factory/material.ts
2445
- import { Color as Color4, LineBasicMaterial as LineBasicMaterial2, MeshStandardMaterial as MeshStandardMaterial2, MeshBasicMaterial as MeshBasicMaterial3, ShaderMaterial as ShaderMaterial2, DoubleSide as DoubleSide4 } from "three";
2662
+ import {
2663
+ Color as Color4,
2664
+ LineBasicMaterial as LineBasicMaterial2,
2665
+ MeshStandardMaterial as MeshStandardMaterial2,
2666
+ MeshBasicMaterial as MeshBasicMaterial3,
2667
+ ShaderMaterial as ShaderMaterial2,
2668
+ DoubleSide as DoubleSide4
2669
+ } from "three";
2446
2670
  function vectorToString(vector) {
2447
2671
  return `${vector.x}-${vector.y}-${vector.z}`;
2448
2672
  }
@@ -2499,8 +2723,18 @@ var MaterialFactory = class {
2499
2723
  this.meshBasicMaterialMap.set(key, material);
2500
2724
  return material;
2501
2725
  }
2502
- createShaderMaterial({ gradualColor, center: center2, maxValue, opacity, direction, max, min }) {
2503
- const key = `${gradualColor.toString()}-${vectorToString(center2)}-${maxValue}-${opacity}-${vectorToString(direction)}`;
2726
+ createShaderMaterial({
2727
+ gradualColor,
2728
+ center: center2,
2729
+ maxValue,
2730
+ opacity,
2731
+ direction,
2732
+ max,
2733
+ min
2734
+ }) {
2735
+ const key = `${gradualColor.toString()}-${vectorToString(
2736
+ center2
2737
+ )}-${maxValue}-${opacity}-${vectorToString(direction)}`;
2504
2738
  if (this.shaderMaterialMap.has(key)) {
2505
2739
  return this.shaderMaterialMap.get(key);
2506
2740
  }
@@ -2515,11 +2749,23 @@ var MaterialFactory = class {
2515
2749
  varying vec3 vColor;
2516
2750
 
2517
2751
  void main() {
2752
+ vec3 lineVec = uMax - uMin; // \u7EBF\u6BB5AB\u7684\u65B9\u5411\u5411\u91CF
2753
+ vec3 AP = position - uMin; // \u5411\u91CFAP
2754
+ float t = dot(AP, lineVec) / dot(lineVec, lineVec); // \u53C2\u6570t
2755
+ t = clamp(t, 0.0, 1.0); // \u9650\u5236t\u5728[0, 1]\u8303\u56F4\u5185\uFF0C\u786E\u4FDD\u6700\u77ED\u8DDD\u79BB\u5728\u7EBF\u6BB5AB\u4E0A
2756
+
2757
+ vec3 closestPoint = uMin + t * lineVec; // \u6700\u77ED\u8DDD\u79BB\u70B9\u7684\u4F4D\u7F6E
2758
+ vec3 vecAP = closestPoint - uMin; // \u5411\u91CFAP'
2759
+ float distance = length(vecAP);
2760
+ float maxLen = length(lineVec);
2761
+
2518
2762
  vec3 direction = normalize(uDirection);
2763
+ float gradient = dot(normalize(normal), normalize(uDirection));
2519
2764
  vec3 currentPosition = position - center;
2520
- float colorFactor = (dot(direction, currentPosition) / maxValue) * 0.5 + 0.5;
2765
+ // float colorFactor = (dot(direction, currentPosition) / maxValue) * 0.5 + 0.5;
2766
+ float colorFactor = distance / maxLen;
2521
2767
 
2522
- vColor = mix(uColor, uGradualColor, colorFactor);
2768
+ vColor = mix(uColor, uGradualColor, 1.0 - t);
2523
2769
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
2524
2770
  }
2525
2771
  `;
@@ -3141,7 +3387,7 @@ function getConfig(config) {
3141
3387
  }
3142
3388
 
3143
3389
  // src/bmap.ts
3144
- import { debounce as debounce2 } from "lodash";
3390
+ import { debounce as debounce3 } from "lodash";
3145
3391
  var MapTypePolar = /* @__PURE__ */ ((MapTypePolar2) => {
3146
3392
  MapTypePolar2[MapTypePolar2["D2"] = 0] = "D2";
3147
3393
  MapTypePolar2[MapTypePolar2["D3"] = 1.1] = "D3";
@@ -3513,7 +3759,7 @@ var BMap = class extends EventDispatcher7 {
3513
3759
  window.addEventListener("keydown", this.onKeydown);
3514
3760
  window.addEventListener("keyup", this.onKeyUp);
3515
3761
  if (this.config.resizeObserver) {
3516
- const observe = new ResizeObserver(debounce2(this.resize, 5));
3762
+ const observe = new ResizeObserver(debounce3(this.resize, 5));
3517
3763
  observe.observe(this.container);
3518
3764
  this.observe = observe;
3519
3765
  }